由于机房温度夏天变高了,就写了一个脚本监控温度,利用nagios可以很直观的实时监控,同时本文对于用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 and result 60 and result