File: embeddedperl.html

package info (click to toggle)
nagios2 2.6-2%2Betch5
  • links: PTS
  • area: main
  • in suites: etch
  • size: 6,856 kB
  • ctags: 4,475
  • sloc: ansic: 64,870; sh: 4,676; makefile: 787; perl: 722
file content (355 lines) | stat: -rw-r--r-- 12,468 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Using The Embedded Perl Interpreter</title>

<STYLE type="text/css">
<!--
        .Default { font-family: verdana,arial,serif; font-size: 8pt; }
        .PageTitle { font-family: verdana,arial,serif; font-size: 12pt; font-weight: bold; }
-->      
</STYLE>

</head>

<body bgcolor="#FFFFFF" text="black" class="Default">

<p>
<div align="center">
<h2 class="PageTitle">Using The Embedded Perl Interpreter</h2>
</div>
</p>

<hr>

<p>
<strong><u>Introduction</u></strong>
</p>

<p>
Stephen Davies has contributed code that allows you to compile Nagios with an embedded Perl interpreter.  This may be of interest to you if you rely heavily on plugins written in Perl.
</p>
<p>
Stanley Hopcroft has worked with the embedded Perl interpreter quite a bit and has commented on the advantages/disadvanges of using it.  He has also given several helpful hints on creating Perl plugins that work properly with the embedded interpreter.  The majority of this documentation comes from his comments.
</p>
<p>
It should be noted that "ePN", as used in this documentation, refers to embedded Perl Nagios, or if you prefer, Nagios compiled with an embedded Perl interpreter.
</p>

<p>
<strong><u>Advantages</u></strong>
</p>

<p>
Some advantages of ePN (embedded Perl Nagios) include:
</p>

<p>
<ul>

<li>Nagios will spend much less time running your Perl plugins because it no longer forks to execute the plugin (each time loading the Perl interpreter). Instead, it executes your plugin by making a library call.<br><br>

<li>It greatly reduces the system impact of Perl plugins and/or allows you to run more checks with Perl plugin than you otherwise would be able to.  In other words, you have less incentive to write plugins in other languages such as C/C++, or Expect/TCL, that are generally recognised to have development times at least an order of magnitude slower than Perl (although they do run about ten times faster also - TCL being an exception).<br><br>

<li>If you are not a C programmer, then you can still get a huge amount of mileage out of Nagios by letting Perl do all the heavy lifting without having Nagios slow right down.  Note however, that the ePN will not speed up your plugin (apart from eliminating the interpreter load time). If you want fast plugins then consider Perl XSUBs (XS), or C <i>after</i> you are sure that your Perl is tuned and that you have a suitable algorithm (Benchmark.pm is <i>invaluable</i> for comparing the performance of Perl language elements).<br><br>

<li>Using the ePN is an excellent opportunity to learn more about Perl.<br><br>

</ul>
</p>

<p>
<strong><u>Disadvantages</u></strong>
</p>

<p>
The disadvantages of ePN (embedded Perl Nagios) are much the same as Apache mod_perl (i.e. Apache with an embedded interpreter) compared to a plain Apache:
</p>

<p>
<ul>

<li>A Perl program that works <i>fine</i> with plain Nagios may <i>not</i> work with the ePN. You may have to modify your plugins to get them to work.<br><br>

<li>Perl plugins are harder to debug under an ePN than under a plain Nagios.<br><br>

<li>Your ePN will have a larger SIZE (memory footprint) than a plain Nagios.<br><br>

<li>Some Perl constructs cannot be used or may behave differently than what you would expect.<br><br>

<li>You may have to be aware of 'more than one way to do it' and choose a way that seems less attractive or obvious.<br><br>

<li>You will need greater Perl knowledge (but nothing very esoteric or stuff about Perl internals - unless your plugin uses XSUBS).<br><br>

</ul>
</p>


<p>
<strong><u>Target Audience</u></strong>
</p>

<p>
<ul>

<li>Average Perl developers; those with an appreciation of the languages powerful features without knowledge of internals or an in depth knowledge of those features.<br><br>

<li>Those with a utilitarian appreciation rather than a great depth of understanding.<br><br>

<li>If you are happy with Perl objects, name management, data structures, and the debugger, that's probably sufficient.<br><br>

</ul>
</p>


<p>
<strong><u>Things you should do when developing a Perl Plugin (ePN or not)</u></strong>
</p>

<p>
<ul>

<li>Always always generate some output<br><br>

<li>Use 'use utils' and import the stuff it exports ($TIMEOUT %ERRORS &amp;print_revision &amp;support)<br><br>

<li>Have a look at how the standard Perl plugins do their stuff e.g.<br><br>

<ul>
<li>Always exit with $ERRORS{CRITICAL}, $ERRORS{OK}, etc.
<li>Use getopt to read command line arguments
<li>Manage timeouts
<li>Call print_usage (supplied by you) when there are no command line arguments
<li>Use standard switch names (eg H 'host', V 'version')
</ul>

</ul>
</p>


<p>
<strong><u>Things you must do to develop a Perl plugin for ePN</u></strong>
</p>

<p>
<ol>


<li>&lt;DATA&gt; can not be used; use here documents instead e.g.<br><br>
<dir>
<pre>
my $data = &lt;&lt;DATA;
portmapper 100000
portmap 100000
sunrpc 100000
rpcbind 100000
rstatd 100001
rstat 100001
rup 100001
..
DATA

%prognum = map { my($a, $b) = split; ($a, $b) } split(/\n/, $data) ;
</pre>
</dir>

<li>BEGIN blocks will not work as you expect. May be best to avoid.<br><br>

<li>Ensure that it is squeaky clean at compile time i.e.<br><br>
<ul>
<li>use strict
<li>use perl -w (other switches [T notably] may not help)
<li>use perl -c
</ul>

<br><br>

<li>Avoid lexical variables (my) with global scope as a means of passing __variable__ data into subroutines. In fact this is __fatal__ if the subroutine is called by the plugin more than once when the check is run.  Such subroutines act as 'closures' that lock the global lexicals first value into subsequent calls of the subroutine. If however, your global
is read-only (a complicated structure for example) this is not a problem. What Bekman <a href="http://perl.apache.org/guide/">recommends you do instead</a>, is any of the following:<br><br>

<ul>
<li>make the subroutine anonymous and call it via a code ref e.g.<br><br>
<dir>
<pre>
turn this                     into  

my $x = 1 ;                   my $x = 1 ;
sub a { .. Process $x ... }   $a_cr = sub { ... Process $x ... } ;
.                             .
.                             .
a ;                           &amp;$a_cr ;
$x = 2                        $x = 2 ;
a ;                           &amp;$a_cr ;

# anon closures __always__ rebind the current lexical value
</pre>
</dir>

<li>put the global lexical and the subroutine using it in their own package (as an object or a module)

<li>pass info to subs as references or aliases (\$lex_var or $_[n])

<li>replace lexicals with package globals and exclude them from 'use strict' objections with 'use vars qw(global1 global2 ..)'
</ul>

<br>

