Tuesday 2 June 2009

Process check in nagios with python

I wanto to implement nagios plugin to check process if it is running for servers. I used paramiko module for ssh in python
My restrictions:
I do not want to install any nagios agent to servers.
I do not want to use ssh autologin.
Reason I am lazy I do not want to visit all servers to generate public keys and copy to nagios server.


nagios configuration file command line
define command{
command_name check_process.py
command_line $USER1$/check_process.py $HOSTADDRESS$ $ARG1$ $ARG2$
}




Add lines below to check_process.py and make executable.

#!/usr/bin/python
# useage command ipaddress process maxcount
import paramiko, getpass
import os,sys,re
import signal


userName="user"
userPass="password"
server=sys.argv[1]
command="ps -ef | grep " + sys.argv[2] + " | grep -v grep |wc -l"
maxcount=int(sys.argv[3])


t = paramiko.Transport((server,22))
try:
t.connect(username=userName,password=userPass,hostkey=None)
except:
print server + ": Bad password or login!"
t.close()
else:
ch = t.open_channel(kind = "session")
ch.exec_command(command)
if (ch.recv_ready):
if int( ch.recv(1000) ) >= maxcount:
print "OK " + sys.argv[2]
t.close()
sys.exit(0)
else:
print "NOK " + sys.argv[2]
sys.exit(2)
t.close()