summaryrefslogtreecommitdiffstats
path: root/web/attachments/456909-nagios-plugins-metadata.patch
blob: 421a1fa85cc808bc95b0d33778002348d4e0f436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
commit ba0449fe8534bdcc03d3a15e36c8af711a2a72d2
Author: Dejan Muhamedagic <dejan@suse.de>
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 <dejan@hello-penguin.com>
+ *
+ *
+ * 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 <dejan@hello-penguin.com>
+ *
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+
+/*
+ * The generic constants for XML
+ */
+
+#define XML_START \
+	"<?xml version=\"1.0\"?>\n" \
+	"<!DOCTYPE resource-agent SYSTEM \"ra-api-1.dtd\">\n" \
+	"<resource-agent name=\"%s\">\n" \
+	"<version>1.0</version>\n\n"
+#define XML_ACTIONS \
+	"<actions>\n" \
+	"<action name=\"start\"   timeout=\"20\" />\n" \
+	"<action name=\"stop\"    timeout=\"15\" />\n" \
+	"<action name=\"status\"  timeout=\"20\" />\n" \
+	"<action name=\"monitor\" timeout=\"20\" interval=\"60\" />\n" \
+	"<action name=\"meta-data\"  timeout=\"15\" />\n" \
+	"</actions>\n"
+#define XML_END \
+	"</resource-agent>\n"
+
+/* <parameters>?</parameters> */
+#define XML_PARAMETERS_BEGIN "<parameters>\n"
+#define XML_PARAMETERS_END "</parameters>\n"
+
+/* <parameter name="ipaddr" required="1" unique="1">
+   <content type="string" default="value"/>
+   ?
+   </parameter>
+ */
+#define XML_PARAMETER_BEGIN \
+	"<parameter name=\"%s\" unique=\"%d\" required=\"%d\">\n"
+#define XML_PARAMETER_CONTENT \
+	"<content type=\"%s\" default=\"%s\" />\n"
+#define XML_PARAMETER_END "</parameter>\n"
+
+/* <shortdesc lang="en">?</shortdesc> */
+#define XML_SHORTDESC_BEGIN \
+	"<shortdesc lang=\"%s\">"
+#define XML_SHORTDESC_END "</shortdesc>\n"
+
+/* <longdesc lang="en">?</longdesc> */
+#define XML_LONGDESC_BEGIN \
+	"<longdesc lang=\"%s\">\n"
+#define XML_LONGDESC_END "</longdesc>\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);