include $(TOPDIR)/rules.mk
PKG_NAME:=prometheus-node-exporter-lua
-PKG_VERSION:=2022.04.18
+PKG_VERSION:=2022.04.23
PKG_RELEASE:=1
PKG_MAINTAINER:=Etienne CHAMPETIER <champetier.etienne@gmail.com>
define Package/prometheus-node-exporter-lua
$(call Package/prometheus-node-exporter-lua/Default)
- DEPENDS:=+luasocket +lua
+ DEPENDS:=+luasocket +lua +uhttpd +uhttpd-mod-lua
endef
define Package/prometheus-node-exporter-lua/install
config prometheus-node-exporter-lua 'main'
option listen_interface 'loopback'
- option listen_ipv6 '0'
option listen_port '9100'
start_service() {
. /lib/functions/network.sh
- local interface ipv6 port bind
+ local interface port bind
config_load prometheus-node-exporter-lua.main
+ config_get keepalive "main" http_keepalive 70
config_get interface "main" listen_interface "loopback"
- config_get_bool ipv6 "main" listen_ipv6 0
config_get port "main" listen_port 9100
+ procd_open_instance
+
+ procd_set_param command /usr/sbin/uhttpd -f -c /dev/null -l / -L /usr/bin/prometheus-node-exporter-lua
+ [ $keepalive -gt 0 ] && procd_append_param command -k $keepalive
+
if [ "$interface" = "*" ]; then
- [ "$ipv6" = 1 ] && bind="::" || bind="0.0.0.0"
+ procd_append_param command -p $port
else
- if [ "$ipv6" = 1 ]; then
- network_get_ipaddr6 bind "$interface"
- else
- network_get_ipaddr bind "$interface"
- fi
-
- network_is_up "$interface" && [ -n "$bind" ] || {
+ 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_open_instance
-
- procd_set_param command /usr/bin/prometheus-node-exporter-lua
- procd_append_param command --bind ${bind}
- procd_append_param command --port ${port}
-
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param respawn
local status, err = pcall(collector.scrape)
if not status then
success = 0
- print(err)
+ io.stderr:write(err)
end
return (socket.gettime() - start_time), success
end
-- Web server-specific functions
-function http_ok_header()
- output("HTTP/1.0 200 OK\r\nServer: lua-metrics\r\nContent-Type: text/plain; version=0.0.4\r\n\r")
-end
-
-function http_not_found()
- output("HTTP/1.0 404 Not Found\r\nServer: lua-metrics\r\nContent-Type: text/plain\r\n\r\nERROR: File Not Found.")
-end
-
-function serve(request)
- local q = request:match("^GET /metrics%??([^ ]*) HTTP/1%.[01]$")
- if q == nil then
- http_not_found()
+function handle_request(env)
+ if env.PATH_INFO ~= '/metrics' then
+ uhttpd.send("Status: 404 Not Found\r\n")
+ uhttpd.send("Server: lua-metrics\r\n")
+ uhttpd.send("Content-Type: text/plain\r\n\r\n")
+ uhttpd.send("ERROR: File Not Found.")
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 = {}
- for c in q:gmatch("collect[^=]*=([^&]+)") do
+ for c in env.QUERY_STRING:gmatch("collect[^=]*=([^&]+)") do
cols[#cols+1] = c
end
if #cols == 0 then
end
run_all_collectors(cols)
end
- client:close()
- return true
end
-- 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_names = {}
ls_fd = io.popen("ls -1 /usr/lib/lua/prometheus-collectors/*.lua")
end
ls_fd:close()
-if port then
- server = assert(socket.bind(bind, port))
-
- while 1 do
- client = server:accept()
- client:settimeout(60)
- local request, err = client:receive()
+output = function (str) uhttpd.send(str.."\n") end
- if not err then
- output = function (str) client:send(str.."\n") end
- if not serve(request) then
- break
- end
- end
- end
-else
+if arg ~= nil then
output = print
run_all_collectors(col_names)
end