Esempio n. 1
0
        private void cmdOK_Click(object sender, EventArgs e)
        {
            if (SelectedEntry == null)
            {
                SelectedEntry = new SqlDatabaseNotifierConfig();
            }
            SqlDatabaseNotifierConfig currentConfig = (SqlDatabaseNotifierConfig)SelectedEntry;

            currentConfig.SqlServer              = txtServer.Text;
            currentConfig.Database               = txtDatabase.Text;
            currentConfig.IntegratedSec          = chkIntegratedSec.Checked;
            currentConfig.UserName               = txtUserName.Text;
            currentConfig.Password               = txtPassword.Text;
            currentConfig.CmndTimeOut            = (int)numericUpDownCmndTimeOut.Value;
            currentConfig.UseSP                  = chkUseSP.Checked;
            currentConfig.CmndValue              = txtCmndValue.Text;
            currentConfig.AlertFieldName         = txtAlertFieldName.Text;
            currentConfig.CollectorFieldName     = txtCollectorFieldName.Text;
            currentConfig.PreviousStateFieldName = txtPreviousStateFieldName.Text;
            currentConfig.CurrentStateFieldName  = txtCurrentStateFieldName.Text;
            currentConfig.DetailsFieldName       = txtDetailsFieldName.Text;
            currentConfig.UseSPForViewer         = chkUseSP2.Checked;
            currentConfig.ViewerName             = txtViewerName.Text;
            currentConfig.DateTimeFieldName      = txtDateTimeFieldName.Text;

            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
Esempio n. 2
0
        public void LoadEditData()
        {
            if (SelectedEntry == null)
            {
                SelectedEntry = new SqlDatabaseNotifierConfig();
            }
            SqlDatabaseNotifierConfig currentConfig = (SqlDatabaseNotifierConfig)SelectedEntry;

            txtServer.Text           = currentConfig.SqlServer;
            txtDatabase.Text         = currentConfig.Database;
            chkIntegratedSec.Checked = currentConfig.IntegratedSec;
            txtUserName.Text         = currentConfig.UserName;
            txtPassword.Text         = currentConfig.Password;
            numericUpDownCmndTimeOut.SaveValueSet(currentConfig.CmndTimeOut);
            chkUseSP.Checked               = currentConfig.UseSP;
            txtCmndValue.Text              = currentConfig.CmndValue;
            txtAlertFieldName.Text         = currentConfig.AlertFieldName;
            txtCollectorFieldName.Text     = currentConfig.CollectorFieldName;
            txtPreviousStateFieldName.Text = currentConfig.PreviousStateFieldName;
            txtCurrentStateFieldName.Text  = currentConfig.CurrentStateFieldName;
            txtDetailsFieldName.Text       = currentConfig.DetailsFieldName;
            chkUseSP2.Checked              = currentConfig.UseSPForViewer;
            txtViewerName.Text             = currentConfig.ViewerName;
            txtDateTimeFieldName.Text      = currentConfig.DateTimeFieldName;
        }
        public override void RefreshDisplayData()
        {
            if (SelectedNotifier != null && SelectedNotifier.AgentConfig != null)
            {
                SqlDatabaseNotifierConfig dbConfig = (SqlDatabaseNotifierConfig)SelectedNotifier.AgentConfig;
                int infos  = 0;
                int warns  = 0;
                int errs   = 0;
                int debugs = 0;
                try
                {
                    lvwMessages.BeginUpdate();
                    if (chkStayCurrent.Checked)
                    {
                        dateTimeChooserTo.SelectedDateTime = DateTime.Now.AddMinutes(1);
                    }
                    int topCount = int.MaxValue;
                    if (cboTopCount.SelectedItem.ToString() != "All")
                    {
                        topCount = int.Parse(cboTopCount.SelectedItem.ToString());
                    }
                    string viewerName             = dbConfig.ViewerName.Replace("'", "''");
                    string alertParamName         = dbConfig.AlertFieldName.Replace("'", "''").Replace("@", "");
                    string collectorParamName     = dbConfig.CollectorFieldName.Replace("'", "''").Replace("@", "");
                    string previousStateParamName = dbConfig.PreviousStateFieldName.Replace("'", "''").Replace("@", "");
                    string currentStateParamName  = dbConfig.CurrentStateFieldName.Replace("'", "''").Replace("@", "");
                    string detailsParamName       = dbConfig.DetailsFieldName.Replace("'", "''").Replace("@", "");
                    string datetimeParamName      = dbConfig.DateTimeFieldName.Replace("'", "''").Replace("@", "");
                    object alertTypeValue         = cboAlertLevel.SelectedIndex;
                    if (cboAlertLevel.SelectedIndex == 4)
                    {
                        alertTypeValue = DBNull.Value;
                    }
                    object currentStateValue = cboState.SelectedIndex;
                    if (cboState.SelectedIndex == 6)
                    {
                        currentStateValue = DBNull.Value;
                    }
                    object categoryValue;
                    if (txtCollector.Text.Length == 0)
                    {
                        categoryValue = DBNull.Value;
                    }
                    else
                    {
                        categoryValue = "%" + txtCollector.Text + "%";
                    }
                    object detailsValue;
                    if (txtSearchText.Text.Length == 0)
                    {
                        detailsValue = DBNull.Value;
                    }
                    else
                    {
                        detailsValue = "%" + txtSearchText.Text + "%";
                    }
                    object collectorTypeValue = DBNull.Value;

                    string sql = dbConfig.UseSPForViewer ? dbConfig.ViewerName : Properties.Resources.SqlDatabaseQueryTemplate
                                 .Replace("@top", cboTopCount.SelectedItem.ToString())
                                 .Replace("InsertDate", datetimeParamName)
                                 .Replace("AlertLevel", alertParamName)
                                 .Replace("Collector", collectorParamName)
                                 .Replace("PreviousState", previousStateParamName)
                                 .Replace("CurrentState", currentStateParamName)
                                 .Replace("Details", detailsParamName)
                                 .Replace("AlertMessages", viewerName);

                    using (SqlConnection conn = new SqlConnection(dbConfig.GetConnectionString()))
                    {
                        try
                        {
                            conn.Open();
                        }
                        catch (System.Data.SqlClient.SqlException ex)
                        {
                            if (ex.Message.Contains("The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement"))
                            {
                                System.Threading.Thread.Sleep(3000);
                                toolStripStatusLabelDetails.Text = "Connection timed out! Retrying...";
                                Application.DoEvents();
                                //try again
                                conn.Open();
                            }
                            else
                            {
                                throw;
                            }
                        }
                        SqlParameter[] paramArr = new SqlParameter[]
                        {
                            new SqlParameter("@Top", topCount),
                            new SqlParameter("@FromDate", dateTimeChooserFrom.SelectedDateTime),
                            new SqlParameter("@ToDate", dateTimeChooserTo.SelectedDateTime),
                            new SqlParameter("@" + alertParamName, alertTypeValue)
                            {
                                DbType = DbType.Byte
                            },
                            new SqlParameter("@" + collectorParamName, categoryValue),
                            new SqlParameter("@" + currentStateParamName, currentStateValue),
                            new SqlParameter("@" + detailsParamName, detailsValue)
                        };
                        using (SqlCommand cmnd = new SqlCommand(sql, conn))
                        {
                            cmnd.Prepare();
                            cmnd.Parameters.AddRange(paramArr);
                            if (dbConfig.UseSPForViewer)
                            {
                                cmnd.CommandType = CommandType.StoredProcedure;
                            }
                            else
                            {
                                cmnd.CommandType = CommandType.Text;
                            }
                            cmnd.CommandTimeout = dbConfig.CmndTimeOut;
                            using (SqlDataReader r = cmnd.ExecuteReader())
                            {
                                lvwMessages.Items.Clear();
                                List <ListViewItem> items = new List <ListViewItem>();
                                while (r.Read())
                                {
                                    SqlDatabaseAlertMessage alertMessage = new SqlDatabaseAlertMessage();
                                    alertMessage.InsertDate    = (DateTime)r[datetimeParamName];
                                    alertMessage.AlertLevel    = AlertLevelConverter.GetAlertLevelFromText(r[alertParamName].ToString());
                                    alertMessage.PreviousState = CollectorStateConverter.GetCollectorStateFromText(r[previousStateParamName].ToString());
                                    alertMessage.CurrentState  = CollectorStateConverter.GetCollectorStateFromText(r[currentStateParamName].ToString());
                                    alertMessage.Collector     = r[collectorParamName].ToString();
                                    alertMessage.Details       = r[detailsParamName].ToString();

                                    ListViewItem lvi = new ListViewItem(alertMessage.InsertDate.ToString("yyyy-MM-dd"));
                                    if (alertMessage.AlertLevel == AlertLevel.Info)// || alertMessage.AlertLevel == AlertLevel.Debug)
                                    {
                                        lvi.ImageIndex = 1;
                                        infos++;
                                    }
                                    else if (alertMessage.AlertLevel == AlertLevel.Warning)
                                    {
                                        lvi.ImageIndex = 2;
                                        warns++;
                                    }
                                    else if (alertMessage.AlertLevel == AlertLevel.Error)
                                    {
                                        lvi.ImageIndex = 3;
                                        errs++;
                                    }
                                    else
                                    {
                                        lvi.ImageIndex = 0;
                                        debugs++;
                                    }
                                    lvi.SubItems.Add(alertMessage.InsertDate.ToString("HH:mm:ss"));
                                    lvi.SubItems.Add(alertMessage.Collector);
                                    lvi.SubItems.Add(alertMessage.CurrentState.ToString());
                                    string details = alertMessage.Details;
                                    if (details.Length > 80)
                                    {
                                        details = details.Substring(0, 80);
                                    }
                                    lvi.SubItems.Add(details);
                                    lvi.Tag = alertMessage;
                                    items.Add(lvi);
                                }
                                lvwMessages.Items.AddRange(items.ToArray());
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    string counts = string.Format("{0} item(s), Info: {1}, Warn: {2}, Err {3}",
                                                  lvwMessages.Items.Count, infos, warns, errs);
                    if (debugs > 0)
                    {
                        counts += " Debug: " + debugs.ToString();
                    }
                    toolStripStatusLabelDetails.Text = counts;
                    lvwMessages.EndUpdate();
                }
            }
        }
Esempio n. 4
0
        private void cmdTest_Click(object sender, EventArgs e)
        {
            string lastStep = "";

            try
            {
                SqlDatabaseNotifierConfig tmpConfig = new SqlDatabaseNotifierConfig();
                lastStep                = "Setting up connection string";
                tmpConfig.SqlServer     = txtServer.Text;
                tmpConfig.Database      = txtDatabase.Text;
                tmpConfig.IntegratedSec = chkIntegratedSec.Checked;
                tmpConfig.UserName      = txtUserName.Text;
                tmpConfig.Password      = txtPassword.Text;

                using (SqlConnection conn = new SqlConnection(tmpConfig.GetConnectionString()))
                {
                    conn.Open();
                    lastStep = "Inserting test message into database";

                    string cmndName               = txtCmndValue.Text.Replace("'", "''");
                    string viewerName             = txtViewerName.Text.Replace("'", "''");
                    string alertParamName         = txtAlertFieldName.Text.Replace("'", "''").Replace("@", "");
                    string categoryParamName      = txtCollectorFieldName.Text.Replace("'", "''").Replace("@", "");
                    string previousStateParamName = txtPreviousStateFieldName.Text.Replace("'", "''").Replace("@", "");
                    string currentStateParamName  = txtCurrentStateFieldName.Text.Replace("'", "''").Replace("@", "");
                    string detailsParamName       = txtDetailsFieldName.Text.Replace("'", "''").Replace("@", "");
                    string datetimeParamName      = txtDateTimeFieldName.Text.Replace("'", "''").Replace("@", "");
                    int    topCount               = 1;

                    string sql = chkUseSP.Checked ? cmndName :
                                 string.Format("Insert {0} ({1}, {2}, {3}, {4}, {5}) values(@{1}, @{2}, @{3}, @{4}, @{5})",
                                               cmndName,
                                               alertParamName,
                                               categoryParamName,
                                               previousStateParamName,
                                               currentStateParamName,
                                               detailsParamName);

                    byte alertTypeValue = 0;
                    byte previousState  = 0;
                    byte currentState   = 0;

                    using (SqlCommand cmnd = new SqlCommand(sql, conn))
                    {
                        SqlParameter[] paramArr = new SqlParameter[]
                        {
                            new SqlParameter("@" + alertParamName, alertTypeValue),
                            new SqlParameter("@" + categoryParamName, "Test"),
                            new SqlParameter("@" + previousStateParamName, previousState),
                            new SqlParameter("@" + currentStateParamName, currentState),
                            new SqlParameter("@" + detailsParamName, "Testing QuickMon database notifier insert")
                        };
                        cmnd.Parameters.AddRange(paramArr);
                        if (chkUseSP.Checked)
                        {
                            cmnd.CommandType = CommandType.StoredProcedure;
                        }
                        else
                        {
                            cmnd.CommandType = CommandType.Text;
                        }
                        cmnd.CommandTimeout = (int)numericUpDownCmndTimeOut.Value;
                        cmnd.ExecuteNonQuery();
                    }

                    lastStep = "Retrieve message from database";
                    sql      = chkUseSP2.Checked ? viewerName : GetQuery(viewerName, alertParamName,
                                                                         categoryParamName, previousStateParamName, currentStateParamName, detailsParamName, datetimeParamName);
                    using (SqlCommand cmnd = new SqlCommand(sql, conn))
                    {
                        SqlParameter[] paramArr = new SqlParameter[]
                        {
                            new SqlParameter("@Top", topCount),
                            new SqlParameter("@FromDate", DateTime.Now.AddDays(-1)),
                            new SqlParameter("@ToDate", DateTime.Now.AddMinutes(1)),
                            new SqlParameter("@" + alertParamName, alertTypeValue),
                            new SqlParameter("@" + categoryParamName, "Test"),
                            new SqlParameter("@" + currentStateParamName, currentState),
                            new SqlParameter("@" + detailsParamName, "Testing QuickMon database notifier insert")
                        };
                        cmnd.Parameters.AddRange(paramArr);
                        if (chkUseSP2.Checked)
                        {
                            cmnd.CommandType = CommandType.StoredProcedure;
                        }
                        else
                        {
                            cmnd.CommandType = CommandType.Text;
                        }
                        cmnd.CommandTimeout = (int)numericUpDownCmndTimeOut.Value;
                        using (SqlDataReader r = cmnd.ExecuteReader())
                        {
                            if (!r.Read())
                            {
                                throw new Exception("No data returned by server");
                            }
                        }
                    }
                }
                MessageBox.Show("Test was successful!", "Test connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("Failed at step: {0}\r\nDetails: {1}", lastStep, ex.Message), "Test connection", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }