1 |
< |
#!/usr/local/bin/perl -w |
1 |
> |
#!/usr/bin/perl -w |
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"; |
110 |
|
chop $response; |
111 |
|
$last_modified = $response; |
112 |
|
|
113 |
< |
print "Config last modified: ". scalar localtime $last_modified . "\n"; |
113 |
> |
print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n"; |
114 |
|
|
115 |
|
print $sock "FILELIST\n"; |
116 |
|
$response = <$sock>; |
129 |
|
chop $response; |
130 |
|
$tcp_update_time = $response; |
131 |
|
|
132 |
< |
print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time.\n"; |
132 |
> |
print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n"; |
133 |
|
|
134 |
|
print $sock "ENDCONFIG\n"; |
135 |
|
$response = <$sock>; |
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 |
|
} |
201 |
|
} |
202 |
|
$disk_info .= "</disk>"; |
203 |
|
|
204 |
+ |
print "$disk_info\n"; |
205 |
+ |
|
206 |
|
my($hostname) = hostname(); |
207 |
< |
($hostname = $hostname) =~ /^([^\.]*?)/; |
208 |
< |
print $hostname; |
209 |
< |
my($domainname) = `cat /etc/resolv.conf`; |
175 |
< |
$domainname =~ s/domain\s*([^\s]*?)/$1/; |
207 |
> |
$hostname =~ s/\..*$//g; |
208 |
> |
`cat /etc/resolv.conf` =~ /domain\s+([^\s]+)/; |
209 |
> |
my($domainname) = $1; |
210 |
|
my($machine_name) = "$hostname.$domainname"; |
211 |
|
my($ip) = inet_ntoa(scalar(gethostbyname($hostname)) || 'localhost'); |
212 |
|
|
213 |
|
# Build the XML packet this way, as we can clearly |
214 |
|
# see the structure and contents... I like this ;-) |
215 |
+ |
# [Note that the server rejects UDP packets that are |
216 |
+ |
# larger than 8196 bytes] |
217 |
|
my($xml) = <<EOF; |
218 |
|
|
219 |
|
<packet seq_no="$seq_no" machine_name="$machine_name" date="$date" type="data" ip="$ip"> |
228 |
|
<platform>$packet{"packet.os.platform"}</platform> |
229 |
|
<sysname>$packet{"packet.os.sysname"}</sysname> |
230 |
|
<version>$packet{"packet.os.version"}</version> |
231 |
+ |
<uptime>$packet{"packet.os.uptime"}</uptime> |
232 |
|
</os> |
233 |
|
<users> |
234 |
|
<count>$packet{"packet.users.count"}</count> |
249 |
|
<swap>$packet{"packet.cpu.swap"}</swap> |
250 |
|
</cpu> |
251 |
|
<memory> |
252 |
< |
<total>$packet{"packet.memory.real"}</total> |
252 |
> |
<total>$packet{"packet.memory.total"}</total> |
253 |
|
<free>$packet{"packet.memory.free"}</free> |
254 |
|
</memory> |
255 |
|
<swap> |
256 |
< |
<total>$packet{"packet.memory.swap_total"}</total> |
257 |
< |
<free>$packet{"packet.memory.swap_free"}</free> |
256 |
> |
<total>$packet{"packet.swap.total"}</total> |
257 |
> |
<free>$packet{"packet.swap.free"}</free> |
258 |
|
</swap> |
259 |
|
$disk_info |
260 |
|
</packet> |
261 |
|
|
262 |
|
EOF |
263 |
|
|
264 |
+ |
# Make the packet smaller by stripping out newlines and leading spaces. |
265 |
|
$xml =~ s/\n\s*//g; |
266 |
|
|
267 |
|
my($sock) = new IO::Socket::INET ( |
274 |
|
close($sock); |
275 |
|
$seq_no++; |
276 |
|
print "-"; |
277 |
+ |
|
278 |
+ |
return; |
279 |
|
} |
280 |
|
|
281 |
+ |
|
282 |
+ |
|
283 |
+ |
|
284 |
+ |
#----------------------------------------------------------------------- |
285 |
+ |
# send_tcp_heartbeat |
286 |
+ |
# Establishes a TCP connection to an i-scream filter. |
287 |
+ |
# The heartbeat is used as a guaranteed "I'm alive" delivery mechanism. |
288 |
+ |
#----------------------------------------------------------------------- |
289 |
|
sub send_tcp_heartbeat() { |
290 |
|
|
291 |
|
my($sock) = new IO::Socket::INET( |
292 |
|
PeerAddr => $filter_addr, |
293 |
|
PeerPort => $tcp_port, |
294 |
|
Proto => 'tcp' |
295 |
< |
) or die "Could not perform heartbeat via TCP: $!\n"; |
295 |
> |
); |
296 |
> |
if (!defined $sock) { |
297 |
> |
print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n"; |
298 |
> |
return; |
299 |
> |
} |
300 |
|
|
249 |
– |
die "Could not connect to the i-scream filter: $!\n" unless $sock; |
250 |
– |
|
301 |
|
# Now run through the configuration process. |
302 |
|
my($response); |
303 |
|
|
353 |
|
|
354 |
|
close($sock); |
355 |
|
print "^"; |
356 |
+ |
|
357 |
+ |
return; |
358 |
|
} |