Page 1 of 1

How can I read an attribute value from a file in check_http

Posted: Tue Dec 19, 2017 2:15 am
by hepe
I have written a bash script to get the authorization header token to be written to the /usr/local/nagios/etc/resource.cfg file. The bash script works fine and in the resource.cfg the token value is saved as below,

ACCESS_TOKEN="Authorization: Bearer 38255d19-724a-4e2c-b8bc-1234retff13"
When configuring the nagios service I need to read the authorization header from the above file.

Code: Select all

define command{
    command_name    check_post_https_with_args
    command_line    /usr/local/nagios/libexec/check_http -H $HOSTADDRESS$ -S -u $ARG1$ -k /usr/local/nagios/etc/resource.cfg echo $ACCESS_TOKEN  --method=POST --post $ARG2$ -T 'application/json' 
} 

Code: Select all

define service{
    use             generic-service 
    host_name           www.cardgen.com
    service_description     post request checker
    is_volatile         0
    check_period            24x7
    check_interval                  1
    max_check_attempts      3
    normal_check_interval       1
    retry_check_interval        1
    contact_groups          admin_group
    notification_interval       120
    notification_period     24x7
    notification_options        w,u,c,r
    check_command check_post_https_with_args!/api/load/validatereadDetails=true!'{\"referenceId:145\",\"amount:500\"}'
}
This seems not to be working, can anyone guide me on how I can access the header value in the command by reading the file?

Re: How can I read an attribute value from a file in check_h

Posted: Tue Dec 19, 2017 10:23 am
by mcapra
I'd write a wrapper script in Bash (or PHP, Python, Perl, whatever) to evaluate ACCESS_TOKEN then plug it into a call to check_http. That wrapper script would then become your check_command. This doesn't require the usage of resource.cfg if your wrapper script evaluates the token then plugs it into the execution's -k argument directly.

Re: How can I read an attribute value from a file in check_h

Posted: Tue Dec 19, 2017 10:50 am
by npolovenko
Thanks, @mcapra!
@hepe, Let us know if you have any other questions.

Re: How can I read an attribute value from a file in check_h

Posted: Tue Jan 16, 2018 6:12 am
by hepe
Thanks for the reply. I manage to get it working. I need to refresh the access token every 60 mins. So I have scheduled a cron job to be run, every 55mins to retive a new access token and save it in resource.cfg as a $USER8$ macro. But it seems nagios caches the resource.cfg file. even the resource.cfg macro value gets overridden by the new value every 60mins, the command picks the old value (the one which was there when nagios server was started )
Can I know a way to tackle this issue?
Thank you.

Re: How can I read an attribute value from a file in check_h

Posted: Tue Jan 16, 2018 5:06 pm
by cdienger
What command did you use to eventually get this to pull the value? Tests on my lab machine show Nagios picking up changes to the config file nearly instantly.

Re: How can I read an attribute value from a file in check_h

Posted: Tue Jan 16, 2018 10:56 pm
by hepe
@cdienger I am using the check_http plugin

Code: Select all

define command{
    command_name    check_get_https_with_args_with_access_token
    command_line    /usr/local/nagios/libexec/check_http -H $HOSTADDRESS$ -S -u $ARG1$ -k $USER8$ --method=GET
}

Re: How can I read an attribute value from a file in check_h

Posted: Wed Jan 17, 2018 4:49 pm
by cdienger
The test I was running was actually on XI and it was reading the resources.cfg for the test function but not for the actual check. Sorry for the confusion! The service does need to be restarted in order to load resources.cfg again.

That said, you could instead use a Linux environment variable in the command. And instead of the cronjob updating the macro it would update the environment variable.

Re: How can I read an attribute value from a file in check_h

Posted: Wed Jan 24, 2018 11:54 pm
by hepe
I had trouble, loading the environmental variable in the check command. Can you guide me on how I can update the command to use the environmental variable value?

Re: How can I read an attribute value from a file in check_h

Posted: Thu Jan 25, 2018 2:39 pm
by tmcdonald
You would probably have more luck using the environment variable in the plugin script itself rather than in the command line, due to the way Nagios handles macros (which also start with a $). Simply referencing something like $HOME or $PWD in your bash script should suffice to access those two environment variables, for example.