nagios python template

来源:互联网 发布:mtv电子相册制作软件 编辑:程序博客网 时间:2024/05/18 03:59

Python Nagios Plugin Template

Archivado en: python, nagios — Etiquetas: python, bash — Ruben @ 11:51

This is a very simple nagios plugin template.

Just run it this way:

$ python template.py host 80 80
You should get an status OK with those values. and WARNING/CRITICAL below 50/50.

01#! /usr/bin/env python
02 
03import sys
04 
05status = 'OK' 0 'WARNING' 1'CRITICAL' 2 'UNKNOWN' 3}
06 
07host =  sys.argv[1]
08warn_limit = int(sys.argv[2])
09#warn_limit = 80
10crit_limit = int(sys.argv[3])
11#crit_limit = 85
12 
13#  put some function here to read the values, I'm forcing to 50
14warn_value = 50
15crit_value = 50
16####
17 
18 
19warn=False
20if warn_value >= warn_limit :
21    warn = True
22 
23crit=False
24if crit_value >= crit_limit:
25    crit = True
26 
27if warn == True:
28 
29       if crit == True:
30               print 'Critical status info'
31               sys.exit(status['CRITICAL'])
32       else:
33               print 'Warning status info'
34               sys.exit(status['WARNING'])
35 
36else:
37       print 'status OK info'
38       sys.exit(status['OK'])

diciembre 16, 2008

Python SNMP Disk Nagios Plugin

Archivado en: GNU/Linux, nagios, python, snmp — Etiquetas: nagios, python, snmp — Ruben @ 16:14

Here is a way of checking disk use by snmp in Nagios. The two first scripts are done the difficult way, an the last one is done the easy way, altought the previous are fun for playing with python.

01#! /usr/bin/env python2.4
02 
03import sys
04import os
05import numpy
06 
07 
08status = { 'OK' : 0 , 'WARNING' : 1, 'CRITICAL' : 2 , 'UNKNOWN' : 3}
09 
10 
11host =  sys.argv[1]
12warn_value = int(sys.argv[2])
13#warn_value = 80
14crit_value = int(sys.argv[3])
15#crit_value = 85
16# An array with the partitions we are not interested in.
17#blacklist = sys.argv[4]
18#blacklist =  ['Memory_Buffers', '/proc', 'sys/' , 'Swap_Space' , 'Real_Memory', 'volatile/', '/contract', 'mnttab/', 'platform/', 'export/']
19#blacklist =  ['Memory_Buffers', 'Swap_Space' , 'contract']
20blacklist =  ['Memory_Buffer', 'Swap_Space', 'Real_Memory', 'contract'  , 'proc', 'dev', 'run', 'mnttab' , 'svc', 'zones', 'nfs', 'object', 'system', 'vol', 'platform' ]
21 
22 
23desc =  os.popen('/usr/bin/snmpwalk -v 2c  -OvQ -c Community' + host + ' hrStorageDescr').read().replace(' ', '_').split()
24size =  os.popen('/usr/bin/snmpwalk -v 2c  -OvQ -c Community ' + host + ' hrStorageSize').read().split()
25used =  os.popen('/usr/bin/snmpwalk -v 2c  -OvQ -c Community ' + host + ' hrStorageUsed').read().split()
26numpy.seterr(divide = 'ignore')
27 
28 
29# Percent:
30used = numpy.array(map(long,used))  
31size = numpy.array(map(long,size))   
32percent =  used * 100 / size
33 
34data = ''
35warn=False
36crit=False
37d = dict()
38 
39def blacklisted(key):
40        for word in blacklist:
41                if word in key:
42                        return True
43        return False
44 
45for i, key in enumerate(desc):
46        if not blacklisted(key) :
47                data +=   key.strip()  + ' ' + str(percent[i] ) + '%   '
48                if percent[i] >= warn_value :  
49                        warn=True
50                        if percent[i] >= crit_value :  
51                                crit=True
52 
53 
54if warn == True:
55 
56        if crit == True:
57                print 'DISK: ' + data.strip()
58                sys.exit(status['CRITICAL'])
59        else:
60                print 'DISK: ' + data.strip()
61                sys.exit(status['WARNING'])
62 
63else:  
64        print 'DISK: '  + data.strip()
65        sys.exit(status['OK'])

This script doesn’t check for snmp object indexes so it leads to some inconsistencies on the response. There are some hrStorageDesc.X who doesn’t have the corresponding hrStorageUsed.X in some linux versions, and the reading in not checking the X index, so this version fixes this problem:

01#! /usr/bin/env python2.4
02 
03import sys
04import os
05import numpy
06 
07 
08status = 'OK' 0 'WARNING' 1'CRITICAL' 2 'UNKNOWN' 3}
09 
10 
11host =  sys.argv[1]
12warn_value = int(sys.argv[2])
13#warn_value = 80
14crit_value = int(sys.argv[3])
15#crit_value = 85
16# An array with the partitions we are not interested in.
17blacklist =  ['Memory_Buffer''Swap_Space''Real_Memory''contract'  'proc''dev''run''mnttab' 'svc''zones''nfs''object''system''vol''platform''export' 'sys''Swap''Physical''Virtual','boot']
18 
19 
20desc_ =  os.popen('/usr/bin/snmpwalk -v 2c -Os -c Community ' + host + ' hrStorageDescr').readlines()
21size_ =  os.popen('/usr/bin/snmpwalk -v 2c -Os -c Community ' + host + ' hrStorageSize').readlines()
22used_ =  os.popen('/usr/bin/snmpwalk -v 2c -Os -c Community ' + host + ' hrStorageUsed').readlines()
23 
24# not in order, so use a dictinary
25desc = dict()
26size = dict()
27used = dict()
28 
29for line in desc_:
30          desc [ line.split()[0].replace('hrStorageDescr.', '') ] =  line.split()[3]
31 
32for key in desc.keys() :
33       for line in size_ :
34               if 'hrStorageSize.' + key in line:
35                       size [ key ] =  line.split()[3]
36       for line in used_ :
37               if 'hrStorageUsed.' + key in line:
38                       used [ key ] =  line.split()[3]
39 
40 
41numpy.seterr(divide = 'ignore')
42 
43 
44# Percent:
45for key in used.keys():
46       try :
47               used[key] = long(used[key]) * 100 / long(size[key])
48       except ZeroDivisionError:
49               used[key] = 0
50 
51 
52data = ''
53warn=False
54crit=False
55= dict()
56 
57def blacklisted(key):
58       for word in blacklist:
59               if word in key:
60                       return True
61       return False
62 
63for key in used.keys():
64       if not blacklisted(desc[key]) :
65               data +=   desc[key] +  '  ' +   str(used[key] )  + '%, '
66               if used[key] >= warn_value :
67                       warn=True
68                       if used[key] >= crit_value :
69                               crit=True
70 
71if warn == True:
72 
73       if crit == True:
74               print 'DISK: ' + data.strip()
75               sys.exit(status['CRITICAL'])
76       else:
77               print 'DISK: ' + data.strip()
78               sys.exit(status['WARNING'])
79 
80else:
81       print 'DISK: '  + data.strip()
82       sys.exit(status['OK'])

Ok, I did it the complicated way, there is no need for this if you use dskPath and dskPercent. No need for chekings or try’s and excepts’ as you can get the percentages in “int”. We don’t even need Blacklist anymore as these values are the ones defined in /etc/snmp/snmpd.com.

ver fuente
imprimir?
01#! /usr/bin/env python2.4
02 
03import sys
04import os
05import numpy
06 
07 
08status = 'OK' 0 'WARNING' 1'CRITICAL' 2 'UNKNOWN' 3}
09 
10 
11host =  sys.argv[1]
12warn_value = int(sys.argv[2])
13#warn_value = 80
14crit_value = int(sys.argv[3])
15#crit_value = 85
16# An array with the partitions we are not interested in.
17blacklist =  ['Memory_Buffer''Swap_Space''Real_Memory''contract'  'proc''dev''run''mnttab' 'svc''zones''nfs''object''system''vol''platform''export' ]
18 
19 
20desc =  os.popen('/usr/bin/snmpwalk -v 2c  -OvQ -c Community ' + host + ' dskPath').read().replace(' ''_').split()
21percent =  os.popen('/usr/bin/snmpwalk -v 2c  -OvQ -c Community ' + host + ' dskPercent').read().split()
22 
23data = ''
24warn=False
25crit=False
26= dict()
27 
28def blacklisted(key):
29       for word in blacklist:
30               if word in key:
31                       return True
32       return False
33 
34for i, key in enumerate(desc):
35       if not blacklisted(key) :
36               data +=   key.strip()  + ' ' + percent[i] + '%   '
37               if int(percent[i]) >= warn_value :
38                       warn=True
39                       if int(percent[i]) >= crit_value :
40                               crit=True
41 
42 
43if warn == True:
44 
45       if crit == True:
46               print 'DISK: ' + data.strip()
47               sys.exit(status['CRITICAL'])
48       else:
49               print 'DISK: ' + data.strip()
50               sys.exit(status['WARNING'])
51 
52else:
53       print 'DISK: '  + data.strip()
54       sys.exit(status['OK'])

原创粉丝点击