您好,登錄后才能下訂單哦!
nagios監(jiān)控溫度之python腳本
由于機房溫度夏天變高了,就寫了一個腳本監(jiān)控溫度,利用nagios可以很直觀的實時監(jiān)控,同時本文對于用Python寫nagios腳本的新手來說也是個借鑒,不啰嗦了代碼如下:
ps:由于涉及到密碼問題,使用“*”來代替密碼部分:
#!/usr/bin/env python # This file is part of the Glue Project. # Copyright (C) by SanPolo Co.Ltd. # All rights reserved. # # See http://www.spolo.org/ for more information. # # SanPolo Co.Ltd # http://www.spolo.org/ # Any copyright issues, please contact: spolo@masols.com # writed by wangzhenyu # 2013-07-23 import os import re import sys import paramiko from optparse import OptionParser TEMP_OK=0 TEMP_WR=1 TEMP_CR=2 TEMP_UK=3 usage= """ Usage: check_temp.py [-h | --help] [-H | --host] example: check_temp.py -H 192.168.1.4 """ def check_temp(hostname): a=0 index=0 regex=re.compile(r'Core.*\+(\d+).*\+(\d+).*\+(\d+)') client=paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect('%s' % hostname, 22, username='root', password="*********", timeout=4) stdin,stdout,stderr = client.exec_command("sensors") temp=stdout.readlines() for line in temp: match=regex.search(line) if match == None: pass else: index+=1 a+=int(match.group(1)) result=a/index if result <= 50: print "the average temprature of core: %d" % result sys.exit(TEMP_OK) elif result >50 and result <= 60: print "the temprature is high" sys.exit(TEMP_WR) elif result > 60 and result <= 80: print "so dangerous check it right now" sys.exit(TEMP_CR) else: print "the temprature is very high shutdown please" sys.exit(TEMP_UK) def getoption(): parser=OptionParser(add_help_option=False) parser.add_option("-h",action="store_const", const=1,dest="use_way",help="%s" % usage) parser.add_option("-H",dest="temp_cpu") (options,args)=parser.parse_args() hostname = options.temp_cpu if options.use_way: print usage if options.temp_cpu: check_temp(hostname) getoption()
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。