Include notionflux in the main repo instead of its own submodule
[notion/jeffpc.git] / contrib / statusd / statusd_nginfo.lua
blobe6af4e026f67f73dab47ff11902047ca86b04711
1 -- Authors: Raffaello Pelagalli <raffa@niah.org>
2 -- License: LGPL, version 2.1 or later
3 -- Last Changed: 2008-05-08
4 --
5 -- statusd_nginfo.lua
6 --
7 -- Made by Raffaello Pelagalli (raffa at niah.org)
8 --
9 -- Started on Sun Mar 9 00:22:31 2008 Raffaello Pelagalli
10 -- Last update Thu May 8 23:29:32 2008 Raffaello Pelagalli
11 --
12 -- This library is free software; you can redistribute it and/or
13 -- modify it under the terms of the GNU Lesser General Public
14 -- License as published by the Free Software Foundation; either
15 -- version 2.1 of the License, or (at your option) any later version.
16 --
17 -- This library is distributed in the hope that it will be useful,
18 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
19 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 -- Lesser General Public License for more details.
21 --
22 -- You should have received a copy of the GNU Lesser General Public
23 -- License along with this library; if not, write to the Free Software
24 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 -- 02111-1307 USA
28 -- Nagios checking script
29 -- Reports nagios status in ion status bar
30 -- Sample configuration:
31 -- mod_statusbar.launch_statusd{
32 -- ...
33 -- nginfo = {
34 -- urls = {
35 -- "http://user1:password1@server1.domain1.tld/cgi-bin/nagios2/nginfo.pl",
36 -- "http://user2:password2@server2.domain2.tld/nagios/cgi-bin/nginfo.pl",
37 -- },
38 -- }
39 -- ...
40 -- }
42 -- Need to be used with nginfo.pl script from
43 -- http://redstack.net/blog/index.php/2008/05/08/nagios-status-report-in-ion3-statusbar.html
45 require "lxp"
46 local ng_timer
47 local error = false
49 local status = {0, 0, 0, 0}
51 local defaults = {
52 update_interval=30*1000,
53 urls = { },
56 local settings = table.join(statusd.get_config("nginfo"), defaults)
58 nginfo_callbacks = {
59 StartElement = function (parser, name)
60 if (name == "current_state") then
61 nginfo_callbacks.CharacterData = function (parser, val)
62 status[tonumber(val) + 1] =
63 status[tonumber(val) + 1] + 1
64 end
65 end
66 end,
67 EndElement = function (parser, name)
68 if (name == "current_state") then
69 nginfo_callbacks.CharacterData = false
70 end
71 end,
72 CharacterData = false,
75 function parse (data)
76 p = lxp.new(nginfo_callbacks)
77 p:parse(b)
78 p:close()
79 end
81 function get_nginfo ()
82 status = {0, 0, 0, 0}
83 error = false
84 local http = require("socket.http")
85 socket.http.TIMEOUT=10
86 local errstr = " ERROR while reading data"
87 for n, url in pairs(settings.urls) do
88 b, c, h = http.request(url)
89 if not (c == 200) then
90 error = true
91 errstr = errstr .. " (NET " .. tostring(c) .. ")"
92 else
93 local st, err = pcall(parse, b)
94 if not st then
95 error = true
96 errstr = errstr .. " (XML" .. err .. ")"
97 end
98 end
99 end
101 if not error then
102 errstr = ""
104 return "OK: " .. tostring(status[1])
105 .. ", WARN: " .. tostring(status[2])
106 .. ", ERROR: " .. tostring(status[3])
107 .. ", UNKN: " .. tostring(status[4])
108 .. errstr
111 local function update_nginfo()
112 statusd.inform("nginfo", get_nginfo())
113 if (status[3] > 0 or status[4] > 0) then
114 statusd.inform("nginfo_hint", "critical")
115 elseif (status[2] > 0) then
116 statusd.inform("nginfo_hint", "important")
117 else
118 statusd.inform("nginfo_hint", "normal")
120 ng_timer:set(settings.update_interval, update_nginfo)
123 -- Init
124 ng_timer=statusd.create_timer()
125 update_nginfo()