ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/host/ihost-perl/ihost.pl
(Generate patch)

Comparing projects/cms/source/host/ihost-perl/ihost.pl (file contents):
Revision 1.31 by pjm2, Thu Mar 15 09:46:18 2001 UTC vs.
Revision 1.49 by tdb, Sat May 18 18:15:56 2002 UTC

# Line 1 | Line 1
1   #!/usr/bin/perl -w
2  
3 + #
4 + # i-scream central monitoring system
5 + # Copyright (C) 2000-2002 i-scream
6 + #
7 + # This program is free software; you can redistribute it and/or
8 + # modify it under the terms of the GNU General Public License
9 + # as published by the Free Software Foundation; either version 2
10 + # of the License, or (at your option) any later version.
11 + #
12 + # This program is distributed in the hope that it will be useful,
13 + # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 + # GNU General Public License for more details.
16 + #
17 + # You should have received a copy of the GNU General Public License
18 + # along with this program; if not, write to the Free Software
19 + # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 + #
21 +
22   # -----------------------------------------------------------
23   # Perl i-scream Host.
24   # http://www.i-scream.org.uk
25   #
26   # An all-in-one script to act as an i-scream host on
27 < # a typical Unix/Linux box.  You may adapt the data-gathering
9 < # methods as you see fit.
10 < #  - pjm2@ukc.ac.uk
27 > # a typical Unix/Linux box.
28   #
29   # $Author$
30   # $Id$
# Line 35 | Line 52 | use vars qw (
52               $fqdn
53               $pidfile
54               $retry_wait
55 +             $ostype
56 +             $key
57 +             @data
58              );
59  
60   if (@ARGV != 2) {
# Line 45 | Line 65 | $filter_manager_addr = $ARGV[0];
65   $filter_manager_port = $ARGV[1];
66  
67   $seq_no = 1;
68 < $retry_wait = 600;
68 > $retry_wait = 60;
69  
70 + # work out our platform, if we can.
71 + $ostype = `uname -s`;
72 + chomp $ostype;
73 + $ostype = "unknown" if not defined $ostype;
74 +
75   # write our PID to a file
76 < $pidfile = "/var/tmp/ihost.pid";
76 > # use home dir by default
77 > #$pidfile = $ENV{"HOME"};
78 > # or drop it in /var/tmp if we can't find HOME
79 > $pidfile = "/var/tmp" if not defined $pidfile;
80 > $pidfile .= "/.ihost.pid";
81   &write_pid();
82  
83   &tcp_configure();
84 + &send_tcp_heartbeat();
85   &send_udp_packet();
86  
87   $last_udp_time = time;
# Line 75 | Line 105 | while (1) {
105      else {
106          $delay = $next_tcp;
107      }
108 <    `sleep $delay`;
108 >    sleep $delay;
109   }
110  
111   # we'll probably never get here...
# Line 90 | Line 120 | exit(0);
120   #-----------------------------------------------------------------------
121   sub wait_then_retry() {
122      print "Will retry configuration with filter manager in $retry_wait seconds.\n";
123 <    `sleep $retry_wait`;
123 >    sleep $retry_wait;
124   }
125  
126  
# Line 120 | Line 150 | sub tcp_configure() {
150      
151          print $sock "STARTCONFIG\n";
152          $response = <$sock>;
153 <        if (!chop $response eq "OK") {
154 <            print "The i-scream server rejected the STARTCONFIG command.  Terminated.";
153 >        if ($response && !($response eq "OK\n")) {
154 >            print "The i-scream server rejected the STARTCONFIG command.\n";
155              close($sock);
156              wait_then_retry();
157              next;
# Line 131 | Line 161 | sub tcp_configure() {
161      
162          print $sock "LASTMODIFIED\n";
163          $response = <$sock>;
164 <        chop $response;
164 >        if (!$response || $response eq "ERROR\n") {
165 >            print "The i-scream server did not provide the LASTMODIFIED value.\n";
166 >            close($sock);
167 >            wait_then_retry();
168 >            next;
169 >        }
170 >        chomp $response;
171          $last_modified = $response;
172      
173          print "Config last modified: ". (scalar localtime $last_modified/1000) . "\n";
174  
175          print $sock "FILELIST\n";
176          $response = <$sock>;
177 <        chop $response;
177 >        if (!$response || $response eq "ERROR\n") {
178 >            print "The i-scream server did not provide a configuration file list.\n";
179 >            close($sock);
180 >            wait_then_retry();
181 >            next;
182 >        }
183 >        chomp $response;
184          $file_list = $response;
185  
186          print "File list obtained: $file_list\n";
187  
188          print $sock "FQDN\n";
189          $response = <$sock>;
190 <        chop $response;
190 >        if (!$response || $response eq "ERROR\n") {
191 >            print "The i-scream server did not tell us our FQDN.\n";
192 >            close($sock);
193 >            wait_then_retry();
194 >            next;
195 >        }
196 >        chomp $response;
197          $fqdn = $response;
198  
199          print "FQDN returned: $fqdn\n";
200  
201          print $sock "UDPUpdateTime\n";
202          $response = <$sock>;
203 <        chop $response;
203 >        if (!$response || $response eq "ERROR\n") {
204 >            print "The i-scream server did not give us a UDPUpdateTime.\n";
205 >            close($sock);
206 >            wait_then_retry();
207 >            next;
208 >        }
209 >        chomp $response;
210          $udp_update_time = $response;
211  
212          print $sock "TCPUpdateTime\n";
213          $response = <$sock>;
214 <        chop $response;
214 >        if (!$response || $response eq "ERROR\n") {
215 >            print "The i-scream server did not give us a TCPUpdateTime.\n";
216 >            close($sock);
217 >            wait_then_retry();
218 >            next;
219 >        }
220 >        chomp $response;
221          $tcp_update_time = $response;
222      
223          print "UDP packet period: $udp_update_time seconds.\nTCP heartbeat period: $tcp_update_time seconds.\n";
224  
225          print $sock "ENDCONFIG\n";
226          $response = <$sock>;
227 <        chomp $response;
168 <        if (!$response eq "OK") {
227 >        if ($response && !($response eq "OK\n")) {
228              print "ENDCONFIG command to server failed.  Terminated.\n";
229              close($sock);
230              wait_then_retry();
# Line 176 | Line 235 | sub tcp_configure() {
235      
236          print $sock "FILTER\n";
237          $response = <$sock>;
238 <        unless (defined($response)) {
238 >        if (!$response) {
239              print "Failed: Could not get a filter address from the filter manager.\n";
240              close($sock);
241              wait_then_retry();
242              next;
243          }
244 <        chop $response;
186 <        $response =~ /(.*);(.*);(.*)/;
244 >        chomp $response;
245          if ($response eq "ERROR") {
246              print "There are no active configured filters for your host.\n";
247              close($sock);
248              wait_then_retry();
249              next;
250          }
251 +        $response =~ /^(.*);(.*);(.*)/;
252          ($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3);
253          unless (defined($filter_addr) && defined($udp_port) && defined($tcp_port)) {
254              print "Failed: Filter address response from server did not make sense: $response\n";
# Line 202 | Line 261 | sub tcp_configure() {
261  
262          print $sock "END\n";
263          $response = <$sock>;
264 <        chop $response;
206 <        if ($response eq "OK") {
264 >        if ($response && ($response eq "OK\n")) {
265              print "Host successfully configured via TCP.\n"
266          }
267          else {
268 <            print "The server failed the host configuration on the END command.";
268 >            print "The server failed the host configuration on the END command.\n";
269              close($sock);
270              wait_then_retry();
271              next;
# Line 222 | Line 280 | sub tcp_configure() {
280   }
281  
282  
225
226
283   #-----------------------------------------------------------------------
284   # send_udp_packet
285   # Sends a UDP packet to an i-scream filter.
# Line 232 | Line 288 | sub tcp_configure() {
288   #-----------------------------------------------------------------------
289   sub send_udp_packet() {
290  
291 <    my(@statgrab) = `./statgrab.pl`;
292 <    my(%packet);
293 <    for (my($i) = 0; $i <= $#statgrab; $i++) {
294 <        $statgrab[$i] =~ /^([^\s]*) (.*)$/;
295 <        $packet{$1} = $2;
291 >    my($plugins_dir) = "plugins";
292 >    
293 >    opendir PLUGINS, $plugins_dir;
294 >    my(@plugins) = readdir PLUGINS;
295 >    foreach my $plugin (@plugins) {
296 >        push @data, `$plugins_dir/$plugin $ostype` if -x "$plugins_dir/$plugin" && -f "$plugins_dir/$plugin";
297      }
298      
299 +    # get some extra data
300      my($date) = time;
301 +    my($ip);
302 +    $ip = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost') or $ip = 'localhost';
303      
304 <    my($disk_info) = "<disk>";
305 <    my($i) = 0;
306 <    while (defined $packet{"packet.disk.p$i.attributes.mount"}) {
307 <        $disk_info .= "<p$i";
308 <        $disk_info .= " name=\"" . $packet{"packet.disk.p$i.attributes.name"} . "\"";
309 <        $disk_info .= " kbytes=\"" . $packet{"packet.disk.p$i.attributes.kbytes"} . "\"";
310 <        $disk_info .= " used=\"" . $packet{"packet.disk.p$i.attributes.used"} . "\"";
251 <        $disk_info .= " avail=\"" . $packet{"packet.disk.p$i.attributes.avail"} . "\"";
252 <        $disk_info .= " mount=\"" . $packet{"packet.disk.p$i.attributes.mount"} . "\"";
253 <        $disk_info .= "></p$i>";
254 <        ++$i;
255 <    }
256 <    $disk_info .= "</disk>";
257 <
258 <    my($ip) = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost');
259 <
260 <    # Build the XML packet this way, as we can clearly
261 <    # see the structure and contents... I like this ;-)
262 <    # [Note that the server rejects UDP packets that are
263 <    # larger than 8196 bytes]
264 <    my($xml) = <<EOF;
304 >    # add some extra data to the array
305 >    push(@data, "packet.attributes.seq_no $seq_no");
306 >    push(@data, "packet.attributes.machine_name $fqdn");
307 >    push(@data, "packet.attributes.date $date");
308 >    push(@data, "packet.attributes.type data");
309 >    push(@data, "packet.attributes.ip $ip");
310 >    push(@data, "packet.attributes.key $key");
311      
312 < <packet seq_no="$seq_no" machine_name="$fqdn" date="$date" type="data" ip="$ip">
313 <    <load>
268 <        <load1>$packet{"packet.load.load1"}</load1>
269 <        <load5>$packet{"packet.load.load5"}</load5>
270 <        <load15>$packet{"packet.load.load15"}</load15>
271 <    </load>
272 <    <os>
273 <        <name>$packet{"packet.os.name"}</name>
274 <        <release>$packet{"packet.os.release"}</release>
275 <        <platform>$packet{"packet.os.platform"}</platform>
276 <        <sysname>$packet{"packet.os.sysname"}</sysname>
277 <        <version>$packet{"packet.os.version"}</version>
278 <        <uptime>$packet{"packet.os.uptime"}</uptime>
279 <    </os>
280 <    <users>
281 <        <count>$packet{"packet.users.count"}</count>
282 <        <list>$packet{"packet.users.list"}</list>
283 <    </users>
284 <    <processes>
285 <        <total>$packet{"packet.processes.total"}</total>
286 <        <sleeping>$packet{"packet.processes.sleeping"}</sleeping>
287 <        <zombie>$packet{"packet.processes.zombie"}</zombie>
288 <        <stopped>$packet{"packet.processes.stopped"}</stopped>
289 <        <cpu>$packet{"packet.processes.cpu"}</cpu>
290 <    </processes>
291 <    <cpu>
292 <        <idle>$packet{"packet.cpu.idle"}</idle>
293 <        <user>$packet{"packet.cpu.user"}</user>
294 <        <kernel>$packet{"packet.cpu.kernel"}</kernel>
295 <        <iowait>$packet{"packet.cpu.iowait"}</iowait>
296 <        <swap>$packet{"packet.cpu.swap"}</swap>
297 <    </cpu>
298 <    <memory>
299 <        <total>$packet{"packet.memory.total"}</total>
300 <        <free>$packet{"packet.memory.free"}</free>
301 <    </memory>
302 <    <swap>
303 <        <total>$packet{"packet.swap.total"}</total>
304 <        <free>$packet{"packet.swap.free"}</free>
305 <    </swap>
306 <    $disk_info
307 < </packet>
312 >    # sort the data
313 >    @data = sort(grep(!/^$/, grep(/^packet\./, @data)));
314  
315 < EOF
315 >    # turn the array into some nice XML
316 >    my($xml) = &make_xml("", "");
317  
311    # Make the packet smaller by stripping out newlines and leading spaces.
312    $xml =~ s/\n\s*//g;
313
318      my($sock) = new IO::Socket::INET (
319                                        PeerPort => $udp_port,
320                                        PeerAddr => $filter_addr,
321                                        Proto => 'udp'
322                                       ) or die "Could not send UDP: $!\n";
323 <
323 >    
324      print $sock $xml or die "Could not send UDP packet: $!\n";
325      close($sock);
326      $seq_no++;
# Line 326 | Line 330 | EOF
330   }
331  
332  
329
330
333   #-----------------------------------------------------------------------
334   # send_tcp_heartbeat
335   # Establishes a TCP connection to an i-scream filter.
336   # The heartbeat is used as a guaranteed "I'm alive" delivery mechanism.
337 + # If we need to reconfigure, then we complete the heartbeat before
338 + # doing so.
339   #-----------------------------------------------------------------------
340   sub send_tcp_heartbeat() {
341  
342 +    my ($doReconfigure) = 0;
343 +
344      my($sock) = new IO::Socket::INET(
345                                       PeerAddr => $filter_addr,
346                                       PeerPort => $tcp_port,
# Line 351 | Line 357 | sub send_tcp_heartbeat() {
357  
358      print $sock "HEARTBEAT\n";
359      $response = <$sock>;
360 <    return if (!defined $response);
355 <    chop $response;
356 <    if (!$response eq "OK") {
360 >    if (!$response eq "OK\n") {
361          close($sock);
362          print "Server gave wrong response to HEARTBEAT: $response\n";
363          &tcp_configure();
# Line 362 | Line 366 | sub send_tcp_heartbeat() {
366      
367      print $sock "CONFIG\n";
368      $response = <$sock>;
369 <    return if (!defined $response);
366 <    chop $response;
367 <    if (!$response eq "OK") {
369 >    if (!$response eq "OK\n") {
370          close($sock);
371          print "Server gave wrong response to CONFIG: $response\n";
372          &tcp_configure();
# Line 373 | Line 375 | sub send_tcp_heartbeat() {
375  
376      print $sock "$file_list\n";
377      $response = <$sock>;
378 <    return if (!defined $response);
377 <    chop $response;
378 <    if (!$response eq "OK") {
378 >    if (!$response eq "OK\n") {
379          close($sock);
380          print "Server gave wrong response to file list: $response\n";
381          &tcp_configure();
# Line 384 | Line 384 | sub send_tcp_heartbeat() {
384      
385      print $sock "$last_modified\n";
386      $response = <$sock>;
387 <    return if (!defined $response);
388 <    chop $response;
389 <    if ($response eq "ERROR") {
387 >    if ($response eq "ERROR\n") {
388          close($sock);
389          print "Server configuration changed.  Reconfiguring with filter manager.\n";
390 <        &tcp_configure();
393 <        return;
390 >        $doReconfigure = 1;
391      }
392 <    if (!$response eq "OK") {
392 >    if (!$response eq "OK\n") {
393          close($sock);
394          print "Server gave wrong response to HEARTBEAT: $response\n";
395          &tcp_configure();
396          return;
397      }
398      
399 +    print $sock "KEY\n";
400 +    $key = <$sock>;
401 +    
402      print $sock "ENDHEARTBEAT\n";
403      $response = <$sock>;
404 <    return if (!defined $response);
405 <    chop $response;
406 <    if (!$response eq "OK") {
404 >    if (!$response eq "OK\n") {
405          close($sock);
406          print "Server gave wrong response to ENDHEARTBEAT: $response\n";
407          &tcp_configure();
# Line 413 | Line 411 | sub send_tcp_heartbeat() {
411      close($sock);
412      print "^";
413      
414 +    &tcp_configure() if $doReconfigure;
415 +    
416      return;
417   }
418  
419 +
420   #-----------------------------------------------------------------------
421   # write_pid
422   # Writes the PID (process ID) of this instance to $pidfile.
# Line 427 | Line 428 | sub write_pid() {
428      close PID;
429      
430      return;
431 + }
432 +
433 + #-----------------------------------------------------------------------
434 + # make_xml
435 + # Turns an array of plugins data into an XML string.
436 + #-----------------------------------------------------------------------
437 + sub make_xml() {
438 +    my($curlevel, $curline) = @_;
439 +    my($xmltemp) = ""; my($curtag) = ""; my($attributes) = "";
440 +    while(1) {
441 +        $curline = shift(@data) if $curline eq "";
442 +        return $xmltemp if not defined $curline;
443 +        chomp $curline;
444 +        # dealing with nest (or attributes)
445 +        if($curline =~ /^$curlevel([^\.\s]+\.)/) {
446 +            $curtag=$1;
447 +            if($curline =~ /^$curlevel$curtag([^\.\s]+)\s+(.*)$/) {
448 +                $xmltemp .= &make_xml("$curlevel$curtag", $curline);
449 +            }
450 +            elsif($curline =~ /^$curlevel$curtag(attributes)\.([^\.\s]+)\s+(.*)$/) {
451 +                $attributes .= " $2=\"$3\"";
452 +            }
453 +            else {
454 +                $xmltemp .= &make_xml("$curlevel$curtag", $curline);
455 +            }
456 +            my($nextline) = $data[0]; chomp $nextline if defined $nextline;
457 +            $curtag =~ s/(.*)\./$1/;
458 +            if((defined $nextline) && ($nextline =~ /^$curlevel$curtag\./)) {
459 +                $curline = "";
460 +            }
461 +            else {
462 +                $xmltemp = "<$curtag$attributes>$xmltemp</$curtag>" unless $curtag eq "";
463 +                return $xmltemp;
464 +            }
465 +        }
466 +        # dealing with value
467 +        elsif($curline =~ /^$curlevel([^\.\s]+)\s+(.*)$/) {
468 +            $curtag=$1;
469 +            $xmltemp=$2;          
470 +            my($nextline) = $data[0]; chomp $nextline if defined $nextline;
471 +            if(defined $nextline && ($nextline =~ /^$curlevel$curtag\./ || $nextline =~ /^$curlevel$curtag\s+/)) {
472 +                $curline = "";
473 +            }
474 +            else {
475 +                $xmltemp = "<$curtag$attributes>$xmltemp</$curtag>" unless $curtag eq "";
476 +                return $xmltemp;
477 +            }
478 +        }
479 +        # dealing with a null value
480 +        elsif($curline =~ /^$curlevel([^\.\s]+)$/) {
481 +            # simply adding a space makes the above elsif deal with it :)
482 +            # just level with an empty tag in the XML
483 +            $curline .= " ";
484 +        }
485 +        # failing all that, skip the line
486 +        else {
487 +            $curline = "";
488 +        }
489 +    }
490   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines