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