commit ba0449fe8534bdcc03d3a15e36c8af711a2a72d2 Author: Dejan Muhamedagic Date: Thu Nov 22 15:47:27 2012 +0100 Support for meta-data (XML) Enables nagios plugins to be deployed as resource agents with pacemaker clusters. The meta-data is defined here: https://github.com/ClusterLabs/resource-agents/blob/master/heartbeat/ra-api-1.dtd This commit does not change behaviour of any plugins. diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 0eb0255..0ef8d50 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am @@ -39,12 +39,12 @@ EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \ check_nagios check_by_ssh check_dns check_nt check_ide_smart \ check_procs check_mysql_query check_apt check_dbi -EXTRA_DIST = t tests utils.c netutils.c sslutils.c popen.c utils.h netutils.h \ - popen.h common.h runcmd.c runcmd.h +EXTRA_DIST = t tests utils.c netutils.c sslutils.c popen.c help.c \ + utils.h netutils.h popen.h common.h runcmd.c runcmd.h help.h PLUGINHDRS = common.h -BASEOBJS = utils.o ../lib/libnagiosplug.a ../gl/libgnu.a +BASEOBJS = utils.o help.o ../lib/libnagiosplug.a ../gl/libgnu.a NETOBJS = netutils.o $(BASEOBJS) $(EXTRA_NETOBJS) SSLOBJS = sslutils.o NETLIBS = $(NETOBJS) $(SOCKETLIBS) @@ -159,6 +159,8 @@ runcmd.o: runcmd.c runcmd.h $(PLUGINHDRS) utils.o: utils.c utils.h $(PLUGINHDRS) +help.o: help.c help.h + netutils.o: netutils.c netutils.h $(PLUGINHDRS) sslutils.o: sslutils.c netutils.h $(PLUGINHDRS) diff --git a/plugins/help.c b/plugins/help.c new file mode 100644 index 0000000..214707e --- /dev/null +++ b/plugins/help.c @@ -0,0 +1,140 @@ +/* + * help.c: custom help functions to print help text and XML + * meta-data + * + * Copyright (C) 2012 Dejan Muhamedagic + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include "common.h" +#include "help.h" + +static void meta_long_desc_from_short(const char *, const char *); +static void meta_long_desc(const char *, const char *); +static void meta_short_desc(const char *, const char *); +static void meta_param(const char *, struct parameter_help *); +static void indent(const char *, const char *); + +static void +meta_long_desc_from_short(const char *lang, const char *s) +{ + printf(XML_LONGDESC_BEGIN, lang); + printf("%s\n", _(s)); + printf(XML_LONGDESC_END); +} + +static void +meta_long_desc(const char *lang, const char *s) +{ + printf(XML_LONGDESC_BEGIN, lang); + printf("%s", _(s)); + printf(XML_LONGDESC_END); +} + +static void +meta_short_desc(const char *lang, const char *s) +{ + printf(XML_SHORTDESC_BEGIN, lang); + printf("%s", _(s)); + printf(XML_SHORTDESC_END); +} + +static void +meta_param(const char *lang, struct parameter_help *ph) +{ + printf(XML_PARAMETER_BEGIN, ph->name, ph->unique, ph->required); + if (ph->long_desc) { + meta_long_desc(lang, ph->long_desc); + } else { + meta_long_desc_from_short(lang, ph->short_desc); + } + meta_short_desc(lang, ph->short_desc); + printf(XML_PARAMETER_CONTENT, ph->type, ph->dflt_value); + printf(XML_PARAMETER_END); +} + +void +print_meta_data(struct help_head *hh, struct parameter_help *ph) +{ + struct parameter_help *p; + + printf(XML_START, hh->name); + if (hh->long_desc) { + meta_long_desc(LANG, hh->long_desc); + } else { + meta_long_desc_from_short(LANG, hh->short_desc); + } + meta_short_desc(LANG, hh->short_desc); + printf("\n"); + printf(XML_PARAMETERS_BEGIN); + printf("\n"); + for (p = ph; p->short_desc; p++) { + meta_param(LANG, p); + printf("\n"); + } + printf(XML_PARAMETERS_END); + printf("\n"); + printf(XML_ACTIONS); + printf("\n"); + printf(XML_END); +} + +static void +indent(const char *s, const char *tab) +{ + const char *p, *q; + int len; + + if (!s ) + return; + for (p = s; p < s+strlen(s) && *p; p = q+1) { + q = strchr(p, '\n'); + if( q ) { + printf("%s%.*s", tab, q-p+1, p); + } else { + printf("%s%s\n", tab, p); + } + } +} + +void +print_help_head(struct help_head *hh) +{ + if (hh->long_desc) { + printf(_(hh->long_desc)); + } else { + printf("%s\n", _(hh->short_desc)); + } +} + +void +print_parameters_help(struct parameter_help *ph) +{ + struct parameter_help *p; + + for (p = ph; p->short_desc; p++) { + if (p->short_opt) { + printf(" -%c, --%s=%s\n", (unsigned char)(p->short_opt), + p->name, p->value_desc); + } else { + printf(" --%s=%s\n", p->name, p->value_desc); + } + indent(_(p->long_desc), " "); + printf(UT_METADATA); + } +} diff --git a/plugins/help.h b/plugins/help.h new file mode 100644 index 0000000..4486424 --- /dev/null +++ b/plugins/help.h @@ -0,0 +1,108 @@ +/* + * help.h: custom help functions to print help text and XML + * meta-data + * + * Copyright (C) 2012 Dejan Muhamedagic + * + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifndef NP_HELP_H +#define NP_HELP_H +/* Header file for nagios plugins help.c */ + +/* This file should be included in all plugins */ + +#include +#include +#include +#include + +/* + * The generic constants for XML + */ + +#define XML_START \ + "\n" \ + "\n" \ + "\n" \ + "1.0\n\n" +#define XML_ACTIONS \ + "\n" \ + "\n" \ + "\n" \ + "\n" \ + "\n" \ + "\n" \ + "\n" +#define XML_END \ + "\n" + +/* ? */ +#define XML_PARAMETERS_BEGIN "\n" +#define XML_PARAMETERS_END "\n" + +/* + + ? + + */ +#define XML_PARAMETER_BEGIN \ + "\n" +#define XML_PARAMETER_CONTENT \ + "\n" +#define XML_PARAMETER_END "\n" + +/* ? */ +#define XML_SHORTDESC_BEGIN \ + "" +#define XML_SHORTDESC_END "\n" + +/* ? */ +#define XML_LONGDESC_BEGIN \ + "\n" +#define XML_LONGDESC_END "\n" + +#define LANG "en" + +#define UT_METADATA _("\ + --metadata\n\ + Print resource agent meta-data.\n") + +struct parameter_help { + const char *name; + int short_opt; + const char *short_desc; + int unique; + int required; + const char *type; + const char *dflt_value; + const char *value_desc; + const char *long_desc; +}; + +struct help_head { + const char *name; + const char *short_desc; + const char *long_desc; +}; + +void print_meta_data(struct help_head *, struct parameter_help *); +void print_parameters_help(struct parameter_help *); +void print_help_head(struct help_head *); + +#endif /* NP_HELP_H */ diff --git a/plugins/utils.h b/plugins/utils.h index 822be94..f0d0e50 100644 --- a/plugins/utils.h +++ b/plugins/utils.h @@ -24,6 +24,8 @@ suite of plugins. */ #define np_extra_opts(acptr,av,pr) av #endif +#include "help.h" + /* Standardize version information, termination */ void support (void);