How does Ubuntu keep track of the "System restart required" flag in motd?

Solution 1:

Check for the presence of /var/run/reboot-required.

Solution 2:

The script that generates the reboot required part of motd is /usr/lib/update-notifier/update-motd-reboot-required which contains:

#!/bin/sh -e
# helper for update-motd

if [ -f /var/run/reboot-required ]; then
        cat /var/run/reboot-required

Your nagios check could check for the existence of /var/run/reboot-required.

Solution 3:

Additionally the file '/var/run/reboot-required.pkgs' lists the packages that requested the reboot. For example:

$ cat /var/run/reboot-required.pkgs 

On Ubuntu Lucid (10.4).

Solution 4:

Debian and Ubuntu packages can trigger the creation of /var/run/reboot-required* in their postinst file by executing the helper script /usr/share/update-notifier/notify-reboot-required

Thus the "official" way to process reboots is handled by the package maintainer. I have been doing it previously in a script by comparing time booted against mtimes in /boot.

Solution 5:

if [ ! -f /var/run/reboot-required ]; then
        # no reboot required (0=OK)
        echo "OK: no reboot required"
        exit 0
        # reboot required (1=WARN)
        echo "WARNING: `cat /var/run/reboot-required`"
        exit 1