|
#!/usr/bin/perl
#
# The line above is needed to tell the script
# where to look for the perl program
#
# After creating a script, you need to tell the computer it's
# an executable program. Do this by typing:
# chmod 755 /path_to_script/name_of_your_script
# 'uptime' is the name of a small command-line application that prints
# the time the server has been running, and the load on the cpu
# What I do below is take the output of 'uptime' and
# store it in a string variable
$result_of_uptime_command = `uptime`;
# This is the result of a typical <uptime> command.
# We are interested in the number and time unit between the words
# right after "up". (1 day in this example below)
#
# 11:38am up 1 day, 23:30, 1 user, load average: 1.39, 1.57, 1.79
# ^^^^^^
# To extract the number from the string use "split" to break it into parts
# $part1 equals everything before "up". $part2 is everything after "up"
($part1, $part2) = split(/up /,$result_of_uptime_command);
# Given the example <uptime> output from above,
# this is what we expect the parts to be:
#
# $part1 = " 11:38am up "
# $part2 = "1 day, 23:30, 1 user, load average: 1.39, 1.57, 1.79"
# Now take $part2 and split it into a number and the time units
($time_integer, $time_unit) = split(/ /,$part2);
# Given the example <uptime> output from above,
# this is what we expect the parts to be:
#
# $time_integer = "1"
# $time_unit = "day, 23:30, 1 user, load average: 1.39, 1.57, 1.79"
#
# We don't care that $time_unit has all that extra crap after the word day.
# We can easily filter it out. We only care about the first few letters
# which will tell us mins or days. Unfortunately, it won't tell us
# the number of hours. We will have to make a special case for
# 1 to 23 hours. Bummer.
# Are you calling this script from a browser? Then uncomment the next line.
#print "Content-type: text/html\n\n";
# The next chuck is an "if (then) else" loop.
# The first line asks: If the time is not empty (!= ""), then do the loop.
if ($time_integer != "")
{
# If time is one, that is 1 min or 1 day, we need don't want "s"
# The symbol =~ checks for a substring match.
if ($time_integer == "1"){
if ($time_unit =~ /min/){$min_hr_day = "minute";
}
if ($time_unit =~ /day/){$min_hr_day = "day";
}
}
# Time must not be one, so we need the plural units
else {
if ($time_unit =~ /min/){$min_hr_day = "minutes";
}
if ($time_unit =~ /day/){$min_hr_day = "days";
}
}
# Cool. We now have an integer and a time unit.
# Let's print them! We're done.
print "$time_integer $min_hr_day\n";
}
# If we get to this "else", that means we are in that awkward first 24 hour
# period and must carve the time into hours and minutes.
# For example, <uptime> lists those first hours as "2:44," so we can use split
# is based on the colon. The "chop" command gets rid of the trailing comma.
else {
($time_hours, $time_minutes) = split(/:/,$time_unit);
# Cool. We have the number of hours and minutes.
# Let's print them. We're done.
print "\n$time_hours hrs, ";
chop $time_minutes;
print "$time_minutes min\n";
}
#the end :)
|