Added mcollective 2.3.1 package
[packages/trusty/mcollective.git] / ext / perl / mc-find-hosts.pl
1 #!/usr/bin/perl
2
3 # A simple Perl client for mcollective that just demonstrates
4 # how to construct requests, send them and process results.
5 #
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.
8
9 # For this to work you need the SSL security plugin in MCollective
10 # 1.0.0 set to operate in YAML mode.
11
12 use YAML::Syck;
13 use Digest::MD5 qw(md5 md5_hex md5_base64);
14 use Crypt::OpenSSL::RSA;
15 use MIME::Base64;
16 use Net::STOMP::Client;
17 use Data::Dumper;
18
19 # The topics from your activemq, /topic/mcollective_dev/...
20 $mcollective_prefix = "mcollective_dev";
21
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";
26
27 # A string representing your sending host
28 $mcollective_client_identity = "devel.your.com-perl";
29
30 # Stomp connection parameters
31 $stomp_host = "localhost";
32 $stomp_port = 6163;
33 $stomp_user = "your";
34 $stomp_password = "secret";
35
36 $YAML::Syck::ImplicitTyping = 1;
37
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";
48
49 $key = "";
50
51 open(SSL, $ssl_private_key);
52         while(<SSL>) {
53                 $key = $key . $_;
54         }
55 close(SSL);
56
57 $rsa = Crypt::OpenSSL::RSA->new_private_key($key);
58 $request{":hash"} = encode_base64($rsa->sign($request{":body"}));
59
60 $mcrequest = Dump(\%request);
61
62 $stomp = Net::STOMP::Client->new(host => $stomp_host, port => $stomp_port);
63 $stomp->connect(login => $stomp_user, passcode => $stomp_password);
64
65 $stomp->message_callback(sub {
66                 my ($self, $frame) = @_;
67
68                 $mc_reply = Load($frame->body);
69                 $mc_body = Load($mc_reply->{":body"});
70                 print $mc_reply->{":senderid"} . "> " . $mc_body . "\n";
71
72                 return($self);
73         });
74
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);
78 $stomp->disconnect();
79
80