private void WriteMonitorState(HenIT.RTF.RTFBuilder rtfBuilder, MonitorState ms) { if (ms != null) { if (FormatUtils.N(ms.ForAgent) != "") { rtfBuilder.FontStyle(FontStyle.Bold).Append("For object: ").FontStyle(FontStyle.Regular).AppendLine(ms.ForAgent); } rtfBuilder.FontStyle(FontStyle.Bold).Append("Time: ").FontStyle(FontStyle.Regular).AppendLine(ms.Timestamp.ToString("yyyy-MM-dd HH:mm:ss")); rtfBuilder.FontStyle(FontStyle.Bold).Append("State: ").FontStyle(FontStyle.Regular).AppendLine(ms.State.ToString()); rtfBuilder.FontStyle(FontStyle.Bold).Append("Duration: ").FontStyle(FontStyle.Regular).AppendLine(ms.CallDurationMS.ToString() + " ms"); if (ms.AlertsRaised != null) { rtfBuilder.FontStyle(FontStyle.Bold).Append("Alert count: ").FontStyle(FontStyle.Regular).AppendLine(ms.AlertsRaised.Count.ToString()); } rtfBuilder.FontStyle(FontStyle.Bold).Append("Executed on: ").FontStyle(FontStyle.Regular).AppendLine(FormatUtils.N(ms.ExecutedOnHostComputer)); rtfBuilder.FontStyle(FontStyle.Bold).Append("Ran as: ").FontStyle(FontStyle.Regular).AppendLine(FormatUtils.N(ms.RanAs)); rtfBuilder.FontStyle(FontStyle.Bold).AppendLine("Value(s): ").FontStyle(FontStyle.Regular).AppendLine(ms.CurrentValue.ToString()); if (ms.RawDetails != null && ms.RawDetails.Length > 0) //ms.State != CollectorState.Good && { rtfBuilder.FontStyle(FontStyle.Bold).AppendLine("Raw details: ").FontStyle(FontStyle.Regular).AppendLine(ms.RawDetails); } if (ms.AlertsRaised != null) { if (ms.AlertsRaised.Count > 0) { string alertSummary = ""; ms.AlertsRaised.ForEach(a => alertSummary += '\t' + a.TrimEnd('\r', '\n').Replace("\r\n", "\r\n\t") + "\r\n"); alertSummary = alertSummary.Trim('\r', '\n'); rtfBuilder.FontStyle(FontStyle.Bold).AppendLine("Alert details:").FontStyle(FontStyle.Regular).AppendLine(alertSummary.TrimEnd('\r', '\n')); } } } }
private string GetQIValue(ListViewItem lvi, WMIConfigEntry wmiConfigEntry) { string results = ""; try { object value = wmiConfigEntry.RunQuery(); MonitorStates currentstate = wmiConfigEntry.GetState(value); results = FormatUtils.N(value, "[null]"); if (currentstate == MonitorStates.Error) { lvi.ImageIndex = 3; } else if (currentstate == MonitorStates.Warning) { lvi.ImageIndex = 2; } else { lvi.ImageIndex = 1; } } catch (Exception ex) { results = ex.Message; } return(results); }
private string AttemptFieldRead(DataRow r, string name) { string returnValue = ""; try { returnValue = FormatUtils.N(r[name], "[Null]"); } catch { } return(returnValue); }
private void LoadDetailView() { lvwDetails.BeginUpdate(); lvwDetails.Items.Clear(); lvwDetails.Columns.Clear(); if (lvwResults.SelectedItems.Count >= 1 && lvwResults.SelectedItems[0].Tag is QueryInstance) { QueryInstance queryInstance = (QueryInstance)lvwResults.SelectedItems[0].Tag; try { Cursor.Current = Cursors.WaitCursor; DataSet ds = queryInstance.RunDetailQuery(); foreach (DataColumn currentDataColumn in ds.Tables[0].Columns) { ColumnHeader newColumn = new ColumnHeader(); newColumn.Tag = currentDataColumn; newColumn.Text = currentDataColumn.Caption; if ((currentDataColumn.DataType == typeof(UInt64)) || (currentDataColumn.DataType == typeof(UInt32)) || (currentDataColumn.DataType == typeof(UInt16)) || (currentDataColumn.DataType == typeof(Int64)) || (currentDataColumn.DataType == typeof(Int32)) || (currentDataColumn.DataType == typeof(Int16))) { newColumn.TextAlign = HorizontalAlignment.Right; } else { newColumn.TextAlign = HorizontalAlignment.Left; } lvwDetails.Columns.Add(newColumn); } foreach (DataRow r in ds.Tables[0].Rows) { ListViewItem lvi = new ListViewItem(FormatUtils.N(r[0], "[Null]")); for (int i = 1; i < lvwDetails.Columns.Count; i++) { lvi.SubItems.Add(FormatUtils.N(r[i], "[Null]")); } lvwDetails.Items.Add(lvi); } lvwDetails.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); } catch (Exception ex) { MessageBox.Show(ex.Message, "View details", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { Cursor.Current = Cursors.Default; } } lvwDetails.EndUpdate(); exportToolStripButton.Enabled = lvwResults.SelectedItems.Count > 0; }
public static string FormatVariables(string input) { return(input.Replace("%LocalHost%", System.Net.Dns.GetHostName()) .Replace("%IPAddress%", FormatUtils.N((from adr in System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName()) where adr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork select adr).FirstOrDefault(), "Err getting IP")) .Replace("%DateTime%", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) .Replace("%Date%", DateTime.Today.ToShortDateString()) .Replace("%Year%", DateTime.Now.Year.ToString()) .Replace("%Month%", DateTime.Now.Month.ToString()) .Replace("%Day%", DateTime.Now.Day.ToString()) .Replace("%Hour%", DateTime.Now.Hour.ToString()) .Replace("%Minute%", DateTime.Now.Minute.ToString()) .Replace("%Second%", DateTime.Now.Second.ToString()) .Replace("%NowToFSDate%", DateTime.Now.ToFileTime().ToString())); }
public void CreateAndConfigureEntry(string agentClassName, string overrideWithConfig = "", bool setAsInitialConfig = false, bool useConfigVars = true) { RegisteredAgent ra = null; if (agentClassName == "Folder") { return; } ra = RegisteredAgentCache.GetRegisteredAgentByClassName(agentClassName); if (ra == null) //in case agent is not loaded or available { throw new Exception("Collector '" + Name + "' with type of '" + agentClassName + "' cannot be loaded! No Assembly of this Agent type found!"); } else { string appliedConfig = ""; if (overrideWithConfig != null && overrideWithConfig.Trim().Length > 0) { appliedConfig = FormatUtils.N(overrideWithConfig); } else { appliedConfig = FormatUtils.N(InitialConfiguration); } //Create Collector instance Collector = CreateAndConfigureEntry(ra, appliedConfig, (useConfigVars ? ConfigVariables : null)); if (setAsInitialConfig) { if (overrideWithConfig != null && overrideWithConfig.Length > 0) { InitialConfiguration = overrideWithConfig; } else if (Collector != null) { InitialConfiguration = Collector.GetDefaultOrEmptyConfigString(); } } CollectorRegistrationDisplayName = ra.DisplayName; CollectorRegistrationName = ra.Name; } }
public override MonitorStates GetState() { MonitorStates returnState = MonitorStates.Good; StringBuilder plainTextDetails = new StringBuilder(); StringBuilder htmlTextTextDetails = new StringBuilder(); LastDetailMsg.PlainText = "Running SQL queries"; LastDetailMsg.HtmlText = ""; int errors = 0; int warnings = 0; int success = 0; double totalValue = 0; try { plainTextDetails.AppendLine(string.Format("SQL Queries")); htmlTextTextDetails.AppendLine(string.Format("SQL Queries")); htmlTextTextDetails.AppendLine("<ul>"); foreach (QueryInstance queryInstance in SqlQueryConfig.Queries) { object value = null; LastDetailMsg.PlainText = string.Format("Running SQL query '{0}' on '{1}\\{2}'", queryInstance.Name, queryInstance.SqlServer, queryInstance.Database); value = queryInstance.RunQuery(); MonitorStates currentstate = queryInstance.GetState(value); if (value != DBNull.Value && value.IsNumber()) { totalValue += double.Parse(value.ToString()); } if (currentstate == MonitorStates.Error) { errors++; plainTextDetails.AppendLine(string.Format("\t'{0}' - value '{1}' - Error (trigger {2})", queryInstance.Name, FormatUtils.N(value, "[null]"), queryInstance.ErrorValue)); htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}' - <b>Error</b> (trigger {2})</li>", queryInstance.Name, FormatUtils.N(value, "[null]"), queryInstance.ErrorValue)); } else if (currentstate == MonitorStates.Warning) { warnings++; plainTextDetails.AppendLine(string.Format("\t'{0}' - value '{1}' - Warning (trigger {2})", queryInstance.Name, FormatUtils.N(value, "[null]"), queryInstance.WarningValue)); htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}' - <b>Warning</b> (trigger {2})</li>", queryInstance.Name, FormatUtils.N(value, "[null]"), queryInstance.WarningValue)); } else { success++; plainTextDetails.AppendLine(string.Format("\t'{0}' - value '{1}'", queryInstance.Name, value)); htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}'</li>", queryInstance.Name, value)); } } htmlTextTextDetails.AppendLine("</ul>"); if (errors > 0 && warnings == 0) { returnState = MonitorStates.Error; } else if (warnings > 0) { returnState = MonitorStates.Warning; } LastDetailMsg.PlainText = plainTextDetails.ToString().TrimEnd('\r', '\n'); LastDetailMsg.HtmlText = htmlTextTextDetails.ToString(); LastDetailMsg.LastValue = totalValue; } catch (Exception ex) { LastError = 1; LastErrorMsg = ex.Message; LastDetailMsg.PlainText = string.Format("Last step: '{0}\r\n{1}", LastDetailMsg.PlainText, ex.Message); LastDetailMsg.HtmlText = string.Format("<blockquote>Last step: '{0}<br />{1}</blockquote>", LastDetailMsg.PlainText, ex.Message); returnState = MonitorStates.Error; } return(returnState); }
//private Mutex sqlWriteMutex = new Mutex(); #endregion public override void RecordMessage(AlertLevel alertLevel, string collectorType, string category, MonitorStates oldState, MonitorStates newState, CollectorMessage collectorMessage) { string lastStep = ""; try { //sqlWriteMutex.WaitOne(); if (connStr.Length == 0) { lastStep = "Setting up connection string"; connStr = dbSettings.GetConnectionString(); } lastStep = "Opening connection"; using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); lastStep = "Inserting test message into database"; string alertParamName = dbSettings.AlertFieldName.Replace("'", "''").Replace("@", ""); string collectorTypeParamName = dbSettings.CollectorTypeFieldName.Replace("'", "''").Replace("@", ""); string categoryParamName = dbSettings.CategoryFieldName.Replace("'", "''").Replace("@", ""); string previousStateParamName = dbSettings.PreviousStateFieldName.Replace("'", "''").Replace("@", ""); string currentStateParamName = dbSettings.CurrentStateFieldName.Replace("'", "''").Replace("@", ""); string detailsParamName = dbSettings.DetailsFieldName.Replace("'", "''").Replace("@", ""); string sql = dbSettings.UseSP ? dbSettings.CmndValue : string.Format("Insert {0} ({1}, {2}, {3}, {4}, {5}, {6}) values(@{1}, @{2}, @{3}, @{4}, @{5}, @{6})", dbSettings.CmndValue, dbSettings.AlertFieldName, collectorTypeParamName, categoryParamName, previousStateParamName, currentStateParamName, detailsParamName); using (SqlCommand cmnd = new SqlCommand(sql, conn)) { SqlParameter[] paramArr = new SqlParameter[] { new SqlParameter("@" + alertParamName, (byte)alertLevel), new SqlParameter("@" + collectorTypeParamName, collectorType), new SqlParameter("@" + categoryParamName, category), new SqlParameter("@" + previousStateParamName, (byte)oldState), new SqlParameter("@" + currentStateParamName, (byte)newState), new SqlParameter("@" + detailsParamName, FormatUtils.N(collectorMessage.PlainText, "No details available")) }; cmnd.Parameters.AddRange(paramArr); if (dbSettings.UseSP) { cmnd.CommandType = CommandType.StoredProcedure; } else { cmnd.CommandType = CommandType.Text; } cmnd.CommandTimeout = dbSettings.CmndTimeOut; cmnd.ExecuteNonQuery(); } } } catch (Exception ex) { throw new Exception("Error recording message in Database notifier\r\nLast step: " + lastStep, ex); } finally { //sqlWriteMutex.ReleaseMutex(); } }
public override MonitorStates GetState() { MonitorStates returnState = MonitorStates.Good; StringBuilder plainTextDetails = new StringBuilder(); StringBuilder htmlTextTextDetails = new StringBuilder(); LastDetailMsg.PlainText = "Running WMI query"; LastDetailMsg.HtmlText = ""; int errors = 0; int warnings = 0; int success = 0; double totalValue = 0; try { plainTextDetails.AppendLine(string.Format("Running {0} WMI queries", WmiIConfig.Entries.Count)); htmlTextTextDetails.AppendLine(string.Format("<i>Running {0} WMI queries'</i>", WmiIConfig.Entries.Count)); htmlTextTextDetails.AppendLine("<ul>"); foreach (WMIConfigEntry wmiConfigEntry in WmiIConfig.Entries) { plainTextDetails.Append(string.Format("\t\t{0} - ", wmiConfigEntry.Name)); htmlTextTextDetails.Append(string.Format("<li>{0} - ", wmiConfigEntry.Name)); object val = wmiConfigEntry.RunQuery(); MonitorStates currentState = wmiConfigEntry.GetState(val); if (currentState == MonitorStates.Error) { errors++; plainTextDetails.AppendLine(string.Format("Machine '{0}' - value '{1}' - Error (trigger {2})", wmiConfigEntry.Machinename, FormatUtils.N(val, "[null]"), wmiConfigEntry.ErrorValue)); htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}' - <b>Error</b> (trigger {2})</li>", wmiConfigEntry.Machinename, FormatUtils.N(val, "[null]"), wmiConfigEntry.ErrorValue)); } else if (currentState == MonitorStates.Warning) { warnings++; plainTextDetails.AppendLine(string.Format("Machine '{0}' - value '{1}' - Warning (trigger {2})", wmiConfigEntry.Machinename, FormatUtils.N(val, "[null]"), wmiConfigEntry.WarningValue)); htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}' - <b>Warning</b> (trigger {2})</li>", wmiConfigEntry.Machinename, FormatUtils.N(val, "[null]"), wmiConfigEntry.WarningValue)); } else { success++; plainTextDetails.AppendLine(string.Format("Machine '{0}' - value '{1}'", wmiConfigEntry.Machinename, val)); htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}'</li>", wmiConfigEntry.Machinename, val)); } if (val != null && val.IsNumber()) { totalValue += double.Parse(val.ToString()); } } //object value = null; //LastDetailMsg.PlainText = string.Format("Running WMI query for '{0}' - '{1}'", WmiIConfig.Machinename, WmiIConfig.StateQuery); //value = WmiIConfig.RunQuery(); //MonitorStates currentState = WmiIConfig.GetState(value); //if (currentState == MonitorStates.Error) //{ // errors++; // plainTextDetails.AppendLine(string.Format("Machine '{0}' - value '{1}' - Error (trigger {2})", WmiIConfig.Machinename, FormatUtils.N(value, "[null]"), WmiIConfig.ErrorValue)); // htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}' - <b>Error</b> (trigger {2})</li>", WmiIConfig.Machinename, FormatUtils.N(value, "[null]"), WmiIConfig.ErrorValue)); //} //else if (currentState == MonitorStates.Warning) //{ // warnings++; // plainTextDetails.AppendLine(string.Format("Machine '{0}' - value '{1}' - Warning (trigger {2})", WmiIConfig.Machinename, FormatUtils.N(value, "[null]"), WmiIConfig.WarningValue)); // htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}' - <b>Warning</b> (trigger {2})</li>", WmiIConfig.Machinename, FormatUtils.N(value, "[null]"), WmiIConfig.WarningValue)); //} //else //{ // success++; // plainTextDetails.AppendLine(string.Format("Machine '{0}' - value '{1}'", WmiIConfig.Machinename, value)); // htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}'</li>", WmiIConfig.Machinename, value)); //} htmlTextTextDetails.AppendLine("</ul>"); if (errors > 0 && warnings == 0) { returnState = MonitorStates.Error; } else if (warnings > 0) { returnState = MonitorStates.Warning; } LastDetailMsg.PlainText = plainTextDetails.ToString().TrimEnd('\r', '\n'); LastDetailMsg.HtmlText = htmlTextTextDetails.ToString(); LastDetailMsg.LastValue = totalValue; } catch (Exception ex) { LastError = 1; LastErrorMsg = ex.Message; LastDetailMsg.PlainText = string.Format("Last step: '{0}\r\n{1}", LastDetailMsg.PlainText, ex.Message); LastDetailMsg.HtmlText = string.Format("<blockquote>Last step: '{0}<br />{1}</blockquote>", LastDetailMsg.PlainText, ex.Message); returnState = MonitorStates.Error; } return(returnState); }
private void LoadDetailView() { lvwDetails.BeginUpdate(); lvwDetails.Items.Clear(); lvwDetails.Columns.Clear(); if (lvwResults.SelectedItems.Count == 1 && lvwResults.SelectedItems[0].Tag is WMIConfigEntry) { WMIConfigEntry wmiConfigEntry = (WMIConfigEntry)lvwResults.SelectedItems[0].Tag; try { Cursor.Current = Cursors.WaitCursor; DataSet ds = wmiConfigEntry.RunDetailQuery(); if (wmiConfigEntry.ColumnNames == null || wmiConfigEntry.ColumnNames.Count == 0) { foreach (DataColumn currentDataColumn in ds.Tables[0].Columns) { ColumnHeader newColumn = new ColumnHeader(); newColumn.Tag = currentDataColumn; newColumn.Text = currentDataColumn.Caption; if ((currentDataColumn.DataType == typeof(UInt64)) || (currentDataColumn.DataType == typeof(UInt32)) || (currentDataColumn.DataType == typeof(UInt16)) || (currentDataColumn.DataType == typeof(Int64)) || (currentDataColumn.DataType == typeof(Int32)) || (currentDataColumn.DataType == typeof(Int16))) { newColumn.TextAlign = HorizontalAlignment.Right; } else { newColumn.TextAlign = HorizontalAlignment.Left; } lvwDetails.Columns.Add(newColumn); } foreach (DataRow r in ds.Tables[0].Rows) { ListViewItem lvi = new ListViewItem(FormatUtils.N(r[0], "[Null]")); for (int i = 1; i < lvwDetails.Columns.Count; i++) { lvi.SubItems.Add(FormatUtils.N(r[i], "[Null]")); } lvwDetails.Items.Add(lvi); } } else { foreach (string colname in wmiConfigEntry.ColumnNames) { ColumnHeader newColumn = new ColumnHeader(); newColumn.Text = colname; lvwDetails.Columns.Add(newColumn); } foreach (DataRow r in ds.Tables[0].Rows) { string firstColumnName = wmiConfigEntry.ColumnNames[0]; ListViewItem lvi = new ListViewItem(AttemptFieldRead(r, firstColumnName)); for (int i = 1; i < wmiConfigEntry.ColumnNames.Count; i++) { lvi.SubItems.Add(AttemptFieldRead(r, wmiConfigEntry.ColumnNames[i])); } lvwDetails.Items.Add(lvi); } } lvwDetails.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize); } catch (Exception ex) { MessageBox.Show(ex.Message, "View details", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { Cursor.Current = Cursors.Default; } } lvwDetails.EndUpdate(); exportToolStripButton.Enabled = lvwResults.SelectedItems.Count > 0; }