MCollective Agents return data and we try to provide as much usable user interface for free. To aid in this we require agents to have DDL files that describe the data that the agent returns.
DDL files are used to configure the client but also to assist with user interface generation. They are used to ask questions that an action needs but also to render the results when the replies come in. For example we turn :freecpu into “Free CPU” when displaying the data based on the DDL.
Previously if data that agents returned required any summarization this had to be done using a custom application. Here is an example from mco nrpe:
Here to get the summary of results displayed in a way that has contextual relevance to the nrpe plugin a custom application had to be written and anyone who interacts with the agent using other RPC clients would not get the benefit of this summary.
By using aggregate plugins and updating the DDL we can now provide such a summary in all result sets and display it using the mco rpc application and any calls to printrpc.
Here you get a similar summary as before, all that had to be done was a simple aggregate plugin be written and distributed with your clients.
The results are shown as above using printrpcstats but you can also get access to the raw data so you can decide to render it in some other way - perhaps using a graph on a web interface.
We provide a number of aggregate plugins with MCollective and anyone can write more.
For examples that already use functions see the rpcutil agent - its collective_info, get_fact, daemon_stats and get_config_item actions all have summaries applied.
NOTE: This feature is available since version 2.1.0
Using existing plugins
Updating the DDL
At present MCollective supplies 3 plugins average(), summary() and sum() you can use these in any agent, here is an example from the rpcutil agent DDL file:
We’ve removed a few lines from this example DDL block leaving only the relevant lines. You can see the agent outputs data called :value and we reference that output in the summary function summary(:value), the result would look like this:
Viewing summaries on the CLI
You can see that the value in this case contains arrays, the summary() function produce the table in the output showing the data distribution.
Producing summaries in your own clients
You can enable the same display in your own code, here is ruby code that has the same affect as the CLI call above:
Without passing in the :summarize => true you would not see the summaries
Getting access to the raw summary results
If you wanted to do something else entirely like produce a graph on a web page of the summaries you can get access to the raw data, here’s some ruby code to show all computed summaries:
As you can see you will get an array of summaries this is because each DDL can use many aggregate calls, this would be an array of all the computed summaries:
There are 2 types of result :collection and :numeric, in the case of numeric results the :value would just be a number.
The aggregate_format is either a user supplied format or a dynamically computed format to display the summary results on the console. In this case each pair of the hash should be displayed using the format to produce a nice right justified list of keys and values.
Writing your own function
We’ll cover writing your own function by looking at the Nagios one from earlier in this example. You can look at the functions supplied with MCollective for more examples using other types than the one below.
First lets look at the DDL for the existing nrpe Agent:
You can see it will return an :exitcode item and from the default value you can gather this is going to be a number. Nagios defines 4 possibly exit codes for a Nagios plugin and we need to convert this :exitcode into a string like WARNING, CRITICAL, UNKNOWN or OK.
Usually when writing any kind of summarizer for an array of results your code might contain 3 phases.
Given a series of Nagios results like this:
You would write a nagios_states() function that does roughly this:
You could optimise the code but you can see there are 3 major stages in the life of this code.
- Set initial values for the return data
- Loop the data building up the state
- Return the data.
Given this, here is our Nagios exitcode summary function, it is roughly the same code with a bit more boiler plate to plugin into mcollective, but the same code can be seen:
This shows that an aggregate function has the same 3 basic parts. First we set the initial state using the startup_hook. We then process each result as it comes in from the network using process_result. Finally we turn that into a the result objects that you saw earlier in the ruby client examples using the summarize method.
Each function needs a startup hook, without one you’ll get exceptions. The startup hook lets you set up the initial state.
The first thing to do is set the type of result this will be. Currently we support 2 types of result either a plain number indicated using :numeric or a complex :collection type that can be a hash with keys and values.
Functions can take display formats in the DDL, in this example we set @aggregate_format to a printf default that would display a table of results but we still let the user supply his own format.
We then just initialize the result hash to and build a map from the English representation of the Nagios status codes.
Every reply that comes in from the network gets passed into your process_result method. The first argument will be just the single value the DDL indicates you are interested in but you’ll also get the whole rely so you can get access to other reply values and such.
This gets called each time, we just look at the value and increment each Nagios status or treat it as an unknown - in case the result data is missformed.
The summarize method lets you take the state you built up and convert that into an answer. The summarize method is optional what you see here is the default action if you do not supply one.
The result_class method accepts either :collection or :numeric as arguments and it is basically a factory for the correct result structure.
Deploy and update the DDL
You should deploy this function into your libdir/aggregate directory called nagios_states.rb on the client machines - no harm deploying it everywhere though.
Update the DDL so it looks like:
Add the last few lines - we check that we’re running in a version of MCollective that supports this feature and then we call our function with the :exitcode results.