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

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
hepe
Posts: 4
Joined: Tue Dec 19, 2017 2:04 am

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

Post 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?
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

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

Post 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.
Former Nagios employee
https://www.mcapra.com/
npolovenko
Support Tech
Posts: 3457
Joined: Mon May 15, 2017 5:00 pm

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

Post by npolovenko »

Thanks, @mcapra!
@hepe, Let us know if you have any other questions.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
hepe
Posts: 4
Joined: Tue Dec 19, 2017 2:04 am

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

Post 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.
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

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

Post 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.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
hepe
Posts: 4
Joined: Tue Dec 19, 2017 2:04 am

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

Post 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
}
User avatar
cdienger
Support Tech
Posts: 5045
Joined: Tue Feb 07, 2017 11:26 am

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

Post 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.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
hepe
Posts: 4
Joined: Tue Dec 19, 2017 2:04 am

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

Post 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?
tmcdonald
Posts: 9117
Joined: Mon Sep 23, 2013 8:40 am

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

Post 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.
Former Nagios employee
Locked