コード例 #1
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);
        }
コード例 #2
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);
                    }

                    //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);
        }