Discussion:
Command Notification to run a python script
jcurry
2013-04-11 15:54:42 UTC
Permalink
jcurry [http://community.zenoss.org/people/jcurry] created the discussion

"Command Notification to run a python script"

To view the discussion, visit: http://community.zenoss.org/message/72786#72786

--------------------------------------------------------------
Zenoss 4.2.3.

I want a command notification to run a python script.
Script is:

[***@zen42 local]$ cat change_summary_command.py
#!/usr/bin/env python
#
# First parameter is evt/evid - event id
import sys
import os
import time
#import Globals

zenhome = os.environ['ZENHOME']

of = open(os.path.join(zenhome, 'local', 'change_summary_command.out'), 'a')
evid = sys.argv[1]

localtime = time.asctime( time.localtime(time.time()) )
of.write(' Time is %s  Event id %s \n' % (localtime, evid))
of.close()
sys.exit()

It is called from an enabled command notification that says:

/opt/zenoss/local/change_summary_command.py  ${evt/evid}

It fails - I am pretty sure in calling the python.
zenactiond.log has:
2013-04-11 16:41:26,997 DEBUG zen.processqueue: execution time 0.292196035385 seconds; queue time 0.000481128692627 seconds; process process /bin/sh -c /opt/zenoss/local/change_summary_command.py  000c29d9-f87b-8309-11e2-a2be44f810f9
2013-04-11 16:41:26,997 DEBUG zen.actions: Command finished: 'A process has ended with a probable error condition: process ended with exit code 1.'

I have tried with both single and double quotes around the whole notification command which produces:
2013-04-11 16:45:40,992 DEBUG zen.processqueue: execution time 0.210268974304 seconds; queue time 0.000412940979004 seconds; process process /bin/sh -c "/opt/zenoss/local/change_summary_command.py  000c29d9-f87b-8309-11e2-a2bedc54638e"
2013-04-11 16:45:40,992 DEBUG zen.actions: Command finished: 'A process has ended with a probable error condition: process ended with exit code 127.'

Have also tried with /usr/bin/python explicitly on the front of the notification command - same 127 error.

Running the command reported in zenactiond.log, from a command line, such as:
/bin/sh -c "/opt/zenoss/local/change_summary_command.py  000c29d9-f87b-8309-11e2-a2bedc54638e"
works perfectly, so I think it has to be to do with shells / environment.

Can anyone share a working example of calling a python script from a command notification in 4.2.3??

Thanks,
Jane
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/72786#72786]

Start a new discussion in zenoss-users at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
jcurry
2013-04-12 11:58:14 UTC
Permalink
jcurry [http://community.zenoss.org/people/jcurry] created the discussion

"Re: Command Notification to run a python script"

To view the discussion, visit: http://community.zenoss.org/message/72795#72795

--------------------------------------------------------------
OK - it works.

Several things here.

1) Script is still identical to the above - note that is uses the environment variable ZENHOME

2) In the command notification configuration you do not want ANY quotes.  If you have quotes around the whole command then the shell is effectively looking for a complete command of '/opt/zenoss/local/change_summary_command.py  000c29d9-f87b-8309-11e2-a2be44f810f9' - which obviously doesn't exist.  Working Command Notification screenshot attached.

3) There have been changes between 4.2 and 4.2.3 with what environment variables actually get passed to the command notification.  In 4.2.3, if you dont pass ZENHOME explicitly then it is unset.  I think prior to 4.2.3 then ZENHOME did get passed without explicit invocation.  Safe answer is to always pass it.

Loading Image... Loading Image...
Cheers,
Jane
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/72795#72795]

Start a new discussion in zenoss-users at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
kenhen93
2013-09-11 18:15:03 UTC
Permalink
kenhen93 [http://community.zenoss.org/people/kenhen93] created the discussion

"Re: Command Notification to run a python script"

To view the discussion, visit: http://community.zenoss.org/message/74608#74608

--------------------------------------------------------------
Hi Jane,

I am curoius if you commented out your import globals for any reason. We have a python script that we are having trouble getting working on 4.x which work in 3.x. It has many imports and we have narrowed down the problem that when zenoss runs the scripts from the event command there is some envinonmental problems as many of import like Globals is not found. The same script runs fine on as the zenoss from the cmd line
--------------------------------------------------------------

Reply to this message by replying to this email -or- go to the discussion on Zenoss Community
[http://community.zenoss.org/message/74608#74608]

Start a new discussion in zenoss-users at Zenoss Community
[http://community.zenoss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2003]
Loading...