prometheus-node-exporter-lua: use uhttpd-mod-lua
listen_ipv6 config option is removed and we now listen on both ipv4 and ipv6 addresses. HTTP keepalive is enabled and set to 70s by default. With uhttpd-mod-lua there is a small change in behavior, all code is loaded/parsed/executed once on startup as before, but now each request is executed in his own fork, so we can't keep a state between requests. Signed-off-by: Etienne Champetier <champetier.etienne@gmail.com>
This commit is contained in:
parent
3afccecdb2
commit
60460f0046
4 changed files with 32 additions and 64 deletions
|
@ -4,7 +4,7 @@
|
||||||
include $(TOPDIR)/rules.mk
|
include $(TOPDIR)/rules.mk
|
||||||
|
|
||||||
PKG_NAME:=prometheus-node-exporter-lua
|
PKG_NAME:=prometheus-node-exporter-lua
|
||||||
PKG_VERSION:=2022.04.18
|
PKG_VERSION:=2022.04.23
|
||||||
PKG_RELEASE:=1
|
PKG_RELEASE:=1
|
||||||
|
|
||||||
PKG_MAINTAINER:=Etienne CHAMPETIER <champetier.etienne@gmail.com>
|
PKG_MAINTAINER:=Etienne CHAMPETIER <champetier.etienne@gmail.com>
|
||||||
|
@ -23,7 +23,7 @@ endef
|
||||||
|
|
||||||
define Package/prometheus-node-exporter-lua
|
define Package/prometheus-node-exporter-lua
|
||||||
$(call Package/prometheus-node-exporter-lua/Default)
|
$(call Package/prometheus-node-exporter-lua/Default)
|
||||||
DEPENDS:=+luasocket +lua
|
DEPENDS:=+luasocket +lua +uhttpd +uhttpd-mod-lua
|
||||||
endef
|
endef
|
||||||
|
|
||||||
define Package/prometheus-node-exporter-lua/install
|
define Package/prometheus-node-exporter-lua/install
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
config prometheus-node-exporter-lua 'main'
|
config prometheus-node-exporter-lua 'main'
|
||||||
option listen_interface 'loopback'
|
option listen_interface 'loopback'
|
||||||
option listen_ipv6 '0'
|
|
||||||
option listen_port '9100'
|
option listen_port '9100'
|
||||||
|
|
|
@ -11,33 +11,30 @@ _log() {
|
||||||
start_service() {
|
start_service() {
|
||||||
. /lib/functions/network.sh
|
. /lib/functions/network.sh
|
||||||
|
|
||||||
local interface ipv6 port bind
|
local interface port bind
|
||||||
|
|
||||||
config_load prometheus-node-exporter-lua.main
|
config_load prometheus-node-exporter-lua.main
|
||||||
|
config_get keepalive "main" http_keepalive 70
|
||||||
config_get interface "main" listen_interface "loopback"
|
config_get interface "main" listen_interface "loopback"
|
||||||
config_get_bool ipv6 "main" listen_ipv6 0
|
|
||||||
config_get port "main" listen_port 9100
|
config_get port "main" listen_port 9100
|
||||||
|
|
||||||
if [ "$interface" = "*" ]; then
|
|
||||||
[ "$ipv6" = 1 ] && bind="::" || bind="0.0.0.0"
|
|
||||||
else
|
|
||||||
if [ "$ipv6" = 1 ]; then
|
|
||||||
network_get_ipaddr6 bind "$interface"
|
|
||||||
else
|
|
||||||
network_get_ipaddr bind "$interface"
|
|
||||||
fi
|
|
||||||
|
|
||||||
network_is_up "$interface" && [ -n "$bind" ] || {
|
|
||||||
_log "defering start until listen interface $interface becomes ready"
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
fi
|
|
||||||
|
|
||||||
procd_open_instance
|
procd_open_instance
|
||||||
|
|
||||||
procd_set_param command /usr/bin/prometheus-node-exporter-lua
|
procd_set_param command /usr/sbin/uhttpd -f -c /dev/null -l / -L /usr/bin/prometheus-node-exporter-lua
|
||||||
procd_append_param command --bind ${bind}
|
[ $keepalive -gt 0 ] && procd_append_param command -k $keepalive
|
||||||
procd_append_param command --port ${port}
|
|
||||||
|
if [ "$interface" = "*" ]; then
|
||||||
|
procd_append_param command -p $port
|
||||||
|
else
|
||||||
|
network_is_up "$interface" || {
|
||||||
|
_log "defering start until listen interface $interface becomes ready"
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
network_get_ipaddr6 bind "$interface"
|
||||||
|
[ -n "$bind" ] && procd_append_param command -p [$bind]:$port
|
||||||
|
network_get_ipaddr bind "$interface"
|
||||||
|
[ -n "$bind" ] && procd_append_param command -p $bind:$port
|
||||||
|
fi
|
||||||
|
|
||||||
procd_set_param stdout 1
|
procd_set_param stdout 1
|
||||||
procd_set_param stderr 1
|
procd_set_param stderr 1
|
||||||
|
|
|
@ -59,7 +59,7 @@ function timed_scrape(collector)
|
||||||
local status, err = pcall(collector.scrape)
|
local status, err = pcall(collector.scrape)
|
||||||
if not status then
|
if not status then
|
||||||
success = 0
|
success = 0
|
||||||
print(err)
|
io.stderr:write(err)
|
||||||
end
|
end
|
||||||
return (socket.gettime() - start_time), success
|
return (socket.gettime() - start_time), success
|
||||||
end
|
end
|
||||||
|
@ -79,22 +79,18 @@ end
|
||||||
|
|
||||||
-- Web server-specific functions
|
-- Web server-specific functions
|
||||||
|
|
||||||
function http_ok_header()
|
function handle_request(env)
|
||||||
output("HTTP/1.0 200 OK\r\nServer: lua-metrics\r\nContent-Type: text/plain; version=0.0.4\r\n\r")
|
if env.PATH_INFO ~= '/metrics' then
|
||||||
end
|
uhttpd.send("Status: 404 Not Found\r\n")
|
||||||
|
uhttpd.send("Server: lua-metrics\r\n")
|
||||||
function http_not_found()
|
uhttpd.send("Content-Type: text/plain\r\n\r\n")
|
||||||
output("HTTP/1.0 404 Not Found\r\nServer: lua-metrics\r\nContent-Type: text/plain\r\n\r\nERROR: File Not Found.")
|
uhttpd.send("ERROR: File Not Found.")
|
||||||
end
|
|
||||||
|
|
||||||
function serve(request)
|
|
||||||
local q = request:match("^GET /metrics%??([^ ]*) HTTP/1%.[01]$")
|
|
||||||
if q == nil then
|
|
||||||
http_not_found()
|
|
||||||
else
|
else
|
||||||
http_ok_header()
|
uhttpd.send("Status: 200 OK\r\n")
|
||||||
|
uhttpd.send("Server: lua-metrics\r\n")
|
||||||
|
uhttpd.send("Content-Type: text/plain; version=0.0.4\r\n\r\n")
|
||||||
local cols = {}
|
local cols = {}
|
||||||
for c in q:gmatch("collect[^=]*=([^&]+)") do
|
for c in env.QUERY_STRING:gmatch("collect[^=]*=([^&]+)") do
|
||||||
cols[#cols+1] = c
|
cols[#cols+1] = c
|
||||||
end
|
end
|
||||||
if #cols == 0 then
|
if #cols == 0 then
|
||||||
|
@ -102,21 +98,10 @@ function serve(request)
|
||||||
end
|
end
|
||||||
run_all_collectors(cols)
|
run_all_collectors(cols)
|
||||||
end
|
end
|
||||||
client:close()
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Main program
|
-- Main program
|
||||||
|
|
||||||
for k,v in ipairs(arg) do
|
|
||||||
if (v == "-p") or (v == "--port") then
|
|
||||||
port = arg[k+1]
|
|
||||||
end
|
|
||||||
if (v == "-b") or (v == "--bind") then
|
|
||||||
bind = arg[k+1]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
col_mods = {}
|
col_mods = {}
|
||||||
col_names = {}
|
col_names = {}
|
||||||
ls_fd = io.popen("ls -1 /usr/lib/lua/prometheus-collectors/*.lua")
|
ls_fd = io.popen("ls -1 /usr/lib/lua/prometheus-collectors/*.lua")
|
||||||
|
@ -127,22 +112,9 @@ for c in ls_fd:lines() do
|
||||||
end
|
end
|
||||||
ls_fd:close()
|
ls_fd:close()
|
||||||
|
|
||||||
if port then
|
output = function (str) uhttpd.send(str.."\n") end
|
||||||
server = assert(socket.bind(bind, port))
|
|
||||||
|
|
||||||
while 1 do
|
if arg ~= nil then
|
||||||
client = server:accept()
|
|
||||||
client:settimeout(60)
|
|
||||||
local request, err = client:receive()
|
|
||||||
|
|
||||||
if not err then
|
|
||||||
output = function (str) client:send(str.."\n") end
|
|
||||||
if not serve(request) then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
output = print
|
output = print
|
||||||
run_all_collectors(col_names)
|
run_all_collectors(col_names)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue