3 """TSAR Event rewrite rule support"""
10 #pylint: disable=C0103,R0903
14 """Represents a condition to check"""
15 def __init__(self, cond):
16 """Condition has the format: <attribute>:<value>
17 where value can be an integer or a string (if surrounded
18 with quotation marks."""
19 (self.attrib, v) = cond.split(":")
25 self.value = int(v, 0)
29 #print self.attrib, " -- ", self.value
32 """Apply the condition to a given object.
34 Checks if the object matches the (attribute,value)
35 pair specified by this condition.
37 return getattr(obj, self.attrib) == self.value
41 return "Cond(%s == %s)" % (self.attrib, self.value)
43 return "Cond(%s == 0x%x)" % (self.attrib, self.value)
47 """Representation of a single rewrite rule.
49 Rules come from a file and consist of a list of (conjunctive)
50 conditions and a rewrite target.
51 * Conditions are a list of <attr> : <val> pairs, separated
53 * The rewrite target is a <attr> : <val> pair
54 * Values can be integer values (any base encoding is
55 supported) or strings (using quotation marks).
56 * Conditions and rewrite target a separated using an arrow: ->
58 def __init__(self, _input):
62 for cond in _input[0].split(","):
63 self.conditions += [Condition(cond)]
65 (self.dest, val) = _input[1].split(":")
66 self.deststring = (val[0] == "\"")
68 self.destval = val[1:-1]
70 self.destval = int(val, 0)
72 def apply(self, event):
73 """Apply the rewrite rule to a given event object.
75 If the event matches all conditions of this rule,
76 the event's target attribute is rewritten.
78 for c in self.conditions:
81 if not c.apply(event):
82 #print "Cond did not hold"
85 #print "Rewriting: %s -> %s" % (self.dest, self.destval)
86 setattr(event, self.dest, self.destval)
89 class EventPreprocessor:
92 Takes a list of rewrite rules from a configuration file.
93 These rewrite rules can then be applied to an event using
94 the process() function.
96 The configuration file is searched either under the name
97 "tsar.rules" in the local directory or by using the
98 TSAR_RULEFILE environment variable if it is set.
101 def tryLocalRuleFile(self):
102 """Try to use local tsar.rules rule file."""
104 self.rulefile = file("tsar.rules", "r")
108 def tryEnvRuleFile(self):
109 """Try to find rule file using TSAR_RULEFILE
110 environment variable.
113 envrule = os.getenv("TSAR_RULEFILE")
115 self.rulefile = tempfile.NamedTemporaryFile(mode="r")
117 self.rulefile = file(envrule, "r")
121 def parseRules(self):
122 """Read rules from the rule file found."""
124 for l in self.rulefile.readlines():
127 if l.strip().startswith("#"):
130 l = l.replace(" ", "")
131 m = re.match("(.*)->(.*)", l.strip())
133 rules += [ProcessorRule(m.groups())]
138 self.tryLocalRuleFile()
139 if self.rulefile is None:
140 self.tryEnvRuleFile()
141 if self.rulefile is None:
142 sys.stderr.write("No rule file found. "
143 "Use TSAR_FULEFILE='' to use none.\n")
146 self.rules = self.parseRules()
148 def process(self, event):
149 """Apply the rewrite rules to the given event."""