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.8 by pjm2, Mon Jan 29 09:19:16 2001 UTC vs.
Revision 1.24 by pjm2, Tue Mar 6 08:38:42 2001 UTC

# Line 2 | Line 2
2  
3   # -----------------------------------------------------------
4   # Perl i-scream Host.
5 + # http://www.i-scream.org.uk
6   #
7   # An all-in-one script to act as an i-scream host on
8   # a typical Unix/Linux box.  You may adapt the data-gathering
# Line 31 | Line 32 | use vars qw (
32               $tcp_port
33               $filter_addr
34               $file_list
35 +             $fqdn
36              );
37  
38   if (@ARGV != 2) {
# Line 48 | Line 50 | $seq_no = 1;
50   $last_udp_time = time;
51   $last_tcp_time = time;
52   while (1) {
53 <    if (time >= $last_udp_time + $udp_update_time) {
53 >    my($time) = time;
54 >    if ($time >= $last_udp_time + $udp_update_time) {
55          &send_udp_packet();
56 <        $last_udp_time = time;
56 >        $last_udp_time = $time;
57      }
58 <    if (time >= $last_tcp_time + $tcp_update_time) {
58 >    if ($time >= $last_tcp_time + $tcp_update_time) {
59          &send_tcp_heartbeat();
60 <        $last_tcp_time = time;
60 >        $last_tcp_time = $time;
61      }
62 <    `sleep 1`;
62 >    my($next_udp) = $udp_update_time - $time + $last_udp_time;
63 >    my($next_tcp) = $tcp_update_time - $time + $last_tcp_time;
64 >    my($delay);
65 >    if ($next_udp < $next_tcp) {
66 >        $delay = $next_udp
67 >    }
68 >    else {
69 >        $delay = $next_tcp;
70 >    }
71 >    `sleep $delay`;
72   }
73  
74   exit(0);
75  
76 +
77 + #-----------------------------------------------------------------------
78 + # tcp_configure
79 + # Establishes a TCP connection to the specified i-scream filter manager.
80 + # The host then requests details from the server, such as the intervals
81 + # at which to send UDP packets.
82 + #-----------------------------------------------------------------------
83   sub tcp_configure() {
84      
85      my($sock) = new IO::Socket::INET(
86                                       PeerAddr => $filter_manager_addr,
87                                       PeerPort => $filter_manager_port,
88                                       Proto => 'tcp'
89 <                                    ) or die "Could not perform configuration via TCP: $!\n";
89 >                                    ) or die "Cannot connect!";
90 >    if (!defined $sock) {
91 >        print "IHOST ERROR: Could not connect to $filter_manager_addr:$filter_manager_port.\n";
92 >        print "Please check that there is an i-scream server at this address.\n";
93 >        print "Program ended.\n";
94 >        exit(1);
95 >    }
96  
97 <    die "Could not connect to the i-scream filter manager: $!\n" unless $sock;
73 <
74 <    # Now run through the configuration process.
97 >    # Now run through the configuration process...
98      my($response);
99      
100      print $sock "STARTCONFIG\n";
# Line 97 | Line 120 | sub tcp_configure() {
120  
121      print "File list obtained: $file_list\n";
122  
123 +    print $sock "FQDN\n";
124 +    $response = <$sock>;
125 +    chop $response;
126 +    $fqdn = $response;
127 +
128 +    print "FQDN returned: $fqdn\n";
129 +
130      print $sock "UDPUpdateTime\n";
131      $response = <$sock>;
132      chop $response;
# Line 121 | Line 151 | sub tcp_configure() {
151      
152      print $sock "FILTER\n";
153      $response = <$sock>;
154 +    unless (defined($response)) {
155 +        print "Failed: Could not get a filter address from the filter manager.\n";
156 +        exit(1);
157 +    }
158      chop $response;
159      $response =~ /(.*);(.*);(.*)/;
160      ($filter_addr, $udp_port, $tcp_port) = ($1, $2, $3);
161 +    unless (defined($filter_addr) && defined($udp_port) && defined($tcp_port)) {
162 +        print "Failed: Filter address response from server did not make sense: $response\n";
163 +        exit(1);
164 +    }
165      
166      print "Got filter data ($filter_addr, $udp_port, $tcp_port)\n";
167  
# Line 141 | Line 179 | sub tcp_configure() {
179      close($sock);
180  
181      print "Configuration finished sucessfully!\n";
182 +    
183 +    return;
184   }
185  
186 +
187 +
188 +
189 + #-----------------------------------------------------------------------
190 + # send_udp_packet
191 + # Sends a UDP packet to an i-scream filter.
192 + # The packet contains XML markup describing some of the machine's state.
193 + # Receipt of UDP packets is not guaranteed.
194 + #-----------------------------------------------------------------------
195   sub send_udp_packet() {
196  
197      my(@statgrab) = `./statgrab.pl`;
198      my(%packet);
199 <    for (my($i) = 0; $i < $#statgrab; $i++) {
199 >    for (my($i) = 0; $i <= $#statgrab; $i++) {
200          $statgrab[$i] =~ /^([^\s]*) (.*)$/;
201          $packet{$1} = $2;
202      }
# Line 157 | Line 206 | sub send_udp_packet() {
206      my($disk_info) = "<disk>";
207      my($i) = 0;
208      while (defined $packet{"packet.disk.p$i.attributes.mount"}) {
209 <        $disk_info .= "<p$i>";
210 <        $disk_info .= qq/<name>$packet{"packet.disk.p$i.attributes.name"}<\/name>/;
211 <        $disk_info .= qq/<kbytes>$packet{"packet.disk.p$i.attributes.kbytes"}<\/kbytes>/;
212 <        $disk_info .= qq/<used>$packet{"packet.disk.p$i.attributes.used"}<\/used>/;
213 <        $disk_info .= qq/<avail>$packet{"packet.disk.p$i.attributes.avail"}<\/avail>/;
214 <        $disk_info .= qq/<mount>$packet{"packet.disk.p$i.attributes.mount"}<\/mount>/;
215 <        $disk_info .= "</p$i>";
209 >        $disk_info .= "<p$i";
210 >        $disk_info .= " name=\"" . $packet{"packet.disk.p$i.attributes.name"} . "\"";
211 >        $disk_info .= " kbytes=\"" . $packet{"packet.disk.p$i.attributes.kbytes"} . "\"";
212 >        $disk_info .= " used=\"" . $packet{"packet.disk.p$i.attributes.used"} . "\"";
213 >        $disk_info .= " avail=\"" . $packet{"packet.disk.p$i.attributes.avail"} . "\"";
214 >        $disk_info .= " mount=\"" . $packet{"packet.disk.p$i.attributes.mount"} . "\"";
215 >        $disk_info .= "></p$i>";
216          ++$i;
217      }
218      $disk_info .= "</disk>";
219  
220 <    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');
220 >    my($ip) = inet_ntoa(scalar(gethostbyname(hostname())) || 'localhost');
221  
222      # Build the XML packet this way, as we can clearly
223      # see the structure and contents... I like this ;-)
224 +    # [Note that the server rejects UDP packets that are
225 +    # larger than 8196 bytes]
226      my($xml) = <<EOF;
227      
228 < <packet seq_no="$seq_no" machine_name="$machine_name" date="$date" type="data" ip="$ip">
228 > <packet seq_no="$seq_no" machine_name="$fqdn" date="$date" type="data" ip="$ip">
229      <load>
230          <load1>$packet{"packet.load.load1"}</load1>
231          <load5>$packet{"packet.load.load5"}</load5>
# Line 191 | Line 237 | sub send_udp_packet() {
237          <platform>$packet{"packet.os.platform"}</platform>
238          <sysname>$packet{"packet.os.sysname"}</sysname>
239          <version>$packet{"packet.os.version"}</version>
240 +        <uptime>$packet{"packet.os.uptime"}</uptime>
241      </os>
242      <users>
243          <count>$packet{"packet.users.count"}</count>
# Line 211 | Line 258 | sub send_udp_packet() {
258          <swap>$packet{"packet.cpu.swap"}</swap>
259      </cpu>
260      <memory>
261 <        <total>$packet{"packet.memory.real"}</total>
261 >        <total>$packet{"packet.memory.total"}</total>
262          <free>$packet{"packet.memory.free"}</free>
263      </memory>
264      <swap>
265 <        <total>$packet{"packet.memory.swap_total"}</total>
266 <        <free>$packet{"packet.memory.swap_free"}</free>
265 >        <total>$packet{"packet.swap.total"}</total>
266 >        <free>$packet{"packet.swap.free"}</free>
267      </swap>
268      $disk_info
269   </packet>
270  
271   EOF
272  
273 +    # Make the packet smaller by stripping out newlines and leading spaces.
274      $xml =~ s/\n\s*//g;
275 <    
275 >
276      my($sock) = new IO::Socket::INET (
277                                        PeerPort => $udp_port,
278                                        PeerAddr => $filter_addr,
279                                        Proto => 'udp'
280 <                                     ) or die "Socket: $!\n";
280 >                                     ) or die "Could not send UDP: $!\n";
281  
282      print $sock $xml or die "Could not send UDP packet: $!\n";
283      close($sock);
284      $seq_no++;
285      print "-";
286 +    
287 +    return;
288   }
289  
290 +
291 +
292 +
293 + #-----------------------------------------------------------------------
294 + # send_tcp_heartbeat
295 + # Establishes a TCP connection to an i-scream filter.
296 + # The heartbeat is used as a guaranteed "I'm alive" delivery mechanism.
297 + #-----------------------------------------------------------------------
298   sub send_tcp_heartbeat() {
299  
300      my($sock) = new IO::Socket::INET(
301                                       PeerAddr => $filter_addr,
302                                       PeerPort => $tcp_port,
303                                       Proto => 'tcp'
304 <                                    ) or die "Could not perform heartbeat via TCP: $!\n";
304 >                                    ) or return;
305 >    if (!defined $sock) {
306 >        print "IHOST WARNING: Failed to deliver a heartbeat to the i-scream filter.\n";
307 >        return;
308 >    }
309  
248    die "Could not connect to the i-scream filter: $!\n" unless $sock;
249
310      # Now run through the configuration process.
311      my($response);
312  
313      print $sock "HEARTBEAT\n";
314      $response = <$sock>;
315 +    return if (!defined $response);
316      chop $response;
317      if (!$response eq "OK") {
318          close($sock);
# Line 261 | Line 322 | sub send_tcp_heartbeat() {
322      
323      print $sock "CONFIG\n";
324      $response = <$sock>;
325 +    return if (!defined $response);
326      chop $response;
327      if (!$response eq "OK") {
328          close($sock);
# Line 270 | Line 332 | sub send_tcp_heartbeat() {
332  
333      print $sock "$file_list\n";
334      $response = <$sock>;
335 +    return if (!defined $response);
336      chop $response;
337      if (!$response eq "OK") {
338          close($sock);
# Line 279 | Line 342 | sub send_tcp_heartbeat() {
342      
343      print $sock "$last_modified\n";
344      $response = <$sock>;
345 +    return if (!defined $response);
346      chop $response;
347      if ($response eq "ERROR") {
348          close($sock);
# Line 293 | Line 357 | sub send_tcp_heartbeat() {
357      
358      print $sock "ENDHEARTBEAT\n";
359      $response = <$sock>;
360 +    return if (!defined $response);
361      chop $response;
362      if (!$response eq "OK") {
363          close($sock);
# Line 302 | Line 367 | sub send_tcp_heartbeat() {
367      
368      close($sock);
369      print "^";
370 +    
371 +    return;
372   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines