Пример #1
0
        public static CollectorState GetState(CollectorAgentReturnValueCheckSequence st,
                                              CollectorAgentReturnValueCompareMatchType goodMatchType, string goodMatchFilter,
                                              CollectorAgentReturnValueCompareMatchType warningMatchType, string warningMatchFilter,
                                              CollectorAgentReturnValueCompareMatchType errorMatchType, string errorMatchFilter,
                                              object value)
        {
            CollectorState currentState = CollectorState.Good;

            if (st == CollectorAgentReturnValueCheckSequence.GWE)
            {
                if (TestResult(goodMatchType, goodMatchFilter, value))
                {
                    currentState = CollectorState.Good;
                }
                else if (TestResult(warningMatchType, warningMatchFilter, value))
                {
                    currentState = CollectorState.Warning;
                }
                else
                {
                    currentState = CollectorState.Error;
                }
            }
            else
            {
                if (TestResult(errorMatchType, errorMatchFilter, value))
                {
                    currentState = CollectorState.Error;
                }
                else if (TestResult(warningMatchType, warningMatchFilter, value))
                {
                    currentState = CollectorState.Warning;
                }
                else
                {
                    currentState = CollectorState.Good;
                }
            }
            return(currentState);
        }
Пример #2
0
        public void FromXml(string configurationString)
        {
            XmlDocument config = new XmlDocument();

            config.LoadXml(configurationString);
            XmlElement root = config.DocumentElement;

            Entries.Clear();
            foreach (XmlElement addressNode in root.SelectNodes("webServices/webService"))
            {
                WSCollectorConfigEntry webServicePingEntry = new WSCollectorConfigEntry();
                webServicePingEntry.ServiceBaseURL     = addressNode.ReadXmlElementAttr("url", "");
                webServicePingEntry.ServiceBindingName = addressNode.ReadXmlElementAttr("serviceBindingName", "");
                webServicePingEntry.MethodName         = addressNode.ReadXmlElementAttr("method");
                string parameterStr = addressNode.ReadXmlElementAttr("paramatersCSV");
                webServicePingEntry.Parameters = new List <string>();
                if (parameterStr.Trim().Length > 0)
                {
                    webServicePingEntry.Parameters.AddRange(parameterStr.Split(','));
                }
                //webServicePingEntry.ResultIsSuccess = addressNode.ReadXmlElementAttr("resultIsSuccess", true);
                webServicePingEntry.ValueExpectedReturnType = WebServiceValueExpectedReturnTypeConverter.FromString(addressNode.ReadXmlElementAttr("valueExpectedReturnType", ""));
                webServicePingEntry.MacroFormatType         = WebServiceMacroFormatTypeConverter.FromString(addressNode.ReadXmlElementAttr("macroFormatType", ""));
                webServicePingEntry.CheckValueArrayIndex    = addressNode.ReadXmlElementAttr("arrayIndex", 0);
                webServicePingEntry.CheckValueColumnIndex   = addressNode.ReadXmlElementAttr("columnIndex", 0);

                if (addressNode.ReadXmlElementAttr("resultIsSuccess", true))
                {
                    webServicePingEntry.ReturnCheckSequence = CollectorAgentReturnValueCheckSequence.GWE;
                }
                else
                {
                    webServicePingEntry.ReturnCheckSequence = CollectorAgentReturnValueCheckSequence.EWG;
                }

                webServicePingEntry.GoodResultMatchType = CollectorAgentReturnValueCompareMatchType.Contains;
                webServicePingEntry.GoodScriptText      = addressNode.ReadXmlElementAttr("valueOrMacro", "");
                if (webServicePingEntry.ValueExpectedReturnType == WebServiceValueExpectedReturnTypeEnum.CheckAvailabilityOnly)
                {
                    webServicePingEntry.GoodScriptText = "[Available]";
                }
                else if (addressNode.ReadXmlElementAttr("useRegEx", false))
                {
                    webServicePingEntry.GoodResultMatchType = CollectorAgentReturnValueCompareMatchType.RegEx;
                }
                else if (webServicePingEntry.GoodScriptText.StartsWith("[Between] "))
                {
                    webServicePingEntry.GoodResultMatchType = CollectorAgentReturnValueCompareMatchType.Between;
                    webServicePingEntry.GoodScriptText      = webServicePingEntry.GoodScriptText.Substring("[Between] ".Length);
                    webServicePingEntry.GoodScriptText      = webServicePingEntry.GoodScriptText.Replace("[and]", "and");
                }
                else if (webServicePingEntry.GoodScriptText.StartsWith("[LargerThan] "))
                {
                    webServicePingEntry.GoodResultMatchType = CollectorAgentReturnValueCompareMatchType.LargerThan;
                    webServicePingEntry.GoodScriptText      = webServicePingEntry.GoodScriptText.Substring("[LargerThan] ".Length);
                }
                else if (webServicePingEntry.GoodScriptText.StartsWith("[SmallerThan] "))
                {
                    webServicePingEntry.GoodResultMatchType = CollectorAgentReturnValueCompareMatchType.SmallerThan;
                    webServicePingEntry.GoodScriptText      = webServicePingEntry.GoodScriptText.Substring("[SmallerThan] ".Length);
                }
                else if (webServicePingEntry.GoodScriptText.StartsWith("[Contains] "))
                {
                    webServicePingEntry.GoodResultMatchType = CollectorAgentReturnValueCompareMatchType.Contains;
                    webServicePingEntry.GoodScriptText      = webServicePingEntry.GoodScriptText.Substring("[Contains] ".Length);
                }
                else if (webServicePingEntry.GoodScriptText.StartsWith("[BeginsWith] "))
                {
                    webServicePingEntry.GoodResultMatchType = CollectorAgentReturnValueCompareMatchType.StartsWith;
                    webServicePingEntry.GoodScriptText      = webServicePingEntry.GoodScriptText.Substring("[BeginsWith] ".Length);
                }
                else if (webServicePingEntry.GoodScriptText.StartsWith("[EndsWith] "))
                {
                    webServicePingEntry.GoodResultMatchType = CollectorAgentReturnValueCompareMatchType.EndsWith;
                    webServicePingEntry.GoodScriptText      = webServicePingEntry.GoodScriptText.Substring("[EndsWith] ".Length);
                }

                webServicePingEntry.WarningResultMatchType = CollectorAgentReturnValueCompareMatchType.Contains;
                webServicePingEntry.WarningScriptText      = "[null]";

                webServicePingEntry.ErrorResultMatchType = CollectorAgentReturnValueCompareMatchType.Contains;
                webServicePingEntry.ErrorScriptText      = "[any]";

                if (webServicePingEntry.ReturnCheckSequence == CollectorAgentReturnValueCheckSequence.EWG)
                {
                    CollectorAgentReturnValueCompareMatchType tmpMT = webServicePingEntry.GoodResultMatchType;
                    string tmpTestValue = webServicePingEntry.GoodScriptText;
                    webServicePingEntry.GoodResultMatchType  = webServicePingEntry.ErrorResultMatchType;
                    webServicePingEntry.GoodScriptText       = webServicePingEntry.ErrorScriptText;
                    webServicePingEntry.ErrorResultMatchType = tmpMT;
                    webServicePingEntry.ErrorScriptText      = tmpTestValue;
                }

                Entries.Add(webServicePingEntry);
            }
            foreach (XmlElement carvceEntryNode in root.SelectNodes("carvcesEntries/carvceEntry"))
            {
                WSCollectorConfigEntry webServicePingEntry = new WSCollectorConfigEntry();
                XmlNode dataSourceNode = carvceEntryNode.SelectSingleNode("dataSource");
                webServicePingEntry.ServiceBaseURL     = dataSourceNode.ReadXmlElementAttr("url", "");
                webServicePingEntry.ServiceBindingName = dataSourceNode.ReadXmlElementAttr("serviceBindingName", "");
                webServicePingEntry.MethodName         = dataSourceNode.ReadXmlElementAttr("method", "");
                string parameterStr = dataSourceNode.ReadXmlElementAttr("paramatersCSV", "");
                webServicePingEntry.Parameters = new List <string>();
                if (parameterStr.Trim().Length > 0)
                {
                    webServicePingEntry.Parameters.AddRange(parameterStr.Split(','));
                }
                webServicePingEntry.ValueExpectedReturnType = WebServiceValueExpectedReturnTypeConverter.FromString(dataSourceNode.ReadXmlElementAttr("valueExpectedReturnType", ""));
                webServicePingEntry.MacroFormatType         = WebServiceMacroFormatTypeConverter.FromString(dataSourceNode.ReadXmlElementAttr("macroFormatType", ""));
                webServicePingEntry.CheckValueArrayIndex    = dataSourceNode.ReadXmlElementAttr("arrayIndex", 0);
                webServicePingEntry.CheckValueColumnIndex   = dataSourceNode.ReadXmlElementAttr("columnIndex", 0);
                webServicePingEntry.PrimaryUIValue          = dataSourceNode.ReadXmlElementAttr("primaryUIValue", false);

                XmlNode testConditionsNode = carvceEntryNode.SelectSingleNode("testConditions");
                if (testConditionsNode != null)
                {
                    webServicePingEntry.ReturnCheckSequence = CollectorAgentReturnValueCompareEngine.CheckSequenceTypeFromString(testConditionsNode.ReadXmlElementAttr("testSequence", "gwe"));
                    XmlNode goodScriptNode = testConditionsNode.SelectSingleNode("success");
                    webServicePingEntry.GoodResultMatchType = CollectorAgentReturnValueCompareEngine.MatchTypeFromString(goodScriptNode.ReadXmlElementAttr("testType", "match"));
                    webServicePingEntry.GoodScriptText      = goodScriptNode.InnerText;
                    if (webServicePingEntry.ValueExpectedReturnType == WebServiceValueExpectedReturnTypeEnum.CheckAvailabilityOnly)
                    {
                        webServicePingEntry.GoodScriptText = "[Available]";
                    }

                    XmlNode warningScriptNode = testConditionsNode.SelectSingleNode("warning");
                    webServicePingEntry.WarningResultMatchType = CollectorAgentReturnValueCompareEngine.MatchTypeFromString(warningScriptNode.ReadXmlElementAttr("testType", "match"));
                    webServicePingEntry.WarningScriptText      = warningScriptNode.InnerText;

                    XmlNode errorScriptNode = testConditionsNode.SelectSingleNode("error");
                    webServicePingEntry.ErrorResultMatchType = CollectorAgentReturnValueCompareEngine.MatchTypeFromString(errorScriptNode.ReadXmlElementAttr("testType", "match"));
                    webServicePingEntry.ErrorScriptText      = errorScriptNode.InnerText;
                }
                else
                {
                    webServicePingEntry.ReturnCheckSequence = CollectorAgentReturnValueCheckSequence.GWE;
                }

                Entries.Add(webServicePingEntry);
            }
        }
        public static bool TestResult(CollectorAgentReturnValueCompareMatchType tcmt, string matchFilter, object resultToTest)
        {
            if (matchFilter.ToLower() == "[any]")
            {
                return(true);
            }
            else if (matchFilter.ToLower() == "[null]" && (resultToTest == null || resultToTest.ToString() == "[null]"))
            {
                return(true);
            }
            switch (tcmt)
            {
            case CollectorAgentReturnValueCompareMatchType.Match:
                return(matchFilter.ToLower() == resultToTest.ToString().Trim('\r', '\n').ToLower());

            case CollectorAgentReturnValueCompareMatchType.DoesNotMatch:
                return(matchFilter.ToLower() != resultToTest.ToString().Trim('\r', '\n').ToLower());

            case CollectorAgentReturnValueCompareMatchType.Contains:
                return(resultToTest.ToString().ToLower().Contains(matchFilter.ToLower()));

            case CollectorAgentReturnValueCompareMatchType.DoesNotContain:
                return(!resultToTest.ToString().ToLower().Contains(matchFilter.ToLower()));

            case CollectorAgentReturnValueCompareMatchType.StartsWith:
                return(resultToTest.ToString().Trim('\r', '\n').ToLower().StartsWith(matchFilter.ToLower()));

            case CollectorAgentReturnValueCompareMatchType.DoesNotStartWith:
                return(!resultToTest.ToString().Trim('\r', '\n').ToLower().StartsWith(matchFilter.ToLower()));

            case CollectorAgentReturnValueCompareMatchType.EndsWith:
                return(resultToTest.ToString().Trim('\r', '\n').ToLower().EndsWith(matchFilter.ToLower()));

            case CollectorAgentReturnValueCompareMatchType.DoesNotEndWith:
                return(!resultToTest.ToString().Trim('\r', '\n').ToLower().EndsWith(matchFilter.ToLower()));

            case CollectorAgentReturnValueCompareMatchType.RegEx:
                System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(resultToTest.ToString().Trim('\r', '\n'), matchFilter, System.Text.RegularExpressions.RegexOptions.Multiline);
                return(match.Success);

            case CollectorAgentReturnValueCompareMatchType.IsNotANumber:
                if (!resultToTest.IsNumber())
                {
                    return(true);
                }
                else
                {
                    return(false);
                }

            case CollectorAgentReturnValueCompareMatchType.IsNumber:
                if (!resultToTest.IsNumber())
                {
                    return(false);
                }
                else if (matchFilter.StartsWith("["))
                {
                    if (matchFilter.ToLower().StartsWith("[between]") && matchFilter.ToLower().Contains("[and]"))
                    {
                        string[] queryItems = matchFilter.Split(' ');
                        if (resultToTest.IsNumber() && queryItems.Length == 4 && queryItems[1].IsNumber() && queryItems[3].IsNumber())
                        {
                            return((double.Parse(queryItems[1]) < double.Parse(resultToTest.ToString())) &&
                                   (double.Parse(resultToTest.ToString()) < double.Parse(queryItems[3])));
                        }
                        else
                        {
                            throw new Exception("Value is not a number or macro syntax invalid!");
                        }
                    }
                    else
                    {
                        throw new Exception("Unknown or invalid macro syntax!");
                    }
                }
                else
                {
                    if (!matchFilter.IsNumber())
                    {
                        throw new Exception("Test script is not a number or macro syntax invalid!");
                    }
                    else
                    {
                        return(double.Parse(matchFilter) == double.Parse(resultToTest.ToString()));
                    }
                }

            case CollectorAgentReturnValueCompareMatchType.LargerThan:
                if (!resultToTest.IsNumber() || !matchFilter.IsNumber())
                {
                    throw new Exception("Test script or value is not a number!");
                }
                else
                {
                    return(double.Parse(resultToTest.ToString()) > double.Parse(matchFilter));
                }

            case CollectorAgentReturnValueCompareMatchType.SmallerThan:
                if (!resultToTest.IsNumber() || !matchFilter.IsNumber())
                {
                    throw new Exception("Test script or value is not a number!");
                }
                else
                {
                    return(double.Parse(resultToTest.ToString()) < double.Parse(matchFilter));
                }

            case CollectorAgentReturnValueCompareMatchType.Between:
                if (!resultToTest.IsNumber())
                {
                    throw new Exception("Value is not a number!");
                }
                else
                {
                    string[] betweenMatchParts = matchFilter.Split(new string[] { " ", ",", "and" }, StringSplitOptions.RemoveEmptyEntries);
                    if (betweenMatchParts.Length < 2)
                    {
                        throw new Exception("Test script format invalid!");
                    }
                    else
                    {
                        return
                            (double.Parse(betweenMatchParts[0]) < double.Parse(resultToTest.ToString()) &&
                             double.Parse(resultToTest.ToString()) < double.Parse(betweenMatchParts[betweenMatchParts.Length - 1]));
                    }
                }

            case CollectorAgentReturnValueCompareMatchType.NotBetween:
                if (!resultToTest.IsNumber())
                {
                    throw new Exception("Value is not a number!");
                }
                else
                {
                    string[] betweenMatchParts = matchFilter.Split(new string[] { " ", ",", "and" }, StringSplitOptions.RemoveEmptyEntries);
                    if (betweenMatchParts.Length < 2)
                    {
                        throw new Exception("Test script format invalid!");
                    }
                    else
                    {
                        return(!(
                                   double.Parse(betweenMatchParts[0]) < double.Parse(resultToTest.ToString()) &&
                                   double.Parse(resultToTest.ToString()) < double.Parse(betweenMatchParts[betweenMatchParts.Length - 1])
                                   ));
                    }
                }

            default:
                return(matchFilter.ToLower() == resultToTest.ToString().ToLower());
            }
        }