root/honeyclient/tags/exp/UP1-kindlund-simpler_agent/bin/StartAgent.pl

Revision 1008, 2.7 kB (checked in by kindlund, 1 year ago)

Merging kindlund-dynamic_updates branch back into trunk.

  • 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::Util::Config qw(getVar);
10 use HoneyClient::Agent;
11 use HoneyClient::Util::SOAP qw(getClientHandle);
12 use Data::Dumper;
13 use MIME::Base64 qw(decode_base64 encode_base64);
14 use Storable qw(thaw nfreeze);
15 use Log::Log4perl qw(:easy);
16
17 # The global logging object.
18 our $LOG = get_logger();
19
20 our ($stub, $som);
21 our $URL = HoneyClient::Agent->init();
22
23 our $agentState = undef;
24 my $tempState = undef;
25 our $faultDetected = 0;
26
27 print "URL: " . $URL. "\n";
28
29 sub _watchdogFaultHandler {
30
31     # Extract arguments.
32     my ($class, $res) = @_;
33
34     # Construct error message.
35     # Figure out if the error occurred in transport or over
36     # on the other side.
37     my $errMsg = $class->transport->status; # Assume transport error.
38
39     if (ref $res) {
40         $errMsg = $res->faultcode . ": ".  $res->faultstring . "\n";
41     }
42
43     if (!$faultDetected) {
44         $LOG->error("Watchdog fault detected, recovering Agent daemon.");
45         $faultDetected = 1;
46     }
47     # XXX: Reenable this, eventually.
48     $LOG->error(__PACKAGE__ . "->_watchdogFaultHandler(): Error occurred during processing.\n" . $errMsg);
49     Carp::carp __PACKAGE__ . "->_watchdogFaultHandler(): Error occurred during processing.\n" . $errMsg;
50
51
52     # Regardless of the error, destroy the Agent process and reinitialize it.
53     # XXX: Sanity check this, eventually.
54     HoneyClient::Agent->destroy();
55
56     # Wait for a small amount of time, in order for the killed process to release
57     # its control of the bound TCP port.
58     sleep 5;
59
60     $URL = HoneyClient::Agent->init();
61
62     # Recreate a new stub handle, in case the global configuration has
63     # changed.
64     $stub = getClientHandle(address   => 'localhost',
65                             namespace => 'HoneyClient::Agent',
66                             fault_handler => \&_watchdogFaultHandler);
67
68     # Restore state information.
69     if (defined($agentState)) {
70         $som = $stub->updateState(encode_base64(nfreeze($agentState)));
71     }
72 }
73
74 $stub = getClientHandle(address   => 'localhost',
75                         namespace => 'HoneyClient::Agent',
76                         fault_handler => \&_watchdogFaultHandler);
77                
78 for (;;) {
79     # TODO: Make this a programmatic value.
80     sleep (5);
81     $som = $stub->getState();
82     if (defined($som) and (ref($som) eq "SOAP::SOM")) {
83         $tempState = $som->result();
84         if (defined($tempState)) {
85             # Make sure the new state is parsable, before saving it.
86             eval {
87                 $tempState = thaw(decode_base64($tempState));
88             };
89             if (!$@) {
90                 $agentState = $tempState;
91             }
92         }
93     }
94 }
95
96 HoneyClient::Agent->destroy();
Note: See TracBrowser for help on using the browser.