









最近发现服务器上的网站关联的一些应用程序池在未知错误的情况下,会被自动停用。导致网站无法访问,于是所加了一个对应用自动停止的监控。
钉钉上面的操作(需要在钉钉电脑版上设置):
告警、监控、DataWorks 等。只有消息内容包含这些关键词时,机器人才会接收。(注意:部分服务如DataWorks要求关键词必须为DataWorks) 110https://oapi.dingtalk.com/robot/send?access_token=xxxxxx 的链接。请立即复制并妥善保存,这是后续配置告警服务的核心地址调试:
如果无法发送成功,可以通过以JSON格式post请求调试
{
"msgtype": "text",
"text": {
"content": "【IIS告警】这是测试消息"
}
}
打开vs创建控制端程序做好后打包成exe,代码如下:
using System; using System.Net.Http; using System.Text; using Microsoft.Web.Administration; using Newtonsoft.Json; using System.Configuration; class IISMonitor { // 钉钉机器人WebHook private static readonly string DingTalkWebHook = ConfigurationManager.AppSettings["hookToken"]; static void Main(string[] args) { string poolList=ConfigurationManager.AppSettings["poollist"]; Console.WriteLine(poolList); // 1. 获取IIS应用程序池状态 using (ServerManager serverManager = new ServerManager()) { foreach (ApplicationPool pool in serverManager.ApplicationPools) { // 跳过默认池(可选) if (pool.Name == "DefaultAppPool") continue; // Console.WriteLine(pool.Name); // 检查状态 if (pool.State != ObjectState.Started && poolList.Contains(pool.Name+",")) { string alertContent = $"【IIS告警】服务器:{Environment.MachineName},应用池:{pool.Name},状态:{pool.State},时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss}"; // 2. 发送钉钉消息 SendDingTalkMessage(alertContent); } } } } // 发送钉钉消息方法 static void SendDingTalkMessage(string content) { var httpClient = new HttpClient(); var message = new { msgtype = "text", text = new { content = content } }; string json = JsonConvert.SerializeObject(message); var contentBytes = new StringContent(json, Encoding.UTF8, "application/json"); try { var response = httpClient.PostAsync(DingTalkWebHook, contentBytes).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("钉钉消息发送成功"); } } catch (Exception ex) { Console.WriteLine($"发送失败:{ex.Message}"); } } }
为了灵活,请求地址和指定的应用程序池名称放在配置文件中。
在windows 任务计划中添加:

另外也可以在事件管理中选择应用程序池禁用事件,当该事件发生时自动发送警告提醒到钉钉:

右键点击该事件:

设置任务事件提醒


报警效果
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。