Esempio n. 1
0
        // returns pid info from pid array stored in class
        public PidInfoData[] QueryMultipleProcesses(List <int> pids)
        {
            if (pids is null)
            {
                throw new ArgumentNullException(nameof(pids));
            }

            var data = new List <PidInfoData>();

            for (int i = 0; i < pids.Count; i++)
            {
                int pid       = pids[i];
                var cpuMemory = processInfoManager.GetCpuMemoryUsageForPid(pid);
                var network   = portStatisticsManager.GetBytesSentLastSecondForPid(pid);
                data.Add(new PidInfoData(cpuMemory, network));
            }
            ;

            return(data.ToArray());
        }
Esempio n. 2
0
        bool ProcessContext(HttpListenerContext context)
        {
            // localhost:port/?pid1=port1,pid2=port2,pid3=port3, ....
            var q = context.Request.RawUrl;

            if (q.StartsWith("/"))
            {
                q = q.Substring(1);
            }
            if (q.StartsWith("?"))
            {
                q = q.Substring(1);
            }

            var parts = q.Split(',');

            var binaryStream = new MemoryStream();
            var o            = new StreamWriter(binaryStream);

            int   pid;
            int   cpu;
            long  memory;
            float network;

            o.WriteLine("{");
            for (int i = 0; i < parts.Length; i++)
            {
                var part    = parts[i];
                var pidPort = part.Split('=');
                if (!int.TryParse(pidPort[0], out pid))
                {
                    return(false);
                }
                if (pid == 0)
                {
                    cpu     = 0;
                    memory  = 0;
                    network = 0;
                }
                else
                {
                    var cpuMemory = processInfoManager.GetCpuMemoryUsageForPid(pid);
                    cpu     = (int)Math.Round(cpuMemory.cpuUsage * 100.0);
                    memory  = cpuMemory.memoryUsageBytes;
                    network = portStatisticsManager.GetBytesSentLastSecondForPid(pid);
                }
                o.Write("\t\"{0}\":{{ \"pid\":{0}, \"cpu\":{1}, \"memory\":{2}, \"network\":{3} }}",
                        pid,
                        cpu,
                        memory,
                        network
                        );

                if (i < parts.Length - 1)
                {
                    o.Write(",");
                }
                o.WriteLine();
            }
            o.Write("}");

            o.Flush();

            context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
            context.Response.ContentType     = "application/json";
            context.Response.ContentLength64 = binaryStream.Length;
            context.Response.OutputStream.Write(binaryStream.GetBuffer(), 0, (int)binaryStream.Length);
            context.Response.OutputStream.Close();

            return(true);
        }