wzp
2021-07-28 864986e4cad03f6b9bba9a7e65379db496b62a6a
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
 
namespace MonitorAlarm
{
    class Program
    {
        public static string userid = System.Configuration.ConfigurationManager.AppSettings["userid"];
        public static string account = System.Configuration.ConfigurationManager.AppSettings["account"];
        public static string password = System.Configuration.ConfigurationManager.AppSettings["password"];
        public static string Mobiles = System.Configuration.ConfigurationManager.AppSettings["Mobiles"];
 
        static void Main(string[] args)
        {
            //设置控制台前景/背景色
            Console.ForegroundColor = ConsoleColor.White;
            Console.BackgroundColor = ConsoleColor.DarkBlue;
            Console.Title = "系统异常监控";
            Console.SetBufferSize(1000, 300);
 
            Thread th = new Thread(new ThreadStart(_Run));
            th.Start();
            //
            
            Console.Read();
        }
 
        
        protected static void _Run()
        {
            Console.WriteLine("异常监控服务启动...");
            ZjunNewSMSNHandler sms = new ZjunNewSMSNHandler();
 
            int SendCount = 0;
            while (true)
            {
                Console.WriteLine("开始检查..."+DateTime.Now.ToString());
 
                try
                {
                    int count = Int32.Parse(DataHander.Instance.GetCount());
                    if (count >= 100 && SendCount<3)
                    {
                        SendCount++;
 
                        Console.BackgroundColor = ConsoleColor.Red;
                        Console.WriteLine($"半个小时前的未发送数据总数:{count}");
                        Console.WriteLine("发送短信告警...");
 
                        string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
                        string sign = gmd5(account + password + timestamp);
                        string content = "【中微】[非常紧急]客户端提交异常,请马上处理!";
                        string result = sms.SendSMS(userid, timestamp, sign, Mobiles, content, "", "");
                        Console.WriteLine($"提交结果:{result}");
                    }
                    else
                    {
                        if(count==0) { SendCount = 0; }
                        Console.BackgroundColor = ConsoleColor.DarkGreen;
                        Console.WriteLine($"半个小时前的未发送数据总数:{count}");
                    }
                }
                catch (Exception ex)
                {
                    Console.BackgroundColor = ConsoleColor.Red;
                    Console.WriteLine($"异常:{ex.Message}");
                }
 
                Console.BackgroundColor = ConsoleColor.DarkBlue;
                Console.WriteLine("检查结束..." + DateTime.Now.ToString());
                Thread.Sleep(60000);
            }
        }
 
        public static String gmd5(string s)
        {
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
            bytes = md5.ComputeHash(bytes);
            md5.Clear();
            string ret = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
                //ret += Convert.ToString(bytes[i],16).PadLeft(2,'0').ToUpper();//转换为大写
            }
            return ret.PadLeft(32, '0');
        }
    }
 
}