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

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

support creating ipv6 sockets when we should, fixes #343

  • Property mode set to 100644
Line 
1 -- Copyright (c) 2010, Michal Taborsky <michal.taborsky@nrholding.com>
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 Memcache instance on the standard port (11211)
32 -- It issues the stats command and translates the output into metrics
33
34 module(..., package.seeall)
35
36 function onload(image)
37   image.xml_description([=[
38 <module>
39   <name>memcached</name>
40   <description><para>Monitor memcache usage metrics.</para></description>
41   <loader>lua</loader>
42   <object>noit.module.memcached</object>
43   <moduleconfig />
44   <checkconfig>
45     <parameter name="port" required="optional" default="11211"
46                allowed="\d+">Specifies the port on which the memcache interface can be reached.</parameter>
47   </checkconfig>
48   <examples>
49     <example>
50       <title>Monitor two memcache instances with management on port 11211</title>
51       <para>The following example pulls all metrics available from memcached running on 10.1.2.3 and 10.1.2.4 using standard port</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="memcached" object="noit.module.memcached"/>
59         </modules>
60         <checks>
61           <check uuid="2d42adbc-7c7a-11dd-a48f-4f59e0b654d3" module="memcached" target="10.1.2.3" />
62           <check uuid="324c2234-7c7a-11dd-8585-cbb783f8267f" module="memcached" 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
83   -- connect to the memcache instance
84   local e = noit.socket(check.target_ip)
85   local rv, err = e:connect(check.target_ip, check.config.port or 11211)
86
87   -- issue the stats command and read everything until the END keyword
88   e:write("stats\r\n")
89   str = e:read("END")
90
91   if rv ~= 0 or not str then
92     -- something went horribly wrong, die with error
93     check.bad()
94     check.unavailable()
95     check.status(err or str or "unknown error")
96     return
97   end
98
99   local i = 0
100  
101   -- parse the output and extract the statistics
102   -- the stats are in the following format:
103   --    STAT metric_name metric_value
104
105   for k, v in string.gmatch(str, "STAT%s+([%w_]+)%s+([%d.]+)%c+") do
106    
107     -- decide which metric is of what type
108     if k == "pid" or k == "accepting_conns" or k == "version" or k == "pointer_size" then 
109       -- these look like numbers, but in fact they are better represented as strings
110       -- because for example we never want to make an average out of them
111       check.metric_string(k,v)
112     elseif k == "rusage_user" or k == "rusage_system" then
113       -- these are real numbers in seconds and fractions of a second
114       check.metric_double(k,v)
115     elseif k == "curr_connections" or k == "connection_structures" or k == "threads" then
116       -- these are always small positive numbers
117       check.metric_uint32(k,v)
118     else
119       -- everything else there can be potentially a big positive number
120       check.metric_uint64(k,v)
121     end
122      
123     i = i + 1
124    
125   end
126
127   -- all went well, report success
128   check.status(string.format("%d stats", i))
129   check.available()
130   check.good()
131 end
132
Note: See TracBrowser for help on using the browser.