2 |
|
|
3 |
|
# ----------------------------------------------------------- |
4 |
|
# Perl i-scream Host. |
5 |
+ |
# http://www.i-scream.org.uk |
6 |
|
# |
7 |
|
# An all-in-one script to act as an i-scream host on |
8 |
|
# a typical Unix/Linux box. You may adapt the data-gathering |
49 |
|
$last_udp_time = time; |
50 |
|
$last_tcp_time = time; |
51 |
|
while (1) { |
52 |
< |
if (time >= $last_udp_time + $udp_update_time) { |
52 |
> |
my($time) = time; |
53 |
> |
if ($time >= $last_udp_time + $udp_update_time) { |
54 |
|
&send_udp_packet(); |
55 |
< |
$last_udp_time = time; |
55 |
> |
$last_udp_time = $time; |
56 |
|
} |
57 |
< |
if (time >= $last_tcp_time + $tcp_update_time) { |
57 |
> |
if ($time >= $last_tcp_time + $tcp_update_time) { |
58 |
|
&send_tcp_heartbeat(); |
59 |
< |
$last_tcp_time = time; |
59 |
> |
$last_tcp_time = $time; |
60 |
|
} |
61 |
< |
`sleep 1`; |
61 |
> |
my($next_udp) = $udp_update_time - $time + $last_udp_time; |
62 |
> |
my($next_tcp) = $tcp_update_time - $time + $last_tcp_time; |
63 |
> |
my($delay); |
64 |
> |
if ($next_udp < $next_tcp) { |
65 |
> |
$delay = $next_udp |
66 |
> |
} |
67 |
> |
else { |
68 |
> |
$delay = $next_tcp; |
69 |
> |
} |
70 |
> |
`sleep $delay`; |
71 |
|
} |
72 |
|
|
73 |
|
exit(0); |
74 |
|
|
75 |
+ |
|
76 |
+ |
#----------------------------------------------------------------------- |
77 |
+ |
# tcp_configure |
78 |
+ |
# Establishes a TCP connection to the specified i-scream filter manager. |
79 |
+ |
# The host then requests details from the server, such as the intervals |
80 |
+ |
# at which to send UDP packets. |
81 |
+ |
#----------------------------------------------------------------------- |
82 |
|
sub tcp_configure() { |
83 |
|
|
84 |
|
my($sock) = new IO::Socket::INET( |
85 |
|
PeerAddr => $filter_manager_addr, |
86 |
|
PeerPort => $filter_manager_port, |
87 |
|
Proto => 'tcp' |
88 |
< |
) or die "Could not perform configuration via TCP: $!\n"; |
88 |
> |
); |
89 |
> |
if (!defined $sock) { |
90 |
> |
print "IHOST ERROR: Could not connect to $filter_manager_addr:$filter_manager_port.\n"; |
91 |
> |
print "Please check that there is an i-scream server at this address.\n"; |
92 |
> |
print "Program ended.\n"; |
93 |
> |
exit(1); |
94 |
> |
} |
95 |
|
|
96 |
< |
die "Could not connect to the i-scream filter manager: $!\n" unless $sock; |
73 |
< |
|
74 |
< |
# Now run through the configuration process. |
96 |
> |
# Now run through the configuration process... |
97 |
|
my($response); |
98 |
|
|
99 |
|
print $sock "STARTCONFIG\n"; |
163 |
|
close($sock); |
164 |
|
|
165 |
|
print "Configuration finished sucessfully!\n"; |
166 |
+ |
|
167 |
+ |
return; |
168 |
|
} |
169 |
|
|
170 |
+ |
|
171 |
+ |
|
172 |
+ |
|
173 |
+ |
#----------------------------------------------------------------------- |
174 |
+ |
# send_udp_packet |
175 |
+ |
# Sends a UDP packet to an i-scream filter. |
176 |
+ |
# The packet contains XML markup describing some of the machine's state. |
177 |
+ |
# Receipt of UDP packets is not guaranteed. |
178 |
+ |
#----------------------------------------------------------------------- |
179 |
|
sub send_udp_packet() { |
180 |
|
|
181 |
|
my(@statgrab) = `./statgrab.pl`; |
182 |
|
my(%packet); |
183 |
< |
for (my($i) = 0; $i < $#statgrab; $i++) { |
183 |
> |
for (my($i) = 0; $i <= $#statgrab; $i++) { |
184 |
|
$statgrab[$i] =~ /^([^\s]*) (.*)$/; |
185 |
|
$packet{$1} = $2; |
186 |
|
} |
190 |
|
my($disk_info) = "<disk>"; |
191 |
|
my($i) = 0; |
192 |
|
while (defined $packet{"packet.disk.p$i.attributes.mount"}) { |
193 |
< |
$disk_info .= "<p$i>"; |
194 |
< |
$disk_info .= qq/<name>$packet{"packet.disk.p$i.attributes.name"}<\/name>/; |
195 |
< |
$disk_info .= qq/<kbytes>$packet{"packet.disk.p$i.attributes.kbytes"}<\/kbytes>/; |
196 |
< |
$disk_info .= qq/<used>$packet{"packet.disk.p$i.attributes.used"}<\/used>/; |
197 |
< |
$disk_info .= qq/<avail>$packet{"packet.disk.p$i.attributes.avail"}<\/avail>/; |
198 |
< |
$disk_info .= qq/<mount>$packet{"packet.disk.p$i.attributes.mount"}<\/mount>/; |
199 |
< |
$disk_info .= "</p$i>"; |
193 |
> |
$disk_info .= "<p$i"; |
194 |
> |
$disk_info .= " name=\"" . $packet{"packet.disk.p$i.attributes.name"} . "\""; |
195 |
> |
$disk_info .= " kbytes=\"" . $packet{"packet.disk.p$i.attributes.kbytes"} . "\""; |
196 |
> |
$disk_info .= " used=\"" . $packet{"packet.disk.p$i.attributes.used"} . "\""; |
197 |
> |
$disk_info .= " avail=\"" . $packet{"packet.disk.p$i.attributes.avail"} . "\""; |
198 |
> |
$disk_info .= " mount=\"" . $packet{"packet.disk.p$i.attributes.mount"} . "\""; |
199 |
> |
$disk_info .= "></p$i>"; |
200 |
|
++$i; |
201 |
|
} |
202 |
|
$disk_info .= "</disk>"; |
210 |
|
|
211 |
|
# Build the XML packet this way, as we can clearly |
212 |
|
# see the structure and contents... I like this ;-) |
213 |
+ |
# [Note that the server rejects UDP packets that are |
214 |
+ |
# larger than 8196 bytes] |
215 |
|
my($xml) = <<EOF; |
216 |
|
|
217 |
|
<packet seq_no="$seq_no" machine_name="$machine_name" date="$date" type="data" ip="$ip"> |
226 |
|
<platform>$packet{"packet.os.platform"}</platform> |
227 |
|
<sysname>$packet{"packet.os.sysname"}</sysname> |
228 |
|
<version>$packet{"packet.os.version"}</version> |
229 |
+ |
<uptime>$packet{"packet.os.uptime"}</uptime> |
230 |
|
</os> |
231 |
|
<users> |
232 |
|
<count>$packet{"packet.users.count"}</count> |
247 |
|
<swap>$packet{"packet.cpu.swap"}</swap> |
248 |
|
</cpu> |
249 |
|
<memory> |
250 |
< |
<total>$packet{"packet.memory.real"}</total> |
250 |
> |
<total>$packet{"packet.memory.total"}</total> |
251 |
|
<free>$packet{"packet.memory.free"}</free> |
252 |
|
</memory> |
253 |
|
<swap> |
254 |
< |
<total>$packet{"packet.memory.swap_total"}</total> |
255 |
< |
<free>$packet{"packet.memory.swap_free"}</free> |
254 |
> |
<total>$packet{"packet.swap.total"}</total> |
255 |
> |
<free>$packet{"packet.swap.free"}</free> |
256 |
|
</swap> |
257 |
|
$disk_info |
258 |
|
</packet> |
259 |
|
|
260 |
|
EOF |
261 |
|
|
262 |
+ |
# Make the packet smaller by stripping out newlines and leading spaces. |
263 |
|
$xml =~ s/\n\s*//g; |
264 |
|
|
265 |
|
my($sock) = new IO::Socket::INET ( |
272 |
|
close($sock); |
273 |
|
$seq_no++; |
274 |
|
print "-"; |
275 |
+ |
|
276 |
+ |
return; |
277 |
|
} |
278 |
|
|
279 |
+ |
|
280 |
+ |
|
281 |
+ |
|
282 |
+ |
#----------------------------------------------------------------------- |
283 |
+ |
# send_tcp_heartbeat |
284 |
+ |
# Establishes a TCP connection to an i-scream filter. |
285 |
+ |
# The heartbeat is used as a guaranteed "I'm alive" delivery mechanism. |
286 |
+ |
#----------------------------------------------------------------------- |
287 |
|
sub send_tcp_heartbeat() { |
288 |
|
|
289 |
|
my($sock) = new IO::Socket::INET( |
290 |
|
PeerAddr => $filter_addr, |
291 |
|
PeerPort => $tcp_port, |
292 |
|
Proto => 'tcp' |
293 |
< |
) or die "Could not perform heartbeat via TCP: $!\n"; |
293 |
> |
); |
294 |
> |
if (!defined $sock) { |
295 |
> |
print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n"; |
296 |
> |
return; |
297 |
> |
} |
298 |
|
|
248 |
– |
die "Could not connect to the i-scream filter: $!\n" unless $sock; |
249 |
– |
|
299 |
|
# Now run through the configuration process. |
300 |
|
my($response); |
301 |
|
|
351 |
|
|
352 |
|
close($sock); |
353 |
|
print "^"; |
354 |
+ |
|
355 |
+ |
return; |
356 |
|
} |