IIS应用程序池监控实现企业微信报警

IIS跑着很多的站点,万一某一个站点停了,影响用户体验,想要实现应用程序池监控,并通知到运维人员。


首先先解决的是如何获取到程序池里面的状态,我这里找到了IIS的专属命令,可以获取所有的IIS状态,就跟图形化界面一样

C:\Windows\System32\inetsrv\appcmd.exe

参数有很多,这里不细讲,只用到咱们用到的一条,找到一篇写的不错的博客:https://www.cnblogs.com/zcwry/p/iis.html 想要更详细了解的可以参考一下


其次是对接企业微信(Python):

headers = {"Content-Type": "text/plain"}
data = {
        "msgtype": "text",
        "text": {"content": "Hello Word ~"}
        }
r = requests.post(
    url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ff9c5af8-bd9f-468d-b7fa-1111111',        # 这里填上自己的企业微信WebHook
    headers=headers, json=data)

详细的可以参考官方文档:https://work.weixin.qq.com/help?person_id=1&doc_id=13376

import subprocess
import requests
import socket
import time
import re

localIP = socket.gethostbyname(socket.gethostname())

posturl = 'http://grafana.xxx.cn:8086/write?db=sre'

while True:
    time.sleep(10)

    start_str = subprocess.check_output('C:\\Windows\\System32\\inetsrv\\appcmd.exe list apppools /state:started')     # 获取IIS所有运行的站点
    for i in start_str.split():     #转成列表,方便迭代查找
        if re.match('(.*).caizhe.org', i):
            data = "iispoolstatus,ip=%s,name=%s,status=%s rlt=%d" % (localIP, i.strip('"'), "start", 0)
            response = requests.post(posturl, data=data)          # 入库,写入influxDB,方便Grafana展示

            
    stop_str = subprocess.check_output('C:\\Windows\\System32\\inetsrv\\appcmd.exe list apppools /state:stopped')
    if stop_str:        
        for i in stop_str.split():
            if re.match('(.*).caizhe.org', i):
                # 准备写入InfluxDB
                data = "iispoolstatus,ip=%s,name=%s,status=%s rlt=%d" % (localIP, i.strip('"'), "stop", 1)
                response = requests.post(posturl, data=data)
                
                # 准备推送企业微信
                headers = {"Content-Type": "text/plain"}
                data = {
                    "msgtype": "text",
                    "text": {
                        "content": "%s_%s status was stopped" % (localIP, i.strip('"'))
                    }
                }
                r = requests.post(
                    url='https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ff9c5af8-bd9f-468d-b7fa-1111111',
                    headers=headers, json=data)

可以通过nssm将脚本封装成服务,然后一直在后台运行:

写一个bat 文件:

C:\gyKit\nssm.exe install PatchCheck python "E:\MyService\PatchCheck\NETVersionCheck.py"
C:\gyKit\nssm.exe set PatchCheck DisplayName "PatchCheck"
C:\gyKit\nssm.exe set PatchCheck Start SERVICE_DEMAND_START
C:\gyKit\nssm.exe set PatchCheck Description "gyyx"
C:\gyKit\nssm.exe set PatchCheck AppStderr "E:\MyService_LOG\PatchCheck\error.log"
C:\gyKit\nssm.exe set PatchCheck AppStdout "E:\MyService_LOG\PatchCheck\_out.log"
C:\gyKit\nssm.exe set PatchCheck AppRotateFiles 1
C:\gyKit\nssm.exe set PatchCheck AppRotateOnline 1
C:\gyKit\nssm.exe set PatchCheck AppRotateBytes 5120000
pause
C:\gyKit\nssm.exe start "PatchCheck"
pause
sc query iislogclean|findstr /C:STATE
pause

参考链接:https://jingyan.baidu.com/article/d2b1d102e0ee6a5c7e37d4b9.html

发表评论