Page 1 of 2

cannot run custom command from anywhere but libexec folder

Posted: Wed Apr 04, 2018 11:44 am
by Nighthawkz
I have a custom .py script provided to myself to monitor dell storage controllers. I'm trying to get nagios to run locally (no way of adding a client to the storage controller).

it's a python script that invokes a jar file.

the script is attached. the JAR file and the script currently sit in /usr/local/nagios/libexec/ and have been given full permissions (777) just to blank cover whilst I test set-up.


I've set up the command in the command.cfg as follows :


define command{
command_name check_compellent_alerts
command_line $USER1$/check_compellent_alert.py -P $USER47$
}

user47 beign the password I'm trying to pass it from the resource.cfg

and the check itself is in a check.cfg :


define service {
use generic-service
host_name c-host-1
service_description Command Utility Script Results
check_command check_compellent_alerts
}


problem is the script will only run - if I run the script manually from within the folder the script is located and add the password parameter I want to pass through as variables within the script itself. . otherwise if I try and run it like
/usr/local/nagios/libexec/check_compellent_alerts.py
or
/usr/local/nagios/libexec/check_compellent_alerts.py -P $USER47$

I get a traceback of "need one more argument to unpack" which I understand to believe meaning it's expecting a parameter to be passed but got nothing.

so now I'm doubting if I've actually set this up right at all and am hoping someone could help advise!

Regards

Re: cannot run custom command from anywhere but libexec fold

Posted: Wed Apr 04, 2018 2:13 pm
by npolovenko
Hello, @Nighthawkz. Let's start from simple. Sign in to your system as a Nagios user and try to run the plugin locally:

Code: Select all

su -nagios

Code: Select all

/usr/local/nagios/libexec/check_compellent_alerts.py -P *password*
You can not pass in macros from the resource.cfg locally:

Code: Select all

/usr/local/nagios/libexec/check_compellent_alerts.py -P $USER47$
Only nagios has access to this macro. So if you were to run the command locally you need to use a normal password.

Next, open the /usr/local/nagios/etc/nagios.cfg file and make sure that:

Code: Select all

enable_environment_macros=1
Also, pay attention to this:

Code: Select all

illegal_macro_output_chars=`~$&|'"<>
That means $USER47$ can not contain any special characters.

Restart the nagios service with:

Code: Select all

service nagios restart

Re: cannot run custom command from anywhere but libexec fold

Posted: Thu Apr 05, 2018 3:34 am
by Nighthawkz
Hi

running it as nagios:

Code: Select all

nagios@nagios:/usr/local/nagios> /usr/local/nagios/libexec/check_compellent_alerts.py
Traceback (most recent call last):
  File "/usr/local/nagios/libexec/check_compellent_alerts.py", line 81, in <module>
    alerts = run_CompCU("alert show")
  File "/usr/local/nagios/libexec/check_compellent_alerts.py", line 39, in run_CompCU
    (CUHeader,blank,CUBreak1,CUUser,CUHost,CUIntent,CUBreak2,CUConnect,CURunning)=lines[:9]
ValueError: need more than 1 value to unpack

unless i CD into the libexec folder and run it as ./check_compellent_alerts.py

in which case it runs correctly.

the enable_environment_macros is set to one and teh password does nto contain any of the illegal characters

EDIT** took the -P out of the code bit as i forgot it had the password in it.....

Re: cannot run custom command from anywhere but libexec fold

Posted: Thu Apr 05, 2018 5:59 am
by danjoh
Have you tried to use the fully qualified path to the jar-file?

I.e.

Code: Select all

output=commands.getoutput('java -jar /usr/local/nagios/libexec/CompCU.jar -host %s -user %s -password %s -c "%s"' % (Host,SCUser,password,command))
instead of

Code: Select all

output=commands.getoutput('java -jar CompCU.jar -host %s -user %s -password %s -c "%s"' % (Host,SCUser,password,command))

Re: cannot run custom command from anywhere but libexec fold

Posted: Thu Apr 05, 2018 12:43 pm
by scottwilkerson
danjoh wrote:Have you tried to use the fully qualified path to the jar-file?
You may also need the fully qualified path to java as well

Code: Select all

output=commands.getoutput('/usr/bin/java -jar /usr/local/nagios/libexec/CompCU.jar -host %s -user %s -password %s -c "%s"' % (Host,SCUser,password,command))

Re: cannot run custom command from anywhere but libexec fold

Posted: Fri Apr 06, 2018 5:40 am
by Nighthawkz
wow - why did i not think of that.

it now works no mater where i run from manually. hopefully it'll also work when i re enable/restart nagios now!

Re: cannot run custom command from anywhere but libexec fold

Posted: Fri Apr 06, 2018 8:50 am
by scottwilkerson
Let us know if you have further problems

Re: cannot run custom command from anywhere but libexec fold

Posted: Fri Apr 13, 2018 7:45 am
by Nighthawkz
apologies for the delay! I now getting the following results when nagios tries to run it automatically:

CRITICAL: Return code of 127 is out of bounds. Make sure the plugin you're trying to run actually exists. (worker: nagios3)

Running it from command line as either root or nagios user doesn't return a result - but doesn't fail either (don't think ti should re returning a result if nothings wrong to be honest)

*EDIT* I told a lie - it should be returning a result as I checked the storage manager itself (which has alerts). It just doesn't return anything. If I input the wrong password onto the command lien to be passed through, it does correctly return that the user could not be logged in due to incorrect log in:

"Couldn't connect to host-1: Error returned from Web Server, make sure certificates are created correctly (Incorrect or missing user login or password)"

Re: cannot run custom command from anywhere but libexec fold

Posted: Fri Apr 13, 2018 7:56 am
by danjoh
When you run it from the command line, what is the exit code?

Hint:

Code: Select all

/usr/bin/java -jar /usr/local/nagios/libexec/CompCU.jar -host <Host> -user <SCUser> -password <password> -c "<command>"
echo $?

Re: cannot run custom command from anywhere but libexec fold

Posted: Fri Apr 13, 2018 8:06 am
by Nighthawkz
I get a correct response if run as root:

Code: Select all

Controller Index  Alert_Type       Message                                                                                                                                                                                                                                                          Status           Date_Created             Object_Name                                                      Category         Count  Acknowledged Date_Cleared             Reference_Number
---------- ------ ---------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------- ------------------------ ---------------------------------------------------------------- ---------------- ------ ------------ ------------------------ ------------------------
77842      1      Alert            Local ISCSI Port 0000000000000000 is Down 
NOTE i had to leave echo $? off. didn't like it kept telling me "Error parsing options:"


If i run as nagios i get the same response expect with an error at the end :

Code: Select all

Successfully finished running Compellent Command Utility (CompCU) application.
Error setting up log file null: File already exists and cant delete it (is it in use?)
Exception saving output file [cu_output.xml]: java.io.FileNotFoundException: cu_output.xml (Permission denied)
java.io.FileNotFoundException: cu_output.xml (Permission denied)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:204)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
        at java.io.FileWriter.<init>(FileWriter.java:57)
        at com.compellent.cli.StorageCenterCLIMain.main(Unknown Source)