#!/usr/bin/perl # # uppers This script perpetually give some system statistics. If # we're giving verbose output, we'll give the whole uptime # info. Otherwise, we'll only give the system load averages. # # usage: # uppers [-sleep | -verbose | -help | -Version] # # Revision History # 1.0 Initial revision. 150214 # 1.1 Added licensing info. 180531 # # Written by Wayne Morrison, 150214. # # Copyright 2015 Wayne Morrison # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # use strict; use Getopt::Long qw(:config no_ignore_case_always); # # Version information. # my $NAME = "uppers"; my $VERS = "$NAME version: 1.1"; ############################################################################ # # Options fields. # my %opts = (); # Options. # # Command line arguments. # my @opts = ( 'sleep=i', # Sleep time. 'verbose', # Give verbose output. 'help', # Give a help message. 'Version', # Display the program version. ); my $verbose = 0; # Verbose flag. my $nap; # Sleep time. my $DEFAULT_SLEEP = 10; # Default sleep time. ############################################################################ main(); exit(0); #----------------------------------------------------------------------------- # Routine: main() # sub main { $| = 1; # # Munch on the options and arguments. # optsandargs(); # # Show uptimes. # uptimes(); } #---------------------------------------------------------------------- # Routine: optsandargs() # # Purpose: Parse the command line for options and arguments. # sub optsandargs { # # Parse the options. # GetOptions(\%opts,@opts) || usage(); # # Check for some immediate-action options. # usage() if(defined($opts{'help'})); version() if(defined($opts{'Version'})); $nap = defined($opts{'sleep'}) ? $opts{'sleep'} : $DEFAULT_SLEEP; $verbose = $opts{'verbose'}; if($nap < 1) { print STDERR "sleep time must be at least 1\n"; exit(1); } } #---------------------------------------------------------------------- # Routine: uptimes() # # Purpose: Perpetually give some system statistics. If we're giving # verbose output, we'll give the whole uptime info. Otherwise, # we'll only give the system load averages. # sub uptimes { while(42) { # # If we're giving verbose output, we'll give the whole uptime # info. Otherwise, we'll only give the system load averages. # if($verbose) { system("uptime"); } else { my $out; # uptime's output. # # Get the system uptime statistics. # $out = `uptime`; chomp($out); # # Pull out the load averages. # $out =~ /averages: (\d+\.\d+) (\d+\.\d+) (\d+\.\d+)$/; # # Print 'em nicely. # printf("%5.2f %5.2f %5.2f\n",$1,$2,$3); } # # Sleep a little. # sleep($nap); } } #---------------------------------------------------------------------- # Routine: version() # # Purpose: Print the version number(s) and exit. # sub version { print STDERR "$VERS\n"; exit(0); } #---------------------------------------------------------------------- # Routine: usage() # # Purpose: Give usage message and exit. # sub usage { print STDERR "usage: uppers [options]\n"; print STDERR "\n"; print STDERR "\twhere [options] are:\n"; print STDERR "\t\t-sleep sleeptime\n"; print STDERR "\n"; print STDERR "\t\t-verbose\n"; print STDERR "\t\t-help\n"; print STDERR "\t\t-Version\n"; exit(0); } 1; ############################################################################## =pod =head1 NAME B - gives system load averages forever =head1 SYNOPSIS uppers [options] =head1 DESCRIPTION B perpetually give some system statistics, sleeping briefly between each report. By default, only the system load averages (as reported by B) will be printed. If the I<-verbose> output is given, the full B output will be printed. This script is making use of the B command; it can easily be replaced by entering commands directly in a shell. However, B provides a fast short-cut to this functionality since it is more quickly invoked than typing in the shell commands each time it is wanted. =head1 OPTIONS B takes the following options: =over 4 =item I<-sleep sleeptime> This option specifies the number of seconds to sleep between each report. =item I<-verbose> This option gives the full B output, rather than just the load averages. =item I<-Version> Display the version information for B. =item I<-help> Display a help message. =back =head1 AUTHOR Wayne Morrison, wayne@waynemorrison.com =head1 LICENSE Copyright 2015 Wayne Morrison Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =head1 SEE ALSO B =cut