Code

must use '&' in attribute values instead of '&'
[nagiosplug.git] / doc / developer-guidelines.sgml
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>
35           
36         <author>
37         <firstname>Subhendu</firstname>
38         <surname>Ghosh</surname>
39         <affiliation>
40                 <address><email>sghosh@sourceforge.net</email></address>
41         </affiliation>
42         </author>
43         
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>
56         
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>
74     <title>About the guidelines</title>
76     <para>The purpose of this guidelines is to provide a reference for
77     the plug-in developers and encourage the standarization of the
78     different kind of plug-ins: C, shell, perl, python, etc.</para>
81     <section> <title>Copyright</title>
83         <para>Nagios Plug-in Development Guidelines Copyright (C) 2000 2001
84                 2002
85         Karl DeBisschop, Ethan Galstad, Hugo Gayosso, Stanley Hopcroft, 
86                 Subhendu Ghosh</para>
88         <para>Permission is granted to make and distribute verbatim
89         copies of this manual provided the copyright notice and this
90         permission notice are preserved on all copies.</para>
92                 <para>The plugins themselves are copyrighted by their respective
93                 authors.</para>
95     </section>
96 </preface>
98 <article>
99 <section id="PlugOutput"><title>Plugin Output for Nagios</title>
100         
101                 <para>You should always print something to STDOUT that tells if the 
102                 service is working or why its failing. Try to keep the output short - 
103                 probably less that 80 characters. Remember that you ideally would like 
104                 the entire output to appear in a pager message, which will get chopped
105                 off after a certain length.</para>
107                 <section><title>Print only one line of text</title>
108                 <para>Nagios will only grab the first line of text from STDOUT
109                 when it notifies contacts about potential problems. If you print
110                 multiple lines, you're out of luck. Remember, keep it short and
111                 to the point.</para>
112                 </section>
114                 <section><title>Verbose output</title>
115                 <para>Use the -v flag for verbose output. You should allow multiple
116                 -v options for additional verbosity, up to a maximum of 3. The standard
117                 type of output should be:</para>
119                 <table id="verbose_levels"><title>Verbose output levels</title>
120                         <tgroup cols="2">
121                                 <thead>
122                                         <row>
123                                                 <entry><para>Verbosity level</para></entry>
124                                                 <entry><para>Type of output</para></entry>
125                                         </row>
126                                 </thead>
127                                 <tbody>
128                                         <row>
129                                                 <entry align=center><para>0</para></entry>
130                                                 <entry><para>Single line, minimal output. Summary</para></entry>
131                                         </row>
132                                         <row>
133                                                 <entry align=center><para>1</para></entry>
134                                                 <entry><para>Single line, additional information (eg list processes that fail)</para></entry>
135                                         </row>
136                                         <row>
137                                                 <entry align=center><para>2</para></entry>
138                                                 <entry><para>Multi line, configuration debug output (eg ps command used)</para></entry>
139                                         </row>
140                                         <row>
141                                                 <entry align=center><para>3</para></entry>
142                                                 <entry><para>Lots of detail for plugin problem diagnosis</para></entry>
143                                         </row>
144                                 </tbody>
145                         </tgroup>
146                 </table>
147                 </section>
149                 <section><title>Screen Output</title>
150                 <para>The plug-in should print the diagnostic and just the
151                 synopsis part of the help message.  A well written plugin would
152                 then have --help as a way to get the verbose help.</para>
153                 <para>Code and output should try to respect the 80x25 size of a
154                 crt (remember when fixing stuff in the server room!)</para>
155                 </section>
156                 
157             <section><title>Return the proper status code</title>
158                 <para>See <xref linkend="ReturnCodes"> below
159                 for the numeric values of status codes and their
160                 description. Remember to return an UNKNOWN state if bogus or
161                 invalid command line arguments are supplied or it you are unable
162                 to check the service.</para>
163                 </section>
164                 
165                 <section><title>Plugin Return Codes</title>
166                 <para>The return codes below are based on the POSIX spec of returning
167                 a positive value.  Netsaint prior to v0.0.7 supported non-POSIX
168                 compliant return code of "-1" for unknown.  Nagios supports POSIX return
169                 codes by default.</para>
171                 <para>Note: Some plugins will on occasion print on STDOUT that an error
172                 occurred and error code is 138 or 255 or some such number.  These
173                 are usually caused by plugins using system commands and having not 
174                 enough checks to catch unexpected output.  Developers should include a
175                 default catch-all for system command output that returns an UNKOWN
176                 return code.</para>
177                 
178                 <table id="ReturnCodes"><title>Plugin Return Codes</title>
179                         <tgroup cols="3">
180                                 <thead>
181                                         <row>
182                                                 <entry><para>Numeric Value</para></entry>
183                                                 <entry><para>Service Status</para></entry>
184                                                 <entry><para>Status Description</para></entry>
185                                         </row>
186                                 </thead>
187                                 <tbody>
188                                         <row>
189                                                 <entry align=center><para>0</para></entry>
190                                                 <entry valign=middle><para>OK</para></entry>
191                                                 <entry><para>The plugin was able to check the service and it 
192                                                 appeared to be functioning properly</para></entry>
193                                         </row>
194                                         <row>
195                                                 <entry align=center><para>1</para></entry>
196                                                 <entry valign=middle><para>Warning</para></entry>
197                                                 <entry><para>The plugin was able to check the service, but it 
198                                                 appeared to be above some "warning" threshold or did not appear 
199                                                 to be working properly</para></entry>
200                                         </row>
201                                         <row>
202                                                 <entry align=center><para>2</para></entry>
203                                                 <entry valign=middle><para>Critical</para></entry>
204                                                 <entry><para>The plugin detected that either the service was not 
205                                                 running or it was above some "critical" threshold</para></entry>
206                                         </row>
207                                         <row>
208                                                 <entry align=center><para>3</para></entry>
209                                                 <entry valign=middle><para>Unknown</para></entry>
210                                                 <entry><para>Invalid command line arguments were supplied to the 
211                                                 plugin or the plugin was unable to check the status of the given 
212                                                 hosts/service</para></entry>
213                                         </row>
214                                 </tbody>
215                         </tgroup>
216                 </table>
218       
219                 </section>
222 </section>
224 <section id="SysCmdAuxFiles"><title>System Commands and Auxiliary Files</title>
226                 <section><title>Don't execute system commands without specifying their
227                 full path</title>
228                 <para>Don't use exec(), popen(), etc. to execute external
229                 commands without explicity using the full path of the external
230                 program.</para>
232                 <para>Doing otherwise makes the plugin vulnerable to hijacking
233                 by a trojan horse earlier in the search path. See the main
234                 plugin distribution for examples on how this is done.</para>
235                 </section>
237                 <section><title>Use spopen() if external commands must be executed</title>
239             <para>If you have to execute external commands from within your
240         plugin and you're writing it in C, use the spopen() function
241                 that Karl DeBisschop has written.</para>
243                 <para>The code for spopen() and spclose() is included with the
244                 core plugin distribution.</para>
245                 </section>
247                 <section><title>Don't make temp files unless absolutely required</title>
249                 <para>If temp files are needed, make sure that the plugin will
250                 fail cleanly if the file can't be written (e.g., too few file
251                 handles, out of disk space, incorrect permissions, etc.) and
252                 delete the temp file when processing is complete.</para>
253                 </section>
255         <section><title>Don't be tricked into following symlinks</title>
257                 <para>If your plugin opens any files, take steps to ensure that
258                 you are not following a symlink to another location on the
259                 system.</para>
260                 </section>
262                 <section><title>Validate all input</title>
264                 <para>use routines in utils.c or utils.pm and write more as needed</para>
265                 </section>
267 </section>
268         
272 <section id="PerlPlugin"><title>Perl Plugins</title>
274                 <para>Perl plugins are coded a little more defensively than other
275                 plugins because of embedded Perl.  When configured as such, embedded
276                 Perl Nagios (ePN) requires stricter use of the some of Perl's features.
277                 This section outlines some of the steps needed to use ePN
278                 effectively.</para>
279           
280                 <orderedlist>
281                         
282                         <listitem><para> Do not use BEGIN and END blocks since they will be called 
283                         the first time and when Nagios shuts down with Embedded Perl (ePN).  In 
284                         particular, do not use BEGIN blocks to initialize variables.</para>
285                         </listitem>
286           
287                         <listitem><para>To use utils.pm, you need to provide a full path to the
288                         module in order for it to work with ePN.</para>
289                         
290           <literallayout>
291           e.g.
292                 use lib "/usr/local/nagios/libexec";
293                 use utils qw(...);
294           </literallayout>
295                         </listitem>
297                         <listitem><para>Perl scripts should be called with "-w"</para>
298                         </listitem>
299                         
300                         <listitem><para>All Perl plugins must compile cleanly under "use strict" - i.e. at
301                         least explicitly package names as in "$main::x" or predeclare every
302                         variable. </para>
303                         
305                         <para>Explicitly initialize each varialable in use.  Otherwise with
306                         caching enabled, the plugin will not be recompilied each time, and
307                         therefore Perl will not reinitialize all the variables.  All old
308                         variable values will still be in effect.</para>
309                         </listitem>
310                         
311                         <listitem><para>Do not use < DATA > (these simply do not compile under ePN).</para>
312                         </listitem>
314                         <listitem><para>Do not use named subroutines</para> 
315                         </listitem>
317                         <listitem><para>If writing to a file (perhaps recording
318                         performance data) explicitly close close it.  The plugin never
319                         calls <emphasis role=strong>exit</emphasis>; that is caught by
320                         p1.pl, so output streams are never closed.</para>
321                         </listitem>
322                 
323                         <listitem><para>As in <xref linkend="runtime"> all plugins need 
324                         to monitor their runtime, specially if they are using network
325                         resources.  Use of the <emphasis>alarm</emphasis> is recommended.
326                         Plugins may import a default time out ($TIMEOUT) from utils.pm.
327                         </para>
328                         </listitem>
330                         <listitem><para>Perl plugins should import %ERRORS from utils.pm
331                         and then "exit $ERRORS{'OK'}" rather than "exit 0"
332                         </para>
333                         </listitem>
334                         
335                 </orderedlist>
336           
337 </section>
339 <section id="runtime"><title>Runtime Timeouts</title>
341                 <para>Plugins have a very limited runtime - typically 10 sec.
342                 As a result, it is very important for plugins to maintain internal
343                 code to exit if runtime exceeds a threshold. </para>
345                 <para>All plugins should timeout gracefully, not just networking
346                 plugins. For instance, df may lock if you have automounted
347                 drives and your network fails - but on first glance, who'd think
348                 df could lock up like that.  Plus, it should just be more error
349                 resistant to be able to time out rather than consume
350                 resources.</para>
351                 
352                 <section><title>Use DEFAULT_SOCKET_TIMEOUT</title>
354                 <para>All network plugins should use DEFAULT_SOCKET_TIMEOUT to timeout</para>
356                 </section>
358                 
359                 <section><title>Add alarms to network plugins</title>
361                 <para>If you write a plugin which communicates with another
362                 networked host, you should make sure to set an alarm() in your
363                 code that prevents the plugin from hanging due to abnormal
364                 socket closures, etc. Nagios takes steps to protect itself
365                 against unruly plugins that timeout, but any plugins you create
366                 should be well behaved on their own.</para>
368                 </section>
370                 
372 </section>
374 <section id="PlugOptions"><title>Plugin Options</title>
375         
376                 <para>A well written plugin should have --help as a way to get 
377                 verbose help. Code and output should try to respect the 80x25 size of a
378                 crt (remember when fixing stuff in the server room!)</para>
379                 
380                 <section><title>Option Processing</title>
382                 <para>For plugins written in C, we recommend the C standard
383                 getopt library for short options. Getopt_long is always available.
384                 </para>
386                 <para>For plugins written in Perl, we recommend Getopt::Long module.</para>
388                 <para>Positional arguments are strongly discouraged.</para>
390                 <para>There are a few reserved options that should not be used
391                 for other purposes:</para>
393                 <literallayout>
394           -V version (--version)
395           -h help (--help)
396           -t timeout (--timeout)
397           -w warning threshold (--warning)
398           -c critical threshold (--critical)
399           -H hostname (--hostname)
400           -v verbose (--verbose)
401                 </literallayout>
403                 <para>In addition to the reserved options above, some other standard options are:</para>
405                 <literallayout>
406           -C SNMP community (--community)
407           -a authentication password (--authentication)
408           -l login name (--logname)
409           -p port or password (--port or --passwd/--password)monitors operational
410           -u url or username (--url or --username)
411                 </literallayout>
412           
413                 <para>Look at check_pgsql and check_procs to see how I currently
414                 think this can work.  Standard options are:</para>
416           
417                 <para>The option -V or --version should be present in all
418                 plugins. For C plugins it should result in a call to print_revision, a
419                 function in utils.c which takes two character arguments, the
420                 command name and the plugin revision.</para>
422                 <para>The -? option, or any other unparsable set of options,
423                 should print out a short usage statement. Character width should
424                 be 80 and less and no more that 23 lines should be printed (it
425                 should display cleanly on a dumb terminal in a server
426                 room).</para>
428                 <para>The option -h or --help should be present in all plugins.
429                 In C plugins, it should result in a call to print_help (or
430                 equivalent).  The function print_help should call print_revision, 
431                 then print_usage, then should provide detailed
432                 help. Help text should fit on an 80-character width display, but
433                 may run as many lines as needed.</para>
435                 <para>The option -v or --verbose should be present in all plugins.
436                 The user should be allowed to specify -v multiple times to increase
437                 the verbosity level, as described in <xref linkend="verbose_levels">.</para>
438     </section>
440     <section>
441       <title>Plugins with more than one type of threshold, or with
442       threshold ranges</title>
444       <para>Old style was to do things like -ct for critical time and
445       -cv for critical value. That goes out the window with POSIX
446       getopt. The allowable alternatives are:</para>
448       <orderedlist>
449         <listitem>
450           <para>long options like -critical-time (or -ct and -cv, I
451           suppose).</para>
452         </listitem>
454         <listitem>
455           <para>repeated options like `check_load -w 10 -w 6 -w 4 -c
456           16 -c 10 -c 10`</para>
457         </listitem>
459         <listitem>
460           <para>for brevity, the above can be expressed as `check_load
461           -w 10,6,4 -c 16,10,10`</para>
462         </listitem>
464         <listitem>
465           <para>ranges are expressed with colons as in `check_procs -C
466           httpd -w 1:20 -c 1:30` which will warn above 20 instances,
467           and critical at 0 and above 30</para>
468         </listitem>
470         <listitem>
471           <para>lists are expressed with commas, so Jacob's check_nmap
472           uses constructs like '-p 1000,1010,1050:1060,2000'</para>
473         </listitem>
475         <listitem>
476           <para>If possible when writing lists, use tokens to make the
477           list easy to remember and non-order dependent - so
478           check_disk uses '-c 10000,10%' so that it is clear which is
479           the precentage and which is the KB values (note that due to
480           my own lack of foresight, that used to be '-c 10000:10%' but
481           such constructs should all be changed for consistency,
482           though providing reverse compatibility is fairly
483           easy).</para>
484         </listitem>
486       </orderedlist>
488       <para>As always, comments are welcome - making this consistent
489       without a host of long options was quite a hassle, and I would
490       suspect that there are flaws in this strategy. 
491       </para>
492     </section>
493 </section>
495 <section id="CodingGuidelines"><title>Coding guidelines</title>
496         <para>See <ulink url="http://www.gnu.org/prep/standards_toc.html">GNU
497         Coding standards</ulink> for general guidelines.</para>
498         <section><title>Comments</title>
499         <para>You should use /* */ for comments and not // as some compilers
500         do not handle the latter form.</para>
501         </section>
503         <section><title>CVS comments</title>
504         <para>When adding CVS comments at commit time, you can use the following prefixes:
505         <variablelist>
506           <varlistentry><term>- comment</term>
507           <listitem>
508             <para>for a comment that can be removed from the Changelog</para>
509           </listitem>
510           </varlistentry>
511           <varlistentry><term>* comment</term>
512           <listitem>
513             <para>for an important amendment to be included into a features list</para>
514           </listitem>
515           </varlistentry>
516         </variablelist>
517         </para>
518         </section>
519 </section>
521 <section id="SubmittingChanges"><title>Submission of new plugins and patches</title>
523         <section id="Patches"><title>Patches</title>
524         <para>If you have a bug patch, please supply a unified or context diff against the
525         version you are using. For new features, please supply a diff against
526         the CVS HEAD version.</para>
528         <para>Patches should be submitted via 
529         <ulink url="http://sourceforge.net/tracker/?group_id=29880&amp;atid=397599">SourceForge's
530         tracker system for Nagiosplug patches</ulink> 
531         and be announced to the nagiosplug-devel mailing list.</para>
532         </section>
534         <section id="New_plugins"><title>New plugins</title>
535         <para>If you would like others to use your plugins and have it included in
536         the standard distribution, please include patches for the relevant
537         configuration files, in particular "configure.in". Otherwise submitted 
538         plugins will be included in the contrib directory.</para>
539         
540         <para>Plugins in the contrib directory are going to be migrated to the
541         standard plugins/plugin-scripts directory as time permits and per user
542         requests. The minimum requirements are:</para>
544       <orderedlist>
545         <listitem>
546           <para>The standard command options are supported (--help, --version,
547           --timeout, --warning, --critical)</para>
548         </listitem>
549         <listitem>
550           <para>It is determined to be not redundant (for instance, we would not 
551                 add a new version of check_disk just because someone had provide 
552                 a plugin that had perf checking - we would incorporate the features 
553                 into an exisiting plugin)</para>
554         </listitem>
555         <listitem>
556           <para>One of the developers has had the time to audit the code and declare
557                 it ready for core</para>
558         </listitem>
559         <listitem>
560           <para>It should also follow code format guidelines, and use functions from
561 utils (perl or c or sh) rather than cooking it's own</para>
562         </listitem>
563       </orderedlist>
565         <para>New plugins should be submitted via 
566         <ulink url="http://sourceforge.net/tracker/?group_id=29880&amp;atid=541465">SourceForge's
567         tracker system for Nagiosplug new plugins</ulink> 
568         and be announced to the nagiosplug-devel mailing list.</para>
569         
570         <para>For new plugins, provide a diff to add to the EXTRAS list (configure.in) 
571         unless you are fairly sure that the plugin will work for all platforms with 
572         no non-standard software added.</para>
574         <para>If possible please submit a test harness. Documentation on sample
575         tests coming soon.</para>
576         </section>
578 </section>
580 </article>
581   
582 </book>