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.

99 lines
2.4 KiB
Plaintext

10 years ago
#!/usr/bin/python
from __future__ import division, print_function
from distutils.version import LooseVersion
import posixpath
10 years ago
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():
10 years ago
m = re.findall('(?<=Debian )\S+', proc_version())
if m:
# Note: it's the _second_ match
version = clean_kernel_version(m[1])
return version
10 years ago
m = re.search('(?<=Linux version )\S+', proc_version())
if m:
version = clean_kernel_version(m.group(0))
10 years ago
return version
10 years ago
10 years ago
def is_debian():
return posixpath.exists('/etc/debian_version')
10 years ago
def is_fedora():
return posixpath.exists('/etc/fedora-release')
10 years ago
def installed_kernel_versions():
10 years ago
if is_debian():
return installed_kernel_versions_debian()
if is_fedora():
return installed_kernel_versions_fedora()
10 years ago
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():
10 years ago
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)
10 years ago
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()