You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.2 KiB
Python

#!/usr/bin/python
from __future__ import division, print_function
import posixpath
import re
import subprocess
import sys
class Version(object):
"""Crude version abstraction for systems without distutils.version"""
def __init__(self, version_str):
self.version_str = version_str
def components(self):
return re.split('\.|-', self.version_str)
def __eq__(self, other):
return self.version_str == other.version_str
def __gt__(self, other):
def num_gt(str_, str_other):
m = re.search('^\d+', str_)
m_other = re.search('^\d+', str_other)
if m and m_other:
return int(m.group(0)) > int(m_other.group(0))
else:
return str_ > str_other
for self_c, other_c in zip(self.components(), other.components()):
if num_gt(self_c, other_c):
return True
return False
# Note: not using functools.total_ordering to support Python 2.6
def proc_version():
with open('/proc/version', 'r') as v:
proc_version = v.next()
return proc_version
def running_kernel_version():
m = re.findall('(?<=Debian )\S+', proc_version())
if m:
# Note: it's the _second_ match
version = clean_kernel_version(m[1])
return version
m = re.search('(?<=Linux version )\S+', proc_version())
if m:
version = clean_kernel_version(m.group(0))
return version
def is_debian():
return posixpath.exists('/etc/debian_version')
def is_fedora():
return posixpath.exists('/etc/fedora-release')
def installed_kernel_versions():
if is_debian():
return installed_kernel_versions_debian()
if is_fedora():
return installed_kernel_versions_fedora()
def installed_kernel_versions_debian():
dpkg_out = subprocess.check_output(
['dpkg-query', '--show', '--showformat', '${Package} ${Version}\n', 'linux-image*'])
dpkg_out = dpkg_out.strip()
versions = dpkg_out.split('\n')
versions = [v for v in versions if re.search('^linux-image-\d', v)]
versions = [clean_kernel_version(v.split(' ')[1]) for v in versions]
return versions
def installed_kernel_versions_fedora():
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 Version(version)
def main():
if len(sys.argv) > 1:
print('This plugin no longer takes the expected kernel version as an argument')
sys.exit(3)
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()