21 |
|
# You'd be silly not to use this ;) |
22 |
|
use strict; |
23 |
|
|
24 |
+ |
# Path's |
25 |
+ |
my($topbin) = "/usr/local/sbin/top"; |
26 |
+ |
my($dfbin) = "/usr/bin/df"; |
27 |
+ |
my($usersbin) = "/usr/ucb/users"; |
28 |
+ |
my($unamebin) = "/usr/bin/uname"; |
29 |
+ |
|
30 |
|
# Run the following components: - |
31 |
|
&print_ident(); |
32 |
|
&include_osver(); |
55 |
|
# sub to print pairs of data, separated by a single space character. |
56 |
|
# If the second argument is undefined, then the pair is still printed, |
57 |
|
# however, the value shall be displayed as the string "unknown". |
58 |
< |
sub print_pair($$) { |
59 |
< |
my($name, $value) = @_; |
58 |
> |
# If $type is non-zero, then "0" is printed instead of "unknown". |
59 |
> |
sub print_pair($$$) { |
60 |
> |
my($type, $name, $value) = @_; |
61 |
|
|
62 |
|
if (!defined $value) { |
63 |
< |
$value = "unknown"; |
63 |
> |
if ($type) { |
64 |
> |
$value = "0.00"; |
65 |
> |
} |
66 |
> |
else { |
67 |
> |
$value = "unknown"; |
68 |
> |
} |
69 |
|
} |
70 |
|
|
71 |
|
# Remove the trailing linefeed if we've not already done so. |
80 |
|
sub include_disk() { |
81 |
|
|
82 |
|
# Run the df program. |
83 |
< |
my(@df) = `df -a`; |
83 |
> |
my(@df) = `$dfbin -ak`; |
84 |
|
|
73 |
– |
# Only look for these partitions at the moment. |
74 |
– |
my(@partition_list) = qw{ / /home /var /tmp }; |
75 |
– |
|
85 |
|
# Go through each line of the program, looking for each thing we want. |
77 |
– |
my($scan_for) = '('.join('|', @partition_list).')'; |
86 |
|
my($partition_no) = 0; |
87 |
|
for (my($i) = 0; $i < $#df; $i++) { |
88 |
|
my($line) = $df[$i]; |
89 |
< |
$line =~ /^$scan_for\s*\(([^\s]*)\s*\):\s*([0-9]*)\s*blocks\s*([0-9]*)\s*files/; |
89 |
> |
$line =~ /^(\/[^\s]*)\s*([0-9]*)\s*([0-9]*)\s*([0-9]*)\s*[^\s]*\s*(\/[^\s]*)\s*/; |
90 |
|
# $4 will not match unless everything before it does... |
91 |
< |
if (defined $4) { |
92 |
< |
my ($partition, $mounted, $blocks, $files) = ($1, $2, $3, $4); |
93 |
< |
&print_pair("packet.disk.p$partition_no.attributes.name", $partition); |
94 |
< |
&print_pair("packet.disk.p$partition_no.mounted", $mounted); |
95 |
< |
&print_pair("packet.disk.p$partition_no.blocks", $blocks); |
96 |
< |
&print_pair("packet.disk.p$partition_no.name", $files); |
91 |
> |
if (defined $5) { |
92 |
> |
my ($filesystem, $kbytes, $used, $avail, $mount) = ($1, $2, $3, $4, $5); |
93 |
> |
&print_pair(0, "packet.disk.p$partition_no.attributes.name", $filesystem); |
94 |
> |
&print_pair(1, "packet.disk.p$partition_no.attributes.kbytes", $kbytes); |
95 |
> |
&print_pair(1, "packet.disk.p$partition_no.attributes.used", $used); |
96 |
> |
&print_pair(1, "packet.disk.p$partition_no.attributes.avail", $avail); |
97 |
> |
&print_pair(0, "packet.disk.p$partition_no.attributes.mount", $mount); |
98 |
|
++$partition_no; |
99 |
|
} |
100 |
|
} |
106 |
|
sub include_users() { |
107 |
|
|
108 |
|
# Find out all users on this machine. |
109 |
< |
my($users) = `users`; |
109 |
> |
my($users) = `$usersbin`; |
110 |
|
my(@users) = split(/\s+/, $users); |
111 |
|
|
112 |
|
my($users_count) = $#users + 1; |
113 |
|
my($users_list) = $users; |
114 |
|
|
115 |
< |
&print_pair("packet.users.count", $users_count); |
116 |
< |
&print_pair("packet.users.list", $users_list); |
115 |
> |
&print_pair(1, "packet.users.count", $users_count); |
116 |
> |
&print_pair(0, "packet.users.list", $users_list); |
117 |
|
} |
118 |
|
|
119 |
|
|
122 |
|
sub include_top() { |
123 |
|
|
124 |
|
# Find out some numbers from top. |
125 |
< |
my(@top) = `top -d2 -s1 0`; |
125 |
> |
my(@top) = `$topbin -d2 -s1 0`; |
126 |
|
my($top) = join(" ", @top); |
127 |
|
$top =~ s/\n//g; |
128 |
|
|
129 |
< |
&print_pair("packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
130 |
< |
&print_pair("packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
131 |
< |
&print_pair("packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); |
132 |
< |
&print_pair("packet.processes.total", $top =~ /([^\s]+?) processes:/); |
133 |
< |
&print_pair("packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
134 |
< |
&print_pair("packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
135 |
< |
&print_pair("packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
136 |
< |
&print_pair("packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
137 |
< |
&print_pair("packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
138 |
< |
&print_pair("packet.cpu.user", $top =~ /([^\s]+?)% user/); |
139 |
< |
&print_pair("packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
140 |
< |
&print_pair("packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
141 |
< |
&print_pair("packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
142 |
< |
&print_pair("packet.memory.real", $top =~ /([^\s]+?)[MG] real/); |
143 |
< |
&print_pair("packet.memory.free", $top =~ /([^\s]+?)[MG] free/); |
144 |
< |
&print_pair("packet.memory.swap_in_use", $top =~ /([^\s]+?)[MG] swap in use/); |
145 |
< |
&print_pair("packet.memory.swap_free", $top =~ /([^\s]+?)[MG] swap free/); |
146 |
< |
|
129 |
> |
&print_pair(1, "packet.load.load1", $top =~ /load averages:\s*([^\s]+?),/); |
130 |
> |
&print_pair(1, "packet.load.load5", $top =~ /load averages:\s*.+?,\s*([^\s]+?),/); |
131 |
> |
&print_pair(1, "packet.load.load15", $top =~ /load averages:\s*.+?,\s*.+?,\s*([^\s]+?)\s*/); |
132 |
> |
&print_pair(1, "packet.processes.total", $top =~ /([^\s]+?) processes:/); |
133 |
> |
&print_pair(1, "packet.processes.sleeping", $top =~ / ([^\s]+?) sleeping/); |
134 |
> |
&print_pair(1, "packet.processes.zombie", $top =~ / ([^\s]+?) zombie/); |
135 |
> |
&print_pair(1, "packet.processes.stopped", $top =~ / ([^\s]+?) stopped/); |
136 |
> |
&print_pair(1, "packet.processes.cpu", $top =~ /([^\s]+?)\s*on cpu/); |
137 |
> |
&print_pair(1, "packet.cpu.idle", $top =~ /([^\s]+?)% idle/); |
138 |
> |
&print_pair(1, "packet.cpu.user", $top =~ /([^\s]+?)% user/); |
139 |
> |
&print_pair(1, "packet.cpu.kernel", $top =~ /([^\s]+?)% kernel/); |
140 |
> |
&print_pair(1, "packet.cpu.iowait", $top =~ /([^\s]+?)% iowait/); |
141 |
> |
&print_pair(1, "packet.cpu.swap", $top =~ /([^\s]+?)% swap/); |
142 |
> |
|
143 |
> |
# The following need to be specified in megabytes. |
144 |
> |
# If they are preceeded by a G, then multiply by 1024. |
145 |
> |
|
146 |
> |
$top =~ /([^\s]+?)([MG]) real/; |
147 |
> |
my($real) = $1; |
148 |
> |
$real*=1024 if $2 eq "G"; |
149 |
> |
&print_pair(1, "packet.memory.real", $real); |
150 |
> |
|
151 |
> |
$top =~ /([^\s]+?)([MG]) free/; |
152 |
> |
my($free) = $1; |
153 |
> |
$free*=1024 if $2 eq "G"; |
154 |
> |
&print_pair(1, "packet.memory.free", $free); |
155 |
> |
|
156 |
> |
$top =~ /([^\s]+?)([MG]) swap in use/; |
157 |
> |
my($swap_in_use) = $1; |
158 |
> |
$swap_in_use*=1024 if $2 eq "G"; |
159 |
> |
# DO NOT print this one out... save it for in a moment... |
160 |
> |
|
161 |
> |
$top =~ /([^\s]+?)([MG]) swap free/; |
162 |
> |
my($swap_free) = $1; |
163 |
> |
$swap_free*=1024 if $2 eq "G"; |
164 |
> |
&print_pair(1, "packet.memory.swap_free", $swap_free); |
165 |
> |
|
166 |
> |
# AJ requested total swap instead of swap_in_use, so here we go! |
167 |
> |
&print_pair(1, "packet.memory.swap_total", $swap_free + $swap_in_use); |
168 |
|
} |
169 |
|
|
170 |
|
# sub to get details of the machine's operating system. |
173 |
|
# Find out details about the operating system |
174 |
|
# If these values remain undefined, then the print_pair |
175 |
|
# function shall show the value to be the string "unknown". |
176 |
< |
my($os_name) = `uname -s`; |
177 |
< |
my($os_release) = `uname -r`; |
178 |
< |
my($os_platform) = `uname -m`; |
179 |
< |
my($os_sysname) = `uname -n`; |
180 |
< |
my($os_version) = `uname -v`; |
176 |
> |
my($os_name) = `$unamebin -s`; |
177 |
> |
my($os_release) = `$unamebin -r`; |
178 |
> |
my($os_platform) = `$unamebin -m`; |
179 |
> |
my($os_sysname) = `$unamebin -n`; |
180 |
> |
my($os_version) = `$unamebin -v`; |
181 |
|
|
182 |
< |
&print_pair("packet.os.name", $os_name); |
183 |
< |
&print_pair("packet.os.release", $os_release); |
184 |
< |
&print_pair("packet.os.platform", $os_platform); |
185 |
< |
&print_pair("packet.os.sysname", $os_sysname); |
186 |
< |
&print_pair("packet.os.version", $os_version); |
182 |
> |
&print_pair(0, "packet.os.name", $os_name); |
183 |
> |
&print_pair(0, "packet.os.release", $os_release); |
184 |
> |
&print_pair(0, "packet.os.platform", $os_platform); |
185 |
> |
&print_pair(0, "packet.os.sysname", $os_sysname); |
186 |
> |
&print_pair(0, "packet.os.version", $os_version); |
187 |
|
|
188 |
|
} |