www.delorie.com/gnu/docs/cfengine/cfengine-Tutorial_32.html   search  
 
Buy GNU books!


GNU cfengine

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.4.5 Writing plugin modules

If the regular mechanisms for setting classes do not produce the results you require for your configuration, you can write your own routines to concoct the classes of your dreams. Plugin modules are added to cfagent programs from within the actionsequence, (see Reference manual). They allow you to write special code for answering questions which are too complex to answer using the other mechanisms above. This allows you to control classes which will be switched on and the moment at which your module attempts to evaluate the condition of the system.

Modules must lie in a special directory defined by the variable moduledirectory. They must have a name of the form `module:mymodule' and they must follow a simple protocol. Cfagent will only execute a module which is owned either by root or the user who is running cfagent, if it lies in the special directory and has the special name. A plug-in module may be written in any language, it can return any output you like, but lines which begin with a `+' sign are treated as classes to be defined (like `-D'), while lines which begin with a `-' sign are treated as classes to be undefined (like `-N'). Lines starting with `=' are variables/macros to be defined. Any other lines of output are cited by cfagent, so you should normally make your module completely silent. Here is an example module written in perl. First we define the module in the cfagent program:

 
 control:

   moduledirectory = ( /local/cfagent/modules )
 
   actionsequence = ( 
                    files 
                    module:myplugin.specialclass 
                    "module:argplugin.specialclass arg1 arg2"
                    copy 
                    )
 ...

Note that the class definitions for the module can also be defined in as AddInstallables, if this is more convenient. NOTE: you must declare the classes before using them in the cfagent configuration, or else those actions will be ignored. Next we write the plugin itself.
 
#!/usr/bin/perl
#
# module:myplugin
#

  # lots of computation....

if (special-condition)
   {
   print "+specialclass";
   }

Modules inherit the environment variables from cfagent and accept arguments, just as a regular shellcommand does.
 
#!/bin/sh
#
# module:myplugin
#

/bin/echo $*

Cfagent defines the classes as an environment variable so that programs have access to these. E.g. try the following module:

 
#!/usr/bin/perl

print "Decoding $ENV{CFALLCLASSES}\n";

@allclasses = split (":","$ENV{CFALLCLASSES}");

while ($c=shift(@allclasses))
  {
  $classes{$c} = 1;
  print "$c is set\n";
  }

Modules can define macros in cfagent by outputting strings of the form
 
=variablename=value


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003