コード例 #1
0
 public void ExtractAttributes(MessageDataItem message)
 {
     if (message.AttributeExists(InputAttribute) && !message.AttributeExists(OutputAttribute))
     {
         try
         {
             IPAddress   hostIPAddress = IPAddress.Parse(message.GetAttributeAsString(InputAttribute));
             IPHostEntry hostInfo      = Dns.GetHostEntry(hostIPAddress);
             message.AddAttribute(OutputAttribute, hostInfo.HostName);
         }
         catch
         {
             // none
         }
     }
 }
コード例 #2
0
 public void ExtractAttributes(MessageDataItem message)
 {
     try
     {
         if (string.IsNullOrWhiteSpace(message.Message) && AddDefaultIfNoPRI)
         {
             AddSyslogPRI(message, facility: DefaultFacility, severity: DefaultSeverity);
             return;
         }
         int    maxSubLen      = message.Message.Length > 20 ? 20 : message.Message.Length;
         string rawMessageHead = message.Message.Substring(0, maxSubLen).Trim();
         if (string.IsNullOrWhiteSpace(rawMessageHead) && AddDefaultIfNoPRI)
         {
             AddSyslogPRI(message, facility: DefaultFacility, severity: DefaultSeverity);
             return;
         }
         if (rawMessageHead[0] == '<')
         {
             if (int.TryParse(rawMessageHead.Substring(1, rawMessageHead.IndexOf('>') - 1), out int priValue))
             {
                 int facility = priValue >> 3;
                 int severity = priValue & 7;
                 AddSyslogPRI(message, facility: facility, severity: severity);
                 return;
             }
         }
         if (AddDefaultIfNoPRI)
         {
             AddSyslogPRI(message, facility: DefaultFacility, severity: DefaultSeverity);
         }
         return;
     }
     catch
     {
         {
             if (!message.AttributeExists("Facility") && !message.AttributeExists("Severity"))
             {
                 AddSyslogPRI(message, facility: DefaultFacility, severity: DefaultSeverity);
             }
         }
     }
 }
コード例 #3
0
ファイル: Expression.cs プロジェクト: MaxxVolk/YASLS.NET
        public bool Evaluate(MessageDataItem dataItem)
        {
            if (And != null)
            {
                return(And.All(x => x?.Evaluate(dataItem) == true));
            }
            if (Or != null)
            {
                return(Or.Any(x => x?.Evaluate(dataItem) == true));
            }
            if (Not != null)
            {
                return(!Not.Evaluate(dataItem));
            }
            if (Exists != null)
            {
                if (Exists.Attribute != null)
                {
                    if (dataItem.AttributeExists(Exists.Attribute.Name ?? ""))
                    {
                        if (dataItem.GetAttributeAsVariant(Exists.Attribute.Name).Type == Exists.Attribute.Type)
                        {
                            return(true);
                        }
                    }
                }
            }
            if (SimpleExpression != null)
            {
                return(SimpleExpression.Evaluate(dataItem));
            }
            if (InExpression != null)
            {
                return(InExpression.Evaluate(dataItem));
            }
            if (RegExExpression != null)
            {
                return(RegExExpression.Evaluate(dataItem));
            }
            if (ModuleExpressionModule != null)
            {
                return(ModuleExpressionModule.IsMatch(dataItem));
            }

            return(false);
        }
