Cron jobs - files /etc/cron.d/*

This module contains the following parsers:

CronForeman - file /etc/cron.d/foreman

class insights.parsers.cron_jobs.CronFile(context)[source]

Bases: Parser

It parses the file at /etc/cron.d/*`.

Each row of the file is converted into a dictionary with keys for each field. And also the whole line is saved for use to the “raw” key. For example one row would look like:

{
  'minute': '*',
  'hour': '*',
  'day_of_month': '*',
  'month': '*',
  'day_of_week': '*',
  'username': 'test',
  'command': '/usr/bin/keystone-manage token_flush > /dev/null 2>&1',
  'raw': '* * * * * * test /usr/bin/keystone-manage token_flush > /dev/null 2>&1'
}

It parses the line in the same way that cron(1) does. Lines that are blank or start with a comment are ignored. Environment lines of the form ‘KEY = value’ (with optional spacing around the equals sign) are stored in the environment dictionary. Lines containing a valid cron job, with five recogniseable time fields, username, a command, are stored in the jobs property and accessed through the pseudo-list interface and the search method. All other lines are stored in the invalid_lines property.

It also recognises the extension of time signature ‘nicknames’, which take place of the first five parts of a standard crontab line:

  • @reboot : Run once after reboot.

  • @yearly : Run once a year, ie. “0 0 1 1 *”.

  • @annually : Run once a year, ie. “0 0 1 1 *”.

  • @monthly : Run once a month, ie. “0 0 1 * *”.

  • @weekly : Run once a week, ie. “0 0 * * 0”.

  • @daily : Run once a day, ie. “0 0 * * *”.

  • @hourly : Run once an hour, ie. “0 * * * *”.

In the case of the @reboot’ nickname, the row will not contain the ‘minute’, ‘hour’, ‘day_of_month’, ‘month’, or ‘day_of_week’ keys, and instead will contain the key ‘time’ with the value @reboot’ (as well as the usual ‘username’ and ‘command’ key). All other nicknames are translated directly into their five-part equivalent and parsed as a normal crontab line.

Sample input:

SHELL=/bin/sh
TEST_HOME=/usr/share/test

15 23 * * *     test    /usr/sbin/testaa 2>&1 >> /var/log/test.log
0 7 * * *       test    /usr/sbin/testbb 2>&1 >> /var/log/test.log
jobs

A list of valid jobs info.

Type:

list

environment

The environment variables.

Type:

dict

invalid_lines

A list of lines which are neither in cron format nor in env variable format.

Type:

list

Raises:

ParseException -- When a line starts with ‘@’, but the nickname is unknown.

Examples

>>> type(cron_obj)
<class 'insights.parsers.cron_jobs.CronFile'>
>>> len(cron_obj.environment)
2
>>> 'TEST_HOME' in cron_obj.environment
True
>>> cron_obj.environment['TEST_HOME']
'/usr/share/test'
>>> len(cron_obj.jobs)
2
>>> cron_obj.jobs[0].get('minute')
'15'
>>> cron_obj.jobs[0].get('hour')
'23'
>>> cron_obj.jobs[0].get('username')
'test'
>>> '/usr/sbin/testaa' in cron_obj.jobs[0].get('command')
True
parse_content(content)[source]

This method must be implemented by classes based on this class.

search(**kwargs)[source]

Return a list of dict for lines that matches the kwargs.

This uses the insights.parsers.keyword_search() function for searching; see its documentation for usage details. If no search parameters are given, a empty list is returned.

class insights.parsers.cron_jobs.CronForeman(context)[source]

Bases: CronFile

Parse the /etc/cron.d/foreman file.

Sample input:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

RAILS_ENV=production
FOREMAN_HOME=/usr/share/foreman

# Clean up the session entries in the database
15 23 * * *     foreman    /usr/sbin/foreman-rake db:sessions:clear 2>&1 | gawk '{ print strftime("[\%Y-\%m-\%d \%H:\%M:\%S]"), $0 }' >>/var/log/foreman/cron.log

# Send out recurring notifications
0 7 * * *       foreman    /usr/sbin/foreman-rake reports:daily 2>&1 | gawk '{ print strftime("[\%Y-\%m-\%d \%H:\%M:\%S]"), $0 }' >>/var/log/foreman/cron.log
0 5 * * 0       foreman    /usr/sbin/foreman-rake reports:weekly 2>&1 | gawk '{ print strftime("[\%Y-\%m-\%d \%H:\%M:\%S]"), $0 }' >>/var/log/foreman/cron.log

Examples

>>> type(foreman_jobs)
<class 'insights.parsers.cron_jobs.CronForeman'>
>>> len(foreman_jobs.environment)
4
>>> 'FOREMAN_HOME' in foreman_jobs.environment
True
>>> foreman_jobs.environment['FOREMAN_HOME']
'/usr/share/foreman'
>>> len(foreman_jobs.jobs)
3
>>> foreman_jobs.jobs[0].get('minute')
'15'
>>> foreman_jobs.jobs[0].get('hour')
'23'
>>> foreman_jobs.jobs[0].get('username')
'foreman'
>>> 'db:sessions:clear' in foreman_jobs.jobs[0].get('command')
True