Writing SimpleRPC Clients

As pointed out in the SimpleRPCIntroduction page you can use the mco rpc CLI to call agents and it will do it’s best to print results in a sane way. When this is not enough you can write your own clients.

Simple RPC clients can do most of what a normal client can do but it makes a lot of things much easier if you stick to the Simple RPC conventions.

This guide shows how to write standalone scripts to interact with your collective. There is a single executable system. You can apply most of the techniques documented here to writing plugins for that application system. See the full reference for the plugin system here. You should try to write your general agent CLIs using this plugin system rather than the stand alone scripts detailed below as that promote a unified interface that behave in a consistent manner.

We’ve recorded a tutorial that will give you a quick look at what is involved in writing agents and a very simple client.

We’ll walk through building a ever more complex example of Hello World here.

The Basic Client

The client is mostly a bunch of helper methods that you use as a Ruby Mixin in your own code, it provides:

  • Standard command line option parsing with help output
  • Ability to add your own command line options
  • Simple access to agents and actions
  • Tools to help you print results
  • Tools to print stats
  • Tools to construct your own filters
  • While retaining full power of MCollective::Client if you need the additional feature sets
  • And being as simple or as complex to match your level of code proficiency

We’ll write a client for the Helloworld agent that you saw in the SimpleRPCIntroduction.

Call an Agent and print the result

A basic hello world client can be seen below:


require 'mcollective'

include MCollective::RPC

mc = rpcclient("helloworld")

printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")



Save this into hello.rb and run it with --help, you should see the standard basic help including filters for discovery.

If you’ve set up the Agent and run the client you should see something along these lines:

$ hello.rb

Finished processing 44 hosts in 375.57 ms

While it ran you would have seen a little progress bar and then just the summary line. The idea is that if you’re talking to a 1000 machine there’s no point in seeing a thousand OK, you only want to see what failed and this is exactly what happens here, you’re only seeing errors.

If you run it with --verbose you’ll see a line of text for every host and also a larger summary of results.

I’ll explain each major line in the code below then add some more features from there:

include MCollective::RPC

mc = rpcclient("helloworld")

The first line pulls in the various helper functions that we provide, this is the Mixin we mentioned earlier.

We then create a new client to the agent “helloworld” that you access through the mc variable.

printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")


To call a specific action you simply have to do mc.echo this calls the echo action, we pass a :msg parameter into it with the string we want echo’d back. The parameters will differ from action to action. It returns a simple array of the results that you can print any way you want, we’ll show that later.

printrpc and printrpcstats are functions used to print the results and stats respectively.


This cleanly disconnects the client from the middleware, some middleware tools like ActiveMQ will log confusing exceptions if you do not do this. It’s good form to always disconnect but isn’t strictly required.

Adjusting the output

Verbosely displaying results

As you see there’s no indication that discovery is happening and as pointed out we do not display results that are ok, you can force verbosity as below on individual requests:

mc = rpcclient("helloworld")

mc.discover :verbose => true

printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC"), :verbose => true

Here we’ve added a :verbose flag and we’ve specifically called the discover method. Usually you don’t need to call discover it will do it on demand. Doing it this way you’ll always see the line:

Determining the amount of hosts matching filter for 2 seconds .... 44

Passing verbose to printrpc forces it to print all the results, failures or not.

If you just wanted to force verbose on for all client interactions, do:

mc = rpcclient("helloworld")
mc.verbose = true

printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")

In this case everything will be verbose, regardless of command line options.

Disabling the progress indicator

You can disable the twirling progress indicator easily:

mc = rpcclient("helloworld")
mc.progress = false

Now whenever you call an action you will not see the progress indicator.

Saving the reports in variables without printing

You can retrieve the stats from the clients and also get text of reports without printing them:

stats = mc.echo(:msg => "Welcome to MCollective Simple RPC").stats

report = stats.report

report will now have the text that would have been displayed by ‘printrpcstats’ you can also use no_response_report to get report text for just the list of hosts that didnt respond.