コード例 #4
0
ファイル: RegExParser.cs プロジェクト: MaxxVolk/YASLS.NET
        }                                       // null if message body

        public void SetField(GroupCollection groups, MessageDataItem inputMessage, ref string outMessage, Dictionary <string, Variant> outAttributes)
        {
            if (!string.IsNullOrEmpty(Input?.Group))
            {
                string dynamicOutputAttribute;
                if (string.IsNullOrEmpty(Input.GroupToOutputAttribute))
                {
                    dynamicOutputAttribute = OutputAttribute;
                }
                else
                {
                    dynamicOutputAttribute = groups[Input.GroupToOutputAttribute].Value;
                }
                switch (Input.Type)
                {
                case VariantType.Boolean:
                    if (bool.TryParse(groups[Input.Group]?.Value, out bool newBoolValue))
                    {
                        if (string.IsNullOrEmpty(dynamicOutputAttribute))
                        {
                            outMessage = newBoolValue.ToString();
                        }
                        else
                        {
                            outAttributes[dynamicOutputAttribute] = new Variant {
                                BooleanValue = newBoolValue, Type = VariantType.Boolean
                            }
                        };
                    }
                    return;

                case VariantType.DateTime:
                    if (string.IsNullOrEmpty(Input.Format))
                    {
                        if (DateTime.TryParse(groups[Input.Group]?.Value, out DateTime newDateTimeValue))
                        {
                            if (string.IsNullOrEmpty(dynamicOutputAttribute))
                            {
                                outMessage = newDateTimeValue.ToString();
                            }
                            else
                            {
                                outAttributes[dynamicOutputAttribute] = new Variant {
                                    DateTimeValue = newDateTimeValue, Type = VariantType.DateTime
                                }
                            }
                        }
                        ;
                    }
                    else
                    {
                        if (DateTime.TryParseExact(groups[Input.Group]?.Value, Input.Format, null, DateTimeStyles.AssumeLocal, out DateTime newDateTimeValue))
                        {
                            if (string.IsNullOrEmpty(dynamicOutputAttribute))
                            {
                                outMessage = newDateTimeValue.ToString();
                            }
                            else
                            {
                                outAttributes[dynamicOutputAttribute] = new Variant {
                                    DateTimeValue = newDateTimeValue, Type = VariantType.DateTime
                                }
                            }
                        }
                        ;
                    }
                    return;

                case VariantType.Float:
                    if (float.TryParse(groups[Input.Group].Value, out float newFloatValue))
                    {
                        if (string.IsNullOrEmpty(dynamicOutputAttribute))
                        {
                            outMessage = (newFloatValue.ToString());
                        }
                        else
                        {
                            outAttributes[dynamicOutputAttribute] = new Variant {
                                FloatValue = newFloatValue, Type = VariantType.Float
                            }
                        }
                    }
                    ;
                    return;

                case VariantType.Int:
                    if (int.TryParse(groups[Input.Group].Value, out int newIntValue))
                    {
                        if (string.IsNullOrEmpty(dynamicOutputAttribute))
                        {
                            outMessage = (newIntValue.ToString());
                        }
                        else
                        {
                            outAttributes[dynamicOutputAttribute] = new Variant {
                                IntValue = newIntValue, Type = VariantType.Int
                            }
                        }
                    }
                    ;
                    return;

                case VariantType.String:
                    if (string.IsNullOrEmpty(dynamicOutputAttribute))
                    {
                        outMessage = groups[Input.Group]?.Value ?? "";
                    }
                    else
                    {
                        outAttributes[dynamicOutputAttribute] = new Variant {
                            StringValue = groups[Input.Group]?.Value, Type = VariantType.String
                        }
                    };
                    return;
                }
            }
            if (Input?.Message == true)
            {
                if (string.IsNullOrEmpty(OutputAttribute))
                {
                    outMessage = inputMessage.Message;
                }
                else
                {
                    outAttributes[OutputAttribute] = new Variant {
                        StringValue = inputMessage.Message, Type = VariantType.String
                    }
                };
                return;
            }
            if (!string.IsNullOrEmpty(Input?.Attribute))
            {
                if (string.IsNullOrEmpty(OutputAttribute))
                {
                    if (inputMessage.AttributeExists(Input.Attribute))
                    {
                        outMessage = inputMessage.GetAttributeAsVariant(Input.Attribute).ToString();
                    }
                }
                else
                {
                    if (inputMessage.AttributeExists(Input.Attribute))
                    {
                        outAttributes[OutputAttribute] = inputMessage.GetAttributeAsVariant(Input.Attribute);
                    }
                }
                return;
            }
            if (Input?.Value != null)
            {
                switch (Input.Type)
                {
                case VariantType.Boolean:
                    if (bool.TryParse(Input.Value, out bool newBoolValue))
                    {
                        if (string.IsNullOrEmpty(OutputAttribute))
                        {
                            outMessage = newBoolValue.ToString();
                        }
                        else
                        {
                            outAttributes[OutputAttribute] = new Variant {
                                BooleanValue = newBoolValue, Type = VariantType.Boolean
                            }
                        };
                    }
                    return;

                case VariantType.DateTime:
                    if (string.IsNullOrEmpty(Input.Format))
                    {
                        if (DateTime.TryParse(Input.Value, out DateTime newDateTimeValue))
                        {
                            if (string.IsNullOrEmpty(OutputAttribute))
                            {
                                outMessage = newDateTimeValue.ToString();
                            }
                            else
                            {
                                outAttributes[OutputAttribute] = new Variant {
                                    DateTimeValue = newDateTimeValue, Type = VariantType.DateTime
                                }
                            }
                        }
                        ;
                    }
                    else
                    {
                        if (DateTime.TryParseExact(Input.Value, Input.Format, null, DateTimeStyles.AssumeLocal, out DateTime newDateTimeValue))
                        {
                            if (string.IsNullOrEmpty(OutputAttribute))
                            {
                                outMessage = newDateTimeValue.ToString();
                            }
                            else
                            {
                                outAttributes[OutputAttribute] = new Variant {
                                    DateTimeValue = newDateTimeValue, Type = VariantType.DateTime
                                }
                            }
                        }
                        ;
                    }
                    return;

                case VariantType.Float:
                    if (float.TryParse(Input.Value, out float newFloatValue))
                    {
                        if (string.IsNullOrEmpty(OutputAttribute))
                        {
                            outMessage = (newFloatValue.ToString());
                        }
                        else
                        {
                            outAttributes[OutputAttribute] = new Variant {
                                FloatValue = newFloatValue, Type = VariantType.Float
                            }
                        }
                    }
                    ;
                    return;

                case VariantType.Int:
                    if (int.TryParse(Input.Value, out int newIntValue))
                    {
                        if (string.IsNullOrEmpty(OutputAttribute))
                        {
                            outMessage = (newIntValue.ToString());
                        }
                        else
                        {
                            outAttributes[OutputAttribute] = new Variant {
                                IntValue = newIntValue, Type = VariantType.Int
                            }
                        }
                    }
                    ;
                    return;

                case VariantType.String:
                    if (string.IsNullOrEmpty(OutputAttribute))
                    {
                        outMessage = Input.Value;
                    }
                    else
                    {
                        outAttributes[OutputAttribute] = new Variant {
                            StringValue = Input.Value, Type = VariantType.String
                        }
                    };
                    return;
                }
            }
        }
    }