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.

Does anyone know how to achieve this? I have zero experience with OpenWrt package distribution. @luizluca?

Ssh into the rooter.

nano /usr/lib/lua/prometheus-collectors/wireguard.lua

Copy/paste the config above.

Crtl + X

Y

Reboot router.

Thank you, but I was talking about how to add it to the OpenWrt package manager, so others can also install it, without visiting this forum thread.

Oh, I found again this thread, I was an idiot, when copy-past the great script by @luizluca you have to select all except the first line, I noticed only now :sweat_smile:

Now works great! Thanks again.