protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());

            HiddenField1.Value = "update";
            var monitorCommand = new MonitorCommand()
            {
                MonitorCommandId = id,
                Name             = GetGridViewText(e, 2),
                Type             = GetGridViewText(e, 3),
                Arg1             = GetGridViewText(e, 4),
                Arg2             = GetGridViewText(e, 5)
            };
            var monitorCommandHelp =
                monitorCommandHelps.Find
                    (x => x.Type == monitorCommand.Type);

            if (IsGrdInputValid(monitorCommand))
            {
                return;
            }
            monitorCommand.Unit = monitorCommandHelp.Unit;
            monitorDb.UpsertMonitorCommand(monitorCommand);
            GridView1.EditIndex = -1;
            LoadData();
        }
Exemple #2
0
 public async static Task <int> Main(string[] args) =>
 await new CommandLineBuilder()
 .AddCommand(ListProcessesCommand.Create())
 .AddCommand(MonitorCommand.Create())
 .AddCommand(CreateConfigurationFileCommand.Create())
 .UseDefaults()
 .Build()
 .InvokeAsync(args);
Exemple #3
0
        public static string GetColumnTitleLimit(
            MonitorCommand monitorCommand,
            List <MonitorCommandLimit> monitorCommandLimits)
        {
            var returnValue         = string.Empty;
            var monitorCommandLimit = monitorCommandLimits
                                      .Find(x => x.Type == monitorCommand.Type);

            if (monitorCommandLimit != null)
            {
                var warningLimit = $"Warning Limit: {monitorCommandLimit.WarningLimit}";
                var errorLimit   = $"Error Limit: {monitorCommandLimit.ErrorLimit}";
                returnValue = $"{warningLimit}, {errorLimit}";
            }
            return(returnValue);
        }
        private bool IsGrdInputValid(MonitorCommand monitorCommand)
        {
            var returnValue = false;


            var monitorCommandHelp = monitorCommandHelps.Find
                                         (x => x.Type == monitorCommand.Type.Trim());

            if (monitorCommandHelp == null)
            {
                lblGrdError.Text += "<br/>Type is incorrect";
                returnValue       = true;
            }
            else
            {
                if (monitorCommand.Name.Length == 0)
                {
                    lblGrdError.Text += "Name is required";
                    returnValue       = true;
                }
                if (
                    (monitorCommand.Arg1.Length == 0 && monitorCommandHelp.Arg1 != string.Empty) ||
                    (monitorCommand.Arg2.Length > 0 && monitorCommandHelp.Arg1.Length == 0))
                {
                    lblGrdError.Text += "<br/>Arg1 is incorrect";
                    returnValue       = true;
                }
                if (
                    (monitorCommand.Arg1.Length == 0 && monitorCommandHelp.Arg1 != string.Empty) ||
                    (monitorCommand.Arg2.Length > 0 && monitorCommandHelp.Arg2.Length == 0))
                {
                    lblGrdError.Text += "<br/>Arg2 is incorrect";
                    returnValue       = true;
                }
            }
            return(returnValue);
        }
Exemple #5
0
        private static string EmailDescription(
            Agent agent,
            MonitorCommand monitorCommand,
            MonitorValue monitorValue)
        {
            var stringBuilder = new StringBuilder();

            stringBuilder.Append("Monitor limit exceeded for ");
            stringBuilder.Append(monitorCommand.Name);
            if (!string.IsNullOrEmpty(monitorCommand.Arg1))
            {
                stringBuilder.Append($": {monitorCommand.Arg1}");
            }
            if (!string.IsNullOrEmpty(monitorCommand.Arg2))
            {
                stringBuilder.Append($": {monitorCommand.Arg2}");
            }
            if (agent != null)
            {
                stringBuilder.Append($" for Agent: ${agent.ScreenName}");
            }
            stringBuilder.Append($" - {monitorValue.Value}{monitorCommand.Unit}");
            return(stringBuilder.ToString());
        }
        private void InsertMonitorCommands()
        {
            HiddenField1.Value = "Insert";
            var monitorCommand = new MonitorCommand()
            {
                Name = txtName.Text.Trim(),
                Type = ddlType.SelectedValue.Trim(),
                Arg1 = txtArg1.Text.Trim(),
                Arg2 = txtArg2.Text.Trim()
            };

            try
            {
                monitorCommand.Unit =
                    monitorCommandHelps.Find
                        (x => x.Type == monitorCommand.Type).Unit;
                monitorDb.InsertMonitorCommand(monitorCommand);
                Response.Redirect(Request.RawUrl);
            }
            catch (Exception ex)
            {
                throw new Exception($"Exception:{ex.Message}");
            }
        }
