11/Mar 2006
By karl
3 min. read
A little script written in perl, which displays the current cpu usage in a bar graph format
#!/usr/bin/perl -w ################################################################################################# # CPU Status Graph # # ------------------------------------------------ # # DESCRIPTION: Shows current cpu loads in percent for user, system, nice, and idle # # also shows a colored bar graph. See screenshots or try it :) # # # ################################################################################################# 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.