Windows服务,线上的监控程序,监控指定的服务
1
高胜
2023-03-22 fd423d7d9d5581c6ddf0858b9a5b1989fc18e2c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using HDLMonitorService.Helper;
using HDLMonitorService.UDP;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace HDLMonitorService.ServiceMonitor
{
    public class ServerMonitor
    {
        /// <summary>
        /// 发送时间
        /// </summary>
        public static DateTime sendTime;
        /// <summary>
        /// 发送了多少次
        /// </summary>
        public static int ServerSendCount;
        /// <summary>
        /// 网络延迟次数
        /// </summary>
        public static int InternetDeleyCount;
 
 
        /// <summary>
        /// 监控数据转发服务
        /// </summary>
        public static void monitorServer()
        {
            new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    try
                    {
                        var sendBytes = new byte[29];
                        var groupNameBytes = Encoding.Default.GetBytes("HDLXX");
                        Array.Copy(groupNameBytes, 0, sendBytes, 9, groupNameBytes.Length);
 
                        sendBytes = Packet.SendBytes(sendBytes, 0x3011, 42, 0);
 
                        UDPServer.AsyncBeginSend(new Packet
                        {
                            Bytes = sendBytes,
                            RemoteEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(ConfigurationManager.AppSettings["ServerIp"]), 9999)
                        });
                        sendTime = DateTime.Now;
                        ServerSendCount++;
 
                        //超过6次有问题
                        if (ServerSendCount == 6)
                        {
                            AlarmHp.ServiceAlarm("Server", "服务已经6次没有响应,请尽快检查!", ConfigurationManager.AppSettings["ServerIp"], "国内-旧服务器设备状态服务");
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHp.WriteLog("UDP服务异常", Newtonsoft.Json.JsonConvert.SerializeObject(ex));
                    }
                    Thread.Sleep(1000 * 10);
                }
            }))
            { IsBackground = true }.Start();
        }
    }
}