前面一篇文章介绍nagios集成微信报警,使用短信平台接口和微信接口思路一样。不能上网的服务器还是需要有个短信报警功能的。废话不多说,直接上代码。
#!/usr/bin/env python #coding:utf-8 import urllib2 import urllib import json import uuid import argparse def url_request(url,values={},method='GET'): if method == 'GET': if len(values) != 0: url_values=urllib.urlencode(values) furl=url+'?'+url_values else: furl=url req=urllib2.Request(furl) elif method == 'POST': data=json.dumps(values,ensure_ascii=False) req=urllib2.Request(url,data) req.get_method=lambda: 'POST' else: pass try: req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36') response = urllib2.urlopen(req) result=json.loads(response.read()) except urllib2.URLError as e: if hasattr(e, 'code'): print 'Error code:',e.code elif hasattr(e, 'reason'): print 'Reason:',e.reason result={} except: result={} return result def send_text_message(content): content_data=content.split('-@@-') notify_type=content_data[0] if notify_type == 'host': type1=content_data[1] host_name=content_data[2] host_state=content_data[3] host_address=content_data[4] host_info=content_data[5] notify_contact=content_data[6] notify_content='** Nagios **\n\nNotification Type: '+ type1 + \ '\nHost: ' + host_name + \ '\nState: ' + host_state + \ '\nAddress: ' + host_address + \ '\nInfo: ' + host_info + '\n' elif notify_type == 'service': type1=content_data[1] service_desc=content_data[2] host_name=content_data[3] host_address=content_data[4] service_state=content_data[5] service_info=content_data[6] notify_contact=content_data[7] notify_content='** Nagios **\n\nNotification Type: '+ type1 + \ '\nService: ' + service_desc + \ '\nHost: ' + host_name + \ '\nAddress: ' + host_address + \ '\nState: ' + service_state + \ '\nInfo: ' + service_info + '\n' else: notify_content='Get nagios message notify info error.\n\nContent: %s' % content notify_contact='13800000000' url="http://192.168.1.250/sms.do" values={ 'loginname':'sijitao', 'password':'blog.nbhao.org', 'content':notify_content, 'mobileid':notify_contact, 'flowid':uuid.uuid1(), } return url_request(url, values, method='GET') def main(): parser=argparse.ArgumentParser(description="Nagios notify by SMS") parser.add_argument("content",default=None,help="notify content,split with -@@-") args = parser.parse_args() content=args.content send_text_message(content) if __name__ == "__main__": main()
这个代码比微信报警简单,send_text_message函数中调用request方法,给短信平台接口发起一个get请求。一个get请求完成后短信就发出去了。
脚本完成后还是一样,放到/usr/local/nagios/python中,在nagios的配置文件commands.cfg添加发送短信命令。例如:
define command{ command_name notify-host-by-sms command_line /usr/local/nagios/python/NotifyBySMS.py "host-@@-$NOTIFICATIONTYPE$-@@-$HOSTNAME$-@@-$HOSTSTATE$-@@-$HOSTADDRESS$-@@-$HOSTOUTPUT$-@@-$CONTACTALIAS$" } define command{ command_name notify-service-by-sms command_line /usr/local/nagios/python/NotifyBySMS.py "service-@@-$NOTIFICATIONTYPE$-@@-$SERVICEDESC$-@@-$HOSTALIAS$-@@-$HOSTADDRESS$-@@-$SERVICESTATE$-@@-$SERVICEOUTPUT$-@@-$CONTACTALIAS$" }
templates.cfg模板文件中添加联系人模板:
define contact{ name sms-contact service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r,f,s host_notification_options d,u,r,f,s service_notification_commands notify-service-by-sms host_notification_commands notify-host-by-sms register 0 }
contacts.cfg联系人中添加微信通知联系人,例如:
define contact{ contact_name zhangnq-sms use sms-contact alias 13800000000 email admin@sijitao.net }
最后在配置service的时候添加zhangnq-sms这个联系人后就可以通过短信平台发送报警邮件了。
参考链接:
评论列表(0条)
谢谢分享