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

Revision acf04f0e1c96bcc37c1fac182d8738bd180c8e06, 4.0 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 8 years ago)

make the lua eventer_t wrap stuff act as methods

  • Property mode set to 100644
Line 
1 -- Copyright (c) 2008, 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 -- This connects to a Varnish instance on the management port (8081)
32 -- It issues the stats comment and translates the output into metrics
33
34 module(..., package.seeall)
35
36 function onload(image)
37   image.xml_description([=[
38 <module>
39   <name>varnish</name>
40   <description><para>Monitor maagement metrics of a Varnish instance.</para></description>
41   <loader>lua</loader>
42   <object>noit.module.varnish</object>
43   <moduleconfig />
44   <checkconfig>
45     <parameter name="port" required="optional" default="8081"
46                allowed="\d+">Specifies the port on which the management interface can be reached.</parameter>
47   </checkconfig>
48   <examples>
49     <example>
50       <title>Monitor two varnish instances with management on port 8081</title>
51       <para>The following example pulls are metrics available from Varnish running on 10.1.2.3 and 10.1.2.4</para>
52       <programlisting><![CDATA[
53       <noit>
54         <modules>
55           <loader image="lua" name="lua">
56             <config><directory>/opt/reconnoiter/libexec/modules-lua/?.lua</directory></config>
57           </loader>
58           <module loader="lua" name="varnish" object="noit.module.varnish"/>
59         </modules>
60         <checks>
61           <check uuid="2d42adbc-7c7a-11dd-a48f-4f59e0b654d3" module="varnish" target="10.1.2.3" />
62           <check uuid="324c2234-7c7a-11dd-8585-cbb783f8267f" module="varnish" target="10.1.2.4" />
63         </checks>
64       </noit>
65       ]]></programlisting>
66     </example>
67   </examples>
68 </module>
69 ]=]);
70   return 0
71 end
72
73 function init(module)
74   return 0
75 end
76
77 function config(module, options)
78   return 0
79 end
80
81 function initiate(module, check)
82   local e = noit.socket()
83   local rv, err = e:connect(check.target, check.config.port or 8081)
84
85   e:write("stats\r\n")
86   str = e:read("\n")
87
88   if rv ~= 0 or not str then
89     check.bad()
90     check.unavailable()
91     check.status(err or str or "unknown error")
92     return
93   end
94
95   local status, len = string.match(str, "^(%d+)%s*(%d+)%s*$")
96   if status then check.available() end
97   -- we want status to be a number
98   status = 0 + status
99   if status ~= 200 then
100     check.bad()
101     check.status(string.format("status %d", status))
102     return
103   end
104
105   local rawstats = e:read(len)
106   local i = 0
107   for v, k in string.gmatch(rawstats, "%s*(%d+)%s+([^\r\n]+)") do
108     k = string.gsub(k, "^%s*", "")
109     k = string.gsub(k, "%s*$", "")
110     k = string.gsub(k, "%s", "_")
111     check.metric(k,v)
112     i = i + 1
113   end
114   check.status(string.format("%d stats", i))
115   check.good()
116 end
117
Note: See TracBrowser for help on using the browser.