Nerds on Linux

March 11, 2006

CPU Statistics

Filed under: — karl @ 9:39 am

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/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 :) 		#
#												#
#	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	#
#												#
#												#
#################################################################################################
 
 
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;
 
}

No Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

You must be logged in to post a comment.

Powered by WordPress