b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | import { cursor } from "uci"; |
| 2 | |
| 3 | const x = ubus.call("wireguard", "status"); |
| 4 | if (!x) |
| 5 | return false; |
| 6 | |
| 7 | const uci = cursor(); |
| 8 | uci.load("network"); |
| 9 | |
| 10 | let m_wg_iface_info = gauge("wireguard_interface_info"); |
| 11 | let m_wg_peer_info = gauge("wireguard_peer_info"); |
| 12 | let m_wg_handshake = gauge ("wireguard_latest_handshake_seconds"); |
| 13 | let m_wg_rx = gauge ("wireguard_received_bytes_total"); |
| 14 | let m_wg_tx = gauge ("wireguard_sent_bytes_total"); |
| 15 | |
| 16 | for (let iface in x) { |
| 17 | const wc = x[iface]; |
| 18 | |
| 19 | m_wg_iface_info({ |
| 20 | name: iface, |
| 21 | public_key: wc["public_key"], |
| 22 | listen_port: wc["listen_port"], |
| 23 | fwmark: wc["fwmark"] || NaN, |
| 24 | }, 1); |
| 25 | |
| 26 | for (let peer in wc["peers"]) { |
| 27 | let description; |
| 28 | uci.foreach('network', `wireguard_${iface}`, (s) => { |
| 29 | if (s.public_key == peer) |
| 30 | description = s.description; |
| 31 | }); |
| 32 | |
| 33 | const pc = wc["peers"][peer]; |
| 34 | |
| 35 | m_wg_peer_info({ |
| 36 | interface: iface, |
| 37 | public_key: peer, |
| 38 | description, |
| 39 | endpoint: pc["endpoint"], |
| 40 | persistent_keepalive_interval: pc["persistent_keepalive_interval"] || NaN, |
| 41 | }, 1); |
| 42 | |
| 43 | const labels = { public_key: peer }; |
| 44 | |
| 45 | m_wg_handshake(labels, pc["last_handshake"]); |
| 46 | m_wg_rx(labels, pc["rx_bytes"]); |
| 47 | m_wg_tx(labels, pc["tx_bytes"]); |
| 48 | } |
| 49 | } |