#!/usr/bin/python
import commands
import sys
from optparse import OptionParser
import re
def get_hadoop_status():
file = open("/usr/local/nagios/test.txt", 'r')
dead_num = 0
line = file.readline()
while ('' != line):
string = line
value = re.findall( '.?datanode\d+\s+(\d).+', string)
if (value == 1):
dead_num += 1
else:
pass
file.close()
if __name__ == '__main__':
parser = OptionParser(usage="%prog [-w] [-c]", version="%prog 1.0")
parser.add_option("-w", "--warning", type="int", dest="w", default=1)
parser.add_option("-c", "--critical", type="int", dest="c", default=3)
(options, args) = parser.parse_args()
if(options.c <= options.w):
print '-c must greater than -w'
sys.exit(1)
get_hadoop_status()
if(dead_num >= options.c) :
print 'CRITICAL - DeadNode number is %d:\n' %(dead_num)
# while (count<dead_num):
# print 'datanode%d\n' %(datanode_num(count))
# count += 1
# print dead_datanode_status
sys.exit(2)
elif(dead_num >= options.w):
print 'WARNING - DeadNode number is %d:\n' %(dead_num)
# while (count<dead_num):
# print 'datanode%d\n' %(datanode_num(count))
# count += 1
# print dead_datanode_status
sys.exit(1)
else:
print 'OK - DeadNode number is %d' %(dead_num)
sys.exit(0)
我是想匹配底下的这种格式文件每行的最后一位,为0时代表正常,为1时代表宕机:
datanode1 1
datanode2 0
datanode3 0
datanode4 0
datanode5 1
datanode6 1
datanode7 1
执行后总是显示底下的错误:
Traceback (most recent call last):
File "./check_datanode_alive", line 30, in ?
get_hadoop_status()
File "./check_datanode_alive", line 13, in get_hadoop_status
value = re.findall( '.?datanode\d+\s+(\d).+', string)
File "/usr/lib64/python2.4/sre.py", line 167, in findall
return _compile(pattern, flags).findall(string)
一只萌萌小番薯
德玛西亚99