Page 1 of 1

Writing to nagios.cmd FIFO via Python

Posted: Mon Apr 02, 2018 10:16 am
by cscott
I'm having a really hard time figuring out why this bit of Python won't write to the nagios.cmd FIFO properly:

Code: Select all

#! /usr/local/bin/python2
import sys
import time

argv = []
if sys.argv.__len__() < 3:
    print "host,service,duration must be provided"
    exit(0)
host = str(sys.argv[1])
service = str(sys.argv[2])
duration = int(sys.argv[3])

currentTime = int(time.time())
endTime = currentTime + duration

with open("/var/spool/nagios/rw/nagios.cmd", "a") as myfile:
  msg = "[%s] SCHEDULE_SVC_DOWNTIME;%s;%s;%s;%s;1;0;%s;Amanda;Backup execution" % (currentTime,host,service,currentTime,endTime,duration)
  print msg
  myfile.write(msg)
myfile.close()
If I take the output of print msg and echo <msg> >> nagios.cmd it works fine. The command is being run as root. A nearly identical bit of Python does work properly. What am I missing?

The code is based of the example provided http://old.nagios.org/developerinfo/ext ... 1522670372.

Re: Writing to nagios.cmd FIFO via Python

Posted: Mon Apr 02, 2018 11:15 am
by npolovenko
Hi,@ cscott.
Your script worked for me.
I changed the python environment variable, and also my cmd file is located in:

Code: Select all

/usr/local/nagios/var/rw/nagios.cmd
I also added a newline sign \n at the end of each command line.

Code: Select all

#! /usr/bin/python
import sys
import time

argv = []
if sys.argv.__len__() < 3:
    print "host,service,duration must be provided"
    exit(0)
host = str(sys.argv[1])
service = str(sys.argv[2])
duration = int(sys.argv[3])

currentTime = int(time.time())
endTime = currentTime + duration

with open("/usr/local/nagios/var/rw/nagios.cmd", "a") as myfile:
  msg = "[%s] SCHEDULE_SVC_DOWNTIME;%s;%s;%s;%s;1;0;%s;Amanda;Backup execution\n" % (currentTime,host,service,currentTime,endTime,duration)
  print msg
  myfile.write(msg)
myfile.close()

Re: Writing to nagios.cmd FIFO via Python

Posted: Mon Apr 02, 2018 3:14 pm
by cscott
The newline character did it. Thanks a lot!

Re: Writing to nagios.cmd FIFO via Python

Posted: Tue Apr 03, 2018 9:56 am
by scottwilkerson
Glad to hear it is resolved! Locking