-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
116 lines (99 loc) · 4.41 KB
/
Copy pathProgram.cs
File metadata and controls
116 lines (99 loc) · 4.41 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ProcessesMonitoring
{
class Program
{
public static bool UserCancelled()
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo k = Console.ReadKey(true);
if ((k.Key >= ConsoleKey.Backspace && k.Key <= ConsoleKey.OemClear))
{ // the user pressed any key
return true; // out of the loop
}
}
return false;
}
public class P_Process
{
public Process process { get; set; }
public DataProcessor data_processor { get; set; }
}
static void Main(string[] args)
{
string postUrl = "";
string processToMonitor = "";
int intervalMS = 10000;
int rotationalNotifyTime = 600000; //600000; // in ms
intervalMS = int.Parse(ConfigurationManager.AppSettings["pollingIntervalMS"]);
rotationalNotifyTime = int.Parse(ConfigurationManager.AppSettings["rotationNotifyTimeMS"]);
postUrl = ConfigurationManager.AppSettings["SlackURL"];
processToMonitor = ConfigurationManager.AppSettings["ProcessName"];
CultureInfo culture = new CultureInfo("en-GB");
Notifier.setDestinationUrl(postUrl);
Notifier.start(rotationalNotifyTime); //10 min
Process[] localAll = Process.GetProcessesByName(processToMonitor);
List<Process> sortedProcesses = new List<Process>(localAll);
sortedProcesses.Sort((x, y) => x.StartTime.CompareTo(y.StartTime));
List<P_Process> monitored_processes = new List<P_Process>();
string StartMessage = "Starting Monitoring:\n";
foreach (var process in sortedProcesses)
{
StartMessage += $"{process.Id} which started at {process.StartTime}\n";
Console.WriteLine($"Adding ({process.ProcessName})[{process.StartTime}] ({process.Id}) to monitor");
ETWwrapper.addProcess(process.Id);
P_Process monitoredProcess = new P_Process();
monitoredProcess.process = process;
monitoredProcess.data_processor = new DataProcessor(intervalMS, 6, process.Id, rotationalNotifyTime/10000);
monitored_processes.Add(monitoredProcess);
}
StartMessage += ":good-luck: :good-luck: :good-luck: :good-luck: :good-luck: :good-luck: :good-luck: :good-luck: :good-luck:";
Notifier.Notify(StartMessage);
string logName = $"{processToMonitor}_{DateTime.Now.Day}_{DateTime.Now.Month}__{DateTime.Now.Hour}_{DateTime.Now.Minute}.csv";
Console.WriteLine($"Writting to {logName}\nConfigurations:");
var appSettings = ConfigurationManager.AppSettings;
foreach (var key in appSettings.AllKeys)
{
Console.WriteLine("{0}={1}", key, appSettings[key]);
}
Log.Start(logName);
ETWwrapper.start();
string header = "Time";
foreach (var process in monitored_processes)
{
header += $",S({process.process.Id}),R({process.process.Id})";
}
Log.Write(header);
while (!UserCancelled())
{
string outputData = "";
outputData += $"{DateTime.Now.ToString(culture)}";
foreach (var process in monitored_processes)
{
SessionData somedata = ETWwrapper.getData(process.process.Id);
process.data_processor.Append(somedata);
double milliseconds_passed_since_data = DateTime.Now.Subtract(somedata.timestamp).TotalMilliseconds;
if (milliseconds_passed_since_data > intervalMS)
{
outputData += $",{0},{0}";
}
else
{
outputData += $",{somedata.sent},{somedata.received}";
}
}
Log.Write(outputData);
Thread.Sleep(intervalMS);
}
}
}
}