mirror of
https://codeberg.org/icewind/prometheus-edge-trigger.git
synced 2026-06-03 18:24:10 +02:00
use mdns 1.1
This commit is contained in:
parent
63943d141e
commit
cf0a704f21
6 changed files with 124 additions and 167 deletions
65
Cargo.lock
generated
65
Cargo.lock
generated
|
|
@ -15,16 +15,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
|
||||
[[package]]
|
||||
name = "async-stream"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"async-stream-impl 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"async-stream-impl 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-stream-impl"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
@ -276,7 +276,7 @@ dependencies = [
|
|||
"indexmap 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio-util 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
|
@ -344,9 +344,9 @@ dependencies = [
|
|||
"itoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pin-project 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pin-project 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tower-service 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"want 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
|
@ -362,7 +362,7 @@ dependencies = [
|
|||
"hyper 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustls 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustls-native-certs 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio-rustls 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"webpki 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
|
@ -375,7 +375,7 @@ dependencies = [
|
|||
"bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"hyper 0.13.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio-tls 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
|
@ -462,16 +462,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
|
||||
[[package]]
|
||||
name = "mdns"
|
||||
version = "1.0.0"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"async-stream 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"async-stream 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"dns-parser 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"err-derive 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures-util 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -595,15 +595,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
|
||||
[[package]]
|
||||
name = "pin-project"
|
||||
version = "0.4.7"
|
||||
version = "0.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"pin-project-internal 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pin-project-internal 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-internal"
|
||||
version = "0.4.7"
|
||||
version = "0.4.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
@ -681,13 +681,13 @@ dependencies = [
|
|||
[[package]]
|
||||
name = "prometheus-edge-detector"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/icewind1991/prometheus-edge-detector#67e25175e20e6ebf452cb1879fa8fcf780d959a8"
|
||||
source = "git+https://github.com/icewind1991/prometheus-edge-detector#5a566f0197f3454da4e349e3b282d325051c4602"
|
||||
dependencies = [
|
||||
"err-derive 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"futures-util 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"reqwest 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -700,12 +700,12 @@ dependencies = [
|
|||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"main_error 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mdns 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mdns 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"prometheus-edge-detector 0.1.0 (git+https://github.com/icewind1991/prometheus-edge-detector)",
|
||||
"reqwest 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
|
@ -816,7 +816,7 @@ dependencies = [
|
|||
"serde_json 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_urlencoded 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio-rustls 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio-tls 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
@ -1047,7 +1047,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "0.2.10"
|
||||
version = "0.2.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
@ -1059,14 +1059,15 @@ dependencies = [
|
|||
"mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pin-project-lite 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio-macros 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio-macros 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "0.2.3"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
|
@ -1078,7 +1079,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
dependencies = [
|
||||
"futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"rustls 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"webpki 0.21.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
|
@ -1088,7 +1089,7 @@ version = "0.3.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1101,7 +1102,7 @@ dependencies = [
|
|||
"futures-sink 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"pin-project-lite 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -1371,8 +1372,8 @@ dependencies = [
|
|||
[metadata]
|
||||
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
|
||||
"checksum anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)" = "7825f6833612eb2414095684fcf6c635becf3ce97fe48cf6421321e93bfbd53c"
|
||||
"checksum async-stream 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "58982858be7540a465c790b95aaea6710e5139bf8956b1d1344d014fa40100b0"
|
||||
"checksum async-stream-impl 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "393356ed99aa7bff0ac486dde592633b83ab02bd254d8c209d5b9f1d0f533480"
|
||||
"checksum async-stream 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "22068c0c19514942eefcfd4daf8976ef1aad84e61539f95cd200c35202f80af5"
|
||||
"checksum async-stream-impl 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "25f9db3b38af870bf7e5cc649167533b493928e50744e2c30ae350230b414670"
|
||||
"checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
|
||||
"checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
|
||||
|
|
@ -1427,7 +1428,7 @@ dependencies = [
|
|||
"checksum main_error 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3516df0fb44d98fe6d6e859d224adfb7b6686447937e5b96308d6061595eed04"
|
||||
"checksum maplit 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
|
||||
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
|
||||
"checksum mdns 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d1d1e461d374d1e092612bd6087031f55c03dd10a38e680dce380181ba0aabf0"
|
||||
"checksum mdns 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "caccbabad4e71a1a261c872ac0bb94ce9958098a1ef4c637f2b4de4f6835ce7e"
|
||||
"checksum memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3197e20c7edb283f87c071ddfc7a2cca8f8e0b888c242959846a6fce03c72223"
|
||||
"checksum mime 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
|
||||
"checksum mime_guess 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1a0ed03949aef72dbdf3116a383d7b38b4768e6f960528cd6a6044aa9ed68599"
|
||||
|
|
@ -1440,8 +1441,8 @@ dependencies = [
|
|||
"checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"
|
||||
"checksum openssl-sys 0.9.53 (registry+https://github.com/rust-lang/crates.io-index)" = "465d16ae7fc0e313318f7de5cecf57b2fbe7511fd213978b457e1c96ff46736f"
|
||||
"checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
||||
"checksum pin-project 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "75fca1c4ff21f60ca2d37b80d72b63dab823a9d19d3cda3a81d18bc03f0ba8c5"
|
||||
"checksum pin-project-internal 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "6544cd4e4ecace61075a6ec78074beeef98d58aa9a3d07d053d993b2946a90d6"
|
||||
"checksum pin-project 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7804a463a8d9572f13453c516a5faea534a2403d7ced2f0c7e100eeff072772c"
|
||||
"checksum pin-project-internal 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "385322a45f2ecf3410c68d2a549a4a2685e8051d0f278e39743ff4e451cb9b3f"
|
||||
"checksum pin-project-lite 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "237844750cfbb86f67afe27eee600dfbbcb6188d734139b534cbfbf4f96792ae"
|
||||
"checksum pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)" = "5894c618ce612a3fa23881b152b608bafb8c56cfc22f434a3ba3120b40f7b587"
|
||||
"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
|
||||
|
|
@ -1487,8 +1488,8 @@ dependencies = [
|
|||
"checksum termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
|
||||
"checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
|
||||
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
|
||||
"checksum tokio 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "c1fc73332507b971a5010664991a441b5ee0de92017f5a0e8b00fd684573045b"
|
||||
"checksum tokio-macros 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "50a61f268a3db2acee8dcab514efc813dc6dbe8a00e86076f935f94304b59a7a"
|
||||
"checksum tokio 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8fdd17989496f49cdc57978c96f0c9fe5e4a58a8bddc6813c449a4624f6a030b"
|
||||
"checksum tokio-macros 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f4b1e7ed7d5d4c2af3d999904b0eebe76544897cdbfb2b9684bed2174ab20f7c"
|
||||
"checksum tokio-rustls 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "141afec0978abae6573065a48882c6bae44c5cc61db9b511ac4abf6a09bfd9cc"
|
||||
"checksum tokio-tls 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7bde02a3a5291395f59b06ec6945a3077602fac2b07eeeaf0dee2122f3619828"
|
||||
"checksum tokio-util 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "571da51182ec208780505a32528fc5512a8fe1443ab960b3f2f3ef093cd16930"
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ edition = "2018"
|
|||
|
||||
[dependencies]
|
||||
prometheus-edge-detector = { version = "0.1", git = "https://github.com/icewind1991/prometheus-edge-detector", default-features = false }
|
||||
mdns = "1.0"
|
||||
mdns = "1.1"
|
||||
tokio = { version = "0.2.4", features = ["macros", "time", "fs"] }
|
||||
main_error = "0.1.0"
|
||||
futures-util = "0.3.1"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use serde::Deserialize;
|
||||
use err_derive::Error;
|
||||
use crate::mdns::resolve_mdns;
|
||||
use err_derive::Error;
|
||||
use serde::Deserialize;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
|
|
@ -38,27 +38,25 @@ pub enum Parameter {
|
|||
impl Parameter {
|
||||
pub async fn get_value(&self) -> Result<String, ParameterError> {
|
||||
match self {
|
||||
Parameter::Mdns {
|
||||
service,
|
||||
host
|
||||
} => {
|
||||
match resolve_mdns(service, host).await? {
|
||||
Some(service) => Ok(format!("{}:{}", service.addr, service.port)),
|
||||
None => Err(ParameterError::MdnsHostNotFound)
|
||||
}
|
||||
}
|
||||
Parameter::Mdns { service, host } => match resolve_mdns(service, host).await? {
|
||||
Some(service) => Ok(service.to_string()),
|
||||
None => Err(ParameterError::MdnsHostNotFound),
|
||||
},
|
||||
Parameter::Value { value } => Ok(value.clone()),
|
||||
Parameter::Service {
|
||||
file, key, value
|
||||
} => {
|
||||
Parameter::Service { file, key, value } => {
|
||||
let content = tokio::fs::read(file).await?;
|
||||
let services: Vec<Service> = serde_json::from_slice(&content)?;
|
||||
services.into_iter().find_map(|service| {
|
||||
service.labels.get(key)
|
||||
.filter(|val| *val == value)
|
||||
.and_then(|_| service.targets.get(0))
|
||||
.cloned()
|
||||
}).ok_or(ParameterError::ServiceNotFound)
|
||||
services
|
||||
.into_iter()
|
||||
.find_map(|service| {
|
||||
service
|
||||
.labels
|
||||
.get(key)
|
||||
.filter(|val| *val == value)
|
||||
.and_then(|_| service.targets.get(0))
|
||||
.cloned()
|
||||
})
|
||||
.ok_or(ParameterError::ServiceNotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -88,7 +86,7 @@ pub struct Config {
|
|||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct PrometheusConfig {
|
||||
pub url: String
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
|
|
@ -111,4 +109,4 @@ pub enum Method {
|
|||
pub struct Service {
|
||||
targets: Vec<String>,
|
||||
labels: HashMap<String, String>,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use main_error::MainError;
|
||||
use tokio::fs::File;
|
||||
use crate::config::Config;
|
||||
use crate::trigger::TriggerManager;
|
||||
use main_error::MainError;
|
||||
use tokio::fs::File;
|
||||
use tokio::prelude::*;
|
||||
|
||||
mod config;
|
||||
|
|
@ -28,4 +28,4 @@ async fn main() -> Result<(), MainError> {
|
|||
println!("Usage {} config.toml", bin);
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
98
src/mdns.rs
98
src/mdns.rs
|
|
@ -1,87 +1,15 @@
|
|||
use tokio::time::timeout;
|
||||
use futures_util::{pin_mut, stream::StreamExt};
|
||||
use mdns::{Record, RecordKind};
|
||||
use std::{net::IpAddr, time::Duration};
|
||||
use std::net::SocketAddr;
|
||||
use std::time::Duration;
|
||||
|
||||
const INTERVAL: Duration = Duration::from_secs(3);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MdnsService {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub addr: IpAddr,
|
||||
pub port: u16,
|
||||
pub async fn resolve_mdns(
|
||||
service: &str,
|
||||
search_name: &str,
|
||||
) -> Result<Option<SocketAddr>, mdns::Error> {
|
||||
Ok(mdns::resolve::one(
|
||||
service,
|
||||
&format!("{}.{}", search_name, service),
|
||||
Duration::from_secs(15),
|
||||
)
|
||||
.await?
|
||||
.and_then(|response| response.socket_address()))
|
||||
}
|
||||
|
||||
pub async fn resolve_mdns(service: &str, search_name: &str) -> Result<Option<MdnsService>, mdns::Error> {
|
||||
let stream = mdns::discover::all(service, INTERVAL)?.listen();
|
||||
pin_mut!(stream);
|
||||
|
||||
let process = async move {
|
||||
while let Some(Ok(response)) = stream.next().await {
|
||||
let id = response.records().find_map(to_id);
|
||||
let addr = response.records().find_map(to_ip_addr);
|
||||
let port = response.records().find_map(to_port);
|
||||
let name = response.records().find_map(to_name);
|
||||
|
||||
if let (Some(id), Some(addr), Some(name), Some(port)) = (id, addr, name, port) {
|
||||
let service = MdnsService {
|
||||
id,
|
||||
name,
|
||||
addr,
|
||||
port,
|
||||
};
|
||||
|
||||
if service.id == search_name {
|
||||
return Some(service);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
};
|
||||
|
||||
match timeout(Duration::from_secs(5), process).await {
|
||||
Err(_) => Ok(None),
|
||||
Ok(res) => Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
fn to_id(record: &Record) -> Option<String> {
|
||||
match &record.kind {
|
||||
RecordKind::PTR(id) => {
|
||||
id.split('.').next().map(|s| s.to_string())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_ip_addr(record: &Record) -> Option<IpAddr> {
|
||||
match record.kind {
|
||||
RecordKind::A(addr) => Some(addr.into()),
|
||||
RecordKind::AAAA(addr) => Some(addr.into()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_port(record: &Record) -> Option<u16> {
|
||||
match record.kind {
|
||||
RecordKind::SRV { port, .. } => Some(port),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_name(record: &Record) -> Option<String> {
|
||||
if let RecordKind::TXT(txt) = &record.kind {
|
||||
txt.iter()
|
||||
.find_map(|pair| {
|
||||
let mut parts = pair.split('=');
|
||||
if let (Some("name"), Some(value)) = (parts.next(), parts.next()) {
|
||||
Some(value.to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,13 @@
|
|||
use main_error::MainError;
|
||||
use std::collections::HashMap;
|
||||
use crate::config::{Parameter, ParameterError, Trigger, Config, Action, Method, Condition};
|
||||
use prometheus_edge_detector::EdgeDetector;
|
||||
use futures_util::future::try_join_all;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use reqwest::Client;
|
||||
use tokio::time::delay_for;
|
||||
use log::{info, error};
|
||||
use crate::config::{Action, Condition, Config, Method, Parameter, ParameterError, Trigger};
|
||||
use err_derive::Error;
|
||||
use futures_util::future::try_join_all;
|
||||
use log::{error, info};
|
||||
use main_error::MainError;
|
||||
use prometheus_edge_detector::EdgeDetector;
|
||||
use reqwest::Client;
|
||||
use std::collections::HashMap;
|
||||
use std::time::{Duration, SystemTime};
|
||||
use tokio::time::delay_for;
|
||||
|
||||
pub struct TriggerManager {
|
||||
http_client: Client,
|
||||
|
|
@ -16,7 +16,10 @@ pub struct TriggerManager {
|
|||
}
|
||||
|
||||
fn now() -> u64 {
|
||||
SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs()
|
||||
SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs()
|
||||
}
|
||||
|
||||
fn since(time: u64) -> u64 {
|
||||
|
|
@ -45,9 +48,12 @@ impl TriggerManager {
|
|||
}
|
||||
|
||||
pub async fn run_triggers(&self) -> Result<(), MainError> {
|
||||
try_join_all(self.triggers.iter().map(|trigger| {
|
||||
self.run_trigger(trigger)
|
||||
})).await?;
|
||||
try_join_all(
|
||||
self.triggers
|
||||
.iter()
|
||||
.map(|trigger| self.run_trigger(trigger)),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -61,7 +67,10 @@ impl TriggerManager {
|
|||
Ok(Some(edge)) => {
|
||||
let elapsed = since(edge);
|
||||
let wait = delay.saturating_sub(elapsed);
|
||||
info!("[{}] Found edge, {}s ago, waiting {}s before triggering", trigger.name, elapsed, wait);
|
||||
info!(
|
||||
"[{}] Found edge, {}s ago, waiting {}s before triggering",
|
||||
trigger.name, elapsed, wait
|
||||
);
|
||||
let wait_delay = Duration::from_secs(wait);
|
||||
delay_for(wait_delay).await;
|
||||
|
||||
|
|
@ -84,7 +93,10 @@ impl TriggerManager {
|
|||
}
|
||||
}
|
||||
Ok(None) => {
|
||||
info!("[{}] No edge found, waiting {}s before looking for new edge", trigger.name, delay);
|
||||
info!(
|
||||
"[{}] No edge found, waiting {}s before looking for new edge",
|
||||
trigger.name, delay
|
||||
);
|
||||
delay_for(delay_duration).await;
|
||||
}
|
||||
Err(e) => {
|
||||
|
|
@ -95,16 +107,31 @@ impl TriggerManager {
|
|||
}
|
||||
}
|
||||
|
||||
async fn get_edge(&self, condition: &Condition, delay: u64) -> Result<Option<u64>, TriggerError> {
|
||||
async fn get_edge(
|
||||
&self,
|
||||
condition: &Condition,
|
||||
delay: u64,
|
||||
) -> Result<Option<u64>, TriggerError> {
|
||||
let query = interpolate_params(&condition.query, &condition.params).await?;
|
||||
Ok(self.edge_detector.get_last_edge(&query, condition.from, condition.to, Duration::from_secs(delay + 60)).await?)
|
||||
Ok(self
|
||||
.edge_detector
|
||||
.get_last_edge(
|
||||
&query,
|
||||
condition.from,
|
||||
condition.to,
|
||||
Duration::from_secs(delay + 60),
|
||||
)
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
||||
async fn interpolate_params(input: &str, params: &HashMap<String, Parameter>) -> Result<String, ParameterError> {
|
||||
let futures = params.values().map(|definition| {
|
||||
Box::pin(definition.get_value())
|
||||
});
|
||||
async fn interpolate_params(
|
||||
input: &str,
|
||||
params: &HashMap<String, Parameter>,
|
||||
) -> Result<String, ParameterError> {
|
||||
let futures = params
|
||||
.values()
|
||||
.map(|definition| Box::pin(definition.get_value()));
|
||||
|
||||
let resolved_params: Vec<String> = try_join_all(futures).await?;
|
||||
let mut result = input.to_string();
|
||||
|
|
@ -120,13 +147,16 @@ async fn interpolate_params(input: &str, params: &HashMap<String, Parameter>) ->
|
|||
async fn test_interpolate() {
|
||||
use maplit::hashmap;
|
||||
|
||||
let result = interpolate_params("foo_$param", &hashmap! {
|
||||
"param".to_string() => Parameter::Value{value: "bar".to_string()}
|
||||
}).await;
|
||||
let result = interpolate_params(
|
||||
"foo_$param",
|
||||
&hashmap! {
|
||||
"param".to_string() => Parameter::Value{value: "bar".to_string()}
|
||||
},
|
||||
)
|
||||
.await;
|
||||
assert_eq!("foo_bar".to_string(), result.unwrap());
|
||||
}
|
||||
|
||||
|
||||
async fn run_action(action: &Action, client: &Client) -> Result<(), TriggerError> {
|
||||
let url = interpolate_params(&action.url, &action.params).await?;
|
||||
|
||||
|
|
@ -138,4 +168,4 @@ async fn run_action(action: &Action, client: &Client) -> Result<(), TriggerError
|
|||
req.send().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue