|
|
|
#!/usr/bin/python
|
|
|
|
from __future__ import division, print_function
|
|
|
|
from distutils.version import LooseVersion
|
|
|
|
import posixpath
|
|
|
|
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.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 LooseVersion(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()
|