CPU Statistics

A little script written in perl, which displays the current cpu usage in a bar graph format

PERL:
#!/usr/bin/perl -w
#################################################################################################
#                      CPU Status Graph - Basic                    #
#         ------------------------------------------------         #
#   DESCRIPTION:   Shows current cpu loads in percent for user, system, nice, and idle     #
#         also shows a colored bar graph. See screenshots or try it :)     #
#                                    #
#   AUTHOR:    Jaguar         #
#                                    #
#   WEBSITE:   http://www.jaginthebox.com      #
#                                    #
#   CREDITS:   HAL9000     Gave me the idea, and determined the default    #
#                  color scheme               #
#                                    #
#         Xlack v1.8-pre2        I used his great system info script to learn    #
#                                    #
#                                    #
#   COMMENTS:  I am by no means a professional programmer,         #
#         if you have any suggestions, or improvements,     #
#         I would be glad to hear them. You can find me on the         #
#         Maddshark IRC Network (irc.maddshark.com) channel #linux      #
#                                    #
#################################################################################################

IRC::register("CPU Status Plugin", "1.0.0", "", "by: Jaguar");
IRC::add_command_handler("cpustat", "cpu_info");

$cL = "\00300";                       # Letter
$cI = "\00307";                        # Information
$cS = "\00314";                        # Separator
$cT = "\00304";                       # Title

$SEP = "|";      # Seperator

$c[0] = "\00300";      
$c[1] = "\00301";      
$c[2] = "\00302";      
$c[3] = "\00303";      
$c[4] = "\00304";      
$c[5] = "\00305";      
$c[6] = "\00306";      
$c[7] = "\00307";      
$c[8] = "\00308";      
$c[9] = "\00309";      
$c[10] = "\00310";   
$c[11] = "\00311";   
$c[12] = "\00312";   
$c[13] = "\00313";
$c[14] = "\00314";   
$c[15] = "\00315";

sub cpu_info {

    # Calculate cpu usage percentage from /proc/stat
    open(STAT, "</proc/stat");
    my ($junk, $cpu_user1, $cpu_nice1, $cpu_sys1, $cpu_idle1) = split(/\s+/, <STAT>);
    close(STAT);
    my $cpu_total1 = $cpu_user1 + $cpu_nice1 + $cpu_sys1 + $cpu_idle1;
    my $cpu_load1 = $cpu_user1 + $cpu_nice1 + $cpu_sys1;
    sleep 2;
    open(STAT, "</proc/stat");
    ($junk, $cpu_user2, $cpu_nice2, $cpu_sys2, $cpu_idle2) = split(/\s+/, <STAT>);
    close(STAT);
    my $cpu_total2 = $cpu_user2 + $cpu_nice2 + $cpu_sys2 + $cpu_idle2;
    my $cpu_load2 = $cpu_user2 + $cpu_nice2 + $cpu_sys2;

    $CPU_USAGE = int((1000 * ($cpu_load2 - $cpu_load1)) / ($cpu_total2 - $cpu_total1))/10;
    $CPU_USER = int((1000 * ($cpu_user2 - $cpu_user1)) / ($cpu_total2 - $cpu_total1))/10;
    $CPU_NICE = int((1000 * ($cpu_nice2 - $cpu_nice1)) / ($cpu_total2 - $cpu_total1))/10;
    $CPU_SYS = int((1000 * ($cpu_sys2 - $cpu_sys1)) / ($cpu_total2 - $cpu_total1))/10;
    $CPU_IDLE = int((1000 * ($cpu_idle2 - $cpu_idle1)) / ($cpu_total2 - $cpu_total1))/10;

     %CPU = ("USER", $CPU_USER,
         "NICE", $CPU_NICE,
         "SYS", $CPU_SYS,
         "IDLE", $CPU_IDLE);

     # Sorted associative array (sorted by value)
     @CPU_VALS = sort by_values (keys (%CPU));

     # Split array into seperate words
     ($one, $two, $three, $four)@CPU_VALS;
     
     ($one_txt, $two_txt, $three_txt, $four_txt)@CPU_VALS;
     
     # Determine value from key eg. USER, SYS, NICE, IDLE
     $one   = $CPU{$one};    # Smallest value
     $two   = $CPU{$two}
     $three = $CPU{$three};
     $four  = $CPU{$four};   # Largest value

    $CPU_PERCENT = $CPU_USAGE
    $CPU_FREEBAR = int($CPU_USAGE/10);
     
    $SCALE = 30;
   
    $one = ($one/100)*$SCALE;
    $two = ($two/100)*$SCALE;
    $three = ($three/100)*$SCALE;
    $four = ($four/100)*$SCALE;
   
    $one = sprintf("%d", $one);
    $two = sprintf("%d", $two);
    $three = sprintf("%d", $three);
    $four = sprintf("%d", $four);
   
    $CPU_BAR = "$c[14]\[$c[4]";
   
    for ($x = 0; $x <$one; $x++)
    {
    $CPU_BAR = $CPU_BAR . cpu_bar_color($one_txt). "\|";
    }
   
    for ($x = 0; $x <$two; $x++)
    {
    $CPU_BAR = $CPU_BAR . cpu_bar_color($two_txt). "\|";
    }
   

    for ($x = 0; $x <$three; $x++)
    {
    $CPU_BAR = $CPU_BAR . cpu_bar_color($three_txt). "\|";
    }
   
    for ($x = 0; $x <$four; $x++)
    {
    $CPU_BAR = $CPU_BAR . cpu_bar_color($four_txt). "\|";
    }
 
    $CPU_BAR = "$CPU_BAR$c[14]\]$c[0]";
   
    IRC::command("$cS\($cT CPU Stats $cS\) $c[15]\[ $c[3]$CPU_USER\% user $c[4] $CPU_SYS\% sys $c[8] $CPU_NICE\% nice $c[0]$CPU_IDLE\% idle $cS$SEP$cL $CPU_BAR $cL\($cI $CPU_PERCENT\% $cL\) $c[15]\]");
   
    return 1;
}

sub by_values
{
     ($CPU{$a} <=> $CPU{$b});
}

sub cpu_bar_color {
 
  my $line = shift(@_);
 
  if ($line =~ /USER/) {
    return "$c[3]";
  }
 
  if ($line =~ /SYS/) {
    return "$c[4]";
  }
 
  if ($line =~ /NICE/) {
    return "$c[8]";
  }
 
  if ($line =~ /IDLE/) {
    return "$c[0]";
  }

  return;
 
}

0 Responses to “CPU Statistics”


  • No Comments

Leave a Reply

You must login to post a comment.