zabbix实时监控服务短信报警

一.监控服务通过端口的监听状态(批量监控)

批量监控端口(也可以自动发现端口,但是自动发现的监听端口可能含我们不想监控的,这里使用手动添加)

监控linux服务器批量端口脚本check_port.py:

#!/usr/bin/env python
#coding:utf-8
import os, json
portlist=["8089",
        "8080",
        "8001",
        "3306"]
port_list=[]
port_dict={"data":None}
for port in portlist:
  pdict={}
  pdict["{#TCP_PORT}"]=port
  port_list.append(pdict)
port_dict["data"]=port_list
jsonStr = json.dumps(port_dict, sort_keys=True, indent=4)
print jsonStr

监控window服务器批量端口脚本check_port.bat:

@echo off
echo {
echo     "data": [
echo        {
echo            "{#TCP_PORT}": "80"
echo        }, 
echo        {
echo            "{#TCP_PORT}": "3306"
echo        },
echo        {
echo            "{#TCP_PORT}": "9001"
echo        }
echo    ]
echo }

可以创建统一监控模板:

1.添加自动发现规则:


2.添加监控项原型:


3.添加触发器类型


4.报警媒介类型:


5.报警媒介用户:


6.触发动作:



二.报警媒介:调用短信平台接口(sms.py)

#!/usr/bin/python
#-*- coding:utf-8 -*-
import httplib
import urllib
import sys
text0 = sys.argv[1]
text1 = text0.replace('^M','').split(":")
text2 = text1[5].split("1.")[1]
text3 = text2.split("(")
#text4 = '告警主机:'+text3[1]
#text5 = '主机端口: '+text3[0]

host = "api.isms.ihuyi.com"
sms_send_uri = "/webservice/isms.php?method=Submit"

#用户名APIID
account  = "APIID" 
#密码 APIKEY
password = "APIKEY"

def send_sms(text, mobile):
    params = urllib.urlencode({'account': account, 'password' : password, 'content': text, 'mobile':mobile,'format':'json' })
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    conn = httplib.HTTPConnection(host, port=80, timeout=30)
    conn.request("POST", sms_send_uri, params, headers)
    response = conn.getresponse()
    response_str = response.read()
    conn.close()
    return response_str 

if __name__ == '__main__':
    #列表添加报警手机号
    mobilelist = ["86 13688888888","86 13677777777","86 1386666666"]
    text = "服务器异常,异常主机:" + text3[1] + "(" + text3[2] +  ")" + ",异常问题:端口" + text3[0] + ",请及时修复。"
    for mobile in mobilelist:
      print(send_sms(text, mobile))




猜你喜欢

转载自blog.csdn.net/Despredao/article/details/80604833