f5e376a11673a8b7c7aaa63f3211664a7acaa19f
1 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
2 <book>
3 <title>Nagios Plug-in Developer Guidelines</title>
5 <bookinfo>
6 <authorgroup>
7 <author>
8 <firstname>Karl</firstname>
9 <surname>DeBisschop</surname>
10 <affiliation>
11 <address><email>karl@debisschop.net</email></address>
12 </affiliation>
13 </author>
15 <author>
16 <firstname>Ethan</firstname>
17 <surname>Galstad</surname>
18 <authorblurb>
19 <para>Author of Nagios</para>
20 <para><ulink url="http://www.nagios.org"></ulink></para>
21 </authorblurb>
22 <affiliation>
23 <address><email>netsaint@linuxbox.com</email></address>
24 </affiliation>
25 </author>
27 <author>
28 <firstname>Hugo</firstname>
29 <surname>Gayosso</surname>
30 <affiliation>
31 <address><email>hgayosso@gnu.org</email></address>
32 </affiliation>
33 </author>
36 <author>
37 <firstname>Subhendu</firstname>
38 <surname>Ghosh</surname>
39 <affiliation>
40 <address><email>sghosh@sourceforge.net</email></address>
41 </affiliation>
42 </author>
44 <author>
45 <firstname>Stanley</firstname>
46 <surname>Hopcroft</surname>
47 <affiliation>
48 <address><email>stanleyhopcroft@sourceforge.net</email></address>
49 </affiliation>
50 </author>
52 </authorgroup>
54 <pubdate>2002</pubdate>
55 <title>Nagios plug-in development guidelines</title>
57 <revhistory>
58 <revision>
59 <revnumber>0.4</revnumber>
60 <date>2 May 2002</date>
61 </revision>
62 </revhistory>
64 <copyright>
65 <year>2000 2001 2002</year>
66 <holder>Karl DeBisschop, Ethan Galstad,
67 Hugo Gayosso, Stanley Hopcroft, Subhendu Ghosh</holder>
68 </copyright>
70 </bookinfo>
73 <preface id="preface"><title>Preface</title>
74 <para>The purpose of this guidelines is to provide a reference for
75 the plug-in developers and encourage the standarization of the
76 different kind of plug-ins: C, shell, perl, python, etc.</para>
78 <para>Nagios Plug-in Development Guidelines Copyright (C) 2000 2001
79 2002
80 Karl DeBisschop, Ethan Galstad, Hugo Gayosso, Stanley Hopcroft,
81 Subhendu Ghosh</para>
83 <para>Permission is granted to make and distribute verbatim
84 copies of this manual provided the copyright notice and this
85 permission notice are preserved on all copies.</para>
87 <para>The plugins themselves are copyrighted by their respective
88 authors.</para>
89 </preface>
91 <article>
92 <section id="DevRequirements"><title>Development platform requirements</title>
93 <para>
94 Nagios plugins are developed to the GNU standard, so any OS which is supported by GNU
95 should run the plugins. While the requirements for compiling the Nagios plugins release
96 is very small, to develop from CVS needs additional software to be installed. These are the
97 minimum levels of software required:
99 <literallayout>
100 gnu make 3.79
101 automake 1.6
102 autoconf 2.54
103 gettext 0.11.5
104 </literallayout>
106 To compile from CVS, after you have checked out the code, run:
107 <literallayout>
108 tools/setup
109 ./configure
110 make
111 make install
112 </literallayout>
113 </para>
114 </section>
116 <section id="PlugOutput"><title>Plugin Output for Nagios</title>
118 <para>You should always print something to STDOUT that tells if the
119 service is working or why its failing. Try to keep the output short -
120 probably less that 80 characters. Remember that you ideally would like
121 the entire output to appear in a pager message, which will get chopped
122 off after a certain length.</para>
124 <section><title>Print only one line of text</title>
125 <para>Nagios will only grab the first line of text from STDOUT
126 when it notifies contacts about potential problems. If you print
127 multiple lines, you're out of luck. Remember, keep it short and
128 to the point.</para>
129 </section>
131 <section><title>Verbose output</title>
132 <para>Use the -v flag for verbose output. You should allow multiple
133 -v options for additional verbosity, up to a maximum of 3. The standard
134 type of output should be:</para>
136 <table id="verbose_levels"><title>Verbose output levels</title>
137 <tgroup cols="2">
138 <thead>
139 <row>
140 <entry><para>Verbosity level</para></entry>
141 <entry><para>Type of output</para></entry>
142 </row>
143 </thead>
144 <tbody>
145 <row>
146 <entry align=center><para>0</para></entry>
147 <entry><para>Single line, minimal output. Summary</para></entry>
148 </row>
149 <row>
150 <entry align=center><para>1</para></entry>
151 <entry><para>Single line, additional information (eg list processes that fail)</para></entry>
152 </row>
153 <row>
154 <entry align=center><para>2</para></entry>
155 <entry><para>Multi line, configuration debug output (eg ps command used)</para></entry>
156 </row>
157 <row>
158 <entry align=center><para>3</para></entry>
159 <entry><para>Lots of detail for plugin problem diagnosis</para></entry>
160 </row>
161 </tbody>
162 </tgroup>
163 </table>
164 </section>
166 <section><title>Screen Output</title>
167 <para>The plug-in should print the diagnostic and just the
168 synopsis part of the help message. A well written plugin would
169 then have --help as a way to get the verbose help.</para>
170 <para>Code and output should try to respect the 80x25 size of a
171 crt (remember when fixing stuff in the server room!)</para>
172 </section>
174 <section><title>Return the proper status code</title>
175 <para>See <xref linkend="ReturnCodes"> below
176 for the numeric values of status codes and their
177 description. Remember to return an UNKNOWN state if bogus or
178 invalid command line arguments are supplied or it you are unable
179 to check the service.</para>
180 </section>
182 <section><title>Plugin Return Codes</title>
183 <para>The return codes below are based on the POSIX spec of returning
184 a positive value. Netsaint prior to v0.0.7 supported non-POSIX
185 compliant return code of "-1" for unknown. Nagios supports POSIX return
186 codes by default.</para>
188 <para>Note: Some plugins will on occasion print on STDOUT that an error
189 occurred and error code is 138 or 255 or some such number. These
190 are usually caused by plugins using system commands and having not
191 enough checks to catch unexpected output. Developers should include a
192 default catch-all for system command output that returns an UNKNOWN
193 return code.</para>
195 <table id="ReturnCodes"><title>Plugin Return Codes</title>
196 <tgroup cols="3">
197 <thead>
198 <row>
199 <entry><para>Numeric Value</para></entry>
200 <entry><para>Service Status</para></entry>
201 <entry><para>Status Description</para></entry>
202 </row>
203 </thead>
204 <tbody>
205 <row>
206 <entry align=center><para>0</para></entry>
207 <entry valign=middle><para>OK</para></entry>
208 <entry><para>The plugin was able to check the service and it
209 appeared to be functioning properly</para></entry>
210 </row>
211 <row>
212 <entry align=center><para>1</para></entry>
213 <entry valign=middle><para>Warning</para></entry>
214 <entry><para>The plugin was able to check the service, but it
215 appeared to be above some "warning" threshold or did not appear
216 to be working properly</para></entry>
217 </row>
218 <row>
219 <entry align=center><para>2</para></entry>
220 <entry valign=middle><para>Critical</para></entry>
221 <entry><para>The plugin detected that either the service was not
222 running or it was above some "critical" threshold</para></entry>
223 </row>
224 <row>
225 <entry align=center><para>3</para></entry>
226 <entry valign=middle><para>Unknown</para></entry>
227 <entry><para>Invalid command line arguments were supplied to the
228 plugin or the plugin was unable to check the status of the given
229 hosts/service</para></entry>
230 </row>
231 </tbody>
232 </tgroup>
233 </table>
236 </section>
238 <section id="thresholdformat"><title>Threshold range format</title>
239 <para>Thresholds ranges define the warning and critical levels for plugins to
240 alert on. The theory is that the plugin will do some sort of check which returns
241 back a numerical value, or metric, which is then compared to the warning and
242 critical thresholds.
243 This is the generalised format for threshold ranges:</para>
245 <literallayout>
246 [@]start:end
247 </literallayout>
249 <para>Notes:</para>
250 <orderedlist>
251 <listitem><para>start < end</para>
252 </listitem>
253 <listitem><para>start and ":" is not required if start=0</para>
254 </listitem>
255 <listitem><para>if range is of format "start:" and end is not specified,
256 assume end is infinity</para>
257 </listitem>
258 <listitem><para>to specify negative infinity, use "~"</para>
259 </listitem>
260 <listitem><para>alert is raised if metric is outside start and end range
261 (inclusive of endpoints)</para>
262 </listitem>
263 <listitem><para>if range starts with "@", then alert if inside this range
264 (inclusive of endpoints)</para>
265 </listitem>
266 </orderedlist>
268 <para>Note: Not all plugins are coded to expect ranges in this format. It is
269 planned for a future release to
270 provide standard libraries to parse and compare metrics against ranges. There
271 will also be some work in providing multiple metrics.</para>
272 </section>
274 <section><title>Performance data</title>
275 <para>Performance data is defined by Nagios as "everything after the | of the plugin output" -
276 please refer to Nagios documentation for information on capturing this data to logfiles.
277 However, it is the responsibility of the plugin writer to ensure the
278 performance data is in a "Nagios plugins" format.
279 This is the expected format:</para>
281 <literallayout>
282 'label'=value[UOM];[warn];[crit];[min];[max]
283 </literallayout>
285 <para>Notes:</para>
286 <orderedlist>
287 <listitem><para>space separated list of label/value pairs</para>
288 </listitem>
289 <listitem><para>label can contain any characters</para>
290 </listitem>
291 <listitem><para>the single quotes for the label are optional. Required if
292 spaces, = or ' are in the label</para>
293 </listitem>
294 <listitem><para>label length is arbitrary, but ideally the first 19 characters
295 are unique (due to a limitation in RRD). Be aware of a limitation in the
296 amount of data that NRPE returns to Nagios</para>
297 </listitem>
298 <listitem><para>to specify a quote character, use two single quotes</para>
299 </listitem>
300 <listitem><para>warn, crit, min or max may be null (for example, if the threshold is
301 not defined or min and max do not apply). Trailing unfilled semicolons can be
302 dropped</para>
303 </listitem>
304 <listitem><para>min and max are not required if UOM=%</para>
305 </listitem>
306 <listitem><para>value, min and max in class [-0-9.]. Must all be the
307 same UOM</para>
308 </listitem>
309 <listitem><para>warn and crit are in the range format (see
310 <xref linkend="thresholdformat">)</para>
311 </listitem>
312 <listitem><para>UOM (unit of measurement) is one of:</para>
313 <orderedlist>
314 <listitem><para>no unit specified - assume a number (int or float)
315 of things (eg, users, processes, load averages)</para>
316 </listitem>
317 <listitem><para>s - seconds (also us, ms)</para></listitem>
318 <listitem><para>% - percentage</para></listitem>
319 <listitem><para>B - bytes (also KB, MB, TB)</para></listitem>
320 <listitem><para>c - a continous counter (such as bytes
321 transmitted on an interface)</para></listitem>
322 </orderedlist>
323 </listitem>
324 </orderedlist>
326 <para>It is up to third party programs to convert the Nagios plugins
327 performance data into graphs.</para>
328 </section>
329 </section>
331 <section id="SysCmdAuxFiles"><title>System Commands and Auxiliary Files</title>
333 <section><title>Don't execute system commands without specifying their
334 full path</title>
335 <para>Don't use exec(), popen(), etc. to execute external
336 commands without explicity using the full path of the external
337 program.</para>
339 <para>Doing otherwise makes the plugin vulnerable to hijacking
340 by a trojan horse earlier in the search path. See the main
341 plugin distribution for examples on how this is done.</para>
342 </section>
344 <section><title>Use spopen() if external commands must be executed</title>
346 <para>If you have to execute external commands from within your
347 plugin and you're writing it in C, use the spopen() function
348 that Karl DeBisschop has written.</para>
350 <para>The code for spopen() and spclose() is included with the
351 core plugin distribution.</para>
352 </section>
354 <section><title>Don't make temp files unless absolutely required</title>
356 <para>If temp files are needed, make sure that the plugin will
357 fail cleanly if the file can't be written (e.g., too few file
358 handles, out of disk space, incorrect permissions, etc.) and
359 delete the temp file when processing is complete.</para>
360 </section>
362 <section><title>Don't be tricked into following symlinks</title>
364 <para>If your plugin opens any files, take steps to ensure that
365 you are not following a symlink to another location on the
366 system.</para>
367 </section>
369 <section><title>Validate all input</title>
371 <para>use routines in utils.c or utils.pm and write more as needed</para>
372 </section>
374 </section>
379 <section id="PerlPlugin"><title>Perl Plugins</title>
381 <para>Perl plugins are coded a little more defensively than other
382 plugins because of embedded Perl. When configured as such, embedded
383 Perl Nagios (ePN) requires stricter use of the some of Perl's features.
384 This section outlines some of the steps needed to use ePN
385 effectively.</para>
387 <orderedlist>
389 <listitem><para> Do not use BEGIN and END blocks since they will be called
390 the first time and when Nagios shuts down with Embedded Perl (ePN). In
391 particular, do not use BEGIN blocks to initialize variables.</para>
392 </listitem>
394 <listitem><para>To use utils.pm, you need to provide a full path to the
395 module in order for it to work with ePN.</para>
397 <literallayout>
398 e.g.
399 use lib "/usr/local/nagios/libexec";
400 use utils qw(...);
401 </literallayout>
402 </listitem>
404 <listitem><para>Perl scripts should be called with "-w"</para>
405 </listitem>
407 <listitem><para>All Perl plugins must compile cleanly under "use strict" - i.e. at
408 least explicitly package names as in "$main::x" or predeclare every
409 variable. </para>
412 <para>Explicitly initialize each varialable in use. Otherwise with
413 caching enabled, the plugin will not be recompilied each time, and
414 therefore Perl will not reinitialize all the variables. All old
415 variable values will still be in effect.</para>
416 </listitem>
418 <listitem><para>Do not use < DATA > (these simply do not compile under ePN).</para>
419 </listitem>
421 <listitem><para>Do not use named subroutines</para>
422 </listitem>
424 <listitem><para>If writing to a file (perhaps recording
425 performance data) explicitly close close it. The plugin never
426 calls <emphasis role=strong>exit</emphasis>; that is caught by
427 p1.pl, so output streams are never closed.</para>
428 </listitem>
430 <listitem><para>As in <xref linkend="runtime"> all plugins need
431 to monitor their runtime, specially if they are using network
432 resources. Use of the <emphasis>alarm</emphasis> is recommended.
433 Plugins may import a default time out ($TIMEOUT) from utils.pm.
434 </para>
435 </listitem>
437 <listitem><para>Perl plugins should import %ERRORS from utils.pm
438 and then "exit $ERRORS{'OK'}" rather than "exit 0"
439 </para>
440 </listitem>
442 </orderedlist>
444 </section>
446 <section id="runtime"><title>Runtime Timeouts</title>
448 <para>Plugins have a very limited runtime - typically 10 sec.
449 As a result, it is very important for plugins to maintain internal
450 code to exit if runtime exceeds a threshold. </para>
452 <para>All plugins should timeout gracefully, not just networking
453 plugins. For instance, df may lock if you have automounted
454 drives and your network fails - but on first glance, who'd think
455 df could lock up like that. Plus, it should just be more error
456 resistant to be able to time out rather than consume
457 resources.</para>
459 <section><title>Use DEFAULT_SOCKET_TIMEOUT</title>
461 <para>All network plugins should use DEFAULT_SOCKET_TIMEOUT to timeout</para>
463 </section>
466 <section><title>Add alarms to network plugins</title>
468 <para>If you write a plugin which communicates with another
469 networked host, you should make sure to set an alarm() in your
470 code that prevents the plugin from hanging due to abnormal
471 socket closures, etc. Nagios takes steps to protect itself
472 against unruly plugins that timeout, but any plugins you create
473 should be well behaved on their own.</para>
475 </section>
479 </section>
481 <section id="PlugOptions"><title>Plugin Options</title>
483 <para>A well written plugin should have --help as a way to get
484 verbose help. Code and output should try to respect the 80x25 size of a
485 crt (remember when fixing stuff in the server room!)</para>
487 <section><title>Option Processing</title>
489 <para>For plugins written in C, we recommend the C standard
490 getopt library for short options. Getopt_long is always available.
491 </para>
493 <para>For plugins written in Perl, we recommend Getopt::Long module.</para>
495 <para>Positional arguments are strongly discouraged.</para>
497 <para>There are a few reserved options that should not be used
498 for other purposes:</para>
500 <literallayout>
501 -V version (--version)
502 -h help (--help)
503 -t timeout (--timeout)
504 -w warning threshold (--warning)
505 -c critical threshold (--critical)
506 -H hostname (--hostname)
507 -v verbose (--verbose)
508 </literallayout>
510 <para>In addition to the reserved options above, some other standard options are:</para>
512 <literallayout>
513 -C SNMP community (--community)
514 -a authentication password (--authentication)
515 -l login name (--logname)
516 -p port or password (--port or --passwd/--password)monitors operational
517 -u url or username (--url or --username)
518 </literallayout>
520 <para>Look at check_pgsql and check_procs to see how I currently
521 think this can work. Standard options are:</para>
524 <para>The option -V or --version should be present in all
525 plugins. For C plugins it should result in a call to print_revision, a
526 function in utils.c which takes two character arguments, the
527 command name and the plugin revision.</para>
529 <para>The -? option, or any other unparsable set of options,
530 should print out a short usage statement. Character width should
531 be 80 and less and no more that 23 lines should be printed (it
532 should display cleanly on a dumb terminal in a server
533 room).</para>
535 <para>The option -h or --help should be present in all plugins.
536 In C plugins, it should result in a call to print_help (or
537 equivalent). The function print_help should call print_revision,
538 then print_usage, then should provide detailed
539 help. Help text should fit on an 80-character width display, but
540 may run as many lines as needed.</para>
542 <para>The option -v or --verbose should be present in all plugins.
543 The user should be allowed to specify -v multiple times to increase
544 the verbosity level, as described in <xref linkend="verbose_levels">.</para>
545 </section>
547 <section>
548 <title>Plugins with more than one type of threshold, or with
549 threshold ranges</title>
551 <para>Old style was to do things like -ct for critical time and
552 -cv for critical value. That goes out the window with POSIX
553 getopt. The allowable alternatives are:</para>
555 <orderedlist>
556 <listitem>
557 <para>long options like -critical-time (or -ct and -cv, I
558 suppose).</para>
559 </listitem>
561 <listitem>
562 <para>repeated options like `check_load -w 10 -w 6 -w 4 -c
563 16 -c 10 -c 10`</para>
564 </listitem>
566 <listitem>
567 <para>for brevity, the above can be expressed as `check_load
568 -w 10,6,4 -c 16,10,10`</para>
569 </listitem>
571 <listitem>
572 <para>ranges are expressed with colons as in `check_procs -C
573 httpd -w 1:20 -c 1:30` which will warn above 20 instances,
574 and critical at 0 and above 30</para>
575 </listitem>
577 <listitem>
578 <para>lists are expressed with commas, so Jacob's check_nmap
579 uses constructs like '-p 1000,1010,1050:1060,2000'</para>
580 </listitem>
582 <listitem>
583 <para>If possible when writing lists, use tokens to make the
584 list easy to remember and non-order dependent - so
585 check_disk uses '-c 10000,10%' so that it is clear which is
586 the precentage and which is the KB values (note that due to
587 my own lack of foresight, that used to be '-c 10000:10%' but
588 such constructs should all be changed for consistency,
589 though providing reverse compatibility is fairly
590 easy).</para>
591 </listitem>
593 </orderedlist>
595 <para>As always, comments are welcome - making this consistent
596 without a host of long options was quite a hassle, and I would
597 suspect that there are flaws in this strategy.
598 </para>
599 </section>
600 </section>
602 <section id="CodingGuidelines"><title>Coding guidelines</title>
603 <para>See <ulink url="http://www.gnu.org/prep/standards_toc.html">GNU
604 Coding standards</ulink> for general guidelines.</para>
605 <section><title>Comments</title>
606 <para>You should use /* */ for comments and not // as some compilers
607 do not handle the latter form.</para>
608 <para>There should not be any named credits in the source code - contributors
609 should be added
610 into the AUTHORS file instead. The only exception to this is if a routine
611 has been copied from another source.</para>
612 </section>
614 <section><title>CVS comments</title>
615 <para>When adding CVS comments at commit time, you can use the following prefixes:
616 <variablelist>
617 <varlistentry><term>- comment</term>
618 <listitem>
619 <para>for a comment that can be removed from the Changelog</para>
620 </listitem>
621 </varlistentry>
622 <varlistentry><term>* comment</term>
623 <listitem>
624 <para>for an important amendment to be included into a features list</para>
625 </listitem>
626 </varlistentry>
627 </variablelist>
628 </para>
629 <para>If the change is due to a contribution, please quote the contributor's name
630 and, if applicable, add the SourceForge Tracker number. Don't forget to
631 update the AUTHORS file.</para>
632 </section>
633 </section>
635 <section id="SubmittingChanges"><title>Submission of new plugins and patches</title>
637 <section id="Patches"><title>Patches</title>
638 <para>If you have a bug patch, please supply a unified or context diff against the
639 version you are using. For new features, please supply a diff against
640 the CVS HEAD version.</para>
642 <para>Patches should be submitted via
643 <ulink url="http://sourceforge.net/tracker/?group_id=29880&atid=397599">SourceForge's
644 tracker system for Nagiosplug patches</ulink>
645 and be announced to the nagiosplug-devel mailing list.</para>
646 </section>
648 <section id="New_plugins"><title>New plugins</title>
649 <para>If you would like others to use your plugins and have it included in
650 the standard distribution, please include patches for the relevant
651 configuration files, in particular "configure.in". Otherwise submitted
652 plugins will be included in the contrib directory.</para>
654 <para>Plugins in the contrib directory are going to be migrated to the
655 standard plugins/plugin-scripts directory as time permits and per user
656 requests. The minimum requirements are:</para>
658 <orderedlist>
659 <listitem>
660 <para>The standard command options are supported (--help, --version,
661 --timeout, --warning, --critical)</para>
662 </listitem>
663 <listitem>
664 <para>It is determined to be not redundant (for instance, we would not
665 add a new version of check_disk just because someone had provide
666 a plugin that had perf checking - we would incorporate the features
667 into an exisiting plugin)</para>
668 </listitem>
669 <listitem>
670 <para>One of the developers has had the time to audit the code and declare
671 it ready for core</para>
672 </listitem>
673 <listitem>
674 <para>It should also follow code format guidelines, and use functions from
675 utils (perl or c or sh) rather than cooking it's own</para>
676 </listitem>
677 </orderedlist>
679 <para>New plugins should be submitted via
680 <ulink url="http://sourceforge.net/tracker/?group_id=29880&atid=541465">SourceForge's
681 tracker system for Nagiosplug new plugins</ulink>
682 and be announced to the nagiosplug-devel mailing list.</para>
684 <para>For new plugins, provide a diff to add to the EXTRAS list (configure.in)
685 unless you are fairly sure that the plugin will work for all platforms with
686 no non-standard software added.</para>
688 <para>If possible please submit a test harness. Documentation on sample
689 tests coming soon.</para>
690 </section>
692 </section>
694 </article>
696 </book>