Prometheus WireGuard exporter

I came across this: Prometheus WireGuard Exporter. Has anyone looked at making something similar for OpenWrt? There is already many prometheus node exporters (written in lua), it would be nice to export WireGuard statistics as well.

2 Likes

Something like this?

/usr/lib/lua/prometheus-collectors/wireguard.lua:
local function scrape()

        local f = assert(io.popen("uci show network | awk -F '[\.]' -v OFS='\t' '$3 ~ \"public_key=\" { print substr($3,13,44),$2 }'"))
        local names = {}
        for line in f:lines() do
                toks = line:gmatch("([^\t]*)\t?")
                names[toks()]=toks()
        end
        f:close()

        local f = assert(io.popen("wg show all dump", 'r'))
        local last_interface
        for line in f:lines() do
                toks = line:gmatch("([^\t]*)\t?")
                local labels = {
                        interface=toks(),
                        public_key=toks(),
                        preshared_key=toks(),
                        endpoint=toks(),
                        allowed_ips=toks()
                }
                labels["name"]=names[labels["public_key"]]
                labels["preshared_key"]=nil

                if last_interface == labels["interface"] then
                        metric("wireguard_latest_handshake_seconds", "gauge", labels, toks())
                        metric("wireguard_received_bytes_total", "counter", labels, toks())
                        metric("wireguard_sent_bytes_total", "counter", labels, toks())
                else
                        last_interface=labels["interface"]
                end
        end
        f:close()
end

return { scrape = scrape }
1 Like

I don't know lua, but that seems to be doing the job :grinning:

1 Like

How do I run this?

Put it at /usr/lib/lua/prometheus-collectors/wireguard.lua, restart the service.

1 Like

It would be nice to have this like an installable package.

1 Like

For me it doesn’t work by the way. Copied, restarted Prometheus-lua-exporter but I can’t find the queries in my Grafana server.