예제 #1
0
        public MonitorState GetCurrentState()
        {
            List <ProcessInfo> processes = null;
            //CollectorState agentState = CollectorState.NotAvailable;
            StringBuilder sbRawDetails  = new StringBuilder();
            object        reportedValue = "";
            MonitorState  currentState  = new MonitorState()
            {
                ForAgent = Description,
                State    = CollectorState.NotAvailable
            };

            try
            {
                processes          = GetValue();
                currentState.State = GetStateFromValue(processes);

                if (processes == null || processes.Count == 0)
                {
                    currentState.CurrentValue     = "No processes!";
                    currentState.CurrentValueUnit = "";
                    sbRawDetails.Append("No process found!");
                }
                else if (ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceCount ||
                         ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceNotRunning ||
                         ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceRunning)
                {
                    currentState.CurrentValue     = processes.Count;
                    currentState.CurrentValueUnit = "process(es)";
                    sbRawDetails.Append($"{processes.Count} process(es)");
                }
                else if (ProcessCollectorTestType == ProcessCollectorTestType.ThreadCount)
                {
                    currentState.CurrentValue = (from ProcessInfo p in processes
                                                 select p.ThreadCount).Max();
                    currentState.CurrentValueUnit = "Thread(s)";
                    sbRawDetails.Append($"{processes.Count} process(es), Threads(Max): {currentState.CurrentValue}");
                }
                else if (ProcessCollectorTestType == ProcessCollectorTestType.ProcessorUsage)
                {
                    var maxCPU = (from ProcessInfo p in processes
                                  select p.CPUPerc).Max();
                    currentState.CurrentValue     = maxCPU.ToString("0.0");
                    currentState.CurrentValueUnit = "%";
                }
                else if (ProcessCollectorTestType == ProcessCollectorTestType.MemoryUsage)
                {
                    var maxMem = (from ProcessInfo p in processes
                                  select p.WorkingSetSize).Max();
                    currentState.CurrentValue     = FormatUtils.FormatFileSize(maxMem, false);
                    currentState.CurrentValueUnit = FormatUtils.FormatFileSizeUnitOnly(maxMem);
                }
            }
            catch (Exception ex)
            {
                currentState.State = CollectorState.Error;
                sbRawDetails.AppendLine(ex.Message);
            }

            foreach (ProcessInfo p in processes)
            {
                MonitorState processState = new MonitorState()
                {
                    ForAgent         = p.ToString(),
                    State            = CollectorState.NotAvailable,
                    CurrentValue     = $"Threads:{p.ThreadCount}, Id:{p.ProcessId}",
                    CurrentValueUnit = ""
                };
                if (ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceCount ||
                    ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceRunning ||
                    ProcessCollectorTestType == ProcessCollectorTestType.ProcessInstanceNotRunning)
                {
                    processState.CurrentValue = $"Id:{p.ProcessId}";
                }
                else if (ProcessCollectorTestType == ProcessCollectorTestType.ThreadCount)
                {
                    processState.CurrentValue     = p.ThreadCount;
                    processState.CurrentValueUnit = "Thread(s)";
                }
                else if (ProcessCollectorTestType == ProcessCollectorTestType.ProcessorUsage)
                {
                    processState.CurrentValue     = p.CPUPerc.ToString("0.0");
                    processState.CurrentValueUnit = "%";
                }
                else if (ProcessCollectorTestType == ProcessCollectorTestType.MemoryUsage)
                {
                    processState.CurrentValue     = FormatUtils.FormatFileSize(p.WorkingSetSize, false);
                    processState.CurrentValueUnit = FormatUtils.FormatFileSizeUnitOnly(p.WorkingSetSize);
                }

                processState.RawDetails = $"ProcessId:{p.ProcessId}\r\n" +
                                          $"Path:{p.Path}\r\n" +
                                          $"CMD:{p.CommandLine}\r\n" +
                                          $"Threads:{p.ThreadCount}\r\n" +
                                          $"CPU:{p.CPUPerc.ToString("0.00")}%\r\n" +
                                          $"Mem:{FormatUtils.FormatFileSize(p.WorkingSetSize)}";


                currentState.ChildStates.Add(processState);
            }

            return(currentState);
        }