<li>Be aware of where you can get more information.<br><br>
<dir>
<p>
Useful information can be had from the usual suspects (the O'Reilly books, plus Damien Conways "Object Oriented Perl") but for the really useful stuff in the right context start at Stas Bekman's mod_perl guide at <a href="http://perl.apache.org/guide/">http://perl.apache.org/guide/</a>.
</p>
<p>
This wonderful book sized document has nothing whatsoever about Nagios, but all about writing Perl programs for the embedded Perl interpreter in Apache (ie Doug MacEacherns mod_perl).
</p>
<p>
The perlembed manpage is essential for context and encouragement.
</p>
<p>
On the basis that Lincoln Stein and Doug MacEachern know a thing or two about Perl and embedding Perl, their book 'Writing Apache Modules with Perl and C' is almost certainly worth looking at.
</p>
</dir>

<li>Be aware that your plugin may return strange values with an ePN and that this is likely to be caused by the problem in item #4 above<br><br>


<li>Be prepared to debug via:<br><br>
<ul>
<li>having a test ePN and 
<li>adding print statements to your plugin to display variable values to STDERR (can't use STDOUT)
<li>adding print statements to p1.pl to display what ePN thinks your plugin is before it tries to run it (vi)
<li>running the ePN in foreground mode (probably in conjunction with the former recommendations)
<li>use the 'Deparse' module on your plugin to see how the parser has optimised it and what the interpreter will actually get. (see 'Constants in Perl' by Sean M. Burke, The Perl Journal, Fall 2001)
</ul>

<br>

<dir>
perl -MO::Deparse &lt;your_program&gt;
</dir>

<br>

<li>Be aware of what ePN is transforming your plugin too, and if all else fails try and debug the transformed version.<br><br>

<dir>
<p>
As you can see below p1.pl rewrites your plugin as a subroutine called 'hndlr' in the package named 'Embed::&lt;something_related_to_your_plugin_file_name&gt;'.
</p>
<p>
Your plugin may be expecting command line arguments in @ARGV so pl.pl also assigns @_ to @ARGV.
</p>
<p>
This in turn gets 'eval' ed and if the eval raises an error (any parse error and run error), the plugin gets chucked out.
</p>
<p>
The following output shows how a test ePN transformed the <i>check_rpc</i> plugin before attempting to execute it. Most of the code from the actual plugin is not shown, as we are interested in only the transformations that the ePN has made to the plugin).  For clarity, transformations are shown in red:
</p>

<dir>
<p>
<pre>
<font color="red">
                package main;
                use subs 'CORE::GLOBAL::exit';
                sub CORE::GLOBAL::exit { die "ExitTrap: $_[0] 
(Embed::check_5frpc)"; }
                package Embed::check_5frpc; sub hndlr { shift(@_);
@ARGV=@_;</font>
#! /usr/bin/perl -w
#
# check_rpc plugin for Nagios
#
# usage:
#    check_rpc host service
#
# Check if an rpc serice is registered and running
# using rpcinfo - $proto $host $prognum 2>&1 |";
#
# Use these hosts.cfg entries as examples
#
# command[check_nfs]=/some/path/libexec/check_rpc $HOSTADDRESS$ nfs
# service[check_nfs]=NFS;24x7;3;5;5;unix-admin;60;24x7;1;1;1;;check_rpc
#
# initial version: 3 May 2000 by Truongchinh Nguyen and Karl DeBisschop
# current status: $Revision: 1.26 $
#
# Copyright Notice: GPL
#
<i>... rest of plugin code goes here (it was removed for brevity) ...</i>
<font color="red">}</font>
</pre>
</p>
</dir>

</dir>

<br>

<li>Don't use 'use diagnostics' in a plugin run by your production ePN.  I think it causes__all__ the Perl plugins to return CRITICAL.<br><br>

<li>Consider using a mini embedded Perl C program to check your plugin.  This is not sufficient to guarantee your plugin will perform Ok with an ePN but if the plugin fails this test it will ceratinly fail with your ePN.  <font color="red">[ A sample mini ePN is included in the <i>contrib/</i> directory of the Nagios distribution for use in testing Perl plugins.  Change to the contrib/ directory and type 'make mini_epn' to compile it.  It must be executed from the same directory that the p1.pl file resides in (this file is distributed with Nagios). ]</font> <br><br>


</ol>
</p>


<p>
<strong><u>Compiling Nagios With The Embedded Perl Interpreter</u></strong>
</p>

<p>
Okay, you can breathe again now.  So do you <i>still</i> want to compile Nagios with the embedded Perl interpreter? ;-)
</p>

<p>
If you want to compile Nagios with the embedded Perl interpreter you need to rerun the configure script with the addition of the <font color="red"><i>--enable-embedded-perl</i></font> option.  If you want the embedded interpreter to cache internally compiled scripts, add the <font color="red"><i>--with-perlcache</i></font> option as well.  Example:
</p>

<p>
<font color="red">
<strong>
<pre>
	./configure --enable-embedded-perl --with-perlcache <i>...other options...</i>
</pre>
</strong>
</font>
</p>

<p>
Once you've rerun the configure script with the new options, make sure to recompile Nagios.  You can check to make sure that Nagios has been compile with the embedded Perl interpreter by executing it with the <i>-m</i> command-line argument.  Output from executing the command will look something like this (notice that the embedded perl interpreter is listed in the options section):
</p>

<p>
<pre>
	[nagios@firestore ]# ./nagios -m

	Nagios 1.0a0
	Copyright (c) 1999-2001 Ethan Galstad (nagios@nagios.org)
	Last Modified: 07-03-2001
	License: GPL

	External Data I/O
	-----------------
	Object Data:      DEFAULT
	Status Data:      DEFAULT
	Retention Data:   DEFAULT
	Comment Data:     DEFAULT
	Downtime Data:    DEFAULT
	Performance Data: DEFAULT


	Options
	-------
	* Embedded Perl compiler (With caching)

</pre>
</p>


<hr>

</body>
</html>