ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/ihost.pl
Revision: 1.4
Committed: Fri Jan 26 17:38:50 2001 UTC (23 years, 10 months ago) by pjm2
Content type: text/plain
Branch: MAIN
Changes since 1.3: +2 -2 lines
Log Message:
And again...

File Contents

# User Rev Content
1 pjm2 1.1 #!/usr/local/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 pjm2 1.2 # $Author: pjm2 $
12 pjm2 1.4 # $Id: ihost.pl,v 1.3 2001/01/26 17:32:32 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     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 . "\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.\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 pjm2 1.4 $hostname =~ s/\..*$//g;
173 pjm2 1.3 print $hostname;
174 pjm2 1.1 my($domainname) = `cat /etc/resolv.conf`;
175     $domainname =~ s/domain\s*([^\s]*?)/$1/;
176     my($machine_name) = "$hostname.$domainname";
177     my($ip) = inet_ntoa(scalar(gethostbyname($hostname)) || 'localhost');
178    
179     # Build the XML packet this way, as we can clearly
180     # see the structure and contents... I like this ;-)
181     my($xml) = <<EOF;
182    
183     <packet seq_no="$seq_no" machine_name="$machine_name" date="$date" type="data" ip="$ip">
184     <load>
185     <load1>$packet{"packet.load.load1"}</load1>
186     <load5>$packet{"packet.load.load5"}</load5>
187     <load15>$packet{"packet.load.load15"}</load15>
188     </load>
189     <os>
190     <name>$packet{"packet.os.name"}</name>
191     <release>$packet{"packet.os.release"}</release>
192     <platform>$packet{"packet.os.platform"}</platform>
193     <sysname>$packet{"packet.os.sysname"}</sysname>
194     <version>$packet{"packet.os.version"}</version>
195     </os>
196     <users>
197     <count>$packet{"packet.users.count"}</count>
198     <list>$packet{"packet.users.list"}</list>
199     </users>
200     <processes>
201     <total>$packet{"packet.processes.total"}</total>
202     <sleeping>$packet{"packet.processes.sleeping"}</sleeping>
203     <zombie>$packet{"packet.processes.zombie"}</zombie>
204     <stopped>$packet{"packet.processes.stopped"}</stopped>
205     <cpu>$packet{"packet.processes.cpu"}</cpu>
206     </processes>
207     <cpu>
208     <idle>$packet{"packet.cpu.idle"}</idle>
209     <user>$packet{"packet.cpu.user"}</user>
210     <kernel>$packet{"packet.cpu.kernel"}</kernel>
211     <iowait>$packet{"packet.cpu.iowait"}</iowait>
212     <swap>$packet{"packet.cpu.swap"}</swap>
213     </cpu>
214     <memory>
215     <total>$packet{"packet.memory.real"}</total>
216     <free>$packet{"packet.memory.free"}</free>
217     </memory>
218     <swap>
219     <total>$packet{"packet.memory.swap_total"}</total>
220     <free>$packet{"packet.memory.swap_free"}</free>
221     </swap>
222     $disk_info
223     </packet>
224    
225     EOF
226    
227     $xml =~ s/\n\s*//g;
228    
229     my($sock) = new IO::Socket::INET (
230     PeerPort => $udp_port,
231     PeerAddr => $filter_addr,
232     Proto => 'udp'
233     ) or die "Socket: $!\n";
234    
235     print $sock $xml or die "Could not send UDP packet: $!\n";
236     close($sock);
237     $seq_no++;
238     print "-";
239     }
240    
241     sub send_tcp_heartbeat() {
242    
243     my($sock) = new IO::Socket::INET(
244     PeerAddr => $filter_addr,
245     PeerPort => $tcp_port,
246     Proto => 'tcp'
247     ) or die "Could not perform heartbeat via TCP: $!\n";
248    
249     die "Could not connect to the i-scream filter: $!\n" unless $sock;
250    
251     # Now run through the configuration process.
252     my($response);
253    
254     print $sock "HEARTBEAT\n";
255     $response = <$sock>;
256     chop $response;
257     if (!$response eq "OK") {
258     close($sock);
259     print "Server gave wrong response to HEARTBEAT: $response\n";
260     return;
261     }
262    
263     print $sock "CONFIG\n";
264     $response = <$sock>;
265     chop $response;
266     if (!$response eq "OK") {
267     close($sock);
268     print "Server gave wrong response to CONFIG: $response\n";
269     return;
270     }
271    
272     print $sock "$file_list\n";
273     $response = <$sock>;
274     chop $response;
275     if (!$response eq "OK") {
276     close($sock);
277     print "Server gave wrong response to file list: $response\n";
278     return;
279     }
280    
281     print $sock "$last_modified\n";
282     $response = <$sock>;
283     chop $response;
284     if ($response eq "ERROR") {
285     close($sock);
286     &tcp_configure();
287     return;
288     }
289     if (!$response eq "OK") {
290     close($sock);
291     print "Server gave wrong response to HEARTBEAT: $response\n";
292     return;
293     }
294    
295     print $sock "ENDHEARTBEAT\n";
296     $response = <$sock>;
297     chop $response;
298     if (!$response eq "OK") {
299     close($sock);
300     print "Server gave wrong response to ENDHEARTBEAT: $response\n";
301     return;
302     }
303    
304     close($sock);
305     print "^";
306     }