Browse Source

add metrics endpoint

main
Milan 3 months ago
parent
commit
b7fb846499
Signed by: petabyteboy GPG Key ID: A6DC6A7CB0B97859
  1. 37
      src/server.rs

37
src/server.rs

@ -106,6 +106,7 @@ impl RustyLG {
(&Method::GET, "/api/routes.json") => self.routes(&req).await,
(&Method::GET, "/api/protocols.json") => self.protocols(&req).await,
(&Method::GET, "/api/graph.json") => self.graph(&req).await,
(&Method::GET, "/api/metrics") => self.metrics(&req).await,
_ => Err(Error::HttpStatus { status: StatusCode::NOT_FOUND, text: "route not found".to_string() }),
};
let resp = match resp {
@ -312,4 +313,40 @@ impl RustyLG {
Err(_) => format!("AS{}", asn),
}
}
async fn metrics(&self, req: &Request<Body>) -> HttpResult {
let _query = query_params!(req);
let command = "show protocols all".to_string();
let resp_text = self.get_bird_output(&command).await
.into_iter()
.map(|(host_name, bird_output)| {
match bird_output.map(parse::protocols) {
Ok(parsed) => {
let protos = parsed.into_iter().map(|(proto_name, parsed_proto)| {
let online = match parsed_proto.state.as_str() {
"up" => "1",
_ => "0",
};
format!("bird_protocol_online{{hostname=\"{}\",protoname=\"{}\"}} {}", host_name, proto_name, online)
})
.collect::<Vec<_>>()
.join("\n");
format!("bird_host_online{{hostname=\"{}\"}} 1\n{}", host_name, protos)
},
Err(_) => {
format!("bird_host_online{{hostname=\"{}\"}} 0", host_name)
},
}
})
.collect::<Vec<_>>()
.join("\n");
Ok(Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/plain; version=0.0.4; charset=UTF-8")
.body(resp_text.into())?)
}
}

Loading…
Cancel
Save