Python Nagios Plugin Template
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
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
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 = 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']
20
blacklist
=
[
'Memory_Buffer'
,
'Swap_Space'
,
'Real_Memory'
,
'contract'
,
'proc'
,
'dev'
,
'run'
,
'mnttab'
,
'svc'
,
'zones'
,
'nfs'
,
'object'
,
'system'
,
'vol'
,
'platform'
]
21
22
23
desc
=
os.popen(
'/usr/bin/snmpwalk -v 2c -OvQ -c Community'
+
host
+
' hrStorageDescr'
).read().replace(
' '
,
'_'
).split()
24
size
=
os.popen(
'/usr/bin/snmpwalk -v 2c -OvQ -c Community '
+
host
+
' hrStorageSize'
).read().split()
25
used
=
os.popen(
'/usr/bin/snmpwalk -v 2c -OvQ -c Community '
+
host
+
' hrStorageUsed'
).read().split()
26
numpy.seterr(divide
=
'ignore'
)
27
28
29
# Percent:
30
used
=
numpy.array(
map
(
long
,used))
31
size
=
numpy.array(
map
(
long
,size))
32
percent
=
used
*
100
/
size
33
34
data
=
''
35
warn
=
False
36
crit
=
False
37
d
=
dict
()
38
39
def
blacklisted(key):
40
for
word
in
blacklist:
41
if
word
in
key:
42
return
True
43
return
False
44
45
for
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
54
if
warn
=
=
True
:
55
56
if
crit
=
=
True
:
57
'DISK: '
+
data.strip()
58
sys.exit(status[
'CRITICAL'
])
59
else
:
60
'DISK: '
+
data.strip()
61
sys.exit(status[
'WARNING'
])
62
63
else
:
64
'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 | d = 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 | d = 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' ]) |