Use common rust formatting
This commit is contained in:
parent
698f36518d
commit
aebcde3808
114
src/main.rs
114
src/main.rs
@ -1,11 +1,8 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
routing::get,
|
http::StatusCode,
|
||||||
Router,
|
response::{IntoResponse, Response},
|
||||||
response::{
|
routing::get,
|
||||||
IntoResponse,
|
Router,
|
||||||
Response,
|
|
||||||
},
|
|
||||||
http::StatusCode,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
@ -13,70 +10,71 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut listen = String::from("[::]:9152");
|
let mut listen = String::from("[::]:9152");
|
||||||
let mut args = std::env::args();
|
let mut args = std::env::args();
|
||||||
args.next();
|
args.next();
|
||||||
loop {
|
loop {
|
||||||
let arg = if let Some(arg) = args.next() {
|
let arg = if let Some(arg) = args.next() {
|
||||||
arg
|
arg
|
||||||
} else {
|
} else {
|
||||||
break
|
break;
|
||||||
};
|
};
|
||||||
|
|
||||||
match arg.as_str() {
|
match arg.as_str() {
|
||||||
"--help" | "-h" => {
|
"--help" | "-h" => {
|
||||||
println!("Prometheus exporter for NixOS systems");
|
println!("Prometheus exporter for NixOS systems");
|
||||||
println!("Use --listen <addr:port> bind the web service.");
|
println!("Use --listen <addr:port> bind the web service.");
|
||||||
println!("Output will be on /metrics endpoint. HTTP 500 if something broke while scraping.");
|
println!("Output will be on /metrics endpoint. HTTP 500 if something broke while scraping.");
|
||||||
std::process::exit(0);
|
std::process::exit(0);
|
||||||
},
|
}
|
||||||
"--listen" => {
|
"--listen" => {
|
||||||
listen = args.next().unwrap();
|
listen = args.next().unwrap();
|
||||||
},
|
}
|
||||||
unknown => {
|
unknown => {
|
||||||
println!("unknown option: {}", unknown);
|
println!("unknown option: {}", unknown);
|
||||||
std::process::exit(1)
|
std::process::exit(1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let app = Router::new().route("/metrics", get(metrics));
|
let app = Router::new().route("/metrics", get(metrics));
|
||||||
|
|
||||||
let addr = SocketAddr::from_str(&listen).unwrap();
|
let addr = SocketAddr::from_str(&listen).unwrap();
|
||||||
println!("listening on http://{}", addr);
|
println!("listening on http://{}", addr);
|
||||||
axum::Server::bind(&addr)
|
axum::Server::bind(&addr)
|
||||||
.serve(app.into_make_service())
|
.serve(app.into_make_service())
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_current_system() -> Result<(String, String), String> {
|
fn get_current_system() -> Result<(String, String), String> {
|
||||||
let symlink = match std::fs::read_link("/run/current-system") {
|
let symlink = match std::fs::read_link("/run/current-system") {
|
||||||
Ok(symlink) => symlink,
|
Ok(symlink) => symlink,
|
||||||
Err(err) => return Err(err.to_string())
|
Err(err) => return Err(err.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let (hash, name) = symlink.file_name()
|
let (hash, name) = symlink
|
||||||
.ok_or_else(String::default)?
|
.file_name()
|
||||||
.to_str()
|
.ok_or_else(String::default)?
|
||||||
.ok_or_else(String::default)?
|
.to_str()
|
||||||
.split_once("-")
|
.ok_or_else(String::default)?
|
||||||
.ok_or_else(String::default)?;
|
.split_once("-")
|
||||||
|
.ok_or_else(String::default)?;
|
||||||
|
|
||||||
Ok((String::from(hash), String::from(name)))
|
Ok((String::from(hash), String::from(name)))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn metrics() -> Response {
|
async fn metrics() -> Response {
|
||||||
let current_system = get_current_system();
|
let current_system = get_current_system();
|
||||||
let (hash, name) = match current_system {
|
let (hash, name) = match current_system {
|
||||||
Ok((hash, name)) => (hash, name),
|
Ok((hash, name)) => (hash, name),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!("failed: {}", err);
|
println!("failed: {}", err);
|
||||||
return (StatusCode::INTERNAL_SERVER_ERROR, "").into_response()
|
return (StatusCode::INTERNAL_SERVER_ERROR, "").into_response();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
(
|
(
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
format!("nixos_nixos_current_system_hash{{hash=\"{}\"}} 1\nnixos_current_system_name{{name=\"{}\"}} 1\n", hash, name)
|
format!("nixos_nixos_current_system_hash{{hash=\"{}\"}} 1\nnixos_current_system_name{{name=\"{}\"}} 1\n", hash, name)
|
||||||
).into_response()
|
).into_response()
|
||||||
|
Loading…
Reference in New Issue
Block a user