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

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

refs #340

This is a rather major change. Targets can now be hostnames in addition
to IP addresses so long as //checks/@resolve_targets is not false.

If a target is entered that does not look like an IP (inet_pton fails)
then the check is marked as needing resolution NP_RESOLVE.

A passive look-aside dns cache has been implemented in noit_check_resolver.c
that is used to power the whole system and some reasonably simply console
command have been provided:

show dns_cache [fqdn1 [fqdn2]] -- shows the state
dns_cache <fqdn> [fqdn2 [fqdn3]] -- submits for lookup
no dns_cache <fqdn> [fqdn2 [fqdn3]] -- purges from cache

The big change is that modules that relied on check->target to be an IP
address are going to explode when names are provided. Instead, modules
should now use target for the provided target (possibly a FQDN) and use
target_ip (check->target_ip or check.target_ip) for a resolved IP address
and also check for the case of empty string: (check->target_ip[0] == '\0')
for the case that resolution has failed. In lua, the target_ip will be
presented as nil in the case of failed name resolution.

I believe I've updated all necessary components of the system for this to
"just work" but people that have implemented their own check should update
them before they elect to use non-IP addresses as targets.

The dns subsystem supports both IPv4 and IPv6, but currently prefers IPv4
addresses if any are present.

  • Property mode set to 100644
Line 
1 -- Copyright (c) 2010, 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>jezebel</name>
37   <description><para>The jezebel module performs services checks against jezebel and simplifies its special-case Resmon output.</para>
38   <para><ulink url="https://labs.omniti.com/trac/resmon"><citetitle>Resmon</citetitle></ulink> is a light-weight resource monitor that exposes health of services over HTTP in XML.</para>
39   <para>This module rides on the http module and provides a secondary phase of XML parsing on the contents that extracts Resmon status messages into metrics that can be trended.</para>
40   </description>
41   <loader>lua</loader>
42   <object>noit.module.jezebel</object>
43   <moduleconfig>
44     <parameter name="url"
45                required="required"
46                allowed=".+">The URL including schema and hostname (as you would type into a browser's location bar).</parameter>
47     <parameter name="port"
48                required="optional"
49                allowed="\d+">The TCP port can be specified to overide the default of 81.</parameter>
50   </moduleconfig>
51   <checkconfig>
52     <parameter name=".+" required="optional" allowed=".*">All check config values are passed through to jezebel for execution.</parameter>
53   </checkconfig>
54   <examples>
55     <example>
56       <title>Checking resmon services on OmniTI Labs.</title>
57       <para>This example checks the Resmon service on OmniTI Labs.</para>
58       <programlisting><![CDATA[
59       <noit>
60         <modules>
61           <loader image="lua" name="lua">
62             <config><directory>/opt/reconnoiter/libexec/modules-lua/?.lua</directory></config>
63           </loader>
64           <jezebel>
65             <config>
66               <url>http://127.0.0.1:8083/dispatch/</url>
67             </config>
68             <module loader="lua" name="com.omniti.jezebel.SampleCheck"
69                     object="noit.module.jezebel"/>
70           </jezebel>
71         </modules>
72         <checks>
73           <labs target="8.8.38.5" module="com.omniti.jezebel.SampleCheck">
74             <check uuid="36b8ba72-7968-11dd-a67f-d39a2cc3f9de"/>
75           </labs>
76         </checks>
77       </noit>
78     ]]></programlisting>
79     </example>
80   </examples>
81 </module>
82 ]=]);
83   return 0
84 end
85
86 function init(module)
87   return 0
88 end
89
90 local configs = {}
91
92 function config(module, options)
93   configs[module.name()] = options
94   return 0
95 end
96
97 local HttpClient = require 'noit.HttpClient'
98
99 function constructXml(check)
100   local doc = noit.parsexml("<check/>")
101   local root = doc:root()
102   root:attr("target", check.target)
103   if(check.target_ip ~= nil) then
104     root:attr("target_ip", check.target_ip)
105   end
106   root:attr("module", check.module)
107   root:attr("name", check.name)
108   root:attr("period", check.period)
109   root:attr("timeout", check.timeout)
110   local config = root:addchild("config")
111   for key, value in pairs(check.config) do
112     config:addchild(key):contents(value)
113   end
114   return doc:tostring()
115 end
116
117 function initiate(module, check)
118     local options = configs[module.name()]
119     local url = options.url or 'http:///'
120     url = url .. '/' .. module.name()
121     local schema, host, port, uri =
122         string.match(url, "^(https?)://([^/:]*):?([0-9]*)(.+)$");
123     local use_ssl = false
124     local good = false
125
126     -- assume the worst.
127     check.bad()
128     check.unavailable()
129
130     if schema == 'http' then
131         port = port or 80
132     elseif schema == 'https' then
133         port = port or 443
134         use_ssl = true
135     else
136         error(schema .. " not supported")
137     end
138
139     local output = ''
140
141     -- callbacks from the HttpClient
142     local callbacks = { }
143     callbacks.consume = function (str) output = output .. str end
144     local client = HttpClient:new(callbacks)
145     local rv, err = client:connect(host, port, use_ssl)
146    
147     if rv ~= 0 then
148         if err ~= nil then err = "jezebel: " .. err end
149         check.status(err or "jezebel: unknown error")
150         return
151     end
152
153     -- perform the request
154     local headers = {}
155     headers.Host = host
156     local xml = constructXml(check)
157     client:do_request("POST", uri, headers, xml)
158     client:get_response()
159
160     -- parse the xml doc
161     local doc = noit.parsexml(output)
162     if doc == nil then
163         noit.log("error", "bad xml: %s\n", output)
164         check.status("bad xml from jezebel")
165         return
166     end
167     check.available()
168
169     local services = 0
170     local metrics = 0
171     local result
172     local status
173     for result in doc:xpath("/ResmonResults/ResmonResult") do
174         services = services + 1
175         local obj
176         for metric in doc:xpath("metric", result) do
177             metrics = metrics + 1
178             local name = metric:attr("name") or "DUMMY"
179             local type = metric:attr("type") or "DUMMY"
180             local value = metric and metric:contents()
181             if name == 'jezebel_status' then
182                 status = value
183             elseif type == 'i' then
184                 check.metric_int32(name, value)
185             elseif type == 'I' then
186                 check.metric_uint32(name, value)
187             elseif type == 'l' then
188                 check.metric_int64(name, value)
189             elseif type == 'L' then
190                 check.metric_uint64(name, value)
191             elseif type == 'n' then
192                 check.metric_double(name, value)
193             elseif type == 's' then
194                 check.metric_string(name, value)
195             else
196                 check.metric(name, value)
197             end
198         end
199     end
200     if services == 1 and metrics > 0 then check.good() else check.bad() end
201     if status == nil then status = 'results=' .. metrics end
202     check.status(status)
203 end
204
Note: See TracBrowser for help on using the browser.