02ea2dda5d87c0136aaf74af8b215874ba0420e8
   1#
   2#   cron.py
   3#
   4#   List the logged (executed) cron jobs and their commands (uses syslog file)
   5#
   6#   NOTE: This file is now deprecated in favour of the newer journald mechanism
   7#   used in cron-journald.py. This parser is still functional but is slower and
   8#   has less features. Please switch over if possible.
   9#
  10
  11import re
  12
  13from logparse.formatting import *
  14from logparse.util import readlog
  15from logparse import config
  16from logparse.load_parsers import Parser
  17from logparse.load_parsers import Parser
  18
  19class Cron(Parser):
  20
  21    def __init__(self):
  22        super().__init__()
  23        self.name = "cron"
  24        self.info = "List the logged (executed) cron jobs and their commands (uses static syslog file)"
  25
  26    def parse_log(self):
  27
  28        logger.warning("NOTE: This cron parser is now deprecated. Please use cron-journald if possible.")
  29
  30        logger.debug("Starting cron section")
  31        section = Section("cron")
  32
  33        matches = re.findall('.*CMD\s*\(\s*(?!.*cd)(.*)\)', readlog(config.prefs.get("logs", "cron")))
  34        num = len(matches)
  35        commands = []
  36        for match in matches:
  37            commands.append(str(match))
  38        logger.info("Found " + str(num) + " cron jobs")
  39        jobs_data = Data(str(num) + " cron jobs run")
  40        section.append_data(jobs_data)
  41
  42        if (num > 0):
  43            logger.debug("Analysing cron commands")
  44            cmd_data = Data("Top cron commands")
  45            cmd_data.items = ("`{0}`".format(x) for x in commands)
  46            cmd_data.orderbyfreq()
  47            cmd_data.truncl(config.prefs.getint("logparse", "maxcmd"))
  48            section.append_data(cmd_data)
  49
  50        logger.info("Finished cron section")
  51        return section