private void cmdTest_Click(object sender, EventArgs e)
        {
            if (DoValidate())
            {
                string lastStep = "Initialize values";
                try
                {
                    Cursor.Current = Cursors.WaitCursor;
                    RegistryQueryCollectorConfigEntry testQueryInstance = new RegistryQueryCollectorConfigEntry();
                    testQueryInstance.Name                   = txtName.Text;
                    testQueryInstance.UseRemoteServer        = chkUseRemoteServer.Checked;
                    testQueryInstance.Server                 = txtServer.Text;
                    testQueryInstance.Path                   = txtPath.Text;
                    testQueryInstance.KeyName                = txtKey.Text;
                    testQueryInstance.ExpandEnvironmentNames = chkExpandEnvNames.Checked;
                    testQueryInstance.RegistryHive           = RegistryQueryCollectorConfigEntry.GetRegistryHiveFromString(cboRegistryHive.Text);

                    if (!chkValueIsANumber.Checked)
                    {
                        testQueryInstance.ReturnValueIsNumber = false;
                        testQueryInstance.ReturnValueInARange = false;
                        testQueryInstance.ReturnValueInverted = false;
                    }
                    else
                    {
                        testQueryInstance.ReturnValueIsNumber = true;
                        testQueryInstance.ReturnValueInARange = chkValueIsInARange.Checked;
                        testQueryInstance.ReturnValueInverted = !chkReturnValueNotInverted.Checked;
                    }

                    testQueryInstance.SuccessValue = cboSuccessValue.Text;
                    testQueryInstance.WarningValue = cboWarningValue.Text;
                    testQueryInstance.ErrorValue   = cboErrorValue.Text;

                    object returnValue = null;
                    returnValue = testQueryInstance.GetValue();
                    CollectorState state = testQueryInstance.EvaluateValue(returnValue);
                    if (state == CollectorState.Good)
                    {
                        MessageBox.Show(string.Format("Success!\r\nValue return: {0}", FormatUtils.FormatArrayToString(returnValue)), "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (state == CollectorState.Warning)
                    {
                        MessageBox.Show(string.Format("Warning!\r\nValue return: {0}", FormatUtils.FormatArrayToString(returnValue)), "Test", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        MessageBox.Show(string.Format("Error!\r\nValue return: {0}", FormatUtils.FormatArrayToString(returnValue)), "Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Failed!\r\nLast step: {0}\r\n{1}", lastStep, ex.Message), "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }
            }
        }
Exemplo n.º 2
0
        public override MonitorState GetCurrentState()
        {
            string         returnedData = "";
            CollectorState agentState   = CollectorState.NotAvailable;

            try
            {
                returnedData = ExecuteCommand();

                CurrentAgentValue = FormatUtils.FormatArrayToString(returnedData, "[null]");
                agentState        = CollectorAgentReturnValueCompareEngine.GetState(ReturnCheckSequence,
                                                                                    GoodResultMatchType, GoodValue,
                                                                                    WarningResultMatchType, WarningValue,
                                                                                    ErrorResultMatchType, ErrorValue,
                                                                                    CurrentAgentValue);
            }
            catch (Exception wsException)
            {
                agentState   = CollectorState.Error;
                returnedData = wsException.Message;
            }

            MonitorState currentState = new MonitorState()
            {
                ForAgent         = Description,
                State            = agentState,
                CurrentValue     = returnedData == null ? "N/A" : returnedData,
                CurrentValueUnit = OutputValueUnit
            };

            return(currentState);
        }
Exemplo n.º 3
0
        //public override MonitorState RefreshState()
        //{
        //    MonitorState returnState = new MonitorState();
        //    string lastAction = "";
        //    int errors = 0;
        //    int success = 0;
        //    int warnings = 0;
        //    double totalValue = 0;
        //    try
        //    {
        //        RegistryQueryCollectorConfig currentConfig = (RegistryQueryCollectorConfig)AgentConfig;
        //        returnState.RawDetails = string.Format("Running {0} registry query(s)", currentConfig.Entries.Count);
        //        returnState.HtmlDetails = string.Format("<b>Running {0} registry query(s)</b>", currentConfig.Entries.Count);

        //        foreach (RegistryQueryCollectorConfigEntry queryInstance in currentConfig.Entries)
        //        {
        //            object value = null;
        //            if (queryInstance.UseRemoteServer)
        //                lastAction = string.Format("Running Registry query '{0}' on '{1}'", queryInstance.Name, queryInstance.Server);
        //            else
        //                lastAction = string.Format("Running Registry query '{0}'", queryInstance.Name);

        //            value = queryInstance.GetValue();
        //            if (!queryInstance.ReturnValueIsNumber && value.IsNumber())
        //                totalValue += double.Parse(value.ToString());

        //            CollectorState instanceState = queryInstance.EvaluateValue(value);

        //            if (instanceState == CollectorState.Error)
        //            {
        //                errors++;
        //                returnState.ChildStates.Add(
        //                       new MonitorState()
        //                       {
        //                           ForAgent = queryInstance.Name,
        //                           State = CollectorState.Error,
        //                           CurrentValue = value,
        //                           RawDetails = string.Format("'{0}' - value '{1}' - Error (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.ErrorValue),
        //                           HtmlDetails = string.Format("'{0}' - value '{1}' - <b>Error</b> (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.ErrorValue),
        //                       });
        //            }
        //            else if (instanceState == CollectorState.Warning)
        //            {
        //                warnings++;
        //                returnState.ChildStates.Add(
        //                       new MonitorState()
        //                       {
        //                           ForAgent = queryInstance.Name,
        //                           State = CollectorState.Warning,
        //                           CurrentValue = value,
        //                           RawDetails = string.Format("'{0}' - value '{1}' - Warning (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.WarningValue),
        //                           HtmlDetails = string.Format("'{0}' - value '{1}' - <b>Warning</b> (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.WarningValue),
        //                       });
        //            }
        //            else
        //            {
        //                success++;
        //                returnState.ChildStates.Add(
        //                       new MonitorState()
        //                       {
        //                           ForAgent = queryInstance.Name,
        //                           State = CollectorState.Good,
        //                           CurrentValue = value,
        //                           RawDetails = string.Format("'{0}' - value '{1}'", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]")),
        //                           HtmlDetails = string.Format("'{0}' - value '{1}'", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]")),
        //                       });


        //            }
        //        }
        //        if (errors > 0 && warnings == 0)
        //            returnState.State = CollectorState.Error;
        //        else if (warnings > 0)
        //            returnState.State = CollectorState.Warning;
        //        else
        //            returnState.State = CollectorState.Good;
        //        returnState.CurrentValue = totalValue;
        //    }
        //    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 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("Path", typeof(string)));
                dt.Columns.Add(new System.Data.DataColumn("Value", typeof(string)));

                RegistryQueryCollectorConfig currentConfig = (RegistryQueryCollectorConfig)AgentConfig;
                foreach (RegistryQueryCollectorConfigEntry entry in currentConfig.Entries)
                {
                    object value = entry.GetValue();
                    if (value.GetType().IsArray)
                    {
                        value = FormatUtils.FormatArrayToString(value);
                    }

                    dt.Rows.Add(entry.Description, value);
                }
            }
            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);
        }
Exemplo n.º 4
0
        public MonitorState GetCurrentState()
        {
            object         wsData     = null;
            CollectorState agentState = CollectorState.NotAvailable;

            try
            {
                wsData = GetValue();

                CurrentAgentValue = FormatUtils.FormatArrayToString(wsData, "[null]");
                agentState        = CollectorAgentReturnValueCompareEngine.GetState(ReturnCheckSequence,
                                                                                    GoodResultMatchType, GoodValue,
                                                                                    WarningResultMatchType, WarningValue,
                                                                                    ErrorResultMatchType, ErrorValue,
                                                                                    CurrentAgentValue);
            }
            catch (Exception wsException)
            {
                agentState = CollectorState.Error;
                wsData     = wsException.Message;
            }

            MonitorState currentState = new MonitorState()
            {
                ForAgent         = Description,
                State            = agentState,
                CurrentValue     = wsData == null ? "N/A" : wsData.ToString(),
                CurrentValueUnit = OutputValueUnit
            };

            return(currentState);
        }
        private void cmdTest_Click(object sender, EventArgs e)
        {
            if (DoValidate())
            {
                string lastStep = "Initialize values";
                try
                {
                    Cursor.Current = Cursors.WaitCursor;
                    RegistryQueryCollectorConfigEntry testQueryInstance = new RegistryQueryCollectorConfigEntry();

                    string name       = ApplyConfigVarsOnField(txtName.Text);
                    string serverName = ApplyConfigVarsOnField(txtServer.Text);
                    string pathName   = ApplyConfigVarsOnField(txtPath.Text);
                    string keyName    = ApplyConfigVarsOnField(txtKey.Text);

                    string successVal = ApplyConfigVarsOnField(txtSuccess.Text);
                    string warningVal = ApplyConfigVarsOnField(txtWarning.Text);
                    string errorVal   = ApplyConfigVarsOnField(txtError.Text);

                    testQueryInstance.Name                   = name;
                    testQueryInstance.UseRemoteServer        = chkUseRemoteServer.Checked;
                    testQueryInstance.Server                 = serverName;
                    testQueryInstance.Path                   = pathName;
                    testQueryInstance.KeyName                = keyName;
                    testQueryInstance.ExpandEnvironmentNames = chkExpandEnvNames.Checked;
                    testQueryInstance.RegistryHive           = RegistryQueryCollectorConfigEntry.GetRegistryHiveFromString(cboRegistryHive.Text);

                    testQueryInstance.ReturnCheckSequence    = (CollectorAgentReturnValueCheckSequence)cboReturnCheckSequence.SelectedIndex;
                    testQueryInstance.GoodValue              = successVal;
                    testQueryInstance.GoodResultMatchType    = (CollectorAgentReturnValueCompareMatchType)cboSuccessMatchType.SelectedIndex;
                    testQueryInstance.WarningValue           = warningVal;
                    testQueryInstance.WarningResultMatchType = (CollectorAgentReturnValueCompareMatchType)cboWarningMatchType.SelectedIndex;
                    testQueryInstance.ErrorValue             = errorVal;
                    testQueryInstance.ErrorResultMatchType   = (CollectorAgentReturnValueCompareMatchType)cboErrorMatchType.SelectedIndex;
                    testQueryInstance.OutputValueUnit        = cboOutputValueUnit.Text;

                    object         returnValue = null;
                    CollectorState state       = testQueryInstance.GetCurrentState().State;
                    returnValue = testQueryInstance.CurrentAgentValue;
                    if (state == CollectorState.Good)
                    {
                        MessageBox.Show(string.Format("Success!\r\nValue return: {0}", FormatUtils.FormatArrayToString(returnValue)), "Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (state == CollectorState.Warning)
                    {
                        MessageBox.Show(string.Format("Warning!\r\nValue return: {0}", FormatUtils.FormatArrayToString(returnValue)), "Test", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        MessageBox.Show(string.Format("Error!\r\nValue return: {0}", FormatUtils.FormatArrayToString(returnValue)), "Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("Failed!\r\nLast step: {0}\r\n{1}", lastStep, ex.Message), "Test", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }
            }
        }
Exemplo n.º 6
0
        public MonitorState GetCurrentState()
        {
            object         wsData     = null;
            CollectorState agentState = CollectorState.NotAvailable;

            try
            {
                wsData = GetValue();
                //if (ReturnValueIsNumber && !wsData.IsNumber())
                //{
                //    agentState = CollectorState.Error;
                //    wsData = "Returned value is not a number! (" + wsData.ToString() + ")";
                //}
                //else
                //{
                CurrentAgentValue = FormatUtils.FormatArrayToString(wsData, "[null]");
                agentState        = CollectorAgentReturnValueCompareEngine.GetState(ReturnCheckSequence,
                                                                                    GoodResultMatchType, GoodValue,
                                                                                    WarningResultMatchType, WarningValue,
                                                                                    ErrorResultMatchType, ErrorValue,
                                                                                    CurrentAgentValue);
                //}
            }
            catch (Exception wsException)
            {
                agentState = CollectorState.Error;
                wsData     = wsException.Message;
            }

            MonitorState currentState = new MonitorState()
            {
                ForAgent         = Description,
                State            = agentState,
                CurrentValue     = wsData == null ? "N/A" : wsData.ToString(),
                CurrentValueUnit = OutputValueUnit
            };

            return(currentState);

            //object value = GetValue();
            //CurrentAgentValue = value;
            //MonitorState currentState = new MonitorState()
            //{
            //    ForAgent = Name,
            //    CurrentValue = FormatUtils.FormatArrayToString(value, "[null]"),
            //    State = EvaluateValue(value)
            //};

            //if (currentState.State == CollectorState.Error)
            //{
            //    currentState.RawDetails = string.Format("'{0}' - value '{1}' - Error (trigger {2})", Name, FormatUtils.FormatArrayToString(value, "[null]"), ErrorValue);
            //    currentState.HtmlDetails = string.Format("'{0}' - value '{1}' - <b>Error</b> (trigger {2})", Name, FormatUtils.FormatArrayToString(value, "[null]"), ErrorValue);
            //}
            //else if (currentState.State == CollectorState.Warning)
            //{
            //    currentState.RawDetails = string.Format("'{0}' - value '{1}' - Warning (trigger {2})", Name, FormatUtils.FormatArrayToString(value, "[null]"), WarningValue);
            //    currentState.HtmlDetails = string.Format("'{0}' - value '{1}' - <b>Warning</b> (trigger {2})", Name, FormatUtils.FormatArrayToString(value, "[null]"), WarningValue);
            //}
            //else
            //{
            //    currentState.RawDetails = string.Format("'{0}' - value '{1}'", Name, FormatUtils.FormatArrayToString(value, "[null]"));
            //    currentState.HtmlDetails = string.Format("'{0}' - value '{1}'", Name, FormatUtils.FormatArrayToString(value, "[null]"));
            //}

            //return currentState;
        }
Exemplo n.º 7
0
        public override MonitorState GetState()
        {
            MonitorState  returnState         = new MonitorState();
            StringBuilder plainTextDetails    = new StringBuilder();
            StringBuilder htmlTextTextDetails = new StringBuilder();
            string        lastAction          = "";
            int           errors     = 0;
            int           success    = 0;
            int           warnings   = 0;
            double        totalValue = 0;

            try
            {
                RegistryQueryCollectorConfig config = (RegistryQueryCollectorConfig)AgentConfig;
                plainTextDetails.AppendLine(string.Format("Running {0} registry query(s)", config.Entries.Count));
                htmlTextTextDetails.AppendLine(string.Format("<b>Running {0} registry query(s)</b>", config.Entries.Count));
                htmlTextTextDetails.AppendLine("<ul>");

                foreach (RegistryQueryInstance queryInstance in config.Entries)
                {
                    object value = null;
                    if (queryInstance.UseRemoteServer)
                    {
                        lastAction = string.Format("Running Registry query '{0}' on '{1}'", queryInstance.Name, queryInstance.Server);
                    }
                    else
                    {
                        lastAction = string.Format("Running Registry query '{0}'", queryInstance.Name);
                    }

                    value = queryInstance.GetValue();
                    if (!queryInstance.ReturnValueIsNumber && value.IsNumber())
                    {
                        totalValue += double.Parse(value.ToString());
                    }

                    CollectorState instanceState = queryInstance.EvaluateValue(value);

                    if (instanceState == CollectorState.Error)
                    {
                        errors++;
                        plainTextDetails.AppendLine(string.Format("\t'{0}' - value '{1}' - Error (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.ErrorValue));
                        htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}' - <b>Error</b> (trigger {2})</li>", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.ErrorValue));
                    }
                    else if (instanceState == CollectorState.Warning)
                    {
                        warnings++;
                        plainTextDetails.AppendLine(string.Format("\t'{0}' - value '{1}' - Warning (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.WarningValue));
                        htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}' - <b>Warning</b> (trigger {2})</li>", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.WarningValue));
                    }
                    else
                    {
                        success++;
                        plainTextDetails.AppendLine(string.Format("\t'{0}' - value '{1}'", queryInstance.Name, FormatUtils.FormatArrayToString(value)));
                        htmlTextTextDetails.AppendLine(string.Format("<li>Machine '{0}' - Value '{1}'</li>", queryInstance.Name, FormatUtils.FormatArrayToString(value)));
                    }
                }
                htmlTextTextDetails.AppendLine("</ul>");
                if (errors > 0 && warnings == 0)
                {
                    returnState.State = CollectorState.Error;
                }
                else if (warnings > 0)
                {
                    returnState.State = CollectorState.Warning;
                }
                else
                {
                    returnState.State = CollectorState.Good;
                }
                returnState.RawDetails   = plainTextDetails.ToString().TrimEnd('\r', '\n');
                returnState.HtmlDetails  = htmlTextTextDetails.ToString();
                returnState.CurrentValue = totalValue;
            }
            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);
        }
Exemplo n.º 8
0
        public CollectorState EvaluateValue(object value)
        {
            CollectorState result = CollectorState.Good;

            if (value == null || value == DBNull.Value)
            {
                if (ErrorValue == "[null]")
                {
                    result = CollectorState.Error;
                }
                else if (WarningValue == "[null]")
                {
                    result = CollectorState.Warning;
                }
                else if (SuccessValue == "[null]")
                {
                    result = CollectorState.Good;
                }
                else if (SuccessValue != "[any]")
                {
                    result = CollectorState.Error;
                }
            }
            else if (value.ToString() == "[notExists]")
            {
                if (ErrorValue == "[notExists]")
                {
                    result = CollectorState.Error;
                }
                else if (WarningValue == "[notExists]")
                {
                    result = CollectorState.Warning;
                }
                else if (SuccessValue == "[notExists]")
                {
                    result = CollectorState.Good;
                }
                else
                {
                    result = CollectorState.Error;
                }
            }
            else //non empty value but it DOES exist
            {
                if (!ReturnValueIsNumber || !ReturnValueInARange) //so it's not a number
                {
                    if (value.GetType().IsArray)
                    {
                        value = FormatUtils.FormatArrayToString(value);
                        //StringBuilder sb = new StringBuilder();
                        //if (value is Byte[]) //binary data
                        //{
                        //    Byte[] valArr = (Byte[])value;
                        //    for(int i = 0; i < valArr.Length; i++)
                        //    {
                        //        sb.AppendFormat("{0:x2}", valArr[i]).Append(",");
                        //    }
                        //    value = sb.ToString().Trim(',');
                        //}
                        //else if (value is string[])
                        //{
                        //    string[] valArr = (string[])value;
                        //    foreach (string line in valArr)
                        //        sb.AppendLine(line);
                        //    value = sb.ToString().TrimEnd('\r','\n');
                        //}
                    }

                    //first eliminate matching values
                    if (SuccessValue == value.ToString())
                    {
                        result = CollectorState.Good;
                    }
                    else if (value.ToString() == ErrorValue)
                    {
                        result = CollectorState.Error;
                    }
                    else if (value.ToString() == WarningValue)
                    {
                        result = CollectorState.Warning;
                    }

                    //test for [contains] <value>, [beginswith] <value> or [endswith] <value>
                    else if (SuccessValue.StartsWith("[contains]") && value.ToString().Contains(SuccessValue.Substring(("[contains]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (SuccessValue.StartsWith("[beginswith]") && value.ToString().StartsWith(SuccessValue.Substring(("[beginswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (SuccessValue.StartsWith("[endswith]") && value.ToString().EndsWith(SuccessValue.Substring(("[endswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (WarningValue.StartsWith("[contains]") && value.ToString().Contains(WarningValue.Substring(("[contains]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (WarningValue.StartsWith("[beginswith]") && value.ToString().StartsWith(WarningValue.Substring(("[beginswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (WarningValue.StartsWith("[endswith]") && value.ToString().EndsWith(WarningValue.Substring(("[endswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (ErrorValue.StartsWith("[contains]") && value.ToString().Contains(ErrorValue.Substring(("[contains]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (ErrorValue.StartsWith("[beginswith]") && value.ToString().StartsWith(ErrorValue.Substring(("[beginswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (ErrorValue.StartsWith("[endswith]") && value.ToString().EndsWith(ErrorValue.Substring(("[endswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }

                    //Existing tests
                    else if (ErrorValue == "[exists]")
                    {
                        result = CollectorState.Error;
                    }
                    else if (WarningValue == "[exists]")
                    {
                        result = CollectorState.Warning;
                    }
                    else if (SuccessValue == "[exists]")
                    {
                        result = CollectorState.Good;
                    }

                    //Any tests
                    else if (ErrorValue == "[any]")
                    {
                        result = CollectorState.Error;
                    }
                    else if (WarningValue == "[any]")
                    {
                        result = CollectorState.Warning;
                    }

                    //Not matching success
                    else if (SuccessValue != "[any]")
                    {
                        result = CollectorState.Warning;
                    }
                }
                else if (!value.IsNumber()) //value must be a number!
                {
                    result = CollectorState.Error;
                }
                else //so it is a number and must be inside a range
                {
                    if (ErrorValue != "[any]" && ErrorValue != "[null]" &&
                        (
                            (!ReturnValueInverted && double.Parse(value.ToString()) >= double.Parse(ErrorValue)) ||
                            (ReturnValueInverted && double.Parse(value.ToString()) <= double.Parse(ErrorValue))
                        )
                        )
                    {
                        result = CollectorState.Error;
                    }
                    else if (WarningValue != "[any]" && WarningValue != "[null]" &&
                             (
                                 (!ReturnValueInverted && double.Parse(value.ToString()) >= double.Parse(WarningValue)) ||
                                 (ReturnValueInverted && double.Parse(value.ToString()) <= double.Parse(WarningValue))
                             )
                             )
                    {
                        result = CollectorState.Warning;
                    }
                }
            }

            return(result);
        }
        public CollectorState EvaluateValue(object value)
        {
            CollectorState result = CollectorState.Good;

            if (value == null || value == DBNull.Value)
            {
                if (ErrorValue == "[null]")
                {
                    result = CollectorState.Error;
                }
                else if (WarningValue == "[null]")
                {
                    result = CollectorState.Warning;
                }
                else if (SuccessValue == "[null]")
                {
                    result = CollectorState.Good;
                }
                else if (SuccessValue != "[any]")
                {
                    result = CollectorState.Error;
                }
            }
            else if (value.ToString() == "[notExists]")
            {
                if (ErrorValue == "[notExists]")
                {
                    result = CollectorState.Error;
                }
                else if (WarningValue == "[notExists]")
                {
                    result = CollectorState.Warning;
                }
                else if (SuccessValue == "[notExists]")
                {
                    result = CollectorState.Good;
                }
                else
                {
                    result = CollectorState.Error;
                }
            }
            else //non empty value but it DOES exist
            {
                if (!ReturnValueIsNumber || !ReturnValueInARange) //so it's not a number
                {
                    if (value.GetType().IsArray)
                    {
                        value = FormatUtils.FormatArrayToString(value);
                    }

                    //first eliminate matching values
                    if (SuccessValue == value.ToString())
                    {
                        result = CollectorState.Good;
                    }
                    else if (value.ToString() == ErrorValue)
                    {
                        result = CollectorState.Error;
                    }
                    else if (value.ToString() == WarningValue)
                    {
                        result = CollectorState.Warning;
                    }

                    //test for [contains] <value>, [beginswith] <value> or [endswith] <value>
                    else if (SuccessValue.StartsWith("[contains]") && value.ToString().Contains(SuccessValue.Substring(("[contains]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (SuccessValue.StartsWith("[beginswith]") && value.ToString().StartsWith(SuccessValue.Substring(("[beginswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (SuccessValue.StartsWith("[endswith]") && value.ToString().EndsWith(SuccessValue.Substring(("[endswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (WarningValue.StartsWith("[contains]") && value.ToString().Contains(WarningValue.Substring(("[contains]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (WarningValue.StartsWith("[beginswith]") && value.ToString().StartsWith(WarningValue.Substring(("[beginswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (WarningValue.StartsWith("[endswith]") && value.ToString().EndsWith(WarningValue.Substring(("[endswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (ErrorValue.StartsWith("[contains]") && value.ToString().Contains(ErrorValue.Substring(("[contains]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (ErrorValue.StartsWith("[beginswith]") && value.ToString().StartsWith(ErrorValue.Substring(("[beginswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }
                    else if (ErrorValue.StartsWith("[endswith]") && value.ToString().EndsWith(ErrorValue.Substring(("[endswith]").Length).Trim()))
                    {
                        result = CollectorState.Good;
                    }

                    //Existing tests
                    else if (ErrorValue == "[exists]")
                    {
                        result = CollectorState.Error;
                    }
                    else if (WarningValue == "[exists]")
                    {
                        result = CollectorState.Warning;
                    }
                    else if (SuccessValue == "[exists]")
                    {
                        result = CollectorState.Good;
                    }

                    //Any tests
                    else if (ErrorValue == "[any]")
                    {
                        result = CollectorState.Error;
                    }
                    else if (WarningValue == "[any]")
                    {
                        result = CollectorState.Warning;
                    }

                    //Not matching success
                    else if (SuccessValue != "[any]")
                    {
                        result = CollectorState.Warning;
                    }
                }
                else if (!value.IsNumber()) //value must be a number!
                {
                    result = CollectorState.Error;
                }
                else //so it is a number and must be inside a range
                {
                    if (ErrorValue != "[any]" && ErrorValue != "[null]" &&
                        (
                            (!ReturnValueInverted && double.Parse(value.ToString()) >= double.Parse(ErrorValue)) ||
                            (ReturnValueInverted && double.Parse(value.ToString()) <= double.Parse(ErrorValue))
                        )
                        )
                    {
                        result = CollectorState.Error;
                    }
                    else if (WarningValue != "[any]" && WarningValue != "[null]" &&
                             (
                                 (!ReturnValueInverted && double.Parse(value.ToString()) >= double.Parse(WarningValue)) ||
                                 (ReturnValueInverted && double.Parse(value.ToString()) <= double.Parse(WarningValue))
                             )
                             )
                    {
                        result = CollectorState.Warning;
                    }
                }
            }

            return(result);
        }
Exemplo n.º 10
0
        public override MonitorState RefreshState()
        {
            MonitorState returnState = new MonitorState();
            string       lastAction  = "";
            int          errors      = 0;
            int          success     = 0;
            int          warnings    = 0;
            double       totalValue  = 0;

            try
            {
                RegistryQueryCollectorConfig currentConfig = (RegistryQueryCollectorConfig)AgentConfig;
                returnState.RawDetails  = string.Format("Running {0} registry query(s)", currentConfig.Entries.Count);
                returnState.HtmlDetails = string.Format("<b>Running {0} registry query(s)</b>", currentConfig.Entries.Count);

                foreach (RegistryQueryCollectorConfigEntry queryInstance in currentConfig.Entries)
                {
                    object value = null;
                    if (queryInstance.UseRemoteServer)
                    {
                        lastAction = string.Format("Running Registry query '{0}' on '{1}'", queryInstance.Name, queryInstance.Server);
                    }
                    else
                    {
                        lastAction = string.Format("Running Registry query '{0}'", queryInstance.Name);
                    }

                    value = queryInstance.GetValue();
                    if (!queryInstance.ReturnValueIsNumber && value.IsNumber())
                    {
                        totalValue += double.Parse(value.ToString());
                    }

                    CollectorState instanceState = queryInstance.EvaluateValue(value);

                    if (instanceState == CollectorState.Error)
                    {
                        errors++;
                        returnState.ChildStates.Add(
                            new MonitorState()
                        {
                            ForAgent     = queryInstance.Name,
                            State        = CollectorState.Error,
                            CurrentValue = value,
                            RawDetails   = string.Format("'{0}' - value '{1}' - Error (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.ErrorValue),
                            HtmlDetails  = string.Format("'{0}' - value '{1}' - <b>Error</b> (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.ErrorValue),
                        });
                    }
                    else if (instanceState == CollectorState.Warning)
                    {
                        warnings++;
                        returnState.ChildStates.Add(
                            new MonitorState()
                        {
                            ForAgent     = queryInstance.Name,
                            State        = CollectorState.Warning,
                            CurrentValue = value,
                            RawDetails   = string.Format("'{0}' - value '{1}' - Warning (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.WarningValue),
                            HtmlDetails  = string.Format("'{0}' - value '{1}' - <b>Warning</b> (trigger {2})", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]"), queryInstance.WarningValue),
                        });
                    }
                    else
                    {
                        success++;
                        returnState.ChildStates.Add(
                            new MonitorState()
                        {
                            ForAgent     = queryInstance.Name,
                            State        = CollectorState.Good,
                            CurrentValue = value,
                            RawDetails   = string.Format("'{0}' - value '{1}'", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]")),
                            HtmlDetails  = string.Format("'{0}' - value '{1}'", queryInstance.Name, FormatUtils.FormatArrayToString(value, "[null]")),
                        });
                    }
                }
                if (errors > 0 && warnings == 0)
                {
                    returnState.State = CollectorState.Error;
                }
                else if (warnings > 0)
                {
                    returnState.State = CollectorState.Warning;
                }
                else
                {
                    returnState.State = CollectorState.Good;
                }
                returnState.CurrentValue = totalValue;
            }
            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);
        }