commit 7a3eac8db28333f5544f73e9ca62a01020f83706 Author: Mike Gerber Date: Sun May 24 18:38:04 2015 +0200 Initial commit diff --git a/check_kernel b/check_kernel new file mode 100644 index 0000000..2076371 --- /dev/null +++ b/check_kernel @@ -0,0 +1,63 @@ +#!/usr/bin/python +from __future__ import division, print_function +from distutils.version import LooseVersion +import re +import subprocess +import sys + + +def proc_version(): + with open('/proc/version', 'r') as v: + proc_version = v.next() + return proc_version + + +def running_kernel_version(): + m = re.search('(?<=Linux version )\S+', proc_version()) + if m: + version = clean_kernel_version(m.group(0)) + else: + version = None + + return version + + +def installed_kernel_versions(): + rpm_out = subprocess.check_output( + ['rpm', '--queryformat=%{VERSION}-%{RELEASE}\n', '-q', 'kernel']) + rpm_out = rpm_out.strip() + + versions = rpm_out.split('\n') + versions = [clean_kernel_version(v) for v in versions] + + return versions + + +def installed_kernel_version(): + return sorted(installed_kernel_versions())[-1] + + +def clean_kernel_version(version): + # arch + version = re.sub('\.(x86_64|i[3-6]86)', '', version) + # Fedora release + version = re.sub('\.fc\d+', '', version) + + return LooseVersion(version) + + +def main(): + running = running_kernel_version() + installed = installed_kernel_version() + + if running == installed: + print('KERNEL OK - running version {}'.format(running)) + sys.exit(0) + else: + print('KERNEL WARNING - running version {}, installed: {}' + .format(running, installed)) + sys.exit(1) + + +if __name__ == '__main__': + main() diff --git a/check_kernel.py b/check_kernel.py new file mode 120000 index 0000000..16eb3df --- /dev/null +++ b/check_kernel.py @@ -0,0 +1 @@ +check_kernel \ No newline at end of file diff --git a/check_kernel_test.py b/check_kernel_test.py new file mode 100644 index 0000000..61aaf29 --- /dev/null +++ b/check_kernel_test.py @@ -0,0 +1,34 @@ +#!/usr/bin/python +from __future__ import division +import sys + +# Require Python 3 for unittest.mock +if sys.version_info[0] < 3: + print("This script requires Python version 3") + sys.exit(1) + +from distutils.version import LooseVersion +import unittest +import unittest.mock + +from check_kernel import clean_kernel_version +import check_kernel + + +class CleanKernelVersionTestCase(unittest.TestCase): + def testFedora(self): + versions = [('4.0.4-301.fc22.x86_64', '4.0.4-301'), + ('4.0.0-1.fc22', '4.0.0-1')] + for dirty, clean in versions: + self.assertEqual(clean_kernel_version(dirty), LooseVersion(clean)) + + +class RunningKernelVersionTestCase(unittest.TestCase): + def testFedora(self): + with unittest.mock.patch.object(check_kernel, 'proc_version', return_value='Linux version 4.0.3-301.fc22.x86_64 (mockbuild@bkernel02.phx2.fedoraproject.org) (gcc version 5.1.1 20150422 (Red Hat 5.1.1-1) (GCC) ) #1 SMP Thu May 21 13:10:33 UTC 2015'): + self.assertEqual(check_kernel.running_kernel_version(), + LooseVersion('4.0.3-301')) + + +if __name__ == '__main__': + unittest.main()