static void Main(string[] args) { var options = new PingControlOptions(); if (!CommandLine.Parser.Default.ParseArguments(args, options)) { Thread.Sleep(2000); Environment.Exit(1); } ServiceControlResult result = new ServiceControlResult(); int retryCount = 0; try { while (retryCount < options.RetryCount) { retryCount++; var presult = PingIt(options.Host); if (presult.Status == IPStatus.Success) { result.ResultCode = 0; result.ResultOutput = string.Format( "Ping successfull. Roundtrip={0}ms", presult.RoundtripTime); break; } else { result.ResultCode = 2; result.ResultOutput = presult.Status.ToString(); } } } catch (Exception ex) { result.ResultCode = 99; result.ResultOutput = ex.Message; if (ex.InnerException != null) result.ResultOutput += " " + ex.InnerException.Message; } Console.WriteLine(result.ToJson()); }
private bool ParseResult( string result, ServiceControlResult sc, out ServiceControlResult scResult) { scResult = sc; try { var presult = JsonConvert.DeserializeObject<ServiceControlResult>(result); scResult.ResultCode = presult.ResultCode; scResult.ResultOutput = presult.ResultOutput; return true; } catch (Exception ex) { scResult.ResultCode = 3; scResult.ResultOutput = "ServiceControl did not produce any output. Check commandline options! " + ex.Message; return false; } }
public Task<ServiceControlResult> Run(Guid hostid, Guid serviceControlId, string executable, string arguments) { var tcs = new TaskCompletionSource<ServiceControlResult>(); var proc = new Process { StartInfo = new ProcessStartInfo { FileName = executable, Arguments = arguments, UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } }; proc.EnableRaisingEvents = true; proc.Exited += (sender, args) => { string output = proc.StandardOutput.ReadToEnd(); ServiceControlResult scResult = new ServiceControlResult(); scResult.DeviceId = hostid; scResult.ServiceControlId = serviceControlId; ParseResult(output, scResult, out scResult); tcs.SetResult(scResult); proc.Dispose(); }; proc.Start(); return tcs.Task; }
static void Main(string[] args) { var options = new HttpControlOptions(); if (!Parser.Default.ParseArguments(args, options)) { Thread.Sleep(2000); Environment.Exit(1); } using (var stream = new StreamWriter(@"C:\Users\SWEPCC.G04\Documents\GitHub\NetOn\Src\HttpControl\bin\Debug\log.txt", true)) { stream.WriteLine("Url: " + options.Url); stream.WriteLine("Expected: " + options.ExpectedResult); } ServiceControlResult result = new ServiceControlResult(); try { int retryCount = 0; while (retryCount < options.RetryCount) { retryCount++; var httpResult = HttpQuery(options.Url); if (!httpResult.IsSuccessStatusCode) { result.ResultCode = 2; result.ResultOutput = string.Format("Error: Http status code {0}, {1}", httpResult.StatusCode, httpResult.ReasonPhrase); } else { if (string.IsNullOrEmpty(options.ExpectedResult)) { result.ResultCode = 0; result.ResultOutput = string.Format("Httpcontrol successfull. Statuscode {0}, {1}", (int)httpResult.StatusCode, httpResult.ReasonPhrase); break; } var validateHtml = HtmlValidation(httpResult, options.ExpectedResult); if (validateHtml.IsValid) { result.ResultCode = 0; result.ResultOutput = validateHtml.Message; break; } else { result.ResultCode = 2; result.ResultOutput = string.Format("Error: Expected html string '{0}' was not found in response.", options.ExpectedResult); } } } } catch (Exception ex) { result.ResultCode = 99; result.ResultOutput = ex.Message; if (ex.InnerException != null) result.ResultOutput += " " + ex.InnerException.Message; if (ex.InnerException.InnerException != null) result.ResultOutput += " " + ex.InnerException.InnerException.Message; } Console.WriteLine(result.ToJson()); }