Here is a code scrap that copies the module out and sets the nagios_plugin_wrapper_installed class, if the module is in place. It is assumed that $(g.local_cf_modules) and @(g.distribution_servers) have been defined in the configuration, and that you are using the Cfengine Community Open Promise-Body Library which defines if_ok(x), my particular version of mycopy takes a slist of servers instead of a scalar:
files:
"$(sys.workdir)/modules/nagios_plugin_wrapper"
perms => mog("755","root","root"),
action => if_elapsed("60"),
copy_from => mycopy("$(g.local_cf_modules)/nagios_plugin_wrapper",
@(g.distribution_servers)),
classes => if_ok("nagios_plugin_wrapper_installed");
You can use the nagios_plugin_wrapper_installed class to control running the
wrapper. I defined this in the nagios_plugin_agent bundle, but you might want
to put it somewhere else.
Best,
--Bruce
On Feb 23, 2012, at 6:36 PM, Robert Carleton wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> I posted an example of calling Nagios plugins from inside of Cfengine a while
> back. One of my co-workers took a look at the code and suggested that I put
> it in a bundle that could be called as a parameterized method promise. I
> think it might be useful to the community so here it is.
>
> First is an example bundle of how you might call the Nagios wrapper:
>
> bundle agent main {
>
> vars:
> "nagios_plugin_path" string => "/opt/local/libexec/nagios";
> methods:
>
> any::
>
> # Use check_dummy for some test cases
>
> # A check that returns ok
> "any"
> usebundle => nagios_plugin_agent("$(nagios_plugin_path)",
> "check_dummy",
> "0 OK");
>
> # A check that returns warning
> "any"
> usebundle => nagios_plugin_agent("$(nagios_plugin_path)",
> "check_dummy",
> "1 Hey!");
>
> # A check that returns critical
> "any"
> usebundle => nagios_plugin_agent("$(nagios_plugin_path)",
> "check_dummy",
> "2 Help!");
>
> # A check that returns unknown
> "any"
> usebundle => nagios_plugin_agent("$(nagios_plugin_path)",
> "check_dummy",
> "3 Clueless!");
>
> }
>
> Note how the plugin path is specified as one of the parameters. This is what
> the cf-agent output looks like for me:
>
> $ /usr/local/sbin/cf-agent --no-lock
> M "/Users/rbc/.cfagent/modules/nagios_plugin_wrapper
> /opt/local/libexec/nagios check_dummy 0 OK":
> M "/Users/rbc/.cfagent/modules/nagios_plugin_wrapper
> /opt/local/libexec/nagios check_dummy 1 Hey!":
> R: Nagios plugin check_dummy on Roberts-Computer.local reports WARNING: Hey!
> on Thu Feb 23 18:21:06 2012
> M "/Users/rbc/.cfagent/modules/nagios_plugin_wrapper
> /opt/local/libexec/nagios check_dummy 2 Help!":
> R: Nagios plugin check_dummy on Roberts-Computer.local reports CRITICAL:
> Help! on Thu Feb 23 18:21:06 2012
> M "/Users/rbc/.cfagent/modules/nagios_plugin_wrapper
> /opt/local/libexec/nagios check_dummy 3 Clueless!":
> R: Nagios plugin check_dummy on Roberts-Computer.local reports UNKNOWN:
> Clueless! on Thu Feb 23 18:21:06 2012
>
> Next I'll show the bundle that calls the module:
>
> bundle agent nagios_plugin_agent(nagios_plugin_path, nagios_plugin_name,
> nagios_plugin_args)
> {
>
> classes:
>
> "nagios_alert" or => {
> "nagios_plugin_$(nagios_plugin_name)_warning",
> "nagios_plugin_$(nagios_plugin_name)_critical",
> "nagios_plugin_$(nagios_plugin_name)_unknown",
> "nagios_plugin_$(nagios_plugin_name)_protocol_error"
> };
>
> commands:
>
> "$(sys.workdir)/modules/nagios_plugin_wrapper $(nagios_plugin_path)
> $(nagios_plugin_name) $(nagios_plugin_args)"
>
> module => "true";
>
> reports:
>
> nagios_alert::
>
> "Nagios plugin $(nagios_plugin_name) on $(sys.host) reports
> $(nagios_plugin_wrapper.output) on $(sys.date)";
> }
>
> Note that the Nagios wrapper module is being called from
> $(sys.workdir)/modules, so you have to figure out someway to get the wrapper
> there. Finally we have the wrapper module itself.
>
> #!/bin/sh
> PREFIX=${1}
> PLUGIN=${2}
> shift 2
> OUTPUT=`$PREFIX/$PLUGIN $@`
> EXIT=$?
> /bin/echo "=output=$OUTPUT"
>
> case "$EXIT" in
> '0')
> echo "+nagios_plugin_${PLUGIN}_ok"
> ;;
> '1')
> echo "+nagios_plugin_${PLUGIN}_warning"
> ;;
> '2')
> echo "+nagios_plugin_${PLUGIN}_critical"
> ;;
> '3')
> echo "+nagios_plugin_${PLUGIN}_unknown"
> ;;
> *)
> echo "+nagios_plugin_${PLUGIN}_protocol_error"
> ;;
> esac
>
> I've been testing this with cfengine-3.1.2 and 3.1.5 compiled from source
> under Mac OS Snow Leopard and Lion. Please let me know if you have any
> comments or questions.
>
> Best,
>
> --Bruce
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (Darwin)
>
> iQEcBAEBAgAGBQJPRs2RAAoJEPy/5L3ndSQsW28IAIXX1Hpxfri6KC03pdNd9zRB
> OG0/SHr/0u1NhhU1fTzeZaiv+qnbgrAaMTETYNWV74VIx3PvE8+b1x89RiKN7wWw
> UsKnchMl0LEjNbr1XXWXvcqUEe9MONKK3Q9kgkNDIOpMsJ1ZE6QmC0JYnnalEBEa
> Mb6uPsgtFVpbYKTqUd4Sr+6gF+nCTQTxNHc2pxAxzDYlTyAI3qcAVof6y/rvvX6D
> Py8MdePBSeqo0TDWZ1htbsBQm89NLwiEHSJawNnVOR2AAUf8d8bYK0lD2d0qaAQU
> DM9qOo0x5dreLt0ZetMgBQoHU6ykv6G4hCRKcJNEnLgRPJVr4qdXkVAsh1hYERg=
> =xBkq
> -----END PGP SIGNATURE-----
> _______________________________________________
> Help-cfengine mailing list
> [email protected]
> https://cfengine.org/mailman/listinfo/help-cfengine
PGP.sig
Description: This is a digitally signed message part
_______________________________________________ Help-cfengine mailing list [email protected] https://cfengine.org/mailman/listinfo/help-cfengine
