Chapter 16. Extending the Agent

Here we’ll introduce you to more complex things you can do in your agent. There isn’t a use for this functionality in our thanks plugin just yet, but it’s good to know what is possible.

We’ll give it the ability to execute external scripts or command lines; how to send custom log lines; and best of all, we’ll review how to blow up tragically, er, I mean error out gracefully. Of course.

Executing Scripts

You can call any external script, written in any programming language (including bash) that is capable of writing out the results to a file in JSON format:

action "python_script" do
  implemented_by "/lovely/little/python/"

The script will be given two types of input:

  • A path to the file containing the request data in JSON format:

    • The first command-line parameter

    • Environment variable $MCOLLECTIVE_REQUEST_FILE

  • A path to the file where the response should be written in JSON format:

    • The second command-line parameter

    • Environment variable $MCOLLECTIVE_REPLY_FILE

The script should write the reply as a JSON hash into the reply file. The return code of your script should be one of the standard result codes from the table in “Results and Exceptions”.

If you do not specify a full path to the script, it will look for the script in the agent’s plugin directory. This makes it easy to bundle your scripts in with your agent plugins:

action "bundled_script" do
  implemented_by ""

  $libdir/agent/agentname ...

Get Learning MCollective now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.