root/src/modules-lua/noit/module/nrpe.lua

Revision becc5b19b0ae1b120a67a60de9ac27bdd9841cbc, 6.1 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 3 years ago)

Nagios NRPE support. I love you Lua

  • Property mode set to 100644
Line 
1 -- Copyright (c) 2009, OmniTI Computer Consulting, Inc.
2 -- All rights reserved.
3 --
4 -- Redistribution and use in source and binary forms, with or without
5 -- modification, are permitted provided that the following conditions are
6 -- met:
7 --
8 --     * Redistributions of source code must retain the above copyright
9 --       notice, this list of conditions and the following disclaimer.
10 --     * Redistributions in binary form must reproduce the above
11 --       copyright notice, this list of conditions and the following
12 --       disclaimer in the documentation and/or other materials provided
13 --       with the distribution.
14 --     * Neither the name OmniTI Computer Consulting, Inc. nor the names
15 --       of its contributors may be used to endorse or promote products
16 --       derived from this software without specific prior written
17 --       permission.
18 --
19 -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 -- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 -- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 -- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 -- OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 -- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 -- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 -- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 -- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 -- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 module(..., package.seeall)
32
33 function onload(image)
34   image.xml_description([=[
35 <module>
36   <name>nrpe</name>
37   <description><para>Nagios NRPE (v2) check.</para></description>
38   <loader>lua</loader>
39   <object>noit.module.nrpe</object>
40   <moduleconfig />
41   <checkconfig>
42     <parameter name="port" required="required"
43                allowed="\d+">Specifies the port on which the management interface can be reached.</parameter>
44     <parameter name="use_ssl" required="optional" allowed="^(?:true|false|on|off)$" default="true">Upgrade TCP connection to use SSL.</parameter>
45     <parameter name="command" required="required" allowed=".+">Command to run on the remote node.</parameter>
46   </checkconfig>
47   <examples>
48     <example>
49       <title>Checking load via NRPE.</title>
50       <para>This check runs check_load on the remote host.</para>
51       <programlisting><![CDATA[
52       <noit>
53         <modules>
54           <loader image="lua" name="lua">
55             <config><directory>/opt/reconnoiter/libexec/modules-lua/?.lua</directory></config>
56           </loader>
57           <module loader="lua" name="nrpe" object="noit.module.nrpe" />
58         </modules>
59         <checks>
60           <check uuid="79ba881e-ad2e-11de-9fb0-a322e3288ca7" name="load"
61                  target="10.0.7.2" module="nrpe" period="10000" timeout="5000">
62             <config><comand>check_load</command></config>
63           </check>
64         </checks>
65       </noit>
66     ]]></programlisting>
67     </example>
68   </examples>
69
70 </module>
71 ]=]);
72   return 0
73 end
74
75 function init(module)
76   return 0
77 end
78
79 function config(module, options)
80   return 0
81 end
82
83 function elapsed(check, name, starttime, endtime)
84     local elapsedtime = endtime - starttime
85     local seconds = string.format('%.3f', noit.timeval.seconds(elapsedtime))
86     check.metric_uint32(name, math.floor(seconds * 1000 + 0.5))
87     return seconds
88 end
89
90 function v2_packet(cmd)
91   local payload = string.pack(">h", 2324) .. cmd ..
92                   string.rep(string.char(0), 1024-string.len(cmd)) .. "SR"
93   local crc = noit.crc32(string.char(0,2,0,1,0,0,0,0) .. payload)
94   return string.char(0,2,0,1) .. string.pack(">I", crc) .. payload
95 end
96
97 function initiate(module, check)
98   local starttime = noit.timeval.now()
99   local max_len = 80
100   check.bad()
101   check.unavailable()
102   check.status("unknown error")
103   local good = false
104   local status = ""
105   local use_ssl = true
106   local port = 5666
107
108   if check.config.port ~= nil then
109     port = check.config.port
110   end
111
112   if check.config.command == nil then
113     check.status("no command")
114     return
115   end
116
117   -- SSL
118   if check.config.use_ssl == "false" or check.config.use_ssl == "off" then
119     use_ssl = false
120   end
121
122   local e = noit.socket(check.target_ip)
123   local rv, err = e:connect(check.target_ip, port)
124   if rv ~= 0 then
125     check.status(err or "connect error")
126     return
127   end
128
129   if use_ssl == true then
130     rv, err = e:ssl_upgrade_socket(nil,nil,nil,"ADH-AES256-SHA")
131     if rv ~= 0 then
132       check.status(err or "ssl error")
133       return
134     end
135   end
136
137   local connecttime = noit.timeval.now()
138   elapsed(check, "tt_connect", starttime, connecttime)
139   if rv ~= 0 then
140     check.status(err or "connection failed")
141     return
142   end
143
144   status = "connected"
145   check.available()
146
147   -- run the command
148   local packet = v2_packet(check.config.command)
149   e:write(packet)
150   local response = e:read(1036)
151   if response == nil or response:len() ~= 1036 then
152     check.status("bad packet length " .. response:len());
153     return
154   end
155
156   local cnt, h1, h2, len, r = string.unpack(response, ">HHIH")
157   if h1 ~= 2 or h2 ~= 2 or r > 3 then
158     check.status(string.format("bad packet integrity[%u,%u,%u,%u]",
159                                h1, h2, len, r))
160     return
161   end
162
163   local result = response:sub(cnt):gsub("%z", "")
164
165   local state = result:match("^(%w+)")
166   if state == "OK" then good = true end
167   check.metric_string("state", state)
168
169   local message = result:match("^%w+ %- ([^|]+)")
170   check.metric_string("message", message)
171
172   local metrics = result:match("|(.+)$")
173   if metrics ~= nil then
174     -- /(?<key>\S+)=(?<value>[^;\s]+)(?=[;\s])/g
175     local exre = noit.pcre('(\\S+)=([^;\\s]+)(?=[;\\s])')
176     local rv = true
177     while rv do
178       rv, m, key, value = exre(metrics)
179       if rv and key ~= nil then
180         check.metric(key, value)
181       end
182     end
183   end
184   -- turnaround time
185   local endtime = noit.timeval.now()
186   local seconds = elapsed(check, "duration", starttime, endtime)
187   status = status .. ',rt=' .. seconds .. 's'
188   if good then check.good() else check.bad() end
189   check.status(status)
190 end
191
Note: See TracBrowser for help on using the browser.