From ee164d2f73cc1c98a879b2f32fe85876073dc6aa Mon Sep 17 00:00:00 2001 From: Francesco Cogno Date: Sun, 9 Jun 2019 20:09:06 +0200 Subject: [PATCH] Exported boilerplate to external crate --- Cargo.lock | 100 +++++++++------- Cargo.toml | 47 ++++---- src/exporter_error.rs | 4 +- src/main.rs | 97 +++++----------- src/render_to_prometheus.rs | 3 - src/wireguard.rs | 224 ++++++++++++++++++++++++------------ 6 files changed, 269 insertions(+), 206 deletions(-) delete mode 100644 src/render_to_prometheus.rs diff --git a/Cargo.lock b/Cargo.lock index fbcb6e8..c696064 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,7 +41,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "backtrace" -version = "0.3.26" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -62,12 +62,12 @@ dependencies = [ [[package]] name = "bitflags" -version = "1.0.4" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "byteorder" -version = "1.3.1" +version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -75,7 +75,7 @@ name = "bytes" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -97,7 +97,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", @@ -109,7 +109,7 @@ name = "cloudabi" version = "0.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -164,8 +164,8 @@ dependencies = [ "atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", "humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.1.6 (registry+https://github.com/rust-lang/crates.io-index)", - "termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.1.7 (registry+https://github.com/rust-lang/crates.io-index)", + "termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -173,7 +173,7 @@ name = "failure" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "backtrace 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)", + "backtrace 0.3.30 (registry+https://github.com/rust-lang/crates.io-index)", "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -184,7 +184,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.35 (registry+https://github.com/rust-lang/crates.io-index)", "synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -203,7 +203,7 @@ name = "fuchsia-zircon" version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -228,10 +228,10 @@ dependencies = [ [[package]] name = "h2" -version = "0.1.21" +version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", @@ -239,7 +239,7 @@ dependencies = [ "indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", - "string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "string 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -285,7 +285,7 @@ dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", "futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", - "h2 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)", + "h2 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "http-body 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -465,8 +465,9 @@ dependencies = [ ] [[package]] -name = "prometheus_wireguard_exporter" -version = "2.0.1" +name = "prometheus_exporter_base" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -475,6 +476,21 @@ dependencies = [ "http 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.12.29 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "prometheus_wireguard_exporter" +version = "2.0.2" +dependencies = [ + "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)", + "http 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "hyper 0.12.29 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "prometheus_exporter_base 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", @@ -612,19 +628,19 @@ dependencies = [ [[package]] name = "regex" -version = "1.1.6" +version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aho-corasick 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "regex-syntax" -version = "0.6.6" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -678,7 +694,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.35 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -708,8 +724,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "string" -version = "0.1.3" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "strsim" @@ -718,7 +737,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "syn" -version = "0.15.34" +version = "0.15.35" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", @@ -733,13 +752,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)", "quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.35 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "termcolor" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -853,12 +872,12 @@ dependencies = [ "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-sync 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-sync 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "tokio-sync" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", @@ -935,7 +954,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "utf8-ranges" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1014,10 +1033,10 @@ dependencies = [ "checksum arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf" -"checksum backtrace 0.3.26 (registry+https://github.com/rust-lang/crates.io-index)" = "1a13fc43f04daf08ab4f71e3d27e1fc27fc437d3e95ac0063a796d92fb40f39b" +"checksum backtrace 0.3.30 (registry+https://github.com/rust-lang/crates.io-index)" = "ada4c783bb7e7443c14e0480f429ae2cc99da95065aeab7ee1b81ada0419404f" "checksum backtrace-sys 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "797c830ac25ccc92a7f8a7b9862bde440715531514594a6154e3d4a54dd769b6" -"checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" -"checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" +"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" +"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" "checksum cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)" = "39f75544d7bbaf57560d2168f28fd649ff9c76153874db88bdbdfd839b1a7e7d" "checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33" @@ -1037,7 +1056,7 @@ dependencies = [ "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum futures 0.1.27 (registry+https://github.com/rust-lang/crates.io-index)" = "a2037ec1c6c1c4f79557762eab1f7eae1f64f6cb418ace90fae88f0942b60139" "checksum futures-cpupool 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "ab90cde24b3319636588d0c35fe03b1333857621051837ed769faefb4c2162e4" -"checksum h2 0.1.21 (registry+https://github.com/rust-lang/crates.io-index)" = "876d91114d78abbde2e1910e3b2d9d0fd1d89b769e20816dfb68d77992cf4158" +"checksum h2 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)" = "1e42e3daed5a7e17b12a0c23b5b2fbff23a925a570938ebee4baca1a9a1a2240" "checksum http 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "eed324f0f0daf6ec10c474f150505af2c143f251722bf9dbd1261bd1f2ee2c1a" "checksum http-body 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6741c859c1b2463a423a1dbce98d418e6c3c3fc720fb0d45528657320920292d" "checksum httparse 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e8734b0cfd3bc3e101ec59100e101c2eecd19282202e87808b3037b442777a83" @@ -1063,6 +1082,7 @@ dependencies = [ "checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337" "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" "checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759" +"checksum prometheus_exporter_base 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f625a154830c0dd2673c701442d2d278f8b37a6f603b91059ec901b38fce6009" "checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "faf4799c5d274f3868a4aae320a0a182cbd2baee377b378f080e16a23e9d80db" "checksum rand 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca" @@ -1078,8 +1098,8 @@ dependencies = [ "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.54 (registry+https://github.com/rust-lang/crates.io-index)" = "12229c14a0f65c4f1cb046a3b52047cdd9da1f4b30f8a39c5063c8bae515e252" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" -"checksum regex 1.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "8f0a0bcab2fd7d1d7c54fa9eae6f43eddeb9ce2e7352f8518a814a4f65d60c58" -"checksum regex-syntax 0.6.6 (registry+https://github.com/rust-lang/crates.io-index)" = "dcfd8681eebe297b81d98498869d4aae052137651ad7b96822f09ceb690d0a96" +"checksum regex 1.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "0b2f0808e7d7e4fb1cb07feb6ff2f4bc827938f24f8c2e6a3beb7370af544bdd" +"checksum regex-syntax 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d76410686f9e3a17f06128962e0ecc5755870bb890c34820c7af7f1db2e1d48" "checksum rustc-demangle 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "a7f4dccf6f4891ebcc0c39f9b6eb1a83b9bf5d747cb439ec6fba4f3b977038af" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" "checksum ryu 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "b96a9549dc8d48f2c283938303c4b5a77aa29bfbc5b54b084fb1630408899a8f" @@ -1092,11 +1112,11 @@ dependencies = [ "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c4488ae950c49d403731982257768f48fada354a5203fe81f9bb6f43ca9002be" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" -"checksum string 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b639411d0b9c738748b5397d5ceba08e648f4f1992231aa859af1a017f31f60b" +"checksum string 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0bbfb8937e38e34c3444ff00afb28b0811d9554f15c5ad64d12b0308d1d1995" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" -"checksum syn 0.15.34 (registry+https://github.com/rust-lang/crates.io-index)" = "a1393e4a97a19c01e900df2aec855a29f71cf02c402e2f443b8d2747c25c5dbe" +"checksum syn 0.15.35 (registry+https://github.com/rust-lang/crates.io-index)" = "641e117d55514d6d918490e47102f7e08d096fdde360247e4a10f7a91a8478d3" "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" -"checksum termcolor 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" +"checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" "checksum termion 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dde0593aeb8d47accea5392b39350015b5eccb12c0d98044d856983d89548dea" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" @@ -1107,7 +1127,7 @@ dependencies = [ "checksum tokio-executor 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "83ea44c6c0773cc034771693711c35c677b4b5a4b21b9e7071704c54de7d555e" "checksum tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5090db468dad16e1a7a54c8c67280c5e4b544f3d3e018f0b913b400261f85926" "checksum tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6af16bfac7e112bea8b0442542161bfc41cbfa4466b580bdda7d18cb88b911ce" -"checksum tokio-sync 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "5b2f843ffdf8d6e1f90bddd48da43f99ab071660cd92b7ec560ef3cdfd7a409a" +"checksum tokio-sync 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2162248ff317e2bc713b261f242b69dbb838b85248ed20bb21df56d60ea4cae7" "checksum tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1d14b10654be682ac43efee27401d792507e30fd8d26389e1da3b185de2e4119" "checksum tokio-threadpool 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "72558af20be886ea124595ea0f806dd5703b8958e4705429dd58b3d8231f72f2" "checksum tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "f2106812d500ed25a4f38235b9cae8f78a09edf43203e16e59c3b769a342a60e" @@ -1116,7 +1136,7 @@ dependencies = [ "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" -"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" +"checksum utf8-ranges 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9d50aa7650df78abf942826607c62468ce18d9019673d4a2ebe1865dbb96ffde" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum want 0.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "797464475f30ddb8830cc529aaaae648d581f99e2036a928877dfde027ddf6b3" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" diff --git a/Cargo.toml b/Cargo.toml index 1cf5f93..e2ed614 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,28 +1,29 @@ [package] -name = "prometheus_wireguard_exporter" -version = "2.0.1" -authors = ["Francesco Cogno "] -description = "Prometheus WireGuard Exporter" -edition = "2018" +name = "prometheus_wireguard_exporter" +version = "2.0.2" +authors = ["Francesco Cogno "] +description = "Prometheus WireGuard Exporter" +edition = "2018" -readme = "README.md" -license = "MIT" -repository = "https://github.com/MindFlavor/prometheus_wireguard_exporter" -documentation = "https://github.com/MindFlavor/prometheus_wireguard_exporter" -homepage = "https://github.com/MindFlavor/prometheus_wireguard_exporter" - -keywords = ["prometheus", "exporter", "wireguard"] -categories = ["database"] +readme = "README.md" +license = "MIT" +repository = "https://github.com/MindFlavor/prometheus_wireguard_exporter" +documentation = "https://github.com/MindFlavor/prometheus_wireguard_exporter" +homepage = "https://github.com/MindFlavor/prometheus_wireguard_exporter" +keywords = ["prometheus", "exporter", "wireguard"] +categories = ["database"] [dependencies] -log = "0.4.6" -env_logger = "0.6.1" -futures = "0.1.27" -clap = "2.33.0" -serde_json = "1.0.39" -serde = "1.0.91" -serde_derive = "1.0.91" -failure = "0.1.5" -hyper = "0.12.29" -http = "0.1.17" +log = "0.4.6" +env_logger = "0.6.1" +futures = "0.1.27" +clap = "2.33.0" +serde_json = "1.0.39" +serde = "1.0.91" +serde_derive = "1.0.91" +failure = "0.1.5" +hyper = "0.12.29" +http = "0.1.17" +prometheus_exporter_base = "0.2.0" + diff --git a/src/exporter_error.rs b/src/exporter_error.rs index b3bc0c2..64b93df 100644 --- a/src/exporter_error.rs +++ b/src/exporter_error.rs @@ -1,5 +1,5 @@ #[derive(Debug, Fail)] -pub(crate) enum PeerEntryParseError { +pub enum PeerEntryParseError { #[fail(display = "PublicKey entry not found in lines: {:?}", lines)] PublicKeyNotFound { lines: Vec }, @@ -8,7 +8,7 @@ pub(crate) enum PeerEntryParseError { } #[derive(Debug, Fail)] -pub(crate) enum ExporterError { +pub enum ExporterError { #[allow(dead_code)] #[fail(display = "Generic error")] Generic {}, diff --git a/src/main.rs b/src/main.rs index 6031f70..888dde1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,65 +2,25 @@ extern crate serde_json; #[macro_use] extern crate failure; use clap; -use clap::Arg; +use clap::{crate_authors, crate_name, crate_version, Arg}; use futures::future::{done, ok, Either, Future}; -use http::StatusCode; -use hyper::service::service_fn; -use hyper::{Body, Request, Response, Server}; -use log::{error, info, trace}; +use hyper::{Body, Request, Response}; +use log::{info, trace}; use std::env; mod options; use options::Options; -mod exporter_error; -use exporter_error::ExporterError; -mod render_to_prometheus; -use render_to_prometheus::RenderToPrometheus; mod wireguard; use std::convert::TryFrom; use std::process::Command; use std::string::String; use wireguard::WireGuard; +mod exporter_error; mod wireguard_config; use wireguard_config::peer_entry_hashmap_try_from; - -fn check_compliance(req: &Request) -> Result<(), Response> { - if req.uri() != "/metrics" { - trace!("uri not allowed {}", req.uri()); - Err(Response::builder() - .status(StatusCode::NOT_FOUND) - .body(hyper::Body::empty()) - .unwrap()) - } else if req.method() != "GET" { - trace!("method not allowed {}", req.method()); - Err(Response::builder() - .status(StatusCode::METHOD_NOT_ALLOWED) - .body(hyper::Body::empty()) - .unwrap()) - } else { - Ok(()) - } -} - -fn handle_request( - req: Request, - options: Options, -) -> impl Future, Error = failure::Error> { - trace!("{:?}", req); - - done(check_compliance(&req)).then(move |res| match res { - Ok(_) => Either::A(perform_request(req, &options).then(|res| match res { - Ok(body) => ok(body), - Err(err) => { - error!("internal server error: {:?}", err); - ok(Response::builder() - .status(StatusCode::INTERNAL_SERVER_ERROR) - .body(hyper::Body::empty()) - .unwrap()) - } - })), - Err(err) => Either::B(ok(err)), - }) -} +extern crate prometheus_exporter_base; +use crate::exporter_error::ExporterError; +use prometheus_exporter_base::render_prometheus; +use std::sync::Arc; fn wg_with_text( wg_config_str: &str, @@ -76,8 +36,8 @@ fn wg_with_text( fn perform_request( _req: Request, - options: &Options, -) -> impl Future, Error = ExporterError> { + options: &Arc, +) -> impl Future, Error = failure::Error> { trace!("perform_request"); // this is needed to satisfy the borrow checker @@ -91,8 +51,8 @@ fn perform_request( .output(), ) .from_err() - .and_then(|output| { - if let Some(extract_names_config_file) = options.extract_names_config_file { + .and_then(move |output| { + if let Some(extract_names_config_file) = &options.extract_names_config_file { Either::A( done(::std::fs::read_to_string(extract_names_config_file)) .from_err() @@ -106,17 +66,20 @@ fn perform_request( trace!("{}", output_str); done(WireGuard::try_from(&output_str as &str)) .from_err() - .and_then(|wg| ok(Response::new(Body::from(wg.render())))) + .and_then(|wg| { + ok(Response::new(Body::from(wg.render_with_names(None)))) + }) }), ) } }) + .from_err() } fn main() { - let matches = clap::App::new("prometheus_wireguard_exporter") - .version("1.2.0") - .author("Francesco Cogno ") + let matches = clap::App::new(crate_name!()) + .version(crate_version!()) + .author(crate_authors!("\n")) .arg( Arg::with_name("port") .short("p") @@ -140,9 +103,15 @@ fn main() { let options = Options::from_claps(&matches); if options.verbose { - env::set_var("RUST_LOG", "prometheus_wireguard_exporter=trace"); + env::set_var( + "RUST_LOG", + format!("{}=trace,prometheus_exporter_base=trace", crate_name!()), + ); } else { - env::set_var("RUST_LOG", "prometheus_wireguard_exporter=info"); + env::set_var( + "RUST_LOG", + format!("{}=info,prometheus_exporter_base=info", crate_name!()), + ); } env_logger::init(); @@ -154,13 +123,7 @@ fn main() { info!("starting exporter on {}", addr); - let new_svc = move || { - let options = options.clone(); - service_fn(move |req| handle_request(req, options.clone())) - }; - - let server = Server::bind(&addr) - .serve(new_svc) - .map_err(|e| eprintln!("server error: {}", e)); - hyper::rt::run(server); + render_prometheus(&addr, options, |request, options| { + Box::new(perform_request(request, options)) + }); } diff --git a/src/render_to_prometheus.rs b/src/render_to_prometheus.rs deleted file mode 100644 index 95132d7..0000000 --- a/src/render_to_prometheus.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub trait RenderToPrometheus { - fn render(&self) -> String; -} diff --git a/src/wireguard.rs b/src/wireguard.rs index cc8fe7c..7e7e7d7 100644 --- a/src/wireguard.rs +++ b/src/wireguard.rs @@ -1,7 +1,7 @@ use crate::exporter_error::ExporterError; -use crate::render_to_prometheus::RenderToPrometheus; use crate::wireguard_config::PeerEntryHashMap; use log::{debug, trace}; +use prometheus_exporter_base::PrometheusCounter; use std::collections::HashMap; use std::convert::TryFrom; use std::net::SocketAddr; @@ -80,10 +80,7 @@ impl TryFrom<&str> for WireGuard { let (remote_ip, remote_port) = if let Some(ip_and_port) = to_option_string(v[3]) { let addr: SocketAddr = ip_and_port.parse::().unwrap(); - ( - Some(addr.ip().to_string()), - Some(addr.port()), - ) + (Some(addr.ip().to_string()), Some(addr.port())) } else { (None, None) }; @@ -122,86 +119,88 @@ impl TryFrom<&str> for WireGuard { impl WireGuard { pub(crate) fn render_with_names(&self, pehm: Option<&PeerEntryHashMap>) -> String { - let mut latest_handshakes = Vec::new(); - let mut sent_bytes = Vec::new(); - let mut received_bytes = Vec::new(); + // these are the exported counters + let pc_sent_bytes_total = PrometheusCounter::new( + "wireguard_sent_bytes_total", + "counter", + "Bytes sent to the peer", + ); + let pc_received_bytes_total = PrometheusCounter::new( + "wireguard_received_bytes_total", + "counter", + "Bytes received from the peer", + ); + let pc_latest_handshake = PrometheusCounter::new( + "wireguard_latest_handshake_seconds", + "gauge", + "Seconds from the last handshake", + ); - if let Some(pehm) = pehm { - // here we try to add the "friendly" name looking in the hashmap - for (interface, endpoints) in self.interfaces.iter() { - for endpoint in endpoints { - // only show remote endpoints - if let Endpoint::Remote(ep) = endpoint { - debug!("{:?}", ep); + // these 3 vectors will hold the intermediate + // values. We use the vector in order to traverse + // the interfaces slice only once: since we need to output + // the values grouped by counter we populate the vectors here + // and then reorder during the final string creation phase. + let mut s_sent_bytes_total = Vec::new(); + s_sent_bytes_total.push(pc_sent_bytes_total.render_header()); + + let mut s_received_bytes_total = Vec::new(); + s_received_bytes_total.push(pc_received_bytes_total.render_header()); + + let mut s_latest_handshake = Vec::new(); + s_latest_handshake.push(pc_latest_handshake.render_header()); + + for (interface, endpoints) in self.interfaces.iter() { + for endpoint in endpoints { + // only show remote endpoints + if let Endpoint::Remote(ep) = endpoint { + debug!("{:?}", ep); + + let mut attributes: Vec<(&str, &str)> = Vec::new(); + attributes.push(("inteface", interface)); + attributes.push(("public_key", &ep.public_key)); + attributes.push(("local_ip", &ep.local_ip)); + attributes.push(("local_subnet", &ep.local_subnet)); + + // let's add the friendly_name attribute if present + // and has meaniningful value + if let Some(pehm) = pehm { if let Some(ep_friendly_name) = pehm.get(&ep.public_key as &str) { if let Some(ep_friendly_name) = ep_friendly_name.name { - sent_bytes.push(format!("wireguard_sent_bytes_total{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\", friendly_name=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep_friendly_name, ep.sent_bytes)); - received_bytes.push(format!("wireguard_received_bytes_total{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\", friendly_name=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep_friendly_name, ep.received_bytes)); - latest_handshakes.push(format!("wireguard_latest_handshake_seconds{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\", friendly_name=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep_friendly_name, ep.latest_handshake)); - } else { - sent_bytes.push(format!("wireguard_sent_bytes_total{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.sent_bytes)); - received_bytes.push(format!("wireguard_received_bytes_total{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.received_bytes)); - latest_handshakes.push(format!("wireguard_latest_handshake_seconds{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.latest_handshake)); + attributes.push(("friendly_name", &ep_friendly_name)); } - } else { - sent_bytes.push(format!("wireguard_sent_bytes_total{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.sent_bytes)); - received_bytes.push(format!("wireguard_received_bytes_total{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.received_bytes)); - latest_handshakes.push(format!("wireguard_latest_handshake_seconds{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.latest_handshake)); } } - } - } - } else { - for (interface, endpoints) in self.interfaces.iter() { - for endpoint in endpoints { - // only show remote endpoints - if let Endpoint::Remote(ep) = endpoint { - debug!("{:?}", ep); - sent_bytes.push(format!("wireguard_sent_bytes_total{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.sent_bytes)); - received_bytes.push(format!("wireguard_received_bytes_total{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.received_bytes)); - latest_handshakes.push(format!("wireguard_latest_handshake_seconds{{inteface=\"{}\", public_key=\"{}\", local_ip=\"{}\", local_subnet=\"{}\"}} {}\n", interface, ep.public_key, ep.local_ip, ep.local_subnet, ep.latest_handshake)); - } + + s_sent_bytes_total + .push(pc_sent_bytes_total.render_counter(Some(&attributes), ep.sent_bytes)); + s_received_bytes_total.push( + pc_received_bytes_total + .render_counter(Some(&attributes), ep.received_bytes), + ); + s_latest_handshake.push( + pc_latest_handshake.render_counter(Some(&attributes), ep.latest_handshake), + ); } } } - let mut s = String::new(); - - s.push_str( - "# HELP wireguard_sent_bytes_total Bytes sent to the peer -# TYPE wireguard_sent_bytes_total counter\n", - ); - for peer in sent_bytes { - s.push_str(&peer); + // now let's join the results and return it to the caller + let mut s = String::with_capacity(s_latest_handshake.len() * 64 * 3); + for item in s_sent_bytes_total { + s.push_str(&item); + } + for item in s_received_bytes_total { + s.push_str(&item); + } + for item in s_latest_handshake { + s.push_str(&item); } - s.push_str( - "# HELP wireguard_received_bytes_total Bytes received from the peer -# TYPE wireguard_received_bytes_total counter\n", - ); - for peer in received_bytes { - s.push_str(&peer); - } - - s.push_str( - "# HELP wireguard_latest_handshake_seconds Seconds from the last handshake -# TYPE wireguard_latest_handshake_seconds gauge\n", - ); - for peer in latest_handshakes { - s.push_str(&peer); - } - - debug!("{}", s); s } } -impl RenderToPrometheus for WireGuard { - fn render(&self) -> String { - self.render_with_names(None) - } -} - #[cfg(test)] mod tests { use super::*; @@ -228,13 +227,96 @@ wg0\t928vO9Lf4+Mo84cWu4k1oRyzf0AR7FTGoPKHGoTMSHk=\t(none)\t5.90.62.106:21741\t10 Endpoint::Remote(re) => re, }; - assert!(e1.public_key == "2S7mA0vEMethCNQrJpJKE81/JmhgtB+tHHLYQhgM6kk="); + assert_eq!( + e1.public_key, + "2S7mA0vEMethCNQrJpJKE81/JmhgtB+tHHLYQhgM6kk=" + ); } #[test] fn test_parse_and_serialize() { let a = WireGuard::try_from(TEXT).unwrap(); - let s = a.render(); + let s = a.render_with_names(None); println!("{}", s); } + + #[test] + fn test_render_to_prometheus_simple() { + const REF : &str= "# HELP wireguard_sent_bytes_total Bytes sent to the peer\n# TYPE wireguard_sent_bytes_total counter\nwireguard_sent_bytes_total{inteface=\"Pippo\",public_key=\"test\",local_ip=\"local_ip\",local_subnet=\"local_subnet\"} 1000\n# HELP wireguard_received_bytes_total Bytes received from the peer\n# TYPE wireguard_received_bytes_total counter\nwireguard_received_bytes_total{inteface=\"Pippo\",public_key=\"test\",local_ip=\"local_ip\",local_subnet=\"local_subnet\"} 5000\n# HELP wireguard_latest_handshake_seconds Seconds from the last handshake\n# TYPE wireguard_latest_handshake_seconds gauge\nwireguard_latest_handshake_seconds{inteface=\"Pippo\",public_key=\"test\",local_ip=\"local_ip\",local_subnet=\"local_subnet\"} 500\n"; + + let re = Endpoint::Remote(RemoteEndpoint { + public_key: "test".to_owned(), + remote_ip: Some("remote_ip".to_owned()), + remote_port: Some(100), + local_ip: "local_ip".to_owned(), + local_subnet: "local_subnet".to_owned(), + latest_handshake: 500, + sent_bytes: 1000, + received_bytes: 5000, + persistent_keepalive: false, + }); + let mut wg = WireGuard { + interfaces: HashMap::new(), + }; + + let mut v = Vec::new(); + v.push(re); + wg.interfaces.insert("Pippo".to_owned(), v); + + let prometheus = wg.render_with_names(None); + + assert_eq!(prometheus, REF); + } + + #[test] + fn test_render_to_prometheus_complex() { + use crate::wireguard_config::PeerEntry; + + const REF :&'static str = "# HELP wireguard_sent_bytes_total Bytes sent to the peer\n# TYPE wireguard_sent_bytes_total counter\nwireguard_sent_bytes_total{inteface=\"Pippo\",public_key=\"test\",local_ip=\"local_ip\",local_subnet=\"local_subnet\"} 1000\nwireguard_sent_bytes_total{inteface=\"Pippo\",public_key=\"second_test\",local_ip=\"local_ip\",local_subnet=\"local_subnet\",friendly_name=\"this is my friendly name\"} 14\n# HELP wireguard_received_bytes_total Bytes received from the peer\n# TYPE wireguard_received_bytes_total counter\nwireguard_received_bytes_total{inteface=\"Pippo\",public_key=\"test\",local_ip=\"local_ip\",local_subnet=\"local_subnet\"} 5000\nwireguard_received_bytes_total{inteface=\"Pippo\",public_key=\"second_test\",local_ip=\"local_ip\",local_subnet=\"local_subnet\",friendly_name=\"this is my friendly name\"} 1000000000\n# HELP wireguard_latest_handshake_seconds Seconds from the last handshake\n# TYPE wireguard_latest_handshake_seconds gauge\nwireguard_latest_handshake_seconds{inteface=\"Pippo\",public_key=\"test\",local_ip=\"local_ip\",local_subnet=\"local_subnet\"} 500\nwireguard_latest_handshake_seconds{inteface=\"Pippo\",public_key=\"second_test\",local_ip=\"local_ip\",local_subnet=\"local_subnet\",friendly_name=\"this is my friendly name\"} 50\n"; + + let re1 = Endpoint::Remote(RemoteEndpoint { + public_key: "test".to_owned(), + remote_ip: Some("remote_ip".to_owned()), + remote_port: Some(100), + local_ip: "local_ip".to_owned(), + local_subnet: "local_subnet".to_owned(), + latest_handshake: 500, + sent_bytes: 1000, + received_bytes: 5000, + persistent_keepalive: false, + }); + let re2 = Endpoint::Remote(RemoteEndpoint { + public_key: "second_test".to_owned(), + remote_ip: Some("remote_ip".to_owned()), + remote_port: Some(100), + local_ip: "local_ip".to_owned(), + local_subnet: "local_subnet".to_owned(), + latest_handshake: 50, + sent_bytes: 14, + received_bytes: 1_000_000_000, + persistent_keepalive: false, + }); + + let mut wg = WireGuard { + interfaces: HashMap::new(), + }; + + let mut v = Vec::new(); + v.push(re1); + v.push(re2); + wg.interfaces.insert("Pippo".to_owned(), v); + + let mut pehm = PeerEntryHashMap::new(); + let pe = PeerEntry { + public_key: "second_test", + allowed_ips: "ignored", + name: Some("this is my friendly name"), + }; + pehm.insert(pe.public_key, pe); + + let prometheus = wg.render_with_names(Some(&pehm)); + + assert_eq!(prometheus, REF); + } + }