ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/plugins/perl/i-scream_top.pl
Revision: 1.3
Committed: Tue Feb 12 17:13:52 2002 UTC (23 years, 10 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.2: +4 -2 lines
Log Message:
FreeBSD memory information was incorrect. Now fixed.

File Contents

# Content
1 #!/usr/bin/perl -w
2
3 #-----------------------------------------------------------------
4 # i-scream host plugin - top parsing (cpu, proc, mem, & swap)
5 # $Author: tdb $
6 # $Id: i-scream_top.pl,v 1.2 2001/12/18 04:07:17 tdb Exp $
7 #
8 # A short perl script to get the cpu loads, process details,
9 # memory usage and swap usage by parsing top.
10 #
11 # nb. this is a rather 'heavy' process to run :(
12 #-----------------------------------------------------------------
13
14
15 $| = 1;
16
17
18 # You'd be silly not to use this ;)
19 use strict;
20
21 # Get the OS type from the args, or try towork it out
22 my($ostype) = $ARGV[0];
23 $ostype = `uname -s` if not defined $ostype;
24 chomp($ostype);
25
26 # Decide which paths we should use.
27 my($topbin);
28 my($sysctlbin);
29 if ($ostype eq "SunOS") {
30 # covers: Solaris 7/8
31 $topbin = "/usr/local/sbin/top -d2 -s1 0";
32 }
33 elsif ($ostype eq "Linux") {
34 # covers: Debian r2.2
35 $topbin = "/usr/bin/top -d1 -n2 -b -p0";
36 }
37 elsif ($ostype eq "FreeBSD") {
38 # covers: FreeBSD 4.X
39 $topbin = "/usr/bin/top -d2 -s1 0";
40 $sysctlbin = "/sbin/sysctl";
41 }
42 else {
43 print "i-scream_top.pl Error: Unable to identify system type - \"$ostype\".\n";
44 print "\"uname -s\" does not report one of the following known types;\n";
45 print " SunOS, Linux, FreeBSD\n";
46 exit(1);
47 }
48
49 # Run the following components: -
50 &include_top();
51
52 # End the program normally.
53 exit(0);
54
55
56
57
58 # sub to print pairs of data, separated by a single space character.
59 # If the second argument is undefined, then the pair is still printed,
60 # however, the value shall be displayed as the the 'default' value
61 # if the passed value was undefined.
62 sub print_pair($$$) {
63 my($default, $name, $value) = @_;
64
65 if (!defined $value) {
66 $value = $default;
67 }
68
69 # Remove the trailing linefeed if we've not already done so.
70 chomp($value);
71
72 # print the pair of data with a space inbetween.
73 print "$name $value\n";
74 }
75
76
77 # sub to run a series of regexps on the output of 'top' to
78 # gather various machine statistics.
79 sub include_top() {
80
81 # Find out some numbers from top.
82 my(@top) = `$topbin`;
83 my($top) = join(" ", @top);
84 $top =~ s/\n/ /g;
85
86 if($ostype eq "SunOS") {
87 &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/);
88 &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/);
89 &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/);
90 &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/);
91 &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*on cpu/);
92 &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
93 &print_pair(0, "packet.cpu.user", $top =~ /([^\s]+?)% user/);
94 &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/);
95 &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/);
96 &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
97
98 # The following need to be specified in megabytes.
99 # If they are preceeded by a G, then multiply by 1024.
100
101 $top =~ /([0-9]+?)([KMG]) real/;
102 my($real) = $1;
103 $real*=1024 if $2 eq "G";
104 $real/=1024 if $2 eq "K";
105 &print_pair(0, "packet.memory.total", $real);
106
107 $top =~ /([0-9]+?)([KMG]) free/;
108 my($free) = $1;
109 $free*=1024 if $2 eq "G";
110 $free/=1024 if $2 eq "K";
111 &print_pair(0, "packet.memory.free", $free);
112
113 $top =~ /([0-9]+?)([KMG]) swap in use/;
114 my($swap_in_use) = $1;
115 $swap_in_use*=1024 if $2 eq "G";
116 $swap_in_use/=1024 if $2 eq "K";
117 # DO NOT print this one out... save it for in a moment...
118
119 $top =~ /([0-9]+?)([KMG]) swap free/;
120 my($swap_free) = $1;
121 $swap_free*=1024 if $2 eq "G";
122 $swap_free/=1024 if $2 eq "K";
123 &print_pair(0, "packet.swap.free", $swap_free);
124
125 &print_pair(0, "packet.swap.total", $swap_free + $swap_in_use);
126 }
127 elsif ($ostype eq "FreeBSD") {
128 &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/);
129 &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/);
130 &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/);
131 &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/);
132 &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*running/);
133 &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
134 &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
135 &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
136 &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
137
138 # FreeBSD is a bit different, we need to get user and nice.
139 my($user) = 0;
140 if($top =~ /([^\s]+?)% user/) { $user += $1; }
141 if($top =~ /([^\s]+?)% nice/) { $user += $1; }
142 &print_pair(0, "packet.cpu.user", $user);
143
144 # The following need to be specified in megabytes.
145 # If they are preceeded by a G, then multiply by 1024.
146
147 # get RAM slightly differently
148 my($real) = `$sysctlbin -n hw.physmem`;
149 my($v_free_count) = `$sysctlbin -n vm.stats.vm.v_free_count`;
150 my($v_page_size) = `$sysctlbin -n vm.stats.vm.v_page_size`;
151 my($free) = $v_free_count * $v_page_size;
152
153 # turn bytes to megabytes
154 $real = ($real / 1024) / 1024;
155 $free = ($free / 1024) / 1024;
156
157 &print_pair(0, "packet.memory.total", $real);
158 &print_pair(0, "packet.memory.free", $free);
159
160 $top =~ /Swap: ([0-9]+?)([KMG]) Total/;
161 my($swap_total) = $1;
162 $swap_total*=1024 if $2 eq "G";
163 $swap_total/=1024 if $2 eq "K";
164 &print_pair(0, "packet.swap.total", $swap_total);
165
166 $top =~ /Swap:.*, ([0-9]+?)([KMG]) Free/;
167 my($swap_free) = $1;
168 $swap_free*=1024 if $2 eq "G";
169 $swap_free/=1024 if $2 eq "K";
170 &print_pair(0, "packet.swap.free", $swap_free);
171 }
172 elsif ($ostype eq "Linux") {
173 my ($top) = "";
174 foreach my $line (@top) {
175 $top = $line . $top;
176 }
177 $top =~ s/\n/ /g;
178
179 &print_pair(0, "packet.processes.total", $top =~ /([0-9]+?) processes:/);
180 &print_pair(0, "packet.processes.sleeping", $top =~ /([0-9]+?) sleeping/);
181 &print_pair(0, "packet.processes.zombie", $top =~ /([0-9]+?) zombie/);
182 &print_pair(0, "packet.processes.stopped", $top =~ /([0-9]+?) stopped/);
183 &print_pair(0, "packet.processes.cpu", $top =~ /([0-9]+?)\s*running/);
184 &print_pair(0, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/);
185 &print_pair(0, "packet.cpu.kernel", $top =~ /([^\s]+?)% system/);
186 &print_pair(0, "packet.cpu.iowait", $top =~ /([^\s]+?)% interrupt/);
187 &print_pair(0, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/);
188
189 # Linux is a bit different, we need to get user and nice.
190 my($user) = 0;
191 if($top =~ /([^\s]+?)% user/) { $user += $1; }
192 if($top =~ /([^\s]+?)% nice/) { $user += $1; }
193 &print_pair(0, "packet.cpu.user", $user);
194
195 # The following need to be specified in megabytes.
196 # If they are preceeded by a G, then multiply by 1024.
197
198 $top =~ /Mem:.*?([0-9]+)([KMG])\s+(av|total)/;
199 my($real) = $1;
200 $real*=1024 if $2 eq "G";
201 $real/=1024 if $2 eq "K";
202 &print_pair(0, "packet.memory.total", int($real));
203
204 $top =~ /Mem:.*?([0-9]+)([KMG])\s+free/;
205 my($free) = $1;
206 $free*=1024 if $2 eq "G";
207 $free/=1024 if $2 eq "K";
208 &print_pair(0, "packet.memory.free", int($free));
209
210 $top =~ /Swap:.*?([0-9]+)([KMG])\s+(av|total)/;
211 my($swap_total) = $1;
212 $swap_total*=1024 if $2 eq "G";
213 $swap_total/=1024 if $2 eq "K";
214 &print_pair(0, "packet.swap.total", int($swap_total));
215
216 $top =~ /Swap:.*?([0-9]+)([KMG])\s+free/;
217 my($swap_free) = $1;
218 $swap_free*=1024 if $2 eq "G";
219 $swap_free/=1024 if $2 eq "K";
220 &print_pair(0, "packet.swap.free", int($swap_free));
221 }
222 else {
223 # we could have some catchall here
224 # but as it stands this means we'll just skip top stuff
225 # for unknown systems
226 }
227 }