GDPR Cookie Consent by Free Privacy Policy Generator
post thumb
Asset Control
by Matthias Hanitzsch-Moonlight/ on 22 Apr 2019

Uniform logging and error handling with ac_utilities.sh

Your standard Asset Control installation includes the script ac_utilities.sh which you can source in your shell scripts to then have logging functions and some level of error handling available.

Let’s look at the logging functions first:

acdba@acbox:~$ . ./ac/bin/ac_utilities.sh
acdba@acbox:~$ ac_log_info "An info message"
+++INFO+++ 20190422_14:36:51 : An info message
acdba@acbox:~$ ac_log_warning "A warning message"
+++WARNING+++ 20190422_14:37:08 : A warning message
acdba@acbox:~$ ac_log_error "An error message"
+++ERROR+++ 20190422_14:37:20 : An error message
acdba@acbox:~$ ac_log_fatal "A fatal message"
+++FATAL+++ 20190422_14:37:31 : A fatal message

To help with error handling you can use the function check_rc. Typically, you would provide two parameters: A message and either 0 or 1. check_rc then does the following:

  • If the exit code of the command that was executed just before check_rc is 0, the message is NOT printed and your script will continue executing.
  • If the previous command did result in an error, the message is printed.
  • Also, if you provided a 0, the script will continue running. However, if you provided a 1 (or other non-zero value), your script will exit with that value.

Let’s see what that looks like:

acdba@acbox:~$ check_rc "This gets printed if the previous command returned non-zero - so not now" 0
acdba@acbox:~$ grep nosuchstring ac/bin/acenv.sh
acdba@acbox:~$ check_rc "This gets printed if the previous command returned non-zero - but now" 0
+++ERROR+++ 20190422_14:43:17 : This gets printed if the previous command returned non-zero - but now
acdba@acbox:~$ grep nosuchstring ac/bin/acenv.sh
acdba@acbox:~$ check_rc "This gets printed if the previous command returned non-zero - and this calls exit 1" 1
+++FATAL+++ 20190422_14:43:25 : This gets printed if the previous command returned non-zero
logout
Connection to acbox closed.
$ 

And here with an example application:

acdba@acbox:~$ cat check_rc_demo.sh
#!/bin/bash

. ac_utilities.sh

ADO=$1

ac_log_info "Start."
echo $ADO | ac_dump_ado.pl -l - > $ADO.acbl
check_rc "Could not retrieve static data of $ADO!" 1

ac_log_info "End."
acdba@acbox:~$ 

The script expects one parameter, an ADO ID. This is piped into ac_dump_ado.pl and the output is redirected to file.

Let’s call the script with an existing ADO:

acdba@acbox:~$ ./check_rc_demo.sh C0.ISS.1
+++INFO+++ 20190422_15:08:53 check_rc_demo.sh: Start.
+++INFO+++ 20190422_15:08:53 check_rc_demo.sh: End.
acdba@acbox:~$ echo $?
0

ac_dump_ado.pl works just fine, we see the End message and have an exit code of 0. All good.

Let’s simulate a failure now with a non-existing ADO:

acdba@acbox:~$ ./check_rc_demo.sh C0.ISS.DOESNOTEXIST
+++INFO+++ 20190422_15:09:08 check_rc_demo.sh: Start.
+++ERROR+++ 20190422_15:09:08 ac_dump_ado.pl: No (matching) ados found in database
+++FATAL+++ 20190422_15:09:08 check_rc_demo.sh: Could not retrieve static data of C0.ISS.DOESNOTEXIST!
acdba@acbox:~$ echo $?
1
acdba@acbox:~$ 

ac_dump_ado.pl returns with an error. This condition is picked up by check_rc, which prints a FATAL message and exits our script with 1. Also, note how the End message is never printed.

comments powered by Disqus