new parser class structure
[logparse.git] / logparse / parsers / cron.py
index 4408ba20bd03516c0d8c37612c47b51933d72c6b..02ea2dda5d87c0136aaf74af8b215874ba0420e8 100644 (file)
 
 import re
 
-from ..formatting import *
-from ..util import readlog, resolve
-from .. import config
-from .. import util
-
-import logging
-logger = logging.getLogger(__name__)
-
-def parse_log():
-
-    logger.warning("NOTE: This cron parser is now deprecated. Please use cron-journald if possible.")
-
-    logger.debug("Starting cron section")
-    section = Section("cron")
-
-    matches = re.findall('.*CMD\s*\(\s*(?!.*cd)(.*)\)', readlog(config.prefs['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")
-    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['maxcmd'])
-        section.append_data(cmd_data)
-
-    logger.info("Finished cron section")
-    return section 
+from logparse.formatting import *
+from logparse.util import readlog
+from logparse import config
+from logparse.load_parsers import Parser
+from logparse.load_parsers import Parser
+
+class Cron(Parser):
+
+    def __init__(self):
+        super().__init__()
+        self.name = "cron"
+        self.info = "List the logged (executed) cron jobs and their commands (uses static syslog file)"
+
+    def parse_log(self):
+
+        logger.warning("NOTE: This cron parser is now deprecated. Please use cron-journald if possible.")
+
+        logger.debug("Starting cron section")
+        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))
+        logger.info("Found " + str(num) + " cron jobs")
+        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 section