Writing to nagios.cmd FIFO via Python

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
cscott
Posts: 2
Joined: Mon Apr 02, 2018 10:12 am

Writing to nagios.cmd FIFO via Python

Post 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.
npolovenko
Support Tech
Posts: 3457
Joined: Mon May 15, 2017 5:00 pm

Re: Writing to nagios.cmd FIFO via Python

Post 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()
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
cscott
Posts: 2
Joined: Mon Apr 02, 2018 10:12 am

Re: Writing to nagios.cmd FIFO via Python

Post by cscott »

The newline character did it. Thanks a lot!
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Writing to nagios.cmd FIFO via Python

Post by scottwilkerson »

Glad to hear it is resolved! Locking
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
Locked