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