If you didn’t want to just print the results out to STDOUT you can also get them back as just text:

report = rpcresults mc.echo(:msg => "Welcome to MCollective Simple RPC")

Applying filters programatically

You can pass filters on the command line using the normal --with-* options but you can also do it programatically. Here’s a new version of the client that only calls machines with the configuration management class /dev_server/ and the fact country=uk

mc = rpcclient("helloworld")

mc.class_filter /dev_server/
mc.fact_filter "country", "uk"

printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")

You can set other filters like agent_filter, identity_filter and compound_filter.

The fact_filter method supports a few other forms in adition to above:

mc.fact_filter "country=uk"
mc.fact_filter "physicalprocessorcount", "4", ">="

This will limit it to all machines in the UK with more than 3 processors.

Resetting filters to empty

If while using the client you wish to reset the filters to an empty set of filters - containing only the agent name that you’re busy addressing you can do it as follows:

mc = rpcclient("helloworld")

mc.class_filter /dev_server/


After this code snippet the filter will only have an agent filter of helloworld set.

Processing Agents in Batches

By default the client will communicate with all machines at the same time. This might not be desired as you might affect a DOS on related components.

You can instruct the client to communicate with remote agents in batches and sleep between each batch.

Any client application has this capability using the --batch and --batch-sleep-time command line options.

You can also enable this programatically either per client or per request:

mc = rpcclient("helloworld")
mc.batch_size = 10
mc.batch_sleep_time = 5

mc.echo(:msg => "hello world")
mc = rpcclient("helloworld")

mc.echo(:msg => "hello world", :batch_size => 10, :batch_sleep_time => 5)

By default batching is disabled and sleep time is 1

Setting the batch_size to 0 will disable batch mode in both examples above, effectively overriding what was supplied on the command line.

Forcing Rediscovery

By default it will only do discovery once per script and then re-use the results, you can though force rediscovery if you had to adjust filters mid run for example.

mc = rpcclient("helloworld")

mc.class_filter /dev_server/
printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")


mc.fact_filter "country", "uk"
printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")

Here we make one echo call - which would do a discovery - we then reset the client, adjust filters and call it again. The 2nd call would do a new discovery and have new client lists etc.

Supplying your own discovery information

A new core messaging mode has been introduced that enables direct non filtered communicatin to specific nodes. This has enabled us to provide an discovery-optional mode but only if the collective is configured to support direct messaging.

mc = rpcclient("helloworld")

