# Prometheus WireGuard exporter

**URL:** <https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048>\
**Category:** Community Builds, Projects & Packages\
**Created:** [April 11, 2022, 9:31pm UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048 "2022-04-11T21:31:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Znerox](https://forum.openwrt.org/letter_avatar/znerox/32/6_449c533f586939b6e5f246632a46421f.png) [@Znerox](https://forum.openwrt.org/u/Znerox)\
**Post date:** [April 11, 2022, 9:31pm UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/1 "2022-04-11T21:31:43Z")

</div>

I came across this: [Prometheus WireGuard Exporter](https://mindflavor.github.io/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.

---

<div class="post-metadata">

**Author:** ![luizluca](https://forum.openwrt.org/user_avatar/forum.openwrt.org/luizluca/32/4994_2.png) [@luizluca](https://forum.openwrt.org/u/luizluca)\
**Post date:** [August 18, 2022, 10:26pm UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/3 "2022-08-18T22:26:37Z")

</div>

Something like this?

```nohighlight
/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 }

```

---

<div class="post-metadata">

**Author:** ![Znerox](https://forum.openwrt.org/letter_avatar/znerox/32/6_449c533f586939b6e5f246632a46421f.png) [@Znerox](https://forum.openwrt.org/u/Znerox)\
**Post date:** [August 19, 2022, 4:36pm UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/4 "2022-08-19T16:36:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Beniamin](https://forum.openwrt.org/user_avatar/forum.openwrt.org/beniamin/32/38843_2.png) [@Beniamin](https://forum.openwrt.org/u/Beniamin)\
**Post date:** [August 21, 2022, 2:57am UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/5 "2022-08-21T02:57:59Z")

</div>

How do I run this?

---

<div class="post-metadata">

**Author:** ![luizluca](https://forum.openwrt.org/user_avatar/forum.openwrt.org/luizluca/32/4994_2.png) [@luizluca](https://forum.openwrt.org/u/luizluca)\
**Post date:** [August 22, 2022, 4:48pm UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/6 "2022-08-22T16:48:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Znerox](https://forum.openwrt.org/letter_avatar/znerox/32/6_449c533f586939b6e5f246632a46421f.png) [@Znerox](https://forum.openwrt.org/u/Znerox)\
**Post date:** [August 23, 2022, 5:57am UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/7 "2022-08-23T05:57:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giuliomagnifico](https://forum.openwrt.org/user_avatar/forum.openwrt.org/giuliomagnifico/32/39021_2.png) [@giuliomagnifico](https://forum.openwrt.org/u/giuliomagnifico)\
**Post date:** [August 23, 2022, 11:43am UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/8 "2022-08-23T11:43:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Znerox](https://forum.openwrt.org/letter_avatar/znerox/32/6_449c533f586939b6e5f246632a46421f.png) [@Znerox](https://forum.openwrt.org/u/Znerox)\
**Post date:** [October 9, 2022, 4:56pm UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/9 "2022-10-09T16:56:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Beniamin](https://forum.openwrt.org/user_avatar/forum.openwrt.org/beniamin/32/38843_2.png) [@Beniamin](https://forum.openwrt.org/u/Beniamin)\
**Post date:** [October 11, 2022, 2:04am UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/10 "2022-10-11T02:04:17Z")

</div>

Ssh into the rooter.

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

Copy/paste the config above.

Crtl + X

Y

Reboot router.

---

<div class="post-metadata">

**Author:** ![Znerox](https://forum.openwrt.org/letter_avatar/znerox/32/6_449c533f586939b6e5f246632a46421f.png) [@Znerox](https://forum.openwrt.org/u/Znerox)\
**Post date:** [October 11, 2022, 4:57pm UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/11 "2022-10-11T16:57:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![giuliomagnifico](https://forum.openwrt.org/user_avatar/forum.openwrt.org/giuliomagnifico/32/39021_2.png) [@giuliomagnifico](https://forum.openwrt.org/u/giuliomagnifico)\
**Post date:** [December 3, 2022, 11:53am UTC](https://forum.openwrt.org/t/prometheus-wireguard-exporter/125048/12 "2022-12-03T11:53:27Z")

</div>

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 😅

Now works great! Thanks again.

 ![Screenshot 2022-12-03 at 12.51.30](https://forum.openwrt.org/uploads/default/original/3X/c/d/cd326b52f36fb9a7a5f9c5b15126a4a5e10a331f.png)

 ![Screenshot 2022-12-03 at 13.11.24](https://forum.openwrt.org/uploads/default/original/3X/d/5/d5b630b8f618dd17611367847ca589adbf383c66.png)
