63a0e9593653265eb5635f809e27ee4beec19c3b
   1#
   2#   cron-journald.py
   3#
   4#   List the logged (executed) cron jobs and their commands (uses journald module)
   5#
   6#   TODO: also output a list of scheduled (future) jobs
   7#
   8
   9from systemd import journal
  10
  11from logparse.formatting import *
  12from logparse import config
  13
  14import logging
  15logger = logging.getLogger(__name__)
  16
  17def parse_log():
  18
  19    logger.debug("Starting cron section")
  20    section = Section("cron")
  21
  22    # Initiate journald reader
  23    j = journal.Reader()
  24    j.this_boot()
  25    j.this_machine()
  26    j.log_level(journal.LOG_INFO)
  27    j.add_match(_COMM="cron")
  28
  29    logger.info("Obtaining cron logs")
  30
  31    messages = [entry["MESSAGE"] for entry in j if "MESSAGE" in entry and " CMD " in entry["MESSAGE"]]
  32
  33    total_jobs = len(messages)
  34
  35    if total_jobs == 0:
  36        logger.warning("Couldn't find any cron commands")
  37        return 1
  38
  39    logger.info("Found " + str(total_jobs) + " cron jobs")
  40    section.append_data(Data("Total of " + plural("cron session", total_jobs) + " executed across all users"))
  41
  42    logger.debug("Analysing cron commands for each user")
  43    users = {}
  44
  45    for msg in messages:
  46        usr_cmd = re.search('\((\S+)\) CMD (.*)', msg)  # [('user', 'cmd')]
  47        if usr_cmd:
  48            if not usr_cmd.group(1) in users:
  49                users[usr_cmd.group(1)] = []
  50            users[usr_cmd.group(1)].append(usr_cmd.group(2))
  51
  52    for usr, cmdlist in users.items():
  53        user_data = Data()
  54        user_data.subtitle = plural("cron session", len(cmdlist)) + " for " + usr
  55        user_data.items = ("`{0}`".format(cmd) for cmd in cmdlist)
  56        user_data.orderbyfreq()
  57        user_data.truncl(config.prefs.getint("logparse", "maxcmd"))
  58        section.append_data(user_data)
  59
  60    logger.info("Finished cron section")
  61
  62    return section