83cb1cfd734469cdc9c6cb535ace27f44826b4bb
   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
  16
  17import logging
  18logger = logging.getLogger(__name__)
  19
  20def parse_log():
  21
  22    logger.warning("NOTE: This cron parser is now deprecated. Please use cron-journald if possible.")
  23
  24    logger.debug("Starting cron section")
  25    section = Section("cron")
  26
  27    matches = re.findall('.*CMD\s*\(\s*(?!.*cd)(.*)\)', readlog(config.prefs.get("logs", "cron")))
  28    num = len(matches)
  29    commands = []
  30    for match in matches:
  31        commands.append(str(match))
  32    logger.info("Found " + str(num) + " cron jobs")
  33    jobs_data = Data(str(num) + " cron jobs run")
  34    section.append_data(jobs_data)
  35
  36    if (num > 0):
  37        logger.debug("Analysing cron commands")
  38        cmd_data = Data("Top cron commands")
  39        cmd_data.items = ("`{0}`".format(x) for x in commands)
  40        cmd_data.orderbyfreq()
  41        cmd_data.truncl(config.prefs.getint("logparse", "maxcmd"))
  42        section.append_data(cmd_data)
  43
  44    logger.info("Finished cron section")
  45    return section