From 5b709b19f10c0a8a96f53167f4df28517584e450 Mon Sep 17 00:00:00 2001 From: Francesco Cogno Date: Wed, 3 Nov 2021 11:14:14 +0100 Subject: [PATCH] Ability to use env vars (#83) * clippy * exported variables * docs * updated deps * typo * version in docs * better docs --- Cargo.lock | 156 +++++++++++++++++++--------------------- Cargo.toml | 5 +- README.md | 29 ++++---- src/exporter_error.rs | 4 +- src/main.rs | 34 +++++++-- src/options.rs | 36 ++++++++-- src/wireguard.rs | 14 ++-- src/wireguard_config.rs | 4 +- 8 files changed, 160 insertions(+), 122 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f537790..c5b6027 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,9 +57,9 @@ checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "blake2b_simd" @@ -86,15 +86,15 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" [[package]] name = "bytes" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" +checksum = "c4872d67bab6358e59559027aa3b9157c53d9358c51423c17554809a8858e0f8" [[package]] name = "cc" -version = "1.0.68" +version = "1.0.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787" +checksum = "d26a6ce4b6a484fa3edb70f7efa6fc430fd2b87285fe8b84304fd0936faa0dc0" [[package]] name = "cfg-if" @@ -193,9 +193,9 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.8.4" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" dependencies = [ "atty", "humantime 2.1.0", @@ -212,24 +212,24 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "futures-channel" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e682a68b29a882df0545c143dc3646daefe80ba479bcdede94d5a703de2871e2" +checksum = "5da6ba8c3bb3c165d3c7319fc1cc8304facf1fb8db99c5de877183c08a273888" dependencies = [ "futures-core", ] [[package]] name = "futures-core" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" +checksum = "88d1c26957f23603395cd326b0ffe64124b818f4449552f960d815cfba83a53d" [[package]] name = "futures-macro" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4c40298486cdf52cc00cd6d6987892ba502c7656a16a4192a9992b1ccedd121" +checksum = "18e4a4b95cea4b4ccbcf1c5675ca7c4ee4e9e75eb79944d07defde18068f79bb" dependencies = [ "autocfg", "proc-macro-hack", @@ -240,15 +240,15 @@ dependencies = [ [[package]] name = "futures-task" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a16bef9fc1a4dddb5bee51c989e3fbba26569cbb0e31f5b303c184e3dd33dae" +checksum = "1d3d00f4eddb73e498a54394f228cd55853bdf059259e8e7bc6e69d408892e99" [[package]] name = "futures-util" -version = "0.3.15" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "feb5c238d27e2bf94ffdfd27b2c29e3df4a68c4193bb6427384259e2bf191967" +checksum = "36568465210a3a6ee45e1f165136d68671471a501e632e9a98d96872222b5481" dependencies = [ "autocfg", "futures-core", @@ -294,9 +294,9 @@ dependencies = [ [[package]] name = "http-body" -version = "0.4.2" +version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60daa14be0e0786db0f03a9e57cb404c9d756eed2b6c62b9ea98ec5743ec75a9" +checksum = "399c583b2979440c60be0821a6199eca73bc3c8dcd9d070d75ac726e2c6186e5" dependencies = [ "bytes", "http", @@ -305,9 +305,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.4.1" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3a87b616e37e93c22fb19bcd386f02f3af5ea98a25670ad0fce773de23c5e68" +checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503" [[package]] name = "httpdate" @@ -332,9 +332,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hyper" -version = "0.14.10" +version = "0.14.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7728a72c4c7d72665fde02204bcbd93b247721025b222ef78606f14513e0fd03" +checksum = "13f67199e765030fa08fe0bd581af683f0d5bc04ea09c2b1102012c5fb90e7fd" dependencies = [ "bytes", "futures-channel", @@ -372,15 +372,15 @@ dependencies = [ [[package]] name = "itoa" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] name = "js-sys" -version = "0.3.51" +version = "0.3.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062" +checksum = "e4bf49d50e2961077d9c99f4b7997d770a1114f087c3c2e0069b36c13fc2979d" dependencies = [ "wasm-bindgen", ] @@ -393,9 +393,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.98" +version = "0.2.101" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" +checksum = "3cb00336871be5ed2c8ed44b60ae9959dc5b9f08539422ed43f09e34ecaeba21" [[package]] name = "log" @@ -408,9 +408,9 @@ dependencies = [ [[package]] name = "memchr" -version = "2.4.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" [[package]] name = "mio" @@ -558,18 +558,18 @@ checksum = "bc881b2c22681370c6a780e47af9840ef841837bc98118431d4e1868bd0c1086" [[package]] name = "proc-macro2" -version = "1.0.27" +version = "1.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +checksum = "b9f5105d4fdaab20335ca9565e106a5d9b82b6219b5ba735731124ac6711d23d" dependencies = [ "unicode-xid", ] [[package]] name = "prometheus_exporter_base" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e31a078176ab4e58d34c158b1c64c0a562670c739372a6b9d81f0fdf149789a" +checksum = "05b214e11b8c1d9a9d659eb097b77326cb74fcde851477adb97d145222d17585" dependencies = [ "clap", "env_logger 0.7.1", @@ -585,18 +585,17 @@ dependencies = [ [[package]] name = "prometheus_wireguard_exporter" -version = "3.5.1" +version = "3.6.0" dependencies = [ "clap", "clippy", - "env_logger 0.8.4", + "env_logger 0.9.0", "http", "hyper", "log", "prometheus_exporter_base", "regex", "serde", - "serde_derive", "serde_json", "thiserror", "tokio", @@ -731,9 +730,9 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.3.1" +version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23a2ac85147a3a11d77ecf1bc7166ec0b92febfa4461c37944e180f319ece467" +checksum = "525bc1abfda2e1998d152c45cf13e696f76d0a4972310b22fac1658b05df7c87" dependencies = [ "bitflags", "core-foundation", @@ -744,9 +743,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.3.0" +version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e4effb91b4b8b6fb7732e670b6cee160278ff8e6bf485c7805d9e319d76e284" +checksum = "a9dd14d83160b528b7bfd66439110573efcfbe281b17fc2ca9f39f550d619c7e" dependencies = [ "core-foundation-sys", "libc", @@ -754,26 +753,15 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.126" +version = "1.0.130" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" - -[[package]] -name = "serde_derive" -version = "1.0.126" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] +checksum = "f12d06de37cf59146fbdecab66aa99f9fe4f78722e3607577a5375d66bd0c913" [[package]] name = "serde_json" -version = "1.0.64" +version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" +checksum = "a7f9e390c27c3c0ce8bc5d725f6e4d30a29d26659494aa4b17535f7522c5c950" dependencies = [ "itoa", "ryu", @@ -782,15 +770,15 @@ dependencies = [ [[package]] name = "slab" -version = "0.4.3" +version = "0.4.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f173ac3d1a7e3b28003f40de0b5ce7fe2710f9b9dc3fc38664cebee46b3b6527" +checksum = "c307a32c1c5c437f38c7fd45d753050587732ba8628319fbdf12a7e289ccc590" [[package]] name = "socket2" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e3dfc207c526015c632472a77be09cf1b6e46866581aecae5cc38fb4235dea2" +checksum = "765f090f0e423d2b55843402a07915add955e7d60657db13707a159727326cad" dependencies = [ "libc", "winapi", @@ -810,9 +798,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" [[package]] name = "syn" -version = "1.0.73" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" +checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84" dependencies = [ "proc-macro2", "quote", @@ -850,18 +838,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.26" +version = "1.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2" +checksum = "602eca064b2d83369e2b2f34b09c70b605402801927c65c11071ac911d299b88" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.26" +version = "1.0.29" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745" +checksum = "bad553cc2c78e8de258400763a647e80e6d1b31ee237275d756f6836d204494c" dependencies = [ "proc-macro2", "quote", @@ -870,9 +858,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.8.1" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98c8b05dc14c75ea83d63dd391100353789f5f24b8b3866542a5e85c8be8e985" +checksum = "b4efe6fc2395938c8155973d7be49fe8d03a843726e285e100a8a383cc0154ce" dependencies = [ "autocfg", "libc", @@ -923,9 +911,9 @@ dependencies = [ [[package]] name = "tracing-core" -version = "0.1.18" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9ff14f98b1a4b289c6248a023c1c2fa1491062964e9fed67ab29c4e4da4a052" +checksum = "2ca517f43f0fb96e0c3072ed5c275fe5eece87e8cb52f4a77b69226d3b1c9df8" dependencies = [ "lazy_static", ] @@ -978,9 +966,9 @@ checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" [[package]] name = "wasm-bindgen" -version = "0.2.74" +version = "0.2.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd" +checksum = "8ce9b1b516211d33767048e5d47fa2a381ed8b76fc48d2ce4aa39877f9f183e0" dependencies = [ "cfg-if", "wasm-bindgen-macro", @@ -988,9 +976,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.74" +version = "0.2.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" +checksum = "cfe8dc78e2326ba5f845f4b5bf548401604fa20b1dd1d365fb73b6c1d6364041" dependencies = [ "bumpalo", "lazy_static", @@ -1003,9 +991,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.74" +version = "0.2.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" +checksum = "44468aa53335841d9d6b6c023eaab07c0cd4bddbcfdee3e2bb1e8d2cb8069fef" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -1013,9 +1001,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.74" +version = "0.2.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" +checksum = "0195807922713af1e67dc66132c7328206ed9766af3858164fb583eedc25fbad" dependencies = [ "proc-macro2", "quote", @@ -1026,15 +1014,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.74" +version = "0.2.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f" +checksum = "acdb075a845574a1fa5f09fd77e43f7747599301ea3417a9fbffdeedfc1f4a29" [[package]] name = "web-sys" -version = "0.3.51" +version = "0.3.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582" +checksum = "224b2f6b67919060055ef1a67807367c2066ed520c3862cc013d26cf893a783c" dependencies = [ "js-sys", "wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index c3c9fe3..04ef8a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "prometheus_wireguard_exporter" -version = "3.5.1" +version = "3.6.0" authors = ["Francesco Cogno "] description = "Prometheus WireGuard Exporter" edition = "2018" @@ -20,11 +20,10 @@ leaky_log = [] [dependencies] log = "0.4.14" -env_logger = "0.8.4" +env_logger = "0.9.0" clap = "2.33.0" serde_json = "1.0" serde = "1.0" -serde_derive = "1.0" thiserror = "1.0" hyper = { version = "0.14", features = ["stream"] } http = "0.2" diff --git a/README.md b/README.md index 91126d9..14aae2a 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,11 @@ [![Crate](https://img.shields.io/crates/v/prometheus_wireguard_exporter.svg)](https://crates.io/crates/prometheus_wireguard_exporter) [![cratedown](https://img.shields.io/crates/d/prometheus_wireguard_exporter.svg)](https://crates.io/crates/prometheus_wireguard_exporter) [![cratelastdown](https://img.shields.io/crates/dv/prometheus_wireguard_exporter.svg)](https://crates.io/crates/prometheus_wireguard_exporter) -[![release](https://img.shields.io/github/release/MindFlavor/prometheus_wireguard_exporter.svg)](https://github.com/MindFlavor/prometheus_wireguard_exporter/tree/3.5.1) -[![tag](https://img.shields.io/github/tag/mindflavor/prometheus_wireguard_exporter.svg)](https://github.com/MindFlavor/prometheus_wireguard_exporter/tree/3.5.1) +[![release](https://img.shields.io/github/release/MindFlavor/prometheus_wireguard_exporter.svg)](https://github.com/MindFlavor/prometheus_wireguard_exporter/tree/3.6.0) +[![tag](https://img.shields.io/github/tag/mindflavor/prometheus_wireguard_exporter.svg)](https://github.com/MindFlavor/prometheus_wireguard_exporter/tree/3.6.0) [![Rust build](https://github.com/mindflavor/prometheus_wireguard_exporter/workflows/Rust/badge.svg)](https://github.com/mindflavor/prometheus_wireguard_exporter/actions?query=workflow%3ARust) -[![commitssince](https://img.shields.io/github/commits-since/mindflavor/prometheus_wireguard_exporter/3.5.1.svg)](https://img.shields.io/github/commits-since/mindflavor/prometheus_wireguard_exporter/3.5.1.svg) +[![commitssince](https://img.shields.io/github/commits-since/mindflavor/prometheus_wireguard_exporter/3.6.0)](https://img.shields.io/github/commits-since/mindflavor/prometheus_wireguard_exporter/3.6.0) [![Docker build](https://github.com/MindFlavor/prometheus_wireguard_exporter/actions/workflows/docker.yml/badge.svg)](https://github.com/qdm12/godevcontainer/actions/workflows/docker.yml) @@ -22,6 +22,7 @@ A Prometheus exporter for [WireGuard](https://www.wireguard.com), written in Rus ## Changelog +* **BREAKING** From release [3.6.0](https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/tag/3.6.0) the exporter takes fallback configuration values from the environment variables. Thanks to [j_r0dd](https://github.com/jr0dd) for the idea. This changes how the exporter evaluates the command line parameters: make sure to consult the documentation on how to convert your command line to the new format. Basically every switch (for example verbose `-v`) not expect values, either `true` or `false`. This is necessary because there is no way to discriminate between an empty environment variable and one that has not been set. * From release [3.5.1](https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/tag/3.5.1) the exporter supports multiple peer files. Thanks to [Tobias Krischer](https://github.com/tobikris) for the idea. * From release [3.5.0](https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/tag/3.5.0) the exporter supports the `friendly_json` tag. Entries prepended with the `friendly_json` tag will output all the entries in the specificed json as Prometheus attributes. Thanks to [DrProxyProSupport](https://github.com/iqdoctor) for the idea. * From release [3.4.1](https://github.com/MindFlavor/prometheus_wireguard_exporter/releases/tag/3.4.0) the exporter supports prepending `sudo` to the `wg` command. This allows to run the exporter as a non root user (although sudoer without password). Thanks to [Jonas Seydel](https://github.com/Thor77) for the idea. @@ -104,16 +105,18 @@ docker build -t mindflavor/prometheus_wireguard_exporter https://github.com/Mind Start the binary with `-h` to get the complete syntax. The parameters are: -| Parameter | Mandatory | Valid values | Default | Accepts multiple occurrences? | Description | -| -- | -- | -- | -- | -- | -- | -| `-v` | no | `` | | No | Enable verbose mode. -| `-a` | no | `` | | No | Prepends sudo to `wg` commands. -| `-l` | no | any valid ip address | `0.0.0.0` | No | Specify the service address. This is the address your Prometheus instance should point to. -| `-p` | no | any valid port number | `9586` | No | Specify the service port. This is the port your Prometheus instance should point to. -| `-n` | no | path to the wireguard configuration file | | Yes | This flag adds the *friendly_name* attribute or the *friendly_json* attributes to the exported entries. See [Friendly tags](#friendly-tags) for more details. Multiple files are allowed (they will be merged as a single file in memory so avoid duplicates). -| `-s` | no | `` | off | No | Enable the allowed ip + subnet split mode for the labels. -| `-r` | no | `` | off | No | Exports peer's remote ip and port as labels (if available). -| `-i` | no | your interface name(s) | `all` | Yes | Specifies the interface(s) passed to the `wg show dump` parameter. Multiple parameters are allowed. +| Parameter | Env | Mandatory | Valid values | Default | Accepts multiple occurrences? | Description | +| -- | -- | -- | -- | -- | -- | -- | +| `-v` | `PROMETHEUS_WIREGUARD_EXPORTER_VERBOSE_ENABLED` | No | `true` or `false` | `false` | No | Enable verbose mode. +| `-a` | `PROMETHEUS_WIREGUARD_EXPORTER_PREPEND_SUDO_ENABLED` | No | `true` or `false` | `false` | No | Prepends sudo to `wg` commands. +| `-l` | `PROMETHEUS_WIREGUARD_EXPORTER_ADDRESS` | No | Any valid IP address | `0.0.0.0` | No | Specify the service address. This is the address your Prometheus instance should point to. +| `-p` | `PROMETHEUS_WIREGUARD_EXPORTER_PORT` | No | Any valid port number | `9586` | No | Specify the service port. This is the port your Prometheus instance should point to. +| `-n` | `PROMETHEUS_WIREGUARD_EXPORTER_CONFIG_FILE_NAMES` | No | Path to the wireguard configuration file | | Yes | This flag adds the *friendly_name* attribute or the *friendly_json* attributes to the exported entries. See [Friendly tags](#friendly-tags) for more details. Multiple files are allowed (they will be merged as a single file in memory so avoid duplicates). +| `-s` | `PROMETHEUS_WIREGUARD_EXPORTER_SEPARATE_ALLOWED_IPS_ENABLED` | No | `true` or `false` | `false` | No | Enable the allowed ip + subnet split mode for the labels. +| `-r` | `PROMETHEUS_WIREGUARD_EXPORTER_EXPORT_REMOTE_IP_AND_PORT_ENABLED` | No | `true` or `false` | `false` | No | Exports peer's remote ip and port as labels (if available). +| `-i` | `PROMETHEUS_WIREGUARD_EXPORTER_INTERFACES` | No | Your interface name(s) | `all` | Yes | Specifies the interface(s) passed to the `wg show dump` parameter. Multiple parameters are allowed. + +Keep in mind that command line values take precedence over environment variables. Once started, the tool will listen on the specified port (or the default one, 9586, if not specified) and return a Prometheus valid response at the url `/metrics`. So to check if the tool is working properly simply browse the `http://localhost:9586/metrics` (or whichever port you choose). diff --git a/src/exporter_error.rs b/src/exporter_error.rs index a52b8ca..aba0f94 100644 --- a/src/exporter_error.rs +++ b/src/exporter_error.rs @@ -37,7 +37,7 @@ pub enum ExporterError { UTF8 { e: std::string::FromUtf8Error }, #[error("JSON format error: {}", e)] - JSON { e: serde_json::error::Error }, + Json { e: serde_json::error::Error }, #[error("IO Error: {}", e)] IO { e: std::io::Error }, @@ -84,7 +84,7 @@ impl From for ExporterError { impl From for ExporterError { fn from(e: serde_json::error::Error) -> Self { - ExporterError::JSON { e } + ExporterError::Json { e } } } diff --git a/src/main.rs b/src/main.rs index e9b986e..364bdf5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ async fn perform_request( .map(|files| { files // if we have values .iter() // for each value - .map(|file| std::fs::read_to_string(&file as &str)) // read the contents into a String + .map(|file| std::fs::read_to_string(file as &str)) // read the contents into a String .collect::, std::io::Error>>() // And transform it into a vec (stopping in case of errors) }) .transpose()? // bail out if there was an error @@ -120,6 +120,8 @@ async fn main() -> Result<(), Box> { .arg( Arg::with_name("addr") .short("l") + .long("address") + .env("PROMETHEUS_WIREGUARD_EXPORTER_ADDRESS") .help("exporter address") .default_value("0.0.0.0") .takes_value(true), @@ -127,6 +129,8 @@ async fn main() -> Result<(), Box> { .arg( Arg::with_name("port") .short("p") + .long("port") + .env("PROMETHEUS_WIREGUARD_EXPORTER_PORT") .help("exporter port") .default_value("9586") .takes_value(true), @@ -134,39 +138,57 @@ async fn main() -> Result<(), Box> { .arg( Arg::with_name("verbose") .short("v") + .long("verbose") + .env("PROMETHEUS_WIREGUARD_EXPORTER_VERBOSE_ENABLED") .help("verbose logging") - .takes_value(false), + .default_value("false") + .takes_value(true), ) .arg( Arg::with_name("prepend_sudo") .short("a") + .long("prepend_sudo") + .env("PROMETHEUS_WIREGUARD_EXPORTER_PREPEND_SUDO_ENABLED") .help("Prepend sudo to the wg show commands") - .takes_value(false), + .default_value("false") + .takes_value(true), ) .arg( Arg::with_name("separate_allowed_ips") .short("s") + .long("separate_allowed_ips") + .env("PROMETHEUS_WIREGUARD_EXPORTER_SEPARATE_ALLOWED_IPS_ENABLED") .help("separate allowed ips and ports") - .takes_value(false), + .default_value("false") + .takes_value(true), ) .arg( Arg::with_name("export_remote_ip_and_port") .short("r") + .long("export_remote_ip_and_port") + .env("PROMETHEUS_WIREGUARD_EXPORTER_EXPORT_REMOTE_IP_AND_PORT_ENABLED") .help("exports peer's remote ip and port as labels (if available)") - .takes_value(false), + .default_value("false") + .takes_value(true), ) .arg( Arg::with_name("extract_names_config_files") .short("n") + .long("extract_names_config_files") + .env("PROMETHEUS_WIREGUARD_EXPORTER_CONFIG_FILE_NAMES") .help("If set, the exporter will look in the specified WireGuard config file for peer names (must be in [Peer] definition and be a comment). Multiple files are supported.") .multiple(true) + .use_delimiter(true) .number_of_values(1) .takes_value(true)) .arg( Arg::with_name("interfaces") .short("i") + .long("interfaces") + .env("PROMETHEUS_WIREGUARD_EXPORTER_INTERFACES") .help("If set specifies the interface passed to the wg show command. It is relative to the same position config_file. In not specified, all will be passed.") .multiple(true) + .use_delimiter(true) .number_of_values(1) .takes_value(true)) .get_matches(); @@ -194,7 +216,7 @@ async fn main() -> Result<(), Box> { info!("using options: {:?}", options); let bind = matches.value_of("port").unwrap(); - let bind = (&bind).parse::().expect("port must be a valid number"); + let bind = bind.parse::().expect("port must be a valid number"); let ip = matches.value_of("addr").unwrap().parse::().unwrap(); let addr = (ip, bind).into(); diff --git a/src/options.rs b/src/options.rs index b15d759..c473d6b 100644 --- a/src/options.rs +++ b/src/options.rs @@ -11,16 +11,44 @@ pub(crate) struct Options { impl Options { pub fn from_claps(matches: &clap::ArgMatches<'_>) -> Options { let options = Options { - verbose: matches.is_present("verbose"), - prepend_sudo: matches.is_present("prepend_sudo"), - separate_allowed_ips: matches.is_present("separate_allowed_ips"), + verbose: matches + .value_of("verbose") + .map(|e| { + e.to_lowercase() + .parse() + .expect("cannot parse verbose as a bool") + }) + .unwrap_or_default(), + prepend_sudo: matches + .value_of("prepend_sudo") + .map(|e| { + e.to_lowercase() + .parse() + .expect("cannot parse prepend_sudo as a bool") + }) + .unwrap_or_default(), + separate_allowed_ips: matches + .value_of("separate_allowed_ips") + .map(|e| { + e.to_lowercase() + .parse() + .expect("cannot parse separate_allowed_ips as a bool") + }) + .unwrap_or_default(), extract_names_config_files: matches .values_of("extract_names_config_files") .map(|e| e.into_iter().map(|e| e.to_owned()).collect()), interfaces: matches .values_of("interfaces") .map(|e| e.into_iter().map(|a| a.to_owned()).collect()), - export_remote_ip_and_port: matches.is_present("export_remote_ip_and_port"), + export_remote_ip_and_port: matches + .value_of("export_remote_ip_and_port") + .map(|e| { + e.to_lowercase() + .parse() + .expect("cannot parse export_remote_ip_and_port as a bool") + }) + .unwrap_or_default(), }; options diff --git a/src/wireguard.rs b/src/wireguard.rs index 0df43a1..788b063 100644 --- a/src/wireguard.rs +++ b/src/wireguard.rs @@ -152,11 +152,11 @@ impl TryFrom<&str> for WireGuard { impl WireGuard { pub fn merge(&mut self, merge_from: &WireGuard) { for (interface_name, endpoints_to_merge) in merge_from.interfaces.iter() { - if let Some(endpoints) = self.interfaces.get_mut(&interface_name as &str) { - endpoints.extend_from_slice(&endpoints_to_merge); + if let Some(endpoints) = self.interfaces.get_mut(interface_name as &str) { + endpoints.extend_from_slice(endpoints_to_merge); } else { let mut new_vec = Vec::new(); - new_vec.extend_from_slice(&endpoints_to_merge); + new_vec.extend_from_slice(endpoints_to_merge); self.interfaces.insert(interface_name.to_owned(), new_vec); } } @@ -211,10 +211,8 @@ impl WireGuard { // store in attibutes their references. attributes_owned is onyl // needed for separate ip+subnet let mut attributes_owned: Vec<(String, String)> = Vec::new(); - let mut attributes: Vec<(&str, &str)> = vec![ - ("interface", interface), - ("public_key", &ep.public_key), - ]; + let mut attributes: Vec<(&str, &str)> = + vec![("interface", interface), ("public_key", &ep.public_key)]; if split_allowed_ips { let v_ip_and_subnet: Vec<(&str, &str)> = ep @@ -290,7 +288,7 @@ impl WireGuard { if export_remote_ip_and_port { if let Some(r_ip) = &ep.remote_ip { - attributes.push(("remote_ip", &r_ip)); + attributes.push(("remote_ip", r_ip)); } if let Some(r_port) = &ep.remote_port { attributes_owned.push(("remote_port".to_string(), r_port.to_string())); diff --git a/src/wireguard_config.rs b/src/wireguard_config.rs index a140b2a..624f1df 100644 --- a/src/wireguard_config.rs +++ b/src/wireguard_config.rs @@ -28,7 +28,7 @@ fn after_char_strip_comment(s: &str, c_split: char) -> &str { let s = after_char(s, c_split); if let Some(idx) = s.find('#') { - &s[..idx].trim() + s[..idx].trim() } else { s } @@ -146,7 +146,7 @@ pub(crate) fn peer_entry_hashmap_try_from( debug!("peer_entry_hashmap_try_from v_blocks == {:?}", v_blocks); for block in &v_blocks { - let p: PeerEntry = PeerEntry::try_from(&block as &[&str])?; + let p: PeerEntry = PeerEntry::try_from(block as &[&str])?; hm.insert(p.public_key, p); }