1 |
pjm2 |
1.6 |
#!/usr/bin/perl -w |
2 |
pjm2 |
1.1 |
|
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 |
pjm2 |
1.2 |
# $Author: pjm2 $ |
12 |
pjm2 |
1.9 |
# $Id: ihost.pl,v 1.8 2001/01/29 09:19:16 pjm2 Exp $ |
13 |
pjm2 |
1.1 |
#------------------------------------------------------------ |
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 |
pjm2 |
1.9 |
my($time) = time; |
52 |
|
|
if ($time >= $last_udp_time + $udp_update_time) { |
53 |
pjm2 |
1.1 |
&send_udp_packet(); |
54 |
pjm2 |
1.9 |
$last_udp_time = $time; |
55 |
pjm2 |
1.1 |
} |
56 |
pjm2 |
1.9 |
if ($time >= $last_tcp_time + $tcp_update_time) { |
57 |
pjm2 |
1.1 |
&send_tcp_heartbeat(); |
58 |
pjm2 |
1.9 |
$last_tcp_time = $time; |
59 |
pjm2 |
1.1 |
} |
60 |
pjm2 |
1.9 |
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 |
pjm2 |
1.1 |
} |
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 |
pjm2 |
1.7 |
print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n"; |
102 |
pjm2 |
1.1 |
|
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 |
pjm2 |
1.7 |
print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n"; |
121 |
pjm2 |
1.1 |
|
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 |
pjm2 |
1.4 |
$hostname =~ s/\..*$//g; |
183 |
pjm2 |
1.8 |
`cat /etc/resolv.conf` =~ /domain\s+([^\s]+)/; |
184 |
|
|
my($domainname) = $1; |
185 |
pjm2 |
1.1 |
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 |
|
|
</os> |
205 |
|
|
<users> |
206 |
|
|
<count>$packet{"packet.users.count"}</count> |
207 |
|
|
<list>$packet{"packet.users.list"}</list> |
208 |
|
|
</users> |
209 |
|
|
<processes> |
210 |
|
|
<total>$packet{"packet.processes.total"}</total> |
211 |
|
|
<sleeping>$packet{"packet.processes.sleeping"}</sleeping> |
212 |
|
|
<zombie>$packet{"packet.processes.zombie"}</zombie> |
213 |
|
|
<stopped>$packet{"packet.processes.stopped"}</stopped> |
214 |
|
|
<cpu>$packet{"packet.processes.cpu"}</cpu> |
215 |
|
|
</processes> |
216 |
|
|
<cpu> |
217 |
|
|
<idle>$packet{"packet.cpu.idle"}</idle> |
218 |
|
|
<user>$packet{"packet.cpu.user"}</user> |
219 |
|
|
<kernel>$packet{"packet.cpu.kernel"}</kernel> |
220 |
|
|
<iowait>$packet{"packet.cpu.iowait"}</iowait> |
221 |
|
|
<swap>$packet{"packet.cpu.swap"}</swap> |
222 |
|
|
</cpu> |
223 |
|
|
<memory> |
224 |
|
|
<total>$packet{"packet.memory.real"}</total> |
225 |
|
|
<free>$packet{"packet.memory.free"}</free> |
226 |
|
|
</memory> |
227 |
|
|
<swap> |
228 |
|
|
<total>$packet{"packet.memory.swap_total"}</total> |
229 |
|
|
<free>$packet{"packet.memory.swap_free"}</free> |
230 |
|
|
</swap> |
231 |
|
|
$disk_info |
232 |
|
|
</packet> |
233 |
|
|
|
234 |
|
|
EOF |
235 |
|
|
|
236 |
|
|
$xml =~ s/\n\s*//g; |
237 |
|
|
|
238 |
|
|
my($sock) = new IO::Socket::INET ( |
239 |
|
|
PeerPort => $udp_port, |
240 |
|
|
PeerAddr => $filter_addr, |
241 |
|
|
Proto => 'udp' |
242 |
|
|
) or die "Socket: $!\n"; |
243 |
|
|
|
244 |
|
|
print $sock $xml or die "Could not send UDP packet: $!\n"; |
245 |
|
|
close($sock); |
246 |
|
|
$seq_no++; |
247 |
|
|
print "-"; |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
sub send_tcp_heartbeat() { |
251 |
|
|
|
252 |
|
|
my($sock) = new IO::Socket::INET( |
253 |
|
|
PeerAddr => $filter_addr, |
254 |
|
|
PeerPort => $tcp_port, |
255 |
|
|
Proto => 'tcp' |
256 |
|
|
) or die "Could not perform heartbeat via TCP: $!\n"; |
257 |
|
|
|
258 |
|
|
die "Could not connect to the i-scream filter: $!\n" unless $sock; |
259 |
|
|
|
260 |
|
|
# Now run through the configuration process. |
261 |
|
|
my($response); |
262 |
|
|
|
263 |
|
|
print $sock "HEARTBEAT\n"; |
264 |
|
|
$response = <$sock>; |
265 |
|
|
chop $response; |
266 |
|
|
if (!$response eq "OK") { |
267 |
|
|
close($sock); |
268 |
|
|
print "Server gave wrong response to HEARTBEAT: $response\n"; |
269 |
|
|
return; |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
print $sock "CONFIG\n"; |
273 |
|
|
$response = <$sock>; |
274 |
|
|
chop $response; |
275 |
|
|
if (!$response eq "OK") { |
276 |
|
|
close($sock); |
277 |
|
|
print "Server gave wrong response to CONFIG: $response\n"; |
278 |
|
|
return; |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
print $sock "$file_list\n"; |
282 |
|
|
$response = <$sock>; |
283 |
|
|
chop $response; |
284 |
|
|
if (!$response eq "OK") { |
285 |
|
|
close($sock); |
286 |
|
|
print "Server gave wrong response to file list: $response\n"; |
287 |
|
|
return; |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
print $sock "$last_modified\n"; |
291 |
|
|
$response = <$sock>; |
292 |
|
|
chop $response; |
293 |
|
|
if ($response eq "ERROR") { |
294 |
|
|
close($sock); |
295 |
|
|
&tcp_configure(); |
296 |
|
|
return; |
297 |
|
|
} |
298 |
|
|
if (!$response eq "OK") { |
299 |
|
|
close($sock); |
300 |
|
|
print "Server gave wrong response to HEARTBEAT: $response\n"; |
301 |
|
|
return; |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
print $sock "ENDHEARTBEAT\n"; |
305 |
|
|
$response = <$sock>; |
306 |
|
|
chop $response; |
307 |
|
|
if (!$response eq "OK") { |
308 |
|
|
close($sock); |
309 |
|
|
print "Server gave wrong response to ENDHEARTBEAT: $response\n"; |
310 |
|
|
return; |
311 |
|
|
} |
312 |
|
|
|
313 |
|
|
close($sock); |
314 |
|
|
print "^"; |
315 |
|
|
} |