Monitor a Process's Network Usage

How to monitor per-process network usage in Python?

Following the third section of this guide provided me with all of the information listed in the post, minus latency. Given that you said you already had measuring latency figured out, I assume this isn't an issue.

Logging this to csv/json/whatever is pretty easy, as all of the information is stored in panda data frames.

As this shows the time the process was created, you can use datetime to generate a new timestamp at the time of logging.

I tested this by logging to a csv after the printing_df variable was initialized, and had no issues.

Trying To Pull CPU & Network Usage Information From Performance Monitor

You'll need at least two reads for every counter, at least a second apart to get a usable reading.

Rearrange as needed but you would need to do something like this:

private static IEnumerable<String> GetProcessStatistics(String[] processesTosearch)
{
Process[] processList = Process.GetProcesses();

foreach (string process in processesTosearch)
{
foreach (Process p in processList)
{
if (p.ProcessName == process)
{
StringBuilder sb = new StringBuilder();
PerformanceCounter CPUperformanceCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
PerformanceCounter NETWORKperformanceCounter = new PerformanceCounter("Process", "IO Data Operations/Sec", p.ProcessName);

// set a baseline
CPUperformanceCounter.NextValue();
NETWORKperformanceCounter.NextValue();

Thread.Sleep(1000);

double cpuData = CPUperformanceCounter.NextValue();
double networkData = NETWORKperformanceCounter.NextValue();

sb.AppendLine("ID: " + p.Id.ToString());
sb.AppendLine("NAME: " + p.ProcessName);
sb.AppendLine("CPU USAGE: " + cpuData);
sb.AppendLine("RAM USAGE: " + ConvertToReadableSize(p.PrivateMemorySize64));
sb.AppendLine("NETWORK USAGE: " + networkData);
yield return sb.ToString();
}
}
}
}


Related Topics



Leave a reply



Submit