1 |
#!/usr/bin/perl -w |
2 |
|
3 |
# ----------------------------------------------------------- |
4 |
# Perl i-scream Host. |
5 |
# |
6 |
# An all-in-one script to act as an i-scream host on |
7 |
# a typical Unix/Linux box. You may adapt the data-gathering |
8 |
# methods as you see fit. |
9 |
# - pjm2@ukc.ac.uk |
10 |
# |
11 |
# $Author: pjm2 $ |
12 |
# $Id: ihost.pl,v 1.9 2001/01/29 16:51:50 pjm2 Exp $ |
13 |
#------------------------------------------------------------ |
14 |
|
15 |
$| = 1; |
16 |
|
17 |
use strict; |
18 |
use IO::Socket; |
19 |
use Sys::Hostname; |
20 |
|
21 |
use vars qw ( |
22 |
$filter_manager_addr |
23 |
$filter_manager_port |
24 |
$seq_no |
25 |
$udp_update_time |
26 |
$tcp_update_time |
27 |
$last_udp_time |
28 |
$last_tcp_time |
29 |
$last_modified |
30 |
$udp_port |
31 |
$tcp_port |
32 |
$filter_addr |
33 |
$file_list |
34 |
); |
35 |
|
36 |
if (@ARGV != 2) { |
37 |
die "Usage: ihost.pl [i-scream filter manager] [TCP port]\n"; |
38 |
} |
39 |
|
40 |
$filter_manager_addr = $ARGV[0]; |
41 |
$filter_manager_port = $ARGV[1]; |
42 |
|
43 |
$seq_no = 1; |
44 |
|
45 |
&tcp_configure(); |
46 |
&send_udp_packet(); |
47 |
|
48 |
$last_udp_time = time; |
49 |
$last_tcp_time = time; |
50 |
while (1) { |
51 |
my($time) = time; |
52 |
if ($time >= $last_udp_time + $udp_update_time) { |
53 |
&send_udp_packet(); |
54 |
$last_udp_time = $time; |
55 |
} |
56 |
if ($time >= $last_tcp_time + $tcp_update_time) { |
57 |
&send_tcp_heartbeat(); |
58 |
$last_tcp_time = $time; |
59 |
} |
60 |
my($next_udp) = $udp_update_time - $time + $last_udp_time; |
61 |
my($next_tcp) = $tcp_update_time - $time + $last_tcp_time; |
62 |
my($delay); |
63 |
if ($next_udp < $next_tcp) { |
64 |
$delay = $next_udp |
65 |
} |
66 |
else { |
67 |
$delay = $next_tcp; |
68 |
} |
69 |
`sleep $delay`; |
70 |
} |
71 |
|
72 |
exit(0); |
73 |
|
74 |
sub tcp_configure() { |
75 |
|
76 |
my($sock) = new IO::Socket::INET( |
77 |
PeerAddr => $filter_manager_addr, |
78 |
PeerPort => $filter_manager_port, |
79 |
Proto => 'tcp' |
80 |
) or die "Could not perform configuration via TCP: $!\n"; |
81 |
|
82 |
die "Could not connect to the i-scream filter manager: $!\n" unless $sock; |
83 |
|
84 |
# Now run through the configuration process. |
85 |
my($response); |
86 |
|
87 |
print $sock "STARTCONFIG\n"; |
88 |
$response = <$sock>; |
89 |
if (!chop $response eq "OK") { |
90 |
print "The i-scream server rejected the STARTCONFIG command. Terminated."; |
91 |
exit(1); |
92 |
} |
93 |
|
94 |
print "Config started okay.\n"; |
95 |
|
96 |
print $sock "LASTMODIFIED\n"; |
97 |
$response = <$sock>; |
98 |
chop $response; |
99 |
$last_modified = $response; |
100 |
|
101 |
print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n"; |
102 |
|
103 |
print $sock "FILELIST\n"; |
104 |
$response = <$sock>; |
105 |
chop $response; |
106 |
$file_list = $response; |
107 |
|
108 |
print "File list obtained: $file_list\n"; |
109 |
|
110 |
print $sock "UDPUpdateTime\n"; |
111 |
$response = <$sock>; |
112 |
chop $response; |
113 |
$udp_update_time = $response; |
114 |
|
115 |
print $sock "TCPUpdateTime\n"; |
116 |
$response = <$sock>; |
117 |
chop $response; |
118 |
$tcp_update_time = $response; |
119 |
|
120 |
print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n"; |
121 |
|
122 |
print $sock "ENDCONFIG\n"; |
123 |
$response = <$sock>; |
124 |
chomp $response; |
125 |
if (!$response eq "OK") { |
126 |
print "ENDCONFIG command to server failed. Terminated.\n"; |
127 |
exit(1); |
128 |
} |
129 |
|
130 |
print "Config ended.\n"; |
131 |
|
132 |
print $sock "FILTER\n"; |
133 |
$response = <$sock>; |
134 |
chop $response; |
135 |
$response =~ /(.*);(.*);(.*)/; |
136 |
($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3); |
137 |
|
138 |
print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n"; |
139 |
|
140 |
print $sock "END\n"; |
141 |
$response = <$sock>; |
142 |
chop $response; |
143 |
if ($response eq "OK") { |
144 |
print "Host successfully configured via TCP.\n" |
145 |
} |
146 |
else { |
147 |
print "The server failed the host configuration on the END command."; |
148 |
exit(1); |
149 |
} |
150 |
|
151 |
close($sock); |
152 |
|
153 |
print "Configuration finished sucessfully!\n"; |
154 |
} |
155 |
|
156 |
sub send_udp_packet() { |
157 |
|
158 |
my(@statgrab) = `./statgrab.pl`; |
159 |
my(%packet); |
160 |
for (my($i) = 0; $i < $#statgrab; $i++) { |
161 |
$statgrab[$i] =~ /^([^\s]*) (.*)$/; |
162 |
$packet{$1} = $2; |
163 |
} |
164 |
|
165 |
my($date) = time; |
166 |
|
167 |
my($disk_info) = "<disk>"; |
168 |
my($i) = 0; |
169 |
while (defined $packet{"packet.disk.p$i.attributes.mount"}) { |
170 |
$disk_info .= "<p$i>"; |
171 |
$disk_info .= qq/<name>$packet{"packet.disk.p$i.attributes.name"}<\/name>/; |
172 |
$disk_info .= qq/<kbytes>$packet{"packet.disk.p$i.attributes.kbytes"}<\/kbytes>/; |
173 |
$disk_info .= qq/<used>$packet{"packet.disk.p$i.attributes.used"}<\/used>/; |
174 |
$disk_info .= qq/<avail>$packet{"packet.disk.p$i.attributes.avail"}<\/avail>/; |
175 |
$disk_info .= qq/<mount>$packet{"packet.disk.p$i.attributes.mount"}<\/mount>/; |
176 |
$disk_info .= "</p$i>"; |
177 |
++$i; |
178 |
} |
179 |
$disk_info .= "</disk>"; |
180 |
|
181 |
my($hostname) = hostname(); |
182 |
$hostname =~ s/\..*$//g; |
183 |
`cat /etc/resolv.conf` =~ /domain\s+([^\s]+)/; |
184 |
my($domainname) = $1; |
185 |
my($machine_name) = "$hostname.$domainname"; |
186 |
my($ip) = inet_ntoa(scalar(gethostbyname($hostname)) || 'localhost'); |
187 |
|
188 |
# Build the XML packet this way, as we can clearly |
189 |
# see the structure and contents... I like this ;-) |
190 |
my($xml) = <<EOF; |
191 |
|
192 |
<packet seq_no="$seq_no" machine_name="$machine_name" date="$date" type="data" ip="$ip"> |
193 |
<load> |
194 |
<load1>$packet{"packet.load.load1"}</load1> |
195 |
<load5>$packet{"packet.load.load5"}</load5> |
196 |
<load15>$packet{"packet.load.load15"}</load15> |
197 |
</load> |
198 |
<os> |
199 |
<name>$packet{"packet.os.name"}</name> |
200 |
<release>$packet{"packet.os.release"}</release> |
201 |
<platform>$packet{"packet.os.platform"}</platform> |
202 |
<sysname>$packet{"packet.os.sysname"}</sysname> |
203 |
<version>$packet{"packet.os.version"}</version> |
204 |
<uptime>$packet{"packet.os.uptime"}</uptime> |
205 |
</os> |
206 |
<users> |
207 |
<count>$packet{"packet.users.count"}</count> |
208 |
<list>$packet{"packet.users.list"}</list> |
209 |
</users> |
210 |
<processes> |
211 |
<total>$packet{"packet.processes.total"}</total> |
212 |
<sleeping>$packet{"packet.processes.sleeping"}</sleeping> |
213 |
<zombie>$packet{"packet.processes.zombie"}</zombie> |
214 |
<stopped>$packet{"packet.processes.stopped"}</stopped> |
215 |
<cpu>$packet{"packet.processes.cpu"}</cpu> |
216 |
</processes> |
217 |
<cpu> |
218 |
<idle>$packet{"packet.cpu.idle"}</idle> |
219 |
<user>$packet{"packet.cpu.user"}</user> |
220 |
<kernel>$packet{"packet.cpu.kernel"}</kernel> |
221 |
<iowait>$packet{"packet.cpu.iowait"}</iowait> |
222 |
<swap>$packet{"packet.cpu.swap"}</swap> |
223 |
</cpu> |
224 |
<memory> |
225 |
<total>$packet{"packet.memory.real"}</total> |
226 |
<free>$packet{"packet.memory.free"}</free> |
227 |
</memory> |
228 |
<swap> |
229 |
<total>$packet{"packet.memory.swap_total"}</total> |
230 |
<free>$packet{"packet.memory.swap_free"}</free> |
231 |
</swap> |
232 |
$disk_info |
233 |
</packet> |
234 |
|
235 |
EOF |
236 |
|
237 |
$xml =~ s/\n\s*//g; |
238 |
|
239 |
my($sock) = new IO::Socket::INET ( |
240 |
PeerPort => $udp_port, |
241 |
PeerAddr => $filter_addr, |
242 |
Proto => 'udp' |
243 |
) or die "Socket: $!\n"; |
244 |
|
245 |
print $sock $xml or die "Could not send UDP packet: $!\n"; |
246 |
close($sock); |
247 |
$seq_no++; |
248 |
print "-"; |
249 |
} |
250 |
|
251 |
sub send_tcp_heartbeat() { |
252 |
|
253 |
my($sock) = new IO::Socket::INET( |
254 |
PeerAddr => $filter_addr, |
255 |
PeerPort => $tcp_port, |
256 |
Proto => 'tcp' |
257 |
) or die "Could not perform heartbeat via TCP: $!\n"; |
258 |
|
259 |
die "Could not connect to the i-scream filter: $!\n" unless $sock; |
260 |
|
261 |
# Now run through the configuration process. |
262 |
my($response); |
263 |
|
264 |
print $sock "HEARTBEAT\n"; |
265 |
$response = <$sock>; |
266 |
chop $response; |
267 |
if (!$response eq "OK") { |
268 |
close($sock); |
269 |
print "Server gave wrong response to HEARTBEAT: $response\n"; |
270 |
return; |
271 |
} |
272 |
|
273 |
print $sock "CONFIG\n"; |
274 |
$response = <$sock>; |
275 |
chop $response; |
276 |
if (!$response eq "OK") { |
277 |
close($sock); |
278 |
print "Server gave wrong response to CONFIG: $response\n"; |
279 |
return; |
280 |
} |
281 |
|
282 |
print $sock "$file_list\n"; |
283 |
$response = <$sock>; |
284 |
chop $response; |
285 |
if (!$response eq "OK") { |
286 |
close($sock); |
287 |
print "Server gave wrong response to file list: $response\n"; |
288 |
return; |
289 |
} |
290 |
|
291 |
print $sock "$last_modified\n"; |
292 |
$response = <$sock>; |
293 |
chop $response; |
294 |
if ($response eq "ERROR") { |
295 |
close($sock); |
296 |
&tcp_configure(); |
297 |
return; |
298 |
} |
299 |
if (!$response eq "OK") { |
300 |
close($sock); |
301 |
print "Server gave wrong response to HEARTBEAT: $response\n"; |
302 |
return; |
303 |
} |
304 |
|
305 |
print $sock "ENDHEARTBEAT\n"; |
306 |
$response = <$sock>; |
307 |
chop $response; |
308 |
if (!$response eq "OK") { |
309 |
close($sock); |
310 |
print "Server gave wrong response to ENDHEARTBEAT: $response\n"; |
311 |
return; |
312 |
} |
313 |
|
314 |
close($sock); |
315 |
print "^"; |
316 |
} |