Merge 6afa841440
into 9d49df0dab
This commit is contained in:
commit
9daad1ace1
|
@ -11,24 +11,27 @@ _log() {
|
||||||
start_service() {
|
start_service() {
|
||||||
. /lib/functions/network.sh
|
. /lib/functions/network.sh
|
||||||
|
|
||||||
local interface port bind4 bind6
|
local interface port bind4 bind6 omit_zero_values
|
||||||
|
|
||||||
config_load prometheus-node-exporter-lua.main
|
config_load prometheus-node-exporter-lua.main
|
||||||
config_get keepalive "main" http_keepalive 70
|
config_get keepalive "main" http_keepalive 70
|
||||||
config_get interface "main" listen_interface "loopback"
|
config_get interface "main" listen_interface "loopback"
|
||||||
config_get port "main" listen_port 9100
|
config_get port "main" listen_port 9100
|
||||||
|
config_get omit_zero_values "main" omit_zero_values 0
|
||||||
|
|
||||||
[ "$interface" = "*" ] || {
|
[ "$interface" = "*" ] || {
|
||||||
network_get_ipaddr bind4 "$interface"
|
network_get_ipaddr bind4 "$interface"
|
||||||
network_get_ipaddr6 bind6 "$interface"
|
network_get_ipaddr6 bind6 "$interface"
|
||||||
[ -n "$bind4$bind6" ] || {
|
[ -n "$bind4$bind6" ] || {
|
||||||
_log "defering start until listen interface $interface becomes ready"
|
_log "defering start until listen interface $interface becomes ready"
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
procd_open_instance
|
procd_open_instance
|
||||||
|
|
||||||
|
[ "$omit_zero_values" -eq 1 ] && procd_set_param env OMIT_ZERO_VALUES=1
|
||||||
|
|
||||||
procd_set_param command /usr/sbin/uhttpd -f -c /dev/null -l / -L /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
|
||||||
[ $keepalive -gt 0 ] && procd_append_param command -k $keepalive
|
[ $keepalive -gt 0 ] && procd_append_param command -k $keepalive
|
||||||
|
|
||||||
|
@ -46,8 +49,7 @@ start_service() {
|
||||||
procd_close_instance
|
procd_close_instance
|
||||||
}
|
}
|
||||||
|
|
||||||
service_triggers()
|
service_triggers() {
|
||||||
{
|
|
||||||
local interface
|
local interface
|
||||||
|
|
||||||
procd_add_reload_trigger "prometheus-node-exporter-lua"
|
procd_add_reload_trigger "prometheus-node-exporter-lua"
|
||||||
|
|
|
@ -8,6 +8,10 @@
|
||||||
|
|
||||||
socket = require("socket")
|
socket = require("socket")
|
||||||
|
|
||||||
|
-- get configs
|
||||||
|
|
||||||
|
local omit_zero_values = os.getenv("OMIT_ZERO_VALUES") == "1"
|
||||||
|
|
||||||
-- Parsing
|
-- Parsing
|
||||||
|
|
||||||
function space_split(s)
|
function space_split(s)
|
||||||
|
@ -22,7 +26,7 @@ function get_contents(filename)
|
||||||
local f = io.open(filename, "rb")
|
local f = io.open(filename, "rb")
|
||||||
local contents = ""
|
local contents = ""
|
||||||
if f then
|
if f then
|
||||||
contents = f:read "*a"
|
contents = f:read("*a")
|
||||||
f:close()
|
f:close()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -34,8 +38,8 @@ end
|
||||||
function print_metric(metric, labels, value)
|
function print_metric(metric, labels, value)
|
||||||
local label_string = ""
|
local label_string = ""
|
||||||
if labels then
|
if labels then
|
||||||
for label,value in pairs(labels) do
|
for label, value in pairs(labels) do
|
||||||
label_string = label_string .. label .. '="' .. value .. '",'
|
label_string = label_string .. label .. '="' .. value .. '",'
|
||||||
end
|
end
|
||||||
label_string = "{" .. string.sub(label_string, 1, -2) .. "}"
|
label_string = "{" .. string.sub(label_string, 1, -2) .. "}"
|
||||||
end
|
end
|
||||||
|
@ -45,7 +49,9 @@ end
|
||||||
function metric(name, mtype, labels, value)
|
function metric(name, mtype, labels, value)
|
||||||
output("# TYPE " .. name .. " " .. mtype)
|
output("# TYPE " .. name .. " " .. mtype)
|
||||||
local outputter = function(labels, value)
|
local outputter = function(labels, value)
|
||||||
print_metric(name, labels, value)
|
if not (omit_zero_values and tonumber(value) == 0) then
|
||||||
|
print_metric(name, labels, value)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if value then
|
if value then
|
||||||
outputter(labels, value)
|
outputter(labels, value)
|
||||||
|
@ -67,10 +73,10 @@ end
|
||||||
function run_all_collectors(collectors)
|
function run_all_collectors(collectors)
|
||||||
local metric_duration = metric("node_scrape_collector_duration_seconds", "gauge")
|
local metric_duration = metric("node_scrape_collector_duration_seconds", "gauge")
|
||||||
local metric_success = metric("node_scrape_collector_success", "gauge")
|
local metric_success = metric("node_scrape_collector_success", "gauge")
|
||||||
for _,cname in pairs(collectors) do
|
for _, cname in pairs(collectors) do
|
||||||
if col_mods[cname] ~= nil then
|
if col_mods[cname] ~= nil then
|
||||||
local duration, success = timed_scrape(col_mods[cname])
|
local duration, success = timed_scrape(col_mods[cname])
|
||||||
local labels = {collector=cname}
|
local labels = { collector = cname }
|
||||||
metric_duration(labels, duration)
|
metric_duration(labels, duration)
|
||||||
metric_success(labels, success)
|
metric_success(labels, success)
|
||||||
end
|
end
|
||||||
|
@ -80,7 +86,7 @@ end
|
||||||
-- Web server-specific functions
|
-- Web server-specific functions
|
||||||
|
|
||||||
function handle_request(env)
|
function handle_request(env)
|
||||||
if env.PATH_INFO ~= '/metrics' then
|
if env.PATH_INFO ~= "/metrics" then
|
||||||
uhttpd.send("Status: 404 Not Found\r\n")
|
uhttpd.send("Status: 404 Not Found\r\n")
|
||||||
uhttpd.send("Server: lua-metrics\r\n")
|
uhttpd.send("Server: lua-metrics\r\n")
|
||||||
uhttpd.send("Content-Type: text/plain\r\n\r\n")
|
uhttpd.send("Content-Type: text/plain\r\n\r\n")
|
||||||
|
@ -91,7 +97,7 @@ function handle_request(env)
|
||||||
uhttpd.send("Content-Type: text/plain; version=0.0.4\r\n\r\n")
|
uhttpd.send("Content-Type: text/plain; version=0.0.4\r\n\r\n")
|
||||||
local cols = {}
|
local cols = {}
|
||||||
for c in env.QUERY_STRING: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
|
||||||
cols = col_names
|
cols = col_names
|
||||||
|
@ -107,12 +113,14 @@ 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")
|
||||||
for c in ls_fd:lines() do
|
for c in ls_fd:lines() do
|
||||||
c = c:match("([^/]+)%.lua$")
|
c = c:match("([^/]+)%.lua$")
|
||||||
col_mods[c] = require('prometheus-collectors.'..c)
|
col_mods[c] = require("prometheus-collectors." .. c)
|
||||||
col_names[#col_names+1] = c
|
col_names[#col_names + 1] = c
|
||||||
end
|
end
|
||||||
ls_fd:close()
|
ls_fd:close()
|
||||||
|
|
||||||
output = function (str) uhttpd.send(str.."\n") end
|
output = function(str)
|
||||||
|
uhttpd.send(str .. "\n")
|
||||||
|
end
|
||||||
|
|
||||||
if arg ~= nil then
|
if arg ~= nil then
|
||||||
output = print
|
output = print
|
||||||
|
|
Loading…
Reference in New Issue