Nagios结合短信平台接口实现SMS短信告警

发布时间:2025-01-24 点击:86
前面一篇文章介绍nagios集成微信报警,使用短信平台接口和微信接口思路一样。不能上网的服务器还是需要有个短信报警功能的。废话不多说,直接上代码。
#!/usr/bin/env python#coding:utf-8import urllib2import urllibimport jsonimport uuidimport argparsedef 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 resultdef 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-smscommand_line /usr/local/nagios/python/notifybysms.py \host-@@-$notificationtype$-@@-$hostname$-@@-$hoststate$-@@-$hostaddress$-@@-$hostoutput$-@@-$contactalias$\}define command{command_name notify-service-by-smscommand_line /usr/local/nagios/python/notifybysms.py \service-@@-$notificationtype$-@@-$servicedesc$-@@-$hostalias$-@@-$hostaddress$-@@-$servicestate$-@@-$serviceoutput$-@@-$contactalias$\}templates.cfg模板文件中添加联系人模板:
define contact{name sms-contactservice_notification_period 24x7host_notification_period 24x7service_notification_options w,u,c,r,f,shost_notification_options d,u,r,f,sservice_notification_commands notify-service-by-smshost_notification_commands notify-host-by-smsregister 0}contacts.cfg联系人中添加微信通知联系人,例如:
define contact{contact_name zhangnq-smsuse sms-contactalias?13800000000email admin@sijitao.net}最后在配置service的时候添加zhangnq-sms这个联系人后就可以通过短信平台发送报警邮件了。
参考链接:
2624_2


为什么要租用服务器
查询域名丰寸公司还未实名
怎么测试ping服务器
阿里云服务器低价活动
在阿里云上租服务器多少钱一年
腾讯云服务器续费半年可以吗
上海云服务器怎么建立网站
不赠送网址吗这个-虚拟主机/数据库问题