--- a/bin/pvl.irker-syslog Sun Jan 13 03:11:25 2013 +0200
+++ b/bin/pvl.irker-syslog Sun Jan 13 03:11:53 2013 +0200
@@ -50,15 +50,50 @@
return options, args
+def apply_irker (irker) :
+ """
+ Handle irker activity.
+ """
+
+ for msg in irker.irk :
+ log.info("irk: %s", msg)
+
+def apply_syslog (options, syslog, rules, irker) :
+ """
+ Handle syslog activity.
+ """
+
+ # syslogs
+ for item in syslog :
+ match, rulepath, apply = rules.apply(item)
+
+ log.debug("%s: %s: %s", item, rulepath, apply)
+
+ target = apply.get('irk', options.irker_target)
+
+ tag = '/'.join(str(rule) for rule in reversed(rulepath[:-1]))
+ text = apply.get('text')
+
+ log.info("%s: %s: %s", target, tag, text)
+
+ if not text :
+ # XXX: plain irk = ... in rule is broken, as it always applies, and skips any further rules
+ continue
+
+ if irker and target :
+ irker[target]('[' + tag + '] ' + text)
+ else :
+ print tag, text
+
def main (argv) :
options, args = parse_options(argv)
-
+
+ # no args
if args :
- # XXX: targets
- target, = args
- else :
- target = None
+ log.error("Usage: pvl.irker-syslog [options]")
+ return 2
+ # setup
log.info("Open syslog...")
syslog = pvl.syslog.args.apply(options)
@@ -76,27 +111,12 @@
# customized mainloop that supports irker.irk
while True :
- # syslogs
- for item in syslog :
- match, rulepath, apply = rules.apply(item)
-
- log.debug("%s: %s: %s", item, rulepath, apply)
-
- target = apply.get('irk', options.irker_target)
-
- tag = '/'.join(str(rule) for rule in reversed(rulepath[:-1]))
- text = apply.get('text')
-
- log.info("%s: %s: %s", target, tag, text)
+ try :
+ apply_syslog(options, syslog, rules, irker)
- if not text :
- # XXX: plain irk = ... in rule is broken, as it always applies, and skips any further rules
- continue
-
- if irker and target :
- irker[target]('[' + tag + '] ' + text)
- else :
- print tag, text
+ except EOFError as ex :
+ log.error("syslog: EOF")
+ return 1
# quit unless we have something to poll
if not syslog.poll :
@@ -113,14 +133,13 @@
if irker.irk in poll :
# irks?
try :
- for msg in irker.irk :
- log.info("irk: %s", msg)
+ apply_irker(irker)
except EOFError :
# exit
log.error("irk: EOF")
return 1
-
+
# done
log.info("Exiting...")
return 0