Windows服务,线上的监控程序,监控指定的服务
1
高胜
2023-05-23 cf31c5a262ad9f8fe66e884c4ca82d6c060422ff
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using HDLMonitorService.Entity;
using HDLMonitorService.Helper;
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 ServiceHandler
    {
        public readonly static ReaderWriterLockSlim RWLock = new ReaderWriterLockSlim();
 
        public static Dictionary<string, ServiceInfo> List = new Dictionary<string, ServiceInfo>();
 
        public static void CheckService()
        {
            new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    var keys = new List<string>();
                    try
                    {
                        RWLock.EnterReadLock();
 
                        foreach (var key in List.Keys)
                        {
                            try
                            {
                                var values = List[key];
                                if (!values.IsOnline)
                                {
                                    keys.Add(key);
                                }
                            }
                            catch (Exception ex) { }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHp.WriteLog("服务器监控异常", Newtonsoft.Json.JsonConvert.SerializeObject(ex));
                    }
                    finally { RWLock.ExitReadLock(); }
                    try
                    {
                        RWLock.EnterWriteLock();
                        foreach (var key in keys)
                        {
                            List.Remove(key);
                        }
                    }
                    catch (Exception ex) { LogHp.WriteLog("服务器监控异常", Newtonsoft.Json.JsonConvert.SerializeObject(ex)); }
                    finally { RWLock.ExitWriteLock(); }
                    Thread.Sleep(1000 * 30);
                }
            }))
            { IsBackground= true }.Start();
        }
 
        public static void ReigsterService(ServiceInfo info)
        {
            info.LastRegisterTime = DateTime.Now;
            info.LastHeartTime = DateTime.Now;
            try
            {
                RWLock.EnterWriteLock();
                List[info.Address] = info;
            }
            catch (Exception ex) { LogHp.WriteLog("服务器监控异常", Newtonsoft.Json.JsonConvert.SerializeObject(ex)); }
            finally { RWLock.ExitWriteLock(); }
        }
 
        public static void HeartThread()
        {
            new Thread(new ThreadStart(() =>
            {
                while (true)
                {
                    try
                    {
                        foreach (var key in List.Keys)
                        {
                            var info = List[key];
                            Task.Run(() => 
                            {
                                try
                                {
                                    HttpHp.Post<object>("http://" + info.Address, "");
                                    info.LastHeartTime = DateTime.Now;
                                    info.AlarmCount = 0;
                                    info.ErrorCount = 0;
                                    LogHp.WriteLog("服务器监控异常", Newtonsoft.Json.JsonConvert.SerializeObject(info));
                                }
                                catch (Exception ex)
                                {
                                    info.ErrorCount += 1;
 
                                    if (info.ErrorCount == int.Parse(ConfigurationManager.AppSettings["LoopErrorCount"].ToString()) && info.AlarmCount < int.Parse(ConfigurationManager.AppSettings["LoopAlarmCount"].ToString()))
                                    {
                                        info.ErrorCount = 0;
                                        info.AlarmCount += 1;
 
                                        AlarmHp.ServiceAlarm(info.ServiceName, "服务已经" + ConfigurationManager.AppSettings["LoopErrorCount"].ToString() + "次没有响应,请尽快检查!", info.PublicAddress, "已检查到服务离线,请尽快检查!");
                                    }
                                }
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHp.WriteLog("服务器监控异常", Newtonsoft.Json.JsonConvert.SerializeObject(ex));
                    }
 
                    Thread.Sleep(1000 * int.Parse(ConfigurationManager.AppSettings["LoopSeconds"].ToString()));
                }
            }))
            { IsBackground = true }.Start();
        }
    }
}