root/honeyclient/tags/exp/UP2-kindlund-filesystem/bin/StartAgent.pl

Revision 304, 2.1 kB (checked in by kindlund, 2 years ago)

Added small delay to watchdog kill part; killed thread needs a little time to relinquish it's hold of a bound TCP port.

  • Property svn:executable set to *
  • Property svn:keywords set to Id "$file"
Line 
1 #!perl -w -Ilib
2
3 # $Id$
4
5 use strict;
6 use warnings;
7 use Carp ();
8
9 use HoneyClient::Agent;
10 use HoneyClient::Util::SOAP qw(getClientHandle);
11 use Data::Dumper;
12 use MIME::Base64 qw(decode_base64 encode_base64);
13 use Storable qw(thaw nfreeze);
14
15 our ($stub, $som);
16 our $URL = HoneyClient::Agent->init();
17
18 our $agentState = undef;
19 my $tempState = undef;
20
21 print "URL: " . $URL. "\n";
22
23 sub _watchdogFaultHandler {
24
25     # Extract arguments.
26     my ($class, $res) = @_;
27
28     # Construct error message.
29     # Figure out if the error occurred in transport or over
30     # on the other side.
31     my $errMsg = $class->transport->status; # Assume transport error.
32
33     if (ref $res) {
34         $errMsg = $res->faultcode . ": ".  $res->faultstring . "\n";
35     }
36
37     print "Watchdog fault detected, recovering Agent daemon.\n";
38     # XXX: Reenable this, eventually.
39     #Carp::carp __PACKAGE__ . "->_watchdogFaultHandler(): Error occurred during processing.\n" . $errMsg;
40
41
42     # Regardless of the error, destroy the Agent process and reinitialize it.
43     # XXX: Sanity check this, eventually.
44     HoneyClient::Agent->destroy();
45
46     # Wait for a small amount of time, in order for the killed process to release
47     # its control of the bound TCP port.
48     sleep 5;
49
50     $URL = HoneyClient::Agent->init();
51
52     # Restore state information.
53     $som = $stub->updateState(encode_base64(nfreeze($agentState)));
54 }
55
56 $stub = getClientHandle(address   => 'localhost',
57                         namespace => 'HoneyClient::Agent',
58                         fault_handler => \&_watchdogFaultHandler);
59                
60 for (;;) {
61     # TODO: Make this a programmatic value.
62     sleep (5);
63     $som = $stub->getState();
64     if (defined($som) and (ref($som) eq "SOAP::SOM")) {
65         $tempState = $som->result();
66         if (defined($tempState)) {
67             # Make sure the new state is parsable, before saving it.
68             eval {
69                 $tempState = thaw(decode_base64($tempState));
70             };
71             if (!$@) {
72                 $agentState = $tempState;
73             }
74         }
75     }
76 }
77
78 HoneyClient::Agent->destroy();
Note: See TracBrowser for help on using the browser.