-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
224 lines (197 loc) · 9.03 KB
/
Program.cs
File metadata and controls
224 lines (197 loc) · 9.03 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using static Repautomator.Tools;
using static Repautomator.Config;
//using Console = System.Console;
namespace Repautomator
{
public class Program
{
private static int CurrentStep { get; set; }
private static List<IDataQuery> Queries { get; set; }
private static IConfigurationRoot Config { get; set; }
private static SplunkDataSource Splunk { get; set; }
public static void Main(string[] args)
{
DateTime startTime = DateTime.Now;
Config = GenerateConfig(args);
Queries = new List<IDataQuery>();
SetupConsole();
ProcessInputs();
WaitForInputs();
ProcessResults();
BuildReport();
DebugConfig();
TimeSpan duration = DateTime.Now - startTime;
Console.WriteLine(String.Format("\nExecution completed in {0}h {1}m {2}s.", duration.Hours, duration.Minutes, duration.Seconds));
}
/// <summary>
/// Configures the Console encoding and colours and writes the Program banner and configuration.
/// </summary>
private static void SetupConsole()
{
string header = @"8888888b. 888 888
888 Y88b 888 888
888 888 888 888
888 d88P .d88b. 88888b. 8888b. 888 888 888888 .d88b. 88888b.d88b. 8888b. 888888 .d88b. 888d888
8888888P"" d8P Y8b 888 ""88b ""88b 888 888 888 d88""""88b 888 ""888 ""88b ""88b 888 d88""""88b 888P""
888 T88b 88888888 888 888 .d888888 888 888 888 888 888 888 888 888 .d888888 888 888 888 888
888 T88b Y8b. 888 d88P 888 888 Y88b 888 Y88b. Y88..88P 888 888 888 888 888 Y88b. Y88..88P 888
888 T88b ""Y8888 88888P"" ""Y888888 ""Y88888 ""Y888 ""Y88P"" 888 888 888 ""Y888888 ""Y888 ""Y88P"" 888
888
888
888 ";
Console.WriteLine(header + "\n");
List<IConfigurationSection> parameters = new List<IConfigurationSection>();
parameters.AddRange(Config.GetSection("ReportParameters").GetChildren());
parameters.AddRange(Config.GetSection("Paths").GetChildren());
parameters.WriteToConsole();
Console.WriteLine();
}
/// <summary>
/// Configures the required IDataSource(s) and launches the queries.
/// </summary>
private static void ProcessInputs()
{
WriteStep("Processing Inputs");
if (Convert.ToBoolean(Config["Inputs:Splunk:Enabled"])) ProcessSplunkInputs();
WriteStepComplete();
}
/// <summary>
/// Processing loop to wait for running IDataQuery(s) to finish.
/// </summary>
private static void WaitForInputs()
{
WriteStep("Waiting for Inputs");
var countTotal = Queries.Count;
var countComplete = Queries.Where(e => e.IsComplete()).Count();
var top = (System.Console.IsOutputRedirected || System.Console.IsErrorRedirected) ? 0 : System.Console.CursorTop;
while (countComplete < countTotal)
{
int pctComplete = Convert.ToInt32((Convert.ToDouble(countComplete) / countTotal) * 100);
if (!System.Console.IsOutputRedirected || !System.Console.IsErrorRedirected) ProgressBar(pctComplete, top, 0);
Thread.Sleep(1000);
countComplete = countComplete = Queries.Where(e => e.IsComplete()).Count();
}
if (!System.Console.IsOutputRedirected || !System.Console.IsErrorRedirected)
{
ProgressBar(100, top, 0);
ProgressBar(null, top, 0);
}
WriteStepComplete();
}
/// <summary>
/// Retrieves the completed IDataQuery(s) results and injects them into the Program Config.
/// </summary>
private static void ProcessResults()
{
WriteStep("Processing Query Results");
foreach (var query in Queries)
{
var configKey = String.Format("{0}:Result", query.Key);
Config[configKey] = query.Result;
}
WriteStepComplete();
}
/// <summary>
/// Creates the WordprocessingDocument report and outputs it according to the configured outputs.
/// </summary>
private static void BuildReport()
{
WriteStep("Building Report");
MemoryStream fileContents = ReportBuilder.BuildReport(Config);
string fileName = null;
if (Convert.ToBoolean(Config["Outputs:File:Enabled"]))
{
fileName = MakeValidFileName(ProcessTemplate(Config, "Outputs:File:FileName"));
var outputPath = Path.Combine(Config["Outputs:File:Directory"], fileName);
WriteFile(fileContents, outputPath);
Thread.Sleep(5000);
}
if (Convert.ToBoolean(Config["Outputs:Email:Enabled"]))
{
fileContents.Position = 0;
fileName = MakeValidFileName(ProcessTemplate(Config, "Outputs:Email:Templates:AttachmentFileName"));
EmailReport(Config, fileContents, fileName);
}
WriteStepComplete();
}
/// <summary>
/// Configures the SplunkDataSource and launches the SplunkDataQuery(s).
/// </summary>
private static void ProcessSplunkInputs()
{
Splunk = new SplunkDataSource(
Config["Inputs:Splunk:Config:Hostname"],
Convert.ToInt32(Config["Inputs:Splunk:Config:Port"]),
Config["Inputs:Splunk:Config:Username"],
Config["Inputs:Splunk:Config:Password"],
Convert.ToInt32(Config["Inputs:Splunk:Config:MaxCount"]),
Convert.ToInt32(Config["Inputs:Splunk:Config:SearchJobTtl"]),
Convert.ToBoolean(Config["Inputs:Splunk:Config:UseTls"]),
Convert.ToBoolean(Config["Inputs:Splunk:Config:ValidateCertificate"])
);
foreach (var category in Config.GetSection("Inputs:Splunk:Queries").GetChildren())
{
Console.WriteLine(String.Format("Starting {0} queries:", category.Key));
foreach (var query in category.GetChildren())
{
var sq = Splunk.Query(query.Path, query["Code"], Convert.ToDateTime(Config["ReportParameters:EarliestTime"]), Convert.ToDateTime(Config["ReportParameters:LatestTime"]));
Queries.Add(sq);
Console.WriteLine(String.Format("\t{0} query submitted", query.Key));
}
Console.WriteLine();
}
}
/// <summary>
/// Dumps the running Config to screen.
/// </summary>
private static void DebugConfig()
{
WriteStep("Dumping in-memory configuration");
if (Convert.ToBoolean(Config["Debug"]))
{
foreach (var section in Config.GetChildren())
{
WriteConfigSection(section, 0);
}
}
}
/// <summary>
/// Writes the current step to screen.
/// </summary>
/// <param name="message">Description of the current step.</param>
public static void WriteStep(string message)
{
CurrentStep++;
if (System.Console.IsOutputRedirected || System.Console.IsErrorRedirected)
{
Console.WriteLine(String.Format("{0}: Step {1}: {2} - Started", DateTime.Now.ToString("s"), CurrentStep, message));
}
else
{
Console.Write(DateTime.Now.ToString("s") + ": ");
Console.WriteLine(String.Format("Step {0}: {1}\n", CurrentStep, message));
}
}
/// <summary>
/// Writes the completion message for the current step to screen.
/// </summary>
public static void WriteStepComplete()
{
if (System.Console.IsOutputRedirected || System.Console.IsErrorRedirected)
{
Console.WriteLine(String.Format("{0}: Step {1}: Complete", DateTime.Now.ToString("s"), CurrentStep));
}
else
{
Console.Write("\t√ ");
Console.WriteLine("Step complete.\n");
}
}
}
}