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.5
Committed: Sat May 18 18:15:57 2002 UTC (23 years, 7 months ago) by tdb
Content type: text/plain
Branch: MAIN
Changes since 1.4: +20 -1 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

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