Skip to content

Commit

Permalink
Minor changes to telemetry (#66)
Browse files Browse the repository at this point in the history
* Ensure every main function in program.cs has telemetry.

* Minor telemetry improvements
  • Loading branch information
gfs authored Apr 5, 2019
1 parent e400a3b commit a2b175b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 80 deletions.
57 changes: 18 additions & 39 deletions Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ static void Main(string[] args)
as AssemblyInformationalVersionAttribute[])[0].InformationalVersion;
Log.Information("AttackSurfaceAnalyzerCli v." + version);
Log.Debug(version);
Telemetry.Setup(false);

var argsResult = Parser.Default.ParseArguments<CollectCommandOptions, CompareCommandOptions, MonitorCommandOptions, ExportMonitorCommandOptions, ExportCollectCommandOptions, ConfigCommandOptions>(args)
.MapResult(
(CollectCommandOptions opts) => SetupTelemetryAndRunCollectCommand(opts),
(CollectCommandOptions opts) => RunCollectCommand(opts),
(CompareCommandOptions opts) => RunCompareCommand(opts),
(MonitorCommandOptions opts) => RunMonitorCommand(opts),
(ExportCollectCommandOptions opts) => RunExportCollectCommand(opts),
Expand Down Expand Up @@ -321,7 +322,6 @@ private static int RunExportCollectCommand(ExportCollectCommandOptions opts)
Log.Debug("Entering RunExportCollectCommand");

DatabaseManager.SqliteFilename = opts.DatabaseFilename;
Telemetry.Setup(false);
Dictionary<string, string> StartEvent = new Dictionary<string, string>();
StartEvent.Add("Version", Helpers.GetVersionString());
StartEvent.Add("OutputPathSet", (opts.OutputPath != null).ToString());
Expand Down Expand Up @@ -518,7 +518,6 @@ private static int RunExportMonitorCommand(ExportMonitorCommandOptions opts)
Logger.Setup(false, opts.Verbose);
#endif
DatabaseManager.SqliteFilename = opts.DatabaseFilename;
Telemetry.Setup(false);

Dictionary<string, string> StartEvent = new Dictionary<string, string>();
StartEvent.Add("Version", Helpers.GetVersionString());
Expand Down Expand Up @@ -574,7 +573,6 @@ private static int RunMonitorCommand(MonitorCommandOptions opts)
#endif
AdminOrQuit();
Filter.LoadFilters(opts.FilterLocation);
Telemetry.Setup(false);
Dictionary<string, string> StartEvent = new Dictionary<string, string>();
StartEvent.Add("Version", Helpers.GetVersionString());
Telemetry.Client.TrackEvent("Begin monitoring", StartEvent);
Expand Down Expand Up @@ -995,35 +993,6 @@ public static void AdminOrQuit()
}
}

public static int SetupTelemetryAndRunCollectCommand(CollectCommandOptions opts)
{

Log.Debug("Before telemetry");
try
{
Telemetry.Setup(false);
Dictionary<string, string> StartEvent = new Dictionary<string, string>();
StartEvent.Add("Version", Helpers.GetVersionString());
StartEvent.Add("Files", opts.EnableFileSystemCollector.ToString());
StartEvent.Add("Ports", opts.EnableNetworkPortCollector.ToString());
StartEvent.Add("Users", opts.EnableUserCollector.ToString());
StartEvent.Add("Certificates", opts.EnableCertificateCollector.ToString());
StartEvent.Add("Registry", opts.EnableRegistryCollector.ToString());
StartEvent.Add("Service", opts.EnableServiceCollector.ToString());

Telemetry.Client.TrackEvent("Begin collecting", StartEvent);
}
catch (Exception e)
{
Log.Debug(e.GetType().ToString());
Log.Debug(e.Message);
}
Log.Debug("After telemetry");
return RunCollectCommand(opts);


}

public static int RunCollectCommand(CollectCommandOptions opts)
{
#if DEBUG
Expand All @@ -1033,6 +1002,16 @@ public static int RunCollectCommand(CollectCommandOptions opts)
#endif
int returnValue = (int)ERRORS.NONE;
AdminOrQuit();
Dictionary<string, string> StartEvent = new Dictionary<string, string>();
StartEvent.Add("Version", Helpers.GetVersionString());
StartEvent.Add("Files", opts.EnableFileSystemCollector.ToString());
StartEvent.Add("Ports", opts.EnableNetworkPortCollector.ToString());
StartEvent.Add("Users", opts.EnableUserCollector.ToString());
StartEvent.Add("Certificates", opts.EnableCertificateCollector.ToString());
StartEvent.Add("Registry", opts.EnableRegistryCollector.ToString());
StartEvent.Add("Service", opts.EnableServiceCollector.ToString());

Telemetry.Client.TrackEvent("Begin collecting", StartEvent);


if (opts.EnableFileSystemCollector || opts.EnableAllCollectors)
Expand Down Expand Up @@ -1089,7 +1068,6 @@ public static int RunCollectCommand(CollectCommandOptions opts)
}
}


string INSERT_RUN = "insert into runs (run_id, file_system, ports, users, services, registry, certificates, type, timestamp, version) values (@run_id, @file_system, @ports, @users, @services, @registry, @certificates, @type, @timestamp, @version)";

using (var cmd = new SqliteCommand(INSERT_RUN, DatabaseManager.Connection, DatabaseManager.Transaction))
Expand Down Expand Up @@ -1123,7 +1101,6 @@ public static int RunCollectCommand(CollectCommandOptions opts)
opts.EnableCertificateCollector = true;
}


cmd.Parameters.AddWithValue("@file_system", opts.EnableFileSystemCollector);
cmd.Parameters.AddWithValue("@ports", opts.EnableNetworkPortCollector);
cmd.Parameters.AddWithValue("@users", opts.EnableUserCollector);
Expand All @@ -1149,7 +1126,8 @@ public static int RunCollectCommand(CollectCommandOptions opts)
returnValue = (int)ERRORS.UNIQUE_ID;
}
}

Log.Information("Starting {0} collectors", collectors.Count.ToString());

foreach (BaseCollector c in collectors)
{
// c.Filters = read filters in here
Expand All @@ -1166,9 +1144,6 @@ public static int RunCollectCommand(CollectCommandOptions opts)
Log.Information("Completed: {0}", c.GetType().Name);
}

Log.Information("Started {0} collectors", collectors.Count.ToString());


DatabaseManager.Commit();
return returnValue;
}
Expand Down Expand Up @@ -1219,7 +1194,10 @@ private static int RunCompareCommand(CompareCommandOptions opts)
Logger.Setup(false, opts.Verbose);
#endif
DatabaseManager.SqliteFilename = opts.DatabaseFilename;
Dictionary<string, string> StartEvent = new Dictionary<string, string>();
StartEvent.Add("Version", Helpers.GetVersionString());

Telemetry.Client.TrackEvent("Begin Compare Command", StartEvent);
Log.Debug("Starting CompareRuns");
var results = CompareRuns(opts);

Expand All @@ -1230,6 +1208,7 @@ private static int RunCompareCommand(CompareCommandOptions opts)

var result = engine.CompileRenderAsync("Output" + Path.DirectorySeparatorChar + "Output.cshtml", results).Result;
File.WriteAllText($"{opts.OutputBaseFilename}.html", result);
Telemetry.Client.TrackEvent("Finish Compare Command", StartEvent);

return 0;
}
Expand Down
5 changes: 0 additions & 5 deletions Gui/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ public ActionResult GetMonitorResults(string RunId, int ResultType, int Offset,
{

var results = new List<OutputFileMonitorResult>();




using (var cmd = new SqliteCommand(GET_MONITOR_RESULTS, DatabaseManager.Connection, DatabaseManager.Transaction))
{
cmd.Parameters.AddWithValue("@run_id", RunId);
Expand Down Expand Up @@ -138,7 +134,6 @@ public ActionResult GetMonitorResults(string RunId, int ResultType, int Offset,

public ActionResult GetResults(string BaseId, string CompareId, int ResultType, int Offset, int NumResults)
{

var results = new List<OutputCompareResult>();

using (var cmd = new SqliteCommand(GET_COMPARISON_RESULTS, DatabaseManager.Connection, DatabaseManager.Transaction))
Expand Down
43 changes: 7 additions & 36 deletions Lib/Collectors/FileSystem/FileSystemCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,39 +156,15 @@ public override bool CanRunOnPlatform()
return RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
}

//public void WriteFaster(SqliteCommand cmd, FileSystemObject obj)
//{
// _numCollected++;
// cmd.Parameters.Clear();
// cmd.Parameters.AddWithValue("@run_id", runId);
// cmd.Parameters.AddWithValue("@row_key", obj.RowKey);
// cmd.Parameters.AddWithValue("@path", obj.Path);
// cmd.Parameters.AddWithValue("@permissions", obj.Permissions ?? "");
// cmd.Parameters.AddWithValue("@size", obj.Size);
// cmd.Parameters.AddWithValue("@hash", obj.ContentHash ?? "");
// cmd.Parameters.AddWithValue("@serialized", JsonConvert.SerializeObject(obj));
// try
// {
// cmd.ExecuteNonQuery();
// }
// catch (Exception e)
// {
// Log.Information(e.StackTrace);
// Log.Information(e.Message);
// Log.Information(e.GetType());
// }
//}

void HandleLogMessageGenerator()
{
}

public override void Execute()
{
if (!CanRunOnPlatform())
{
return;
}
Dictionary<string, string> EndEvent = new Dictionary<string, string>();
EndEvent.Add("Version", Helpers.GetVersionString());
Telemetry.Client.TrackEvent("Start file collector", EndEvent);
var watch = System.Diagnostics.Stopwatch.StartNew();
wb = new WriteBuffer(runId);
Start();
Expand Down Expand Up @@ -273,18 +249,13 @@ public override void Execute()
t.Milliseconds);
Log.Information("Completed FileSystemCollector in " + answer);
Log.Information("Flushing data");
watch = System.Diagnostics.Stopwatch.StartNew();

Dictionary<string, string> EndEvent = new Dictionary<string, string>();
EndEvent.Add("Version", Helpers.GetVersionString());
EndEvent.Add("Duration", t.ToString())
Telemetry.Client.TrackEvent("End file collector", EndEvent);
DatabaseManager.Commit();

watch.Stop();
t = TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds);
answer = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",
t.Hours,
t.Minutes,
t.Seconds,
t.Milliseconds);
Log.Information("Flush completed in " + answer);
}
}
}

0 comments on commit a2b175b

Please sign in to comment.