Search Exchange

Search All Sites

Nagios Live Webinars

Let our experts show you how Nagios can help your organization.

Contact Us

Phone: 1-888-NAGIOS-1
Email: sales@nagios.com

Login

Remember Me

check_uptime

Rating
4 votes
Favoured:
1
Current Version
1.02
Last Release Date
2009-11-12
Compatible With
  • Nagios 2.x
  • Nagios 3.x
Hits
207229
Files:
FileDescription
check_uptimecheck_uptime
Network Monitoring Software - Download Nagios XI
Log Management Software - Nagios Log Server - Download
Netflow Analysis Software - Nagios Network Analyzer - Download
Simple script to check the uptime of a Linux, Unix, or Solaris host. This check is informational only, it always returns 'OK'

Sample use:
define service{
host_name SERVER
service_description Uptime
check_command check_uptime
}

Sample output:
System Uptime - up 9 days, 5 Hours, 02 Minutes
Reviews (4)
bypelicanmedia, July 6, 2017
...incorrect results depending on time format...

Using:

struptime=`uptime | awk '{print $2,$3,$4,$5}'`

if [[ $struptime == *day* ]]; then
struptime1=${struptime%,}
else
struptime1=${struptime%, *}
fi

strdayshours=${struptime1%%:*}' hours '
strminutes=${struptime1##*:}
strminutes1=${strminutes%%,*}' minutes'

echo "System Uptime -" $strdayshours$strminutes1
exit 0


Machine up for 10 minutes:
Command = uptime
Result = 12:36:04 up 10 min, 1 user, load average: 0.00, 0.00, 0.00
Command = uptime | awk '{print $2,$3,$4,$5}'
Result = up 10 min, 1
Command = /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_uptime
Result = System Uptime - up 10 min hours up 10 min minutes

Machine up for 2 hours:
Command = uptime
Result = 12:48:27 up 2:02, 2 users, load average: 0.12, 0.06, 0.06
Command = uptime | awk '{print $2,$3,$4,$5}'
Result = up 2:02, 2 users,
Command = /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_uptime
Result = System Uptime - up 2 hours 02 minutes

Arguments $2,$3,$4,$5 are different depending if the time is in HH:MM format it works (not sure on days yet as have rebooted my servers) but breaks if it contains 'min'.

Guess it needs another if statement, but I am not knowledgable enough to work it out...
byytaborda17, April 15, 2013
1 of 1 people found this review helpful
The output is like this:
"System Uptime - up 16 Hours, 23, 2 users Minutes"

so, I added this line: "strminutes1=${strminutes%%,*}' Minutes'"

this way the output, comes this way "System Uptime - up 5 Hours, 36 Minutes" or "System Uptime - up 10 days, 10 Hours, 49 Minutes"

Complete code:
struptime=`uptime | awk '{print $2,$3,$4,$5}'`
struptime1=${struptime%,}
strdayshours=${struptime1%%:*}' Hours, '
strminutes=${struptime1##*:}
strminutes1=${strminutes%%,*}' Minutes'


echo "System Uptime -" $strdayshours$strminutes1
exit 0

Thanks a lot!
byIker Höek, February 6, 2013
2 of 2 people found this review helpful
Works as expected when uptime is higher than a day.

When it is not, the output looks like this:
"System Uptime - up 11 Hours, 16, 2 users Minutes"

I just put the line 7 inside an if that checks if there is an instance of "day" in the variable and formats accordingly:

if [[ $struptime == *day* ]]; then
struptime1=${struptime%,}
else
struptime1=${struptime%, *}
fi

I hope it helps =)
bylinux_samurai, September 6, 2011
This checks does what is intended. A simple bash script that awk's the uptime with output being user-friendly. We have implemented this check on Red Hat & CentOS 5 servers.