nagios python template

Python Nagios Plugin Template

Archivado en:  pythonnagios — Etiquetas:   — 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  
03 import sys
04  
05 status = 'OK' 0 'WARNING' 1'CRITICAL' 2 'UNKNOWN' 3}
06  
07 host =  sys.argv[1]
08 warn_limit = int(sys.argv[2])
09 #warn_limit = 80
10 crit_limit = int(sys.argv[3])
11 #crit_limit = 85
12  
13 #  put some function here to read the values, I'm forcing to 50
14 warn_value = 50
15 crit_value = 50
16 ####
17  
18  
19 warn=False
20 if warn_value >= warn_limit :
21     warn = True
22  
23 crit=False
24 if crit_value >= crit_limit:
25     crit = True
26  
27 if 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  
36 else:
37        print 'status OK info'
38        sys.exit(status['OK'])

diciembre 16, 2008

Python SNMP Disk Nagios Plugin

Archivado en:  GNU/Linuxnagiospythonsnmp — Etiquetas:   — 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  
03 import sys
04 import os
05 import numpy
06  
07  
08 status = 'OK' 0 'WARNING' 1'CRITICAL' 2 'UNKNOWN' 3}
09  
10  
11 host =  sys.argv[1]
12 warn_value = int(sys.argv[2])
13 #warn_value = 80
14 crit_value = int(sys.argv[3])
15 #crit_value = 85
16 # An array with the partitions we are not interested in.
17 blacklist =  ['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  
20 desc_ =  os.popen('/usr/bin/snmpwalk -v 2c -Os -c Community ' + host + ' hrStorageDescr').readlines()
21 size_ =  os.popen('/usr/bin/snmpwalk -v 2c -Os -c Community ' + host + ' hrStorageSize').readlines()
22 used_ =  os.popen('/usr/bin/snmpwalk -v 2c -Os -c Community ' + host + ' hrStorageUsed').readlines()
23  
24 # not in order, so use a dictinary
25 desc = dict()
26 size = dict()
27 used = dict()
28  
29 for line in desc_:
30           desc [ line.split()[0].replace('hrStorageDescr.', '') ] =  line.split()[3]
31  
32 for 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  
41 numpy.seterr(divide = 'ignore')
42  
43  
44 # Percent:
45 for 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  
52 data = ''
53 warn=False
54 crit=False
55 = dict()
56  
57 def blacklisted(key):
58        for word in blacklist:
59                if word in key:
60                        return True
61        return False
62  
63 for 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  
71 if 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  
80 else:
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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值