3 # A simple Perl client for mcollective that just demonstrates
4 # how to construct requests, send them and process results.
6 # This is in effect a mc-find-hosts equivelant, you can fill in
7 # filters in the request and only the matching ones will reply.
9 # For this to work you need the SSL security plugin in MCollective
10 # 1.0.0 set to operate in YAML mode.
13 use Digest::MD5 qw(md5 md5_hex md5_base64);
14 use Crypt::OpenSSL::RSA;
16 use Net::STOMP::Client;
19 # The topics from your activemq, /topic/mcollective_dev/...
20 $mcollective_prefix = "mcollective_dev";
22 # Path to your SSL private key and what it's called so the
23 # mcollectived will load yours
24 $ssl_private_key = "/path/to/your.pem";
25 $ssl_private_key_name = "you";
27 # A string representing your sending host
28 $mcollective_client_identity = "devel.your.com-perl";
30 # Stomp connection parameters
31 $stomp_host = "localhost";
34 $stomp_password = "secret";
36 $YAML::Syck::ImplicitTyping = 1;
38 $request{":msgtime"} = time();
39 $request{":filter"}{"identity"} = [];
40 $request{":filter"}{"fact"} = [];
41 $request{":filter"}{"agent"} = [];
42 $request{":filter"}{"cf_class"} = [];
43 $request{":requestid"} = md5_hex(time() . $$);
44 $request{":callerid"} = "cert=${ssl_private_key_name}";
45 $request{":senderid"} = $mcollective_client_identity;
46 $request{":body"} = Dump("ping");
47 $request{":msgtarget"} = "/topic/${mcollective_prefix}.discovery.command";
51 open(SSL, $ssl_private_key);
57 $rsa = Crypt::OpenSSL::RSA->new_private_key($key);
58 $request{":hash"} = encode_base64($rsa->sign($request{":body"}));
60 $mcrequest = Dump(\%request);
62 $stomp = Net::STOMP::Client->new(host => $stomp_host, port => $stomp_port);
63 $stomp->connect(login => $stomp_user, passcode => $stomp_password);
65 $stomp->message_callback(sub {
66 my ($self, $frame) = @_;
68 $mc_reply = Load($frame->body);
69 $mc_body = Load($mc_reply->{":body"});
70 print $mc_reply->{":senderid"} . "> " . $mc_body . "\n";
75 $stomp->subscribe(destination => "/topic/${mcollective_prefix}.discovery.reply");
76 $stomp->send(destination => "/topic/${mcollective_prefix}.discovery.command", body => $mcrequest);
77 $stomp->wait_for_frames(callback => sub { return(0) }, timeout => 5);