Skip to content

Instantly share code, notes, and snippets.

@Luzifer
Last active January 2, 2016 10:29
Show Gist options
  • Save Luzifer/8290705 to your computer and use it in GitHub Desktop.
Save Luzifer/8290705 to your computer and use it in GitHub Desktop.
Simple Nagios check for NRPE / PeriodicNoise to check whether an Ubuntu server required reboot
#!/usr/bin/env python
import apt, os, re, sys, subprocess
if os.path.exists('/var/run/reboot-required'):
print 'reboot-required marker file exists'
sys.exit(1)
kernels = []
cache = apt.Cache(memonly=True)
for package in cache:
if package.installed and re.match('^linux-image-[0-9].*', package.name):
kernels.append(package.name)
kernels.sort()
latest = kernels[-1]
active_kernel = subprocess.check_output(['uname', '-r']).strip()
if 'linux-image-%s' % active_kernel == latest:
print 'reboot-required marker not found, kernel matches'
sys.exit(0)
else:
print 'reboot-required marker not found, kernel version does not match: %s != %s' % (latest, active_kernel)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment