migrate configuration system to the stdlib ConfigParser
[logparse.git] / logparse / parsers / cron.py
index 2c7289b2a7315e0b04dc7c9b88c71b7d7de66f8e..83cb1cfd734469cdc9c6cb535ace27f44826b4bb 100644 (file)
@@ -1,38 +1,45 @@
 #
 #   cron.py
 #
-#   List the logged (executed) cron jobs and their commands
-#   TODO: also output a list of scheduled (future) jobs
+#   List the logged (executed) cron jobs and their commands (uses syslog file)
+#
+#   NOTE: This file is now deprecated in favour of the newer journald mechanism
+#   used in cron-journald.py. This parser is still functional but is slower and
+#   has less features. Please switch over if possible.
+#
 
 import re
 
-from ..formatting import *
-from ..util import readlog, resolve
-from .. import config
+from logparse.formatting import *
+from logparse.util import readlog
+from logparse import config
 
 import logging
 logger = logging.getLogger(__name__)
 
 def parse_log():
-    output = ''
+
+    logger.warning("NOTE: This cron parser is now deprecated. Please use cron-journald if possible.")
+
     logger.debug("Starting cron section")
-    output += opentag('div', 1, 'cron', 'section')
-    matches = re.findall('.*CMD\s*\(\s*(?!.*cd)(.*)\)', readlog(config.prefs['logs']['cron']))
-    num = sum(1 for line in matches)
+    section = Section("cron")
+
+    matches = re.findall('.*CMD\s*\(\s*(?!.*cd)(.*)\)', readlog(config.prefs.get("logs", "cron")))
+    num = len(matches)
     commands = []
     for match in matches:
         commands.append(str(match))
-    # commands.append([str(match)for match in matches])
-    #logger.debug("found cron command " + str(commands))
     logger.info("Found " + str(num) + " cron jobs")
-    subtitle = str(num) + " cron jobs run"
-    output += writetitle("cron")
-    output += writedata(subtitle)
-    if (len(matches) > 0):
-        commands = addtag(commands, 'code')
-        commands = orderbyfreq(commands)
-        commands = truncl(commands, config.prefs['maxcmd'])
-        output += writedata("top cron commands", [c for c in commands])
-    output += closetag('div', 1)
+    jobs_data = Data(str(num) + " cron jobs run")
+    section.append_data(jobs_data)
+
+    if (num > 0):
+        logger.debug("Analysing cron commands")
+        cmd_data = Data("Top cron commands")
+        cmd_data.items = ("`{0}`".format(x) for x in commands)
+        cmd_data.orderbyfreq()
+        cmd_data.truncl(config.prefs.getint("logparse", "maxcmd"))
+        section.append_data(cmd_data)
+
     logger.info("Finished cron section")
-    return output
+    return section