nagios监控温度脚本-创新互联

           nagios监控温度之python脚本

10余年的湘桥网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整湘桥建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联公司从事“湘桥网站设计”,“湘桥网站推广”以来,每个客户项目都认真落实执行。

  由于机房温度夏天变高了,就写了一个脚本监控温度,利用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:
        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()

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


名称栏目:nagios监控温度脚本-创新互联
文章转载:http://pwwzsj.com/article/dsgejp.html