Howto create custom Nagios Plugins using Python in 3 steps.
http://www.ibm.com/developerworks/aix/library/au-nagios/
http://www.linux-mag.com/id/7706/
The following procedures will create a localhost cpu load check using Python on CentOS.
1)Create the service check that will call your plug-in.
vi /etc/nagios/objects/localhost.cfg and add this code block-
#2012Mar27
#
#Testing
define service{
use local-service
host_name localhost
service_description Python LoadAverage
check_period 24x7
notification_options c,r
notifications_enabled 0
check_command check_mygetloadavg
}
2)Register the plugin with Nagios and give its path.
vi /etc/nagios/objects/commands.cfg and add this code block-
#2012Mar28
#
#Test plugin using a Python script.
define command{
command_name check_mygetloadavg
command_line /usr/lib64/nagios/plugins/check_nagios_host_cpuload.py
}
3)Create the plugin.
vi /usr/lib64/nagios/plugins/check_nagios_host_cpuload.py
#!/usr/bin/env python
#2012Mar23
#
#Python script to check Nagios server cpu load average.
#
#http://www.ibm.com/developerworks/aix/library/au-nagios/
#
#Exit Codes
#exit code 0 = service is working properly.
#exit code 1 = service in a Warning state.
#exit code 2 = service in a Critical state.
#exit code 3 = service in an Unknown state.
#
import os.sys
(d1, d2, d3) = os.getloadavg()
if d1 >= 5.0:
print "GETLOADAVG CRITICAL: Load avg is %.2f" % (d1)
sys.exit(2)
elif d1 >= 2.0;
print "GETLOADAVG WARNING: Load avg is %.2f" % (d1)
sys.exit(1)
else:
print "GETLOADAVG OK: Load avg is %.2f" % (d1)
sys.exit(0)
Set the permissions- -rwxr-xr-x 1 root root 639 Mar 28 20:50 check_nagios_host_cpuload.py
Save all files. Check for errors-
nagios -v /etc/nagios/nagios.cfg
If error free then restart Nagios-
service nagios reload.
Open your Nagios dashboard and the new check will exist and is active.
No comments:
Post a Comment