#! /usr/bin/python # 經過在dmesg命令輸出中查找匹配nfs: (.*)來定位nfs錯誤 # 因爲dmesg命令輸出不帶時間戳,因此使用臨時文件tmp_tag來定位上次已經檢查過的行數 # 當tmp_tag不存在,或者tmp_tag中的內容非法(沒法轉換成數字)生成新的tmp_tag以後腳本就退出 # 以nagios插件的方式給出,修改print相關很容易改爲其餘方式 import subprocess import re import sys import os.path class Check_errorinfo_in_dmesg(): def __init__(self,target_info): self.target_info=target_info self.dmesg_desc='' self.alert_infos={} self.now_rows=0 self.last_rows=0 self.tmp_tag='/tmp/'+os.path.basename(sys.argv[0])+'.tag' def get_dmesg_output(self): command=['dmesg'] self.dmesg_desc=subprocess.Popen(command,stdout=subprocess.PIPE).stdout self.now_rows=len(self.dmesg_desc.readlines()) self.dmesg_desc=subprocess.Popen(command,stdout=subprocess.PIPE).stdout def get_display_string(self): return_message='' for ip in self.alert_infos: return_message=return_message+ip+':'+str(self.alert_infos[ip])+' - ' print(return_message) def exception_exit(self): with open(self.tmp_tag,'w') as tmp_file: tmp_file.write(str(self.now_rows)) sys.exit(2) def normal_exit(self): with open(self.tmp_tag,'w') as tmp_file: tmp_file.write(str(self.now_rows)) print('OK') sys.exit(0) def handle_lineno_in_tmpfile(self): try: with open(self.tmp_tag) as tmp_file: self.last_rows=tmp_file.read() self.last_rows=int(self.last_rows.strip()) except Exception: self.normal_exit() if self.now_rows==self.last_rows: print('OK') sys.exit(0) if self.now_rows-self.last_rows<0: self.normal_exit() def check_errorinfo(self): self.get_dmesg_output() self.handle_lineno_in_tmpfile() target_re=re.compile(self.target_info) line_number=1 for line_info in self.dmesg_desc: if line_number-self.last_rows<0: line_number+=1 continue line_infos=target_re.search(line_info) if line_infos: line_info=line_info.strip() try: self.alert_infos[line_infos.group(1)]+=1 except KeyError: self.alert_infos[line_infos.group(1)]=1 if self.alert_infos: self.get_display_string() self.exception_exit() else: self.normal_exit() if __name__ == '__main__': Check_errorinfo_in_dmesg('nfs: (.*)').check_errorinfo()