developer-guidelines.sgml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
  2. <book>
  3. <title>Nagios Plug-in Developer Guidelines</title>
  4. <bookinfo>
  5. <authorgroup>
  6. <author>
  7. <firstname>Karl</firstname>
  8. <surname>DeBisschop</surname>
  9. <affiliation>
  10. <address><email>karl@debisschop.net</email></address>
  11. </affiliation>
  12. </author>
  13. <author>
  14. <firstname>Ethan</firstname>
  15. <surname>Galstad</surname>
  16. <authorblurb>
  17. <para>Author of Nagios</para>
  18. <para><ulink url="http://www.nagios.org"></ulink></para>
  19. </authorblurb>
  20. <affiliation>
  21. <address><email>netsaint@linuxbox.com</email></address>
  22. </affiliation>
  23. </author>
  24. <author>
  25. <firstname>Hugo</firstname>
  26. <surname>Gayosso</surname>
  27. <affiliation>
  28. <address><email>hgayosso@gnu.org</email></address>
  29. </affiliation>
  30. </author>
  31. <author>
  32. <firstname>Subhendu</firstname>
  33. <surname>Ghosh</surname>
  34. <affiliation>
  35. <address><email>sghosh@sourceforge.net</email></address>
  36. </affiliation>
  37. </author>
  38. <author>
  39. <firstname>Stanley</firstname>
  40. <surname>Hopcroft</surname>
  41. <affiliation>
  42. <address><email>stanleyhopcroft@sourceforge.net</email></address>
  43. </affiliation>
  44. </author>
  45. </authorgroup>
  46. <pubdate>2002</pubdate>
  47. <title>Nagios plug-in development guidelines</title>
  48. <revhistory>
  49. <revision>
  50. <revnumber>0.4</revnumber>
  51. <date>2 May 2002</date>
  52. </revision>
  53. </revhistory>
  54. <copyright>
  55. <year>2000 2001 2002</year>
  56. <holder>Karl DeBisschop, Ethan Galstad,
  57. Hugo Gayosso, Stanley Hopcroft, Subhendu Ghosh</holder>
  58. </copyright>
  59. </bookinfo>
  60. <preface id=preface>
  61. <title>About the guidelines</title>
  62. <para>The purpose of this guidelines is to provide a reference for
  63. the plug-in developers and encourage the standarization of the
  64. different kind of plug-ins: C, shell, perl, python, etc.</para>
  65. <section> <title>Copyright</title>
  66. <para>Nagios Plug-in Development Guidelines Copyright (C) 2000 2001
  67. 2002
  68. Karl DeBisschop, Ethan Galstad, Hugo Gayosso, Stanley Hopcroft,
  69. Subhendu Ghosh</para>
  70. <para>Permission is granted to make and distribute verbatim
  71. copies of this manual provided the copyright notice and this
  72. permission notice are preserved on all copies.</para>
  73. <para>The plugins themselves are copyrighted by their respective
  74. authors.</para>
  75. </section>
  76. </preface>
  77. <article>
  78. <section id="PlugOutput"><title>Plugin Output for Nagios</title>
  79. <para>You should always print something to STDOUT that tells if the
  80. service is working or why its failing. Try to keep the output short -
  81. probably less that 80 characters. Remember that you ideally would like
  82. the entire output to appear in a pager message, which will get chopped
  83. off after a certain length.</para>
  84. <section><title>Print only one line of text</title>
  85. <para>Nagios will only grab the first line of text from STDOUT
  86. when it notifies contacts about potential problems. If you print
  87. multiple lines, you're out of luck. Remember, keep it short and
  88. to the point.</para>
  89. </section>
  90. <section><title>Screen Output</title>
  91. <para>The plug-in should print the diagnostic and just the
  92. synopsis part of the help message. A well written plugin would
  93. then have --help as a way to get the verbose help.</para>
  94. <para>Code and output should try to respect the 80x25 size of a
  95. crt (remember when fixing stuff in the server room!)</para>
  96. </section>
  97. <section><title>Return the proper status code</title>
  98. <para>See <xref linkend="ReturnCodes"> below
  99. for the numeric values of status codes and their
  100. description. Remember to return an UNKNOWN state if bogus or
  101. invalid command line arguments are supplied or it you are unable
  102. to check the service.</para>
  103. </section>
  104. <section><title>Plugin Return Codes</title>
  105. <para>The return codes below are based on the POSIX spec of returning
  106. a positive value. Netsaint prior to v0.0.7 supported non-POSIX
  107. compliant return code of "-1" for unknown. Nagios supports POSIX return
  108. codes by default.</para>
  109. <para>Note: Some plugins will on occasion print on STDOUT that an error
  110. occurred and error code is 138 or 255 or some such number. These
  111. are usually caused by plugins using system commands and having not
  112. enough checks to catch unexpected output. Developers should include a
  113. default catch-all for system command output that returns an UNKOWN
  114. return code.</para>
  115. <table id="ReturnCodes"><title>Plugin Return Codes</title>
  116. <tgroup cols="3">
  117. <thead>
  118. <row>
  119. <entry><para>Numeric Value</para></entry>
  120. <entry><para>Service Status</para></entry>
  121. <entry><para>Status Description</para></entry>
  122. </row>
  123. </thead>
  124. <tbody>
  125. <row>
  126. <entry align=center><para>0</para></entry>
  127. <entry valign=middle><para>OK</para></entry>
  128. <entry><para>The plugin was able to check the service and it
  129. appeared to be functioning properly</para></entry>
  130. </row>
  131. <row>
  132. <entry align=center><para>1</para></entry>
  133. <entry valign=middle><para>Warning</para></entry>
  134. <entry><para>The plugin was able to check the service, but it
  135. appeared to be above some "warning" threshold or did not appear
  136. to be working properly</para></entry>
  137. </row>
  138. <row>
  139. <entry align=center><para>2</para></entry>
  140. <entry valign=middle><para>Critical</para></entry>
  141. <entry><para>The plugin detected that either the service was not
  142. running or it was above some "critical" threshold</para></entry>
  143. </row>
  144. <row>
  145. <entry align=center><para>3</para></entry>
  146. <entry valign=middle><para>Unknown</para></entry>
  147. <entry><para>Invalid command line arguments were supplied to the
  148. plugin or the plugin was unable to check the status of the given
  149. hosts/service</para></entry>
  150. </row>
  151. </tbody>
  152. </tgroup>
  153. </table>
  154. </section>
  155. </section>
  156. <section id="SysCmdAuxFiles"><title>System Commands and Auxiliary Files</title>
  157. <section><title>Don't execute system commands without specifying their
  158. full path</title>
  159. <para>Don't use exec(), popen(), etc. to execute external
  160. commands without explicity using the full path of the external
  161. program.</para>
  162. <para>Doing otherwise makes the plugin vulnerable to hijacking
  163. by a trojan horse earlier in the search path. See the main
  164. plugin distribution for examples on how this is done.</para>
  165. </section>
  166. <section><title>Use spopen() if external commands must be executed</title>
  167. <para>If you have to execute external commands from within your
  168. plugin and you're writing it in C, use the spopen() function
  169. that Karl DeBisschop has written.</para>
  170. <para>The code for spopen() and spclose() is included with the
  171. core plugin distribution.</para>
  172. </section>
  173. <section><title>Don't make temp files unless absolutely required</title>
  174. <para>If temp files are needed, make sure that the plugin will
  175. fail cleanly if the file can't be written (e.g., too few file
  176. handles, out of disk space, incorrect permissions, etc.) and
  177. delete the temp file when processing is complete.</para>
  178. </section>
  179. <section><title>Don't be tricked into following symlinks</title>
  180. <para>If your plugin opens any files, take steps to ensure that
  181. you are not following a symlink to another location on the
  182. system.</para>
  183. </section>
  184. <section><title>Validate all input</title>
  185. <para>use routines in utils.c or utils.pm and write more as needed</para>
  186. </section>
  187. </section>
  188. <section id="PerlPlugin"><title>Perl Plugins</title>
  189. <para>Perl plugins are coded a little more defensively than other
  190. plugins because of embedded Perl. When configured as such, embedded
  191. Perl Nagios (ePN) requires stricter use of the some of Perl's features.
  192. This section outlines some of the steps needed to use ePN
  193. effectively.</para>
  194. <orderedlist>
  195. <listitem><para> Do not use BEGIN and END blocks since they will be called
  196. the first time and when Nagios shuts down with Embedded Perl (ePN). In
  197. particular, do not use BEGIN blocks to initialize variables.</para>
  198. </listitem>
  199. <listitem><para>To use utils.pm, you need to provide a full path to the
  200. module in order for it to work with ePN.</para>
  201. <literallayout>
  202. e.g.
  203. use lib "/usr/local/nagios/libexec";
  204. use utils qw(...);
  205. </literallayout>
  206. </listitem>
  207. <listitem><para>Perl scripts should be called with "-w"</para>
  208. </listitem>
  209. <listitem><para>All Perl plugins must compile cleanly under "use strict" - i.e. at
  210. least explicitly package names as in "$main::x" or predeclare every
  211. variable. </para>
  212. <para>Explicitly initialize each varialable in use. Otherwise with
  213. caching enabled, the plugin will not be recompilied each time, and
  214. therefore Perl will not reinitialize all the variables. All old
  215. variable values will still be in effect.</para>
  216. </listitem>
  217. <listitem><para>Do not use < DATA > (these simply do not compile under ePN).</para>
  218. </listitem>
  219. <listitem><para>Do not use named subroutines</para>
  220. </listitem>
  221. <listitem><para>If writing to a file (perhaps recording
  222. performance data) explicitly close close it. The plugin never
  223. calls <emphasis role=strong>exit</emphasis>; that is caught by
  224. p1.pl, so output streams are never closed.</para>
  225. </listitem>
  226. <listitem><para>As in <xref linkend="runtime"> all plugins need
  227. to monitor their runtime, specially if they are using network
  228. resources. Use of the <emphasis>alarm</emphasis> is recommended.
  229. Plugins may import a default time out ($TIMEOUT) from utils.pm.
  230. </para>
  231. </listitem>
  232. <listitem><para>Perl plugins should import %ERRORS from utils.pm
  233. and then "exit $ERRORS{'OK'}" rather than "exit 0"
  234. </para>
  235. </listitem>
  236. </orderedlist>
  237. </section>
  238. <section id="runtime"><title>Runtime Timeouts</title>
  239. <para>Plugins have a very limited runtime - typically 10 sec.
  240. As a result, it is very important for plugins to maintain internal
  241. code to exit if runtime exceeds a threshold. </para>
  242. <para>All plugins should timeout gracefully, not just networking
  243. plugins. For instance, df may lock if you have automounted
  244. drives and your network fails - but on first glance, who'd think
  245. df could lock up like that. Plus, it should just be more error
  246. resistant to be able to time out rather than consume
  247. resources.</para>
  248. <section><title>Use DEFAULT_SOCKET_TIMEOUT</title>
  249. <para>All network plugins should use DEFAULT_SOCKET_TIMEOUT to timeout</para>
  250. </section>
  251. <section><title>Add alarms to network plugins</title>
  252. <para>If you write a plugin which communicates with another
  253. networked host, you should make sure to set an alarm() in your
  254. code that prevents the plugin from hanging due to abnormal
  255. socket closures, etc. Nagios takes steps to protect itself
  256. against unruly plugins that timeout, but any plugins you create
  257. should be well behaved on their own.</para>
  258. </section>
  259. </section>
  260. <section id="PlugOptions"><title>Plugin Options</title>
  261. <para>A well written plugin should have --help as a way to get
  262. verbose help. Code and output should try to respect the 80x25 size of a
  263. crt (remember when fixing stuff in the server room!)</para>
  264. <section><title>Option Processing</title>
  265. <para>For plugins written in C, we recommend the C standard
  266. getopt library for short options. If using getopt_long, check to
  267. be sure that HAVE_GETOPT_H is defined (configure checks this and
  268. sets the #define in common/config.h).</para>
  269. <para>For plugins written in Perl, we recommend Getopt::Long module.</para>
  270. <para>Positional arguments are strongly discouraged.</para>
  271. <para>There are a few reserved options that should not be used
  272. for other purposes:</para>
  273. <literallayout>
  274. -V version (--version)
  275. -h help (--help)
  276. -t timeout (--timeout)
  277. -w warning threshold (--warning)
  278. -c critical threshold (--critical)
  279. -H hostname (--hostname)
  280. </literallayout>
  281. <para>In addition to the reserved options above, some other standard options are:</para>
  282. <literallayout>
  283. -C SNMP community (--community)
  284. -a authentication password (--authentication)
  285. -l login name (--logname)
  286. -p port or password (--port or --passwd/--password)monitors operational
  287. -u url or username (--url or --username)
  288. </literallayout>
  289. <para>Look at check_pgsql and check_procs to see how I currently
  290. think this can work. Standard options are:</para>
  291. <para>The option -V or --version should be present in all
  292. plugins. For C plugins it should result in a call to print_revision, a
  293. function in utils.c which takes two character arguments, the
  294. command name and the plugin revision.</para>
  295. <para>The -? option, or any other unparsable set of options,
  296. should print out a short usage statement. Character width should
  297. be 80 and less and no more that 23 lines should be printed (it
  298. should display cleanly on a dumb terminal in a server
  299. room).</para>
  300. <para>The option -h or --help should be present in all plugins.
  301. In C plugins, it should result in a call to print_help (or
  302. equivalent). The function print_help should call print_revision,
  303. then print_usage, then should provide detailed
  304. help. Help text should fit on an 80-character width display, but
  305. may run as many lines as needed.</para>
  306. </section>
  307. <section>
  308. <title>Plugins with more than one type of threshold, or with
  309. threshold ranges</title>
  310. <para>Old style was to do things like -ct for critical time and
  311. -cv for critical value. That goes out the window with POSIX
  312. getopt. The allowable alternatves are:</para>
  313. <orderedlist>
  314. <listitem>
  315. <para>long options like -critical-time (or -ct and -cv, I
  316. suppose).</para>
  317. </listitem>
  318. <listitem>
  319. <para>repeated options like `check_load -w 10 -w 6 -w 4 -c
  320. 16 -c 10 -c 10`</para>
  321. </listitem>
  322. <listitem>
  323. <para>for brevity, the above can be expressed as `check_load
  324. -w 10,6,4 -c 16,10,10`</para>
  325. </listitem>
  326. <listitem>
  327. <para>ranges are expressed with colons as in `check_procs -C
  328. httpd -w 1:20 -c 1:30` which will warn above 20 instances,
  329. and critical at 0 and above 30</para>
  330. </listitem>
  331. <listitem>
  332. <para>lists are expressed with commas, so Jacob's check_nmap
  333. uses constructs like '-p 1000,1010,1050:1060,2000'</para>
  334. </listitem>
  335. <listitem>
  336. <para>If possible when writing lists, use tokens to make the
  337. list easy to remember and non-order dependent - so
  338. check_disk uses '-c 10000,10%' so that it is clear which is
  339. the precentage and which is the KB values (note that due to
  340. my own lack of foresight, that used to be '-c 10000:10%' but
  341. such constructs should all be changed for consistency,
  342. though providing reverse compatibility is fairly
  343. easy).</para>
  344. </listitem>
  345. </orderedlist>
  346. <para>As always, comments are welcome - making this consistent
  347. without a host of long options was quite a hassle, and I would
  348. suspect that there are flaws in this strategy. Perhaps clear
  349. long-options is the most important of the above choices, but not
  350. all POSIX systems have C libraries for long options, so the
  351. short forms must exist as well.</para>
  352. </section>
  353. </section>
  354. <section id="SubmittingChanges"><title>New submissions and patches</title>
  355. <para>If you would like other to use your plugins and have it included in
  356. the standard distribution, please include patches for the relavant
  357. configuration files, in particular "configure.in" Otherwise submitted
  358. plugins will be included in the contrib directory.</para>
  359. <para>Plugins in the contrib directory are going to be migrated to the
  360. standard plugins/plugin-scripts directory as time permits and per user
  361. requests</para>
  362. <para>Patches should be submitted via the SourceForge and be announced to
  363. the mailing list.</para>
  364. <para>For new plugins, provide a diff to add to the EXTRAS list (configure.in)
  365. unless you are fairly sure that the plugin will work for all platforms with
  366. no non-standard software added.</para>
  367. <para>If possible please submit a test harness. Documentation on sample
  368. tests coming soon.</para>
  369. </section>
  370. </article>
  371. </book>