Commit a581e361 authored by Kevin Lyda's avatar Kevin Lyda

Initial commit.

Project to check correctness of system crontab files.  On most
cron implementations these would be the /etc/crontab file and
files in /etc/cron.d
parents
build
*.pyc
This diff is collapsed.
lyda
sundell
anj
stschmidt
group:org-tools
group:managedsystems
chkcrontab.py is a script to check crontab files like those in /etc/cron.d
and /etc/crontab. It tries to catch glaring errors and warn on suspect
lines in a crontab file. Some valid lines will generate warnings.
Certain silly yet valid crontab lines will generate errors as well.
Run this by doing ./chkcrontab.py crontab_file
Errors will cause a non-zero exit code. Warnings alone will not.
To see sample output for a bad crontab, run the following:
./scripts/chkcrontab.py ./tests/test_crontab
See the ./tests/test_crontab.disable crontab for how to disable warnings
and errors.
#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""chkcrontab - Checks the correctness of crontab files.
Parse crontab files and check each type of line for potential syntax errors.
Basic usage:
chkcrontab crontab_file
"""
__author__ = 'lyda@google.com (Kevin Lyda)'
import os
import sys
import chkcrontab_lib as check
def main(argv):
if len(argv) != 2:
print 'ERROR: No crontab file was specified.'
sys.exit(1)
log = check.LogCounter()
print 'Checking correctness of %s' % argv[1]
return check.CheckCrontab(argv[1], log)
if __name__ == '__main__':
main(sys.argv)
This diff is collapsed.
#!/usr/bin/env python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Installer for chkcrontab.
This installs the chkcrontab command and the crontab.check module.
"""
from distutils.core import setup
setup(
name='chkcrontab',
version='1.0',
url='http://code.google.com/p/chkcrontab',
author='Kevin Lyda',
author_email='lyda@google.com',
description='A tool to detect crontab errors',
long_description=open('README').read(),
py_modules=['chkcrontab_lib'],
scripts=['chkcrontab.py'],
keywords='check lint crontab',
# See http://pypi.python.org/pypi?%3Aaction=list_classifiers
classifiers=['Development Status :: 5 - Production/Stable',
'Environment :: Console',
'License :: OSI Approved :: Apache Software License',
'Operating System :: POSIX',
'Programming Language :: Python :: 2.5',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Topic :: Utilities',
],
)
#!/usr/bin/python
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Run ***ALL*** the testorz!!!"""
import os
import sys
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
BASE_PATH = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(BASE_PATH, '..'))
if __name__ == '__main__':
test_dir = os.path.dirname(globals().get('__file__', os.getcwd()))
tests = unittest.TestLoader().discover(test_dir)
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(tests)
if not result.wasSuccessful():
sys.exit(1)
This diff is collapsed.
# Good comment
# FAIL 1 for bad comment.
Bad comment
GOOD=VAR
# WARN 1 for questionable var assignment.
BAD=$VAR
# FAIL 1 for empty assignment.
BAD_EMPTY=
OK_EMPTY=""
# FAIL 1 for whitespace assignment.
BAD_SPACE=
OK_SPACE=" "
# WARN 1 for worrisome time spec.
* 3 * * * root Warn for hour not minute
# FAIL 1 for bad time spec.
- * * * * root Bad Line
1 * * * * root Good Line
# FAIL 1 for bad time spec.
1 ** * * * root Bad Line
# WARN 1 for questionable user.
1 * * * * ro0ot Bad Line
# WARN 1 for questionable bare %.
1 * * * * root Command with %
1,2 * * * * root Sequence
1-2 * * * * root Range
# FAIL 1 for bad time spec.
1--2 * * * * root BadRange
# FAIL 1 for bad time spec.
1-2 *, * * * root BadRange
# FAIL 1 for bad time spec.
2-1 * * * * root Bad Range
1-20/2 * * * * root Range/Step
# FAIL 1 for bad time spec.
1-20/0 * * * * root Bad Range/Step
# FAIL 1 for bad time spec.
1/2 * * * * root Bad Step
*/2 * * * * root Good Step
# FAIL 1 for bad time spec.
*/61 * * * * root Bad Step
# FAIL 3 for bad time spec.
1-2/61 * * * * root Bad Step
# FAIL 1 for bad time spec.
1-61/2 * * * * root Bad Range
# FAIL 5 for bad time spec.
61 25 32 13 8 root Over by one
# FAIL 5 for bad time spec.
-1 -1 0 0 -1 root Under by one
# FAIL 2 for bad time spec.
1 * * * root "Missing Field"
# WARN 0 for complicated time interaction (disabled; too many false pos)
1,4,6,*/5 */3,2,7 1,4,5 1-3 Wed-Sun root Complicated
# WARN 0 for complicated time interaction (disabled; too many false pos)
1,4,6,*/5 */3,2,7 1,4,5 Mar-JUN Wed-Sun root Complicated
# FAIL 1 for bad time spec.
1,4,6,*/5 */3,2,7 1,4,5 Max-JUN * root Bad Month
# FAIL 1 for bad time spec.
1,4,6,*/5 */3,2,7 1,4,5 Ma-JUN * root Bad Month
# FAIL 1 for bad time spec.
1,4,6,*/5 */3,2,7 * * mO root Bad Day
1,4,6,*/5 */3,2,7 * * mOn-Fri root Good Day Range
1,4,6,*/5 */3,2,7 * * mOn-Fri/2 root Good Day Range Step
# FAIL 1 for bad time spec.
1,4,6,*/5 */3,2,7 * * mOn/2 root Bad Day Step
1,4,6,*/5 */3,2,7 * * mOn-Fri/8 root Good Day Range Step
# WARN 1 for probable missing user.
@monthly Missing User
@monthly root Good @ Command
# FAIL 1 for bad time spec.
@m0nthly root Bad @ Command
# WARN 1 for filename issue.
# chkcrontab: disable-msg=USER_NOT_FOUND
# chkcrontab: disable-msg=BARE_PERCENT
# chkcrontab: disable-msg=FIELD_PARSE_ERROR
# chkcrontab: disable-msg=FIELD_VALUE_ERROR
# chkcrontab: disable-msg=INVALID_AT
# chkcrontab: disable-msg=INVALID_USER
# chkcrontab: disable-msg=LINE_ERROR
# chkcrontab: disable-msg=QUOTE_VALUES
# chkcrontab: disable-msg=SHELL_VAR
# chkcrontab: disable-msg=USER_NOT_FOUND
# chkcrontab: disable-msg=HOURS_NOT_MINUTES
# Good comment
# disable fail 1 for bad comment.
Bad comment
GOOD=VAR
# disable warn 1 for questionable var assignment.
BAD=$VAR
# disable fail 1 for empty assignment.
BAD_EMPTY=
OK_EMPTY=""
# disable fail 1 for whitespace assignment.
BAD_SPACE=
OK_SPACE=" "
# disable warn 1 for bad time spec.
* 3 * * * root Warn for hours not minutes
# disable fail 1 for bad time spec.
- * * * * root Bad Line
1 * * * * root Good Line
# disable fail 1 for bad time spec.
1 ** * * * root Bad Line
# disable warn 1 for questionable user.
1 * * * * ro0ot Bad Line
# disable warn 1 for questionable bare %.
1 * * * * root Command with %
1,2 * * * * root Sequence
1-2 * * * * root Range
# disable fail 1 for bad time spec.
1--2 * * * * root BadRange
# disable fail 1 for bad time spec.
1-2 *, * * * root BadRange
# disable fail 1 for bad time spec.
2-1 * * * * root Bad Range
1-20/2 * * * * root Range/Step
# disable fail 1 for bad time spec.
1-20/0 * * * * root Bad Range/Step
# disable fail 1 for bad time spec.
1/2 * * * * root Bad Step
*/2 * * * * root Good Step
# disable fail 1 for bad time spec.
*/61 * * * * root Bad Step
# disable fail 3 for bad time spec.
1-2/61 * * * * root Bad Step
# disable fail 1 for bad time spec.
1-61/2 * * * * root Bad Range
# disable fail 5 for bad time spec.
61 25 32 13 8 root Over by one
# disable fail 5 for bad time spec.
-1 -1 0 0 -1 root Under by one
# disable fail 2 for bad time spec.
1 * * * root "Missing Field"
# disable warn 0 for complicated time interaction (disabled; too many false pos)
1,4,6,*/5 */3,2,7 1,4,5 1-3 Wed-Sun root Complicated
# disable warn 0 for complicated time interaction (disabled; too many false pos)
1,4,6,*/5 */3,2,7 1,4,5 Mar-JUN Wed-Sun root Complicated
# disable fail 1 for bad time spec.
1,4,6,*/5 */3,2,7 1,4,5 Max-JUN * root Bad Month
# disable fail 1 for bad time spec.
1,4,6,*/5 */3,2,7 1,4,5 Ma-JUN * root Bad Month
# disable fail 1 for bad time spec.
1,4,6,*/5 */3,2,7 * * mO root Bad Day
1,4,6,*/5 */3,2,7 * * mOn-Fri root Good Day Range
1,4,6,*/5 */3,2,7 * * mOn-Fri/2 root Good Day Range Step
# chkcrontab: enable-msg=FIELD_VALUE_ERROR
# FAIL 1 for bad time spec.
1,4,6,*/5 */3,2,7 * * mOn/2 root Bad Day Step
1,4,6,*/5 */3,2,7 * * mOn-Fri/8 root Good Day Range Step
# disable warn 1 for probable missing user.
@monthly Missing User
@monthly root Good @ Command
# disable fail 1 for bad time spec.
@m0nthly root Bad @ Command
# WARN 1 for questionable file name.
# Good comment
# FAIL 2 for bad chkcrontab command and bad error
# chkcrontab: ENABLE-MSG=KITTEN
# disable warn 1 for questionable var assignment.
# chkcrontab: disable-msg=SHELL_VAR
BAD=$VAR
# disable warn 1 for questionable user.
# chkcrontab: disable-msg=USER_NOT_FOUND
1 * * * * ro0ot Bad User
# disable warn 1 for questionable bare %.
# chkcrontab: disable-msg=BARE_PERCENT
1 * * * * root Command with % (this will not warn)
# WARN 1 for questionable bare %.
# chkcrontab: enable-msg=BARE_PERCENT
1 * * * * root Command with % (this will warn)
# WARN 1 for probable missing user.
# chkcrontab: enable-msg=USER_NOT_FOUND
@monthly Missing User
# WARN 1 for questionable file name.
# Good comment
# WARN 1 for questionable var assignment.
BAD=$VAR
# WARN 1 for questionable user.
1 * * * * ro0ot Bad User
# WARN 1 for questionable bare %.
1 * * * * root Command with %
# WARN 0 for complicated time interaction (disabled; too many false pos)
59 23 29 11 4 root Both DOW and DOM specified.
# WARN 1 for probable missing user.
@monthly Missing User
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment