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 self.deprecated = True
26 self.successor = "cron_journald"
27
28 def parse_log(self):
29
30 logger.warning("NOTE: This cron parser is now deprecated. Please use cron-journald if possible.")
31
32 logger.debug("Starting cron section")
33 section = Section("cron")
34
35 matches = re.findall('.*CMD\s*\(\s*(?!.*cd)(.*)\)', readlog(config.prefs.get("logs", "cron")))
36 num = len(matches)
37 commands = []
38 for match in matches:
39 commands.append(str(match))
40 logger.info("Found " + str(num) + " cron jobs")
41 jobs_data = Data(str(num) + " cron jobs run")
42 section.append_data(jobs_data)
43
44 if (num > 0):
45 logger.debug("Analysing cron commands")
46 cmd_data = Data("Top cron commands")
47 cmd_data.items = ("`{0}`".format(x) for x in commands)
48 cmd_data.orderbyfreq()
49 cmd_data.truncl(config.prefs.getint("logparse", "maxcmd"))
50 section.append_data(cmd_data)
51
52 logger.info("Finished cron section")
53 return section