You are here

Home » Blogs » Konstantin Boyandin's blog

Uptime checker

Uptime pills

Uptime contest

Boasting high uptime values is a popular ego contest on the Net. However, at times uptime can be not only a tool to inflate self-respect, but a tool to detect unusual or unexpected reboots.

When it comes to VPS, I happened to use services where host machines (nodes) were rebooted often, at times without serious reason. Naturally, a task to register uptime intervals has appeared. It is solved easily with a script you can download via the following link:

check-uptime.sh.txt (1033 bytes, SHA1 checksum a43ce663e12bf5764d5d7a33c2de52b78aedca43).

You will need a calculator utility, 'bc', that is usually present in most Linux/other distributions. Just issue 'sudo yum install bc -y' or similar command.

Script description

Rename script to whatever name you prefer, put it in a directory of your choice and run, passing a log file name as a parameter.

The script reads uptime data from /proc/uptime file, calculates the boot time (Unix epoch) and compares the boot time with the last entry of log file specified. If boot time differs by no more than 2 seconds (a small arbitrary value will do; 2 seconds is taken because calculation give an error within 1 second).

In case the drift (difference between currently calculated and stored boot time) is above the threshold, a new record is appended to the log file. Otherwise, the last record is replaced.

Script stores records for each uptime interval detected in the format "epoch uptime", one per line. Epoch is seconds passed since1-st January of 1970, and the uptime is measured in seconds.

All you have to do is to add to your crontab a line like

* * * * * /usr/local/bin/check-uptime.sh ~/uptime.log 1> /dev/null 2> /dev/null
Change the log file path appropriately. Now you can store and analyze uptime intervals with 1 minute granularity. It's unlikely a good idea to run script more often.

Script should work on all types of Un*x systems with just small modifications.

Share/Save