public override List <System.Data.DataTable> GetDetailDataTables() { List <System.Data.DataTable> tables = new List <System.Data.DataTable>(); System.Data.DataTable dt = new System.Data.DataTable(); try { dt.Columns.Add(new System.Data.DataColumn("Script name", typeof(string))); dt.Columns.Add(new System.Data.DataColumn("Response", typeof(string))); PowerShellScriptRunnerCollectorConfig currentConfig = (PowerShellScriptRunnerCollectorConfig)AgentConfig; foreach (PowerShellScriptRunnerEntry entry in currentConfig.Entries) { string output = "N/A"; try { output = entry.RunScript(); } catch (Exception ex) { output = ex.Message; } dt.Rows.Add(entry.Name, output); } } catch (Exception ex) { dt = new System.Data.DataTable("Exception"); dt.Columns.Add(new System.Data.DataColumn("Text", typeof(string))); dt.Rows.Add(ex.ToString()); } tables.Add(dt); return(tables); }
public override void LoadDisplayData() { if (Collector != null && Collector.AgentConfig != null) { PowerShellScriptRunnerCollectorConfig sqlQueryConfig = (PowerShellScriptRunnerCollectorConfig)Collector.AgentConfig; lvwScripts.Items.Clear(); foreach (PowerShellScriptRunnerEntry queryInstance in sqlQueryConfig.Entries) { ListViewItem lvi = new ListViewItem(queryInstance.Name); lvi.ImageIndex = 0; lvi.Tag = queryInstance; lvwScripts.Items.Add(lvi); } } base.LoadDisplayData(); }
public PowerShellScriptRunnerCollector() { AgentConfig = new PowerShellScriptRunnerCollectorConfig(); }
public override MonitorState RefreshState() { MonitorState returnState = new MonitorState(); string lastAction = ""; int errors = 0; int warnings = 0; int success = 0; try { PowerShellScriptRunnerCollectorConfig currentConfig = (PowerShellScriptRunnerCollectorConfig)AgentConfig; //returnState.RawDetails = string.Format("Calling {0} services", currentConfig.Entries.Count); //returnState.HtmlDetails = string.Format("<b>Calling {0} services</b>", currentConfig.Entries.Count); foreach (PowerShellScriptRunnerEntry entry in currentConfig.Entries) { CollectorState currentState = CollectorState.NotAvailable; string output = "N/A"; try { lastAction = "Running PowerShell script " + entry.Description; output = entry.RunScript(); lastAction = "Checking states of " + entry.Description; currentState = entry.GetState(output); lastAction = output; } catch (Exception wsException) { currentState = CollectorState.Error; lastAction = wsException.Message; output = wsException.Message; } if (output == null) { output = "N/A"; } if (currentState == CollectorState.Error) { errors++; returnState.ChildStates.Add( new MonitorState() { ForAgent = entry.Description, State = CollectorState.Error, CurrentValue = output //, //RawDetails = string.Format("'{0}' (Error)", output), //HtmlDetails = string.Format("'{0}' (<b>Error</b>)", output) }); } else if (currentState == CollectorState.Warning) { warnings++; returnState.ChildStates.Add( new MonitorState() { ForAgent = entry.Description, State = CollectorState.Warning, CurrentValue = output //, //RawDetails = string.Format("'{0}' (Warning)", output), //HtmlDetails = string.Format("'{0}' (<b>Warning</b>)", output) }); } else { success++; returnState.ChildStates.Add( new MonitorState() { ForAgent = entry.Description, State = CollectorState.Good, CurrentValue = output //, //RawDetails = string.Format("'{0}'", output), //HtmlDetails = string.Format("'{0}'", output) }); } } //returnState.CurrentValue = pingTotalTime; if (errors > 0 && warnings == 0 && success == 0) // any errors { returnState.State = CollectorState.Error; } else if (errors > 0 || warnings > 0) //any warnings { returnState.State = CollectorState.Warning; } else { returnState.State = CollectorState.Good; } } catch (Exception ex) { returnState.RawDetails = ex.Message; returnState.HtmlDetails = string.Format("<p><b>Last action:</b> {0}</p><blockquote>{1}</blockquote>", lastAction, ex.Message); returnState.State = CollectorState.Error; } return(returnState); }
public override MonitorState GetState() { MonitorState returnState = new MonitorState(); StringBuilder plainTextDetails = new StringBuilder(); StringBuilder htmlTextTextDetails = new StringBuilder(); string lastAction = ""; int errors = 0; int warnings = 0; int success = 0; try { PowerShellScriptRunnerCollectorConfig psScriptConfig = (PowerShellScriptRunnerCollectorConfig)AgentConfig; plainTextDetails.AppendLine(string.Format("Running {0} PowerShell scripts", psScriptConfig.Entries.Count)); htmlTextTextDetails.AppendLine(string.Format("<i>Running {0} PowerShell scripts'</i>", psScriptConfig.Entries.Count)); htmlTextTextDetails.AppendLine("<ul>"); StringBuilder sbTotalResults = new StringBuilder(); foreach (PowerShellScriptRunnerEntry pssrEntry in psScriptConfig.Entries) { lastAction = string.Format("Running PowerShell script for {0} - ", pssrEntry.Name); sbTotalResults.AppendLine(pssrEntry.Name); plainTextDetails.Append(string.Format("\t{0} - ", pssrEntry.Name)); htmlTextTextDetails.Append(string.Format("<li>{0} - ", pssrEntry.Name)); string scriptResult = pssrEntry.RunScript(); CollectorState currentState = pssrEntry.GetState(scriptResult); if (currentState == CollectorState.Error) { errors++; plainTextDetails.AppendLine(string.Format("\t\tScript '{0}' - Error\r\n\t\t Result: '{1}'", pssrEntry.Name, FormatUtils.N(scriptResult, "[null]"))); htmlTextTextDetails.AppendLine(string.Format("<li>Script '{0}' - <b>Error</b><br>Result: <blockquote>{1}</blockquote></li>", pssrEntry.Name, FormatUtils.N(scriptResult, "[null]"))); } else if (currentState == CollectorState.Warning) { warnings++; plainTextDetails.AppendLine(string.Format("\t\tScript '{0}' - Warning\r\n\t\t Result: '{1}'", pssrEntry.Name, FormatUtils.N(scriptResult, "[null]"))); htmlTextTextDetails.AppendLine(string.Format("<li>Script '{0}' - <b>Warning</b><br>Result: <blockquote>{1}</blockquote></li>", pssrEntry.Name, FormatUtils.N(scriptResult, "[null]"))); } else { success++; plainTextDetails.AppendLine(string.Format("\t\tScript '{0}'\r\n\t\t Result: '{1}'", pssrEntry.Name, FormatUtils.N(scriptResult, "[null]"))); htmlTextTextDetails.AppendLine(string.Format("<li>Script '{0}'<br>Result: <blockquote>{1}</blockquote></li>", pssrEntry.Name, FormatUtils.N(scriptResult, "[null]"))); } if (scriptResult != null) { sbTotalResults.AppendLine(scriptResult); } else { sbTotalResults.AppendLine("[null]"); } } htmlTextTextDetails.AppendLine("</ul>"); returnState.RawDetails = plainTextDetails.ToString().TrimEnd('\r', '\n'); returnState.HtmlDetails = htmlTextTextDetails.ToString(); returnState.CurrentValue = sbTotalResults.ToString(); if (errors > 0 && warnings == 0 && success == 0) { returnState.State = CollectorState.Error; } else if (errors > 0 || warnings > 0) { returnState.State = CollectorState.Warning; } else { returnState.State = CollectorState.Good; } } catch (Exception ex) { returnState.RawDetails = ex.Message; returnState.HtmlDetails = string.Format("<p><b>Last action:</b> {0}</p><blockquote>{1}</blockquote>", lastAction, ex.Message); returnState.State = CollectorState.Error; } return(returnState); }