mc.discover(:nodes => ["host1", "host2", "host3"]

printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")

This will immediately, without doing discovery, communicate just with these 3 hosts. It will do normal failure reporting as with normal discovery based requests but will just be much faster as the 2 second discovery overhead is avoided.

The goal with this feature is for cases such as deployment tools where you have a known expectation of which machines to deploy to and you always want to know if that fails. In that use case a discovery based approach is not 100% suitable as you won’t know about down machines. This way you can provide your own source of truth.

When using the direct mode messages have a TTL associated with them that defaults to 60 seconds. Since 1.3.2 you can set the TTL globally in the configuration file but you can also set it on the client:

mc = rpcclient("helloworld")
mc.ttl = 3600

mc.discover(:nodes => ["host1", "host2", "host3"]

printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")

With the TTL set to 3600 if any of the hosts are down at the time of the request the request will wait on the middleware and should they come back up before 3600 has passed since request time they will then perform the requested action.

Only sending requests to a subset of discovered nodes

By default all nodes that get discovered will get the request. This isn’t always desirable maybe you want to deploy only to a random subset of hosts or maybe you have a service exposed over MCollective that you want to treat as a HA service and so only speak with one host that provides the functionality.

You can limit the hosts to talk to either using a number or a percentage, the code below shows both:

mc = rpcclient("helloworld")

mc.limit_targets = "10%"
printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")

This will pick 10% of the discovered hosts - or 1 if 10% is less than 1 - and only target those nodes with your request. You can also set it to an integer.

There are two possible modes for choosing the targets. You can configure a global default method but also set it on your client:

mc = rpcclient("helloworld")

mc.limit_targets = "10%"
mc.limit_method = :random
printrpc mc.echo(:msg => "Welcome to MCollective Simple RPC")

The above code will force a :random selection, you can also set it to :first

Gaining access to the full MCollective::Client

If you wanted to work with the Client directly as in WritingAgents after perhaps setting up some queries or gathering data first you can gain access to the client, you might also need access to the options array that was parsed out from the command line and any subsequent filters that you added.

mc = rpcclient("helloworld")

client = mc.client
options = mc.options

The first call will set up the CLI option parsing, create clients etc, you can then just grab the client and options and go on as per WritingAgents. This is a much quicker way to write full power clients, by just by short-circuiting the options parsing etc.

Dealing with the results directly

The biggest reason that you’d write custom clients is probably if you wanted to do custom processing of the results, there are 2 options to do it.

Results and Exceptions

Results have a set structure and depending on how you access the results you will either get Exceptions or result codes.

Status Code Description Exception Class
0 OK  
1 OK, failed. All the data parsed ok, we have a action matching the request but the requested action could not be completed. RPCAborted
2 Unknown action UnknownRPCAction
3 Missing data MissingRPCData
4 Invalid data InvalidRPCData
5 Other error UnknownRPCError

Just note these now, I’ll reference them later down.

Simple RPC style results

Simple RPC provides a trimmed down version of results from the basic Client library. You’d choose to use this if you just want to do simple things or maybe you’re just learning Ruby. You’ll get to process the results after the call is either done or timed out completely.

This is an important difference between the two approaches, in one you can parse the results as it comes in, in the other you will only get results after processing is done. This would be the main driving facter for choosing one over the other.

Here’s an example that will print out results in a custom way.

mc.echo(:msg => "hello world").each do |resp|
   printf("%-40s: %s\n", resp[:sender], resp[:data][:msg])

This will produce a result something like this:

dev1.you.net                          : hello world
dev2.you.net                          : hello world
dev3.you.net                          : hello world

The each in the above code just loops through the array of results. Results are an array of Hashes, the data you got for above has the following structure:

 :data=>{:msg => "hello world"},
 :data=>{:msg => "hello world"},

The :statuscode matches the table above so you can make decisions based on each result’s status.

Gaining access to MCollective::Client#req results

You can get access to each result in real time, in this case you will supply a block that will be invoked for each result as it comes in. The result set will be exactly as from the full blown client.

In this mode there will be no progress indicator, you’ll deal with results as and when they come in not after the fact as in the previous example.

mc.echo(:msg => "hello world") do |resp|
   if resp[:body][:statuscode] == 0
      printf("%-40s: %s\n", resp[:senderid], resp[:body][:data])
      puts "The RPC agent returned an error: #{resp[:body][:statusmsg]}"

The output will be the same as above

In this mode the results you get will be like this:

 :body=>{:statusmsg=>"OK", :statuscode=>0, :data=>{:msg => "hello world"}},

You can additionally gain access to a SimpleRPC style result in addition to the more complex native client results:

mc.echo(:msg => "hello world") do |resp, simpleresp|
   if resp[:body][:statuscode] == 0
      printf("%-40s: %s\n", simpleresp[:sender], simpleresp[:data][:msg])
      puts "The RPC agent returned an error: #{resp[:body][:statusmsg]}"

You can still use printrpc to print these style of results and gain advantage of the DDL and so forth:

mc.echo(:msg => "hello world") do |resp, simpleresp|
   printrpc simpleresp

This should give you a simpler result set to deal with

Adding custom command line options

You can look at the mco rpc script for a big sample, here I am just adding a simple --msg option to our script so you can customize the message that will be sent and received.


require 'mcollective'

include MCollective::RPC

options = rpcoptions do |parser, options|
   parser.define_head "Generic Echo Client"
   parser.banner = "Usage: hello [options] [filters] --msg MSG"

   parser.on('-m', '--msg MSG', 'Message to pass') do |v|
      options[:msg] = v

unless options.include?(:msg)
   puts("You need to specify a message with --msg")
   exit! 1

mc = rpcclient("helloworld", :options => options)

mc.echo(:msg => options[:msg]).each do |resp|
   printf("%-40s: %s\n", resp[:sender], resp[:data][:msg])

This version of the code should be run like this:

% test.rb --msg foo
dev1.you.net                          : foo
dev2.you.net                          : foo
dev3.you.net                          : foo

Documentation for the Options Parser can be found in its code.

And finally if you add options as above rather than try to parse it yourself you will get help integration for free:

% hello.rb --help
Usage: hello [options] [filters] --msg MSG
Generic Echo Client
    -m, --msg MSG                    Message to pass

Common Options
    -c, --config FILE                Load configuratuion from file rather than default
        --dt SECONDS                 Timeout for doing discovery
    -t, --timeout SECONDS            Timeout for calling remote agents
    -q, --quiet                      Do not be verbose
    -v, --verbose                    Be verbose
    -h, --help                       Display this screen

Host Filters
        --wf, --with-fact fact=val   Match hosts with a certain fact
        --wc, --with-class CLASS     Match hosts with a certain configuration management class
        --wa, --with-agent AGENT     Match hosts with a certain agent
        --wi, --with-identity IDENT  Match hosts with a certain configured identity

Disabling command line parsing and supplying your options programatically

Sometimes, perhaps when embedding an MCollective client into another tool like Puppet, you do not want MCollective to do any command line parsing as there might be conflicting command line options etc.

This can be achieved by supplying an options hash to the SimpleRPC client:

include MCollective::RPC

options =  MCollective::Util.default_options

client = rpcclient("test", {:options => options})

This will create a RPC client for the agent test without any options parsing at all.

To set options like discovery timeout and so forth you will need use either the client utilities or manipulate the hash upfront, the client utility methods is the best. The code below sets the discovery timeout in a way that does not require you to know any internal structures or the content of the options hash.

options =  MCollective::Util.default_options

client = rpcclient("test", {:options => options})
client.discovery_timeout = 4

Using this method of creating custom options hashes mean we can make internal changes to MCollective without affecting your code in the future.

Sending SimpleRPC requests without discovery and blocking

Usually this section will not apply to you. The client libraries support sending a request without waiting for a reply. This could be useful if you want to clean yum caches but don’t really care if it actually happens everywhere.

You will loose these abilities:

  • Knowing if your request was received by any agents
  • Any stats about processing times etc
  • Any information about the success or failure of your request

The above should make it clear already that this is a limited use case, it’s essentially a broadcast request with no feedback loop.

The code below will send a request to the runonce action for an agent puppetd, once the request is dispatched I will have no idea if it got handled etc, my code will just continue onwards.

p = rpcclient("puppetd")

p.identity_filter "your.box.com"
reqid = p.runonce(:forcerun => true, :process_results => false)

This will honor any attached filters set either programatically or through the command line, it will send the request but will just not handle any responses. All it will do is return the request id.

Doing your own discovery

For web applications you’d probably use cached copied of Registration data to do discovery rather than wait for MC to do discovery between each request.

To do this, you’ll need to construct a filter and a list of expected hosts and then do a custom call:

puppet = rpcclient("puppetd")

printrpc puppet.custom_request("runonce", {:forcerun => true}, "your.box.com", {"identity" => "your.box.com"})

This will do a call with exactly the same stats, block and other semantics as a normal call like:

printrpc puppet.runonce(:forcerun => true)

But instead of doing any discovery it will use the host list and filter you supplied in the call.

The rpcutil Helper Agent

A helper agent called rpcutil is included that helps you gather stats, inventory etc about the running daemon.

↑ Back to top