Mini Shell
Direktori : /opt/sharedrads/ |
|
Current File : //opt/sharedrads/server-load |
#!/usr/bin/perl
# encoding: utf-8
# author: Kyle Yetter <kyle@ohboyohboyohboy.org>
# created on: August 13, 2011
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
our $VERSION = "1.0";
our $RED = 31;
our $GREEN = 32;
our $YELLOW = 33;
our $BLEACH = 0;
sub usage {
my $status = shift || 0;
my $name = basename( $0 );
my $usage =
qq(
| NAME
| $name v$VERSION
|
| USAGE
| $name [ --help | --version | --bleach ]
|
| Print the server's load averages normalized against the number
| of processors on the server.
|
| OPTIONS
| -b, --bleach Do not use ANSI color escapes in the output
| -h, --help Show this help message
| -v, --version Print the program's version number and exit
|
);
$usage =~ s/^\n|\n\s*$//g;
$usage =~ s/^\s*\| ?//mg;
print $usage, "\n";
exit 0;
}
sub c {
my ( $str, $color ) = @_;
unless( $BLEACH ) {
$str = "\e[${color}m$str\e[0m";
}
return $str;
}
GetOptions(
"b|bleach" => \$BLEACH,
"h|help" => \&usage,
"v|version" => sub { print "$VERSION\n"; exit( 0 ); }
);
my $cpu_count = 0;
open( PROC_INFO, '/proc/cpuinfo' );
while ( <PROC_INFO> ) {
$cpu_count += 1 if /^processor\b/;
}
close( PROC_INFO );
$cpu_count = 1 unless $cpu_count;
open( LOAD_AVG, '/proc/loadavg' );
my @load_avg = ( split( /\s+/, <LOAD_AVG> ) )[ 0..2 ];
close( LOAD_AVG );
my @normalized = map { $_ / $cpu_count; } @load_avg;
my @titles = qw( 1m 5m 15m );
my @colors = map {
my $val = $_;
my $color;
if ( $val > 1.0 ) {
$color = $RED;
} elsif ( $val > 0.7 ) {
$color = $YELLOW;
} else {
$color = $GREEN;
}
$color;
} @normalized;
my $max = ( sort @load_avg )[ 2 ];
my $w = length( sprintf( '%.1f', $max ) ) + 1;
if ( $w < 4 ) { $w = 4; }
my $f_mask = "%$w.1f";
my $s_mask = "%${w}s";
print( " ", join( " ", map { c( sprintf( $s_mask, $_ ), 4 ); } @titles ), "\n" );
print( "Actual: ", join( " ", map { c( sprintf( $f_mask, $load_avg[ $_ ] ), $colors[ $_ ] ); } 0 ... 2 ), "\n" );
print( "Normalized: ", join( " ", map { c( sprintf( $f_mask, $normalized[ $_ ] ), $colors[ $_ ] ); } 0 ... 2 ), "\n" );
Zerion Mini Shell 1.0