1
0
Fork 0
nixos-exporter/src/main.rs

106 lines
3.2 KiB
Rust
Raw Normal View History

2022-12-31 00:29:17 +01:00
use axum::{
2023-01-03 22:13:01 +01:00
http::StatusCode,
2023-03-23 18:43:09 +01:00
response::IntoResponse,
2023-01-03 22:13:01 +01:00
routing::get,
Router,
2022-12-31 00:29:17 +01:00
};
2023-01-04 01:10:30 +01:00
use std::collections::HashMap;
2022-12-31 00:29:17 +01:00
use std::net::SocketAddr;
use std::str::FromStr;
2023-05-09 08:40:39 +02:00
use nixos_exporter::nixos::NixStorePath;
2023-01-04 01:10:30 +01:00
2023-01-08 20:45:18 +01:00
#[derive(Clone)]
struct AppState {
listen: String,
}
impl AppState{
pub fn new() -> Self {
Self {
listen: String::from("[::]:9152"),
}
}
pub fn is_valid(self) -> bool {
2023-05-09 08:40:39 +02:00
return true;
2023-03-25 13:42:58 +01:00
}
}
2023-01-08 20:45:18 +01:00
2022-12-31 00:29:17 +01:00
#[tokio::main]
async fn main() {
2023-01-08 20:45:18 +01:00
let mut app_state = AppState::new();
2023-01-03 22:13:01 +01:00
let mut args = std::env::args();
2023-05-09 08:40:39 +02:00
let _name = args.next().unwrap();
2023-01-03 22:13:01 +01:00
loop {
let arg = if let Some(arg) = args.next() {
arg
} else {
break;
};
2022-12-31 00:29:17 +01:00
2023-01-03 22:13:01 +01:00
match arg.as_str() {
"--help" | "-h" => {
println!("Prometheus exporter for NixOS systems");
println!("Use --listen <addr:port> bind the web service.");
println!("Output will be on /metrics endpoint. HTTP 500 if something broke while scraping.");
std::process::exit(0);
}
"--listen" => {
2023-01-08 20:45:18 +01:00
app_state.listen = args.next().unwrap();
}
2023-01-03 22:13:01 +01:00
unknown => {
println!("unknown option: {}", unknown);
std::process::exit(1)
}
}
2022-12-31 00:29:17 +01:00
}
2023-01-08 20:45:18 +01:00
if !app_state.clone().is_valid() {
std::process::exit(1);
}
2023-05-09 08:40:39 +02:00
let app = Router::new();
let app = app.route("/metrics", get(metrics));
2023-01-08 20:45:18 +01:00
let app = app.with_state(app_state.clone());
let addr = SocketAddr::from_str(&app_state.listen.clone()).unwrap();
2023-01-03 22:13:01 +01:00
println!("listening on http://{}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
2022-12-31 00:29:17 +01:00
}
async fn metrics() -> Result<(StatusCode, impl IntoResponse), (StatusCode, impl IntoResponse)> {
2023-03-25 13:42:58 +01:00
let nix_store_paths = HashMap::from([
2023-03-25 16:18:36 +01:00
("current_system", NixStorePath::from_str_symlink("/run/current-system")
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?),
("current_system_kernel", NixStorePath::from_str_symlink("/run/current-system/kernel")
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?),
("booted_system", NixStorePath::from_str_symlink("/run/booted-system")
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?),
("booted_system_kernel", NixStorePath::from_str_symlink("/run/booted-system/kernel")
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?),
2023-03-25 13:42:58 +01:00
]);
let mut out = String::new();
2023-03-25 13:42:58 +01:00
for (infix, nix_store_path) in nix_store_paths.iter() {
out.push_str(nix_store_path.clone().to_prometheus_metric(infix.to_string())
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?.as_str());
}
2023-03-25 16:18:36 +01:00
out.push_str(format!("nixos_current_system_kernel_is_booted_system_kernel{{}} {}", (
nix_store_paths.get("current_system_kernel").ok_or_else(|| (StatusCode::INTERNAL_SERVER_ERROR, String::from("")))?.hash
== nix_store_paths.get("booted_system_kernel").ok_or_else(|| (StatusCode::INTERNAL_SERVER_ERROR, String::from("")))?.hash
) as i32).as_str()
);
2023-03-23 18:43:09 +01:00
return Ok((
2023-01-04 01:10:30 +01:00
StatusCode::OK,
out,
2023-03-23 18:43:09 +01:00
));
2023-01-04 01:10:30 +01:00
}