Exemple #7
0
        /// <summary>
        /// 移除监控的设备或没有指定设备就移除整个指令,或对无效(心跳超时)指令移除
        /// </summary>
        /// <param name="info">指令内容:service='halt',device,id,all='true'</param>
        /// <param name="client">客户端连接信息</param>
        public void Haltdev(NameValueCollection info, ClientInfo client)
        {
            if (null == info)
            {
                info = new NameValueCollection();
            }
            string cmdid = info["id"];
            string devid = info["device"];
            string all   = info["all"];

            if (!string.IsNullOrEmpty(devid))
            {
                devid += "," + devid + ",";
                devid  = devid.Replace(",,", ",");
            }
            if (!string.IsNullOrEmpty(all))
            {
                all = all.ToLower();
            }
            ServiceTool.LogMessage("停止巡检:" + cmdid + "\r\n设备:" + devid, null, EventLogEntryType.Warning);
            //移除指定的设备监控,或移除指令,或移除无效指令
            TimeSpan ts    = new TimeSpan(0, 5, 0);
            DateTime dtNow = DateTime.Now;

            ClientMap[] maps = this.clientlist.ToArray();
            for (int i = maps.Length - 1; i > -1; i--)
            {
                if (null == maps[i] || null == maps[i].client.Client || !maps[i].client.Client.Connected)
                {
                    MonitorCommand[] cmdsd = new MonitorCommand[0];
                    if (null != maps[i])
                    {
                        maps[i].cmds.Clear();
                    }
                    this.clientlist.Remove(maps[i]);
                    ServiceTool.LogMessage("清除指令" + maps[i].client.IPEndPoint, null, EventLogEntryType.Warning);
                    continue;
                }
                MonitorCommand[] cmds = maps[i].cmds.ToArray();
                for (int m = 0; m < cmds.Length; m++)
                {
                    MonitorCommand cmd = cmds[m];
                    if (cmdid != cmd.cmdid && dtNow - cmd.dtbeat > ts)
                    {
                        maps[i].cmds.Remove(cmd);
                        continue;
                    }
                    if (cmdid == cmd.cmdid && string.IsNullOrEmpty(devid))
                    {
                        maps[i].cmds.Remove(cmd);
                    }
                    else if ((cmdid == cmd.cmdid || "true" == all) && cmd.devices.Contains(devid))
                    {
                        cmd.devices = cmd.devices.Replace(devid, ",");
                    }
                }
                if (maps[i].cmds.Count < 1)
                {
                    this.clientlist.Remove(maps[i]);
                }
                //移除巡检设备
                if ("true" == all && !string.IsNullOrEmpty(devid))
                {
                    devid = devid.Replace(",", "");
                    DeviceBase[] devices = this.devlist.ToArray();
                    for (int m = 0; m < devices.Length; m++)
                    {
                        if (devid != devices[m].DevID)
                        {
                            continue;
                        }
                        this.devlist.Remove(devices[m]);
                        devices[m].StopGather();
                        ServiceTool.LogMessage("清除设备:" + devices[m].DevID, null, EventLogEntryType.Warning);
                        break;
                    }
                }
            }
            //不是持续巡检设备,在没有接收指令时停止巡检
            DeviceBase[] devs = this.devlist.ToArray();
            for (int i = 0; i < devs.Length; i++)
            {
                if (null == devs[i])
                {
                    this.devlist.Remove(devs[i]);
                    continue;
                }
                if (devs[i].IsPatrol)
                {
                    continue;
                }
                devid = "," + devs[i].DevID + ",";
                bool isfind = false;
                maps = this.clientlist.ToArray();
                for (int m = 0; m < maps.Length; m++)
                {
                    MonitorCommand[] cmds = maps[m].cmds.ToArray();
                    for (int k = 0; k < cmds.Length; k++)
                    {
                        if (cmds[k].devices.Contains(devid))
                        {
                            isfind = true;
                        }
                        if (isfind)
                        {
                            break;
                        }
                    }
                    if (isfind)
                    {
                        break;
                    }
                }
                if (isfind)
                {
                    break;
                }
                this.devlist.Remove(devs[i]);
                devs[i].StopGather();
                ServiceTool.LogMessage("清除设备:" + devs[i].DevID, null, EventLogEntryType.Warning);
            }
            this.returnInfo(info, client, true);
        }
Exemple #8
0
        /// <summary>
        /// 监控设备,巡检指令心跳,增加巡检设备
        /// 间隔超过5分钟自动认为该指令退出
        /// </summary>
        /// <param name="info">指令内容:service='monitor',device,id,patrol='true'(patrol持续执行)</param>
        /// <param name="client">客户端连接信息</param>
        public void Monitordev(NameValueCollection info, ClientInfo client)
        {
            if (null == info || null == client || null == client.Client || !client.Client.Connected)
            {
                return;
            }
            string cmdid = info["id"];
            string devid = info["device"];

            if (string.IsNullOrEmpty(cmdid))
            {
                return;
            }
            ServiceTool.LogMessage("启动巡检:" + cmdid + "\r\n设备:" + devid, null, EventLogEntryType.Warning);
            //增加客户端连接信息
            ClientMap[] cms = this.clientlist.ToArray();
            ClientMap   cm  = null;

            for (int i = cms.Length - 1; i > -1; i--)
            {
                if (null == cms[i] || null == cms[i].client.Client || !cms[i].client.Client.Connected)
                {
                    continue;
                }
                if (client != cms[i].client)
                {
                    continue;
                }
                cm = cms[i];
                break;
            }
            if (null == cm)
            {
                cm        = new ClientMap();
                cm.client = client;
                this.clientlist.Add(cm);
            }
            //添加监听指令
            MonitorCommand cmd = null;

            foreach (MonitorCommand mcd in cm.cmds)
            {
                if (cmdid == mcd.cmdid)
                {
                    cmd = mcd;
                    break;
                }
            }
            if (null == cmd)
            {
                cmd       = new MonitorCommand();
                cmd.cmdid = cmdid;
                cm.cmds.Add(cmd);
            }
            cmd.dtbeat = DateTime.Now;

            //增加监听巡检设备
            if (string.IsNullOrEmpty(devid))
            {
                this.returnInfo(info, client, true);
                return;
            }
            devid = "," + devid + ",";
            devid = devid.Replace(",,", ",");
            if (cmd.devices.Contains(devid))
            {
                this.returnInfo(info, client, true);
                return;
            }
            //加入设备列表,并增加巡检设备
            if (string.IsNullOrEmpty(cmd.devices))
            {
                cmd.devices = devid;
            }
            else
            {
                cmd.devices += devid.Substring(1);
            }
            bool ispatrol = false;

            if ("true" == info["patrol"])
            {
                ispatrol = true;
            }
            devid = devid.Replace(",", "");
            this.addDevice(devid, ispatrol);
            this.returnInfo(info, client, true);
        }
 /// <summary>
 /// 移除监控的设备或没有指定设备就移除整个指令,或对无效(心跳超时)指令移除
 /// </summary>
 /// <param name="info">指令内容:service='halt',device,id,all='true'</param>
 /// <param name="client">客户端连接信息</param>
 public void Haltdev(NameValueCollection info, ClientInfo client)
 {
     if (null == info)
         info = new NameValueCollection();
     string cmdid = info["id"];
     string devid = info["device"];
     string all = info["all"];
     if (!string.IsNullOrEmpty(devid))
     {
         devid += "," + devid + ",";
         devid = devid.Replace(",,", ",");
     }
     if (!string.IsNullOrEmpty(all))
         all = all.ToLower();
     ServiceTool.LogMessage("停止巡检:" + cmdid + "\r\n设备:" + devid, null, EventLogEntryType.Warning);
     //移除指定的设备监控,或移除指令,或移除无效指令
     TimeSpan ts = new TimeSpan(0, 5, 0);
     DateTime dtNow = DateTime.Now;
     ClientMap[] maps = this.clientlist.ToArray();
     for (int i = maps.Length - 1; i > -1; i--)
     {
         if (null == maps[i] || null == maps[i].client.Client || !maps[i].client.Client.Connected)
         {
             MonitorCommand[] cmdsd = new MonitorCommand[0];
             if (null != maps[i])
                 maps[i].cmds.Clear();
             this.clientlist.Remove(maps[i]);
             ServiceTool.LogMessage("清除指令" + maps[i].client.IPEndPoint, null, EventLogEntryType.Warning);
             continue;
         }
         MonitorCommand[] cmds = maps[i].cmds.ToArray();
         for (int m = 0; m < cmds.Length; m++)
         {
             MonitorCommand cmd = cmds[m];
             if (cmdid != cmd.cmdid && dtNow - cmd.dtbeat > ts)
             {
                 maps[i].cmds.Remove(cmd);
                 continue;
             }
             if (cmdid == cmd.cmdid && string.IsNullOrEmpty(devid))
                 maps[i].cmds.Remove(cmd);
             else if ((cmdid == cmd.cmdid || "true" == all) && cmd.devices.Contains(devid))
                 cmd.devices = cmd.devices.Replace(devid, ",");
         }
         if (maps[i].cmds.Count < 1)
             this.clientlist.Remove(maps[i]);
         //移除巡检设备
         if ("true" == all && !string.IsNullOrEmpty(devid))
         {
             devid = devid.Replace(",", "");
             DeviceBase[] devices = this.devlist.ToArray();
             for (int m = 0; m < devices.Length; m++)
             {
                 if (devid != devices[m].DevID)
                     continue;
                 this.devlist.Remove(devices[m]);
                 devices[m].StopGather();
                 ServiceTool.LogMessage("清除设备:" + devices[m].DevID, null, EventLogEntryType.Warning);
                 break;
             }
         }
     }
     //不是持续巡检设备,在没有接收指令时停止巡检
     DeviceBase[] devs = this.devlist.ToArray();
     for (int i = 0; i < devs.Length; i++)
     {
         if (null == devs[i])
         {
             this.devlist.Remove(devs[i]);
             continue;
         }
         if (devs[i].IsPatrol)
             continue;
         devid = "," + devs[i].DevID + ",";
         bool isfind = false;
         maps = this.clientlist.ToArray();
         for (int m = 0; m < maps.Length; m++)
         {
             MonitorCommand[] cmds = maps[m].cmds.ToArray();
             for (int k = 0; k < cmds.Length; k++)
             {
                 if (cmds[k].devices.Contains(devid))
                     isfind = true;
                 if (isfind) break;
             }
             if (isfind) break;
         }
         if (isfind) break;
         this.devlist.Remove(devs[i]);
         devs[i].StopGather();
         ServiceTool.LogMessage("清除设备:" + devs[i].DevID, null, EventLogEntryType.Warning);
     }
     this.returnInfo(info, client, true);
 }
        /// <summary>
        /// 监控设备,巡检指令心跳,增加巡检设备
        /// 间隔超过5分钟自动认为该指令退出
        /// </summary>
        /// <param name="info">指令内容:service='monitor',device,id,patrol='true'(patrol持续执行)</param>
        /// <param name="client">客户端连接信息</param>
        public void Monitordev(NameValueCollection info, ClientInfo client)
        {
            if (null == info || null == client || null == client.Client || !client.Client.Connected)
                return;
            string cmdid = info["id"];
            string devid = info["device"];
            if (string.IsNullOrEmpty(cmdid))
                return;
            ServiceTool.LogMessage("启动巡检:" + cmdid + "\r\n设备:" + devid, null, EventLogEntryType.Warning);
            //增加客户端连接信息
            ClientMap[] cms = this.clientlist.ToArray();
            ClientMap cm = null;
            for (int i = cms.Length - 1; i > -1; i--)
            {
                if (null == cms[i] || null == cms[i].client.Client || !cms[i].client.Client.Connected)
                    continue;
                if (client != cms[i].client)
                    continue;
                cm = cms[i];
                break;
            }
            if (null == cm)
            {
                cm = new ClientMap();
                cm.client = client;
                this.clientlist.Add(cm);
            }
            //添加监听指令
            MonitorCommand cmd = null;
            foreach (MonitorCommand mcd in cm.cmds)
                if (cmdid == mcd.cmdid)
                {
                    cmd = mcd;
                    break;
                }
            if (null == cmd)
            {
                cmd = new MonitorCommand();
                cmd.cmdid = cmdid;
                cm.cmds.Add(cmd);
            }
            cmd.dtbeat = DateTime.Now;

            //增加监听巡检设备
            if (string.IsNullOrEmpty(devid))
            {
                this.returnInfo(info, client, true);
                return;
            }
            devid = "," + devid + ",";
            devid = devid.Replace(",,", ",");
            if (cmd.devices.Contains(devid))
            {
                this.returnInfo(info, client, true);
                return;
            }
            //加入设备列表,并增加巡检设备
            if (string.IsNullOrEmpty(cmd.devices))
                cmd.devices = devid;
            else
                cmd.devices += devid.Substring(1);
            bool ispatrol = false;
            if ("true" == info["patrol"])
                ispatrol = true;
            devid = devid.Replace(",", "");
            this.addDevice(devid, ispatrol);
            this.returnInfo(info, client, true);
        }
Exemple #11
0
 private static string EmailSubject(Agent agent, MonitorCommand monitorCommand)
 {
     return($"Monitor limit is exceeded ${(agent?.ScreenName) ?? string.Empty}: {monitorCommand.Name}");
 }