use axum::{ extract::{State, Query}, http::StatusCode, response::IntoResponse, routing::get, Router, }; use std::collections::HashMap; use std::net::SocketAddr; use std::str::FromStr; use nixos_exporter::nixos::NixStorePath; #[derive(Clone)] struct AppState { listen: String, prometheus_url: String, prometheus_query_tag_template: String, hydra_url: String, hydra_job_template: String, } impl AppState{ pub fn new() -> Self { Self { listen: String::from("[::]:9152"), prometheus_url: String::from(""), prometheus_query_tag_template: String::from("instance=\"{}\""), hydra_url: String::from(""), hydra_job_template: String::from("nixfiles/nixfiles/nixosConfigurations.{}"), } } pub fn is_valid(self) -> bool { let mut valid = true; if self.prometheus_url == String::from("") { println!("Prometheus url is not specified"); valid = false; } if self.hydra_url == String::from("") { println!("Hydra url is not specified"); valid = false; } return valid; } } #[tokio::main] async fn main() { let mut app_state = AppState::new(); let mut args = std::env::args(); let _name = args.next().unwrap(); loop { let arg = if let Some(arg) = args.next() { arg } else { break; }; match arg.as_str() { "--help" | "-h" => { println!("Prometheus exporter for NixOS systems"); println!("Use --listen bind the web service."); println!("Output will be on /metrics endpoint. HTTP 500 if something broke while scraping."); std::process::exit(0); } "--listen" => { app_state.listen = args.next().unwrap(); } "--prometheus-url" => { app_state.prometheus_url = args.next().unwrap(); } "--prometheus-query-tag-template" => { app_state.prometheus_query_tag_template = args.next().unwrap(); } "--hydra-url" => { app_state.hydra_url = args.next().unwrap(); } "--hydra-job-template" => { app_state.hydra_job_template = args.next().unwrap(); } unknown => { println!("unknown option: {}", unknown); std::process::exit(1) } } } if !app_state.clone().is_valid() { std::process::exit(1); } let app = Router::new(); let app = app.route("/metrics", get(check)); let app = app.with_state(app_state.clone()); let addr = SocketAddr::from_str(&app_state.listen.clone()).unwrap(); println!("listening on http://{}", addr); axum::Server::bind(&addr) .serve(app.into_make_service()) .await .unwrap(); } async fn check(State(app_state): State, Query(params): Query>) -> Result<(StatusCode, impl IntoResponse), (StatusCode, impl IntoResponse)> { let target = params.get("target") .ok_or_else(|| (StatusCode::NOT_FOUND, "specify target"))?; if target.contains("\"") { return Err((StatusCode::INTERNAL_SERVER_ERROR, "Invalid target name")); } let client = reqwest::Client::new(); let prometheus_req = client.get(format!("{}/api/v1/query?query=nixos_current_system_hash{{{}}}", app_state.prometheus_url, app_state.prometheus_query_tag_template.clone().replace("{}", target))) .header("Accept", "application/json") .send().await .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Promehteus can't get reached"))?; if prometheus_req.status() != reqwest::StatusCode::OK { return Err((StatusCode::NOT_FOUND, "Target does not exist in Hydra")); } let prometheus_body = prometheus_req.json::().await .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Prometheus"))?; let current_system_hash = prometheus_body["data"]["result"][0]["metric"]["hash"].as_str() .ok_or_else(|| (StatusCode::INTERNAL_SERVER_ERROR, "No current metric found in Prometheus"))?; let hydra_req = client.get(format!("{}/job/{}/latest", app_state.hydra_url, app_state.hydra_job_template.clone().replace("{}", target))) .header("Accept", "application/json") .send().await .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Hydra can't get reached"))?; if hydra_req.status() != reqwest::StatusCode::OK { return Err((StatusCode::NOT_FOUND, "Target does not exist in Hydra")); } let hydra_body = hydra_req.json::().await .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Hydra"))?; let nix_store_path = hydra_body["buildoutputs"]["out"]["path"].as_str() .ok_or_else(|| (StatusCode::INTERNAL_SERVER_ERROR, "No buildoutput found in Hydra"))?; let hydra_system_hash = NixStorePath::from_path_buf(std::path::PathBuf::from(nix_store_path)) .map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid store path returned by Hydra"))? .hash; let mut status = "0"; if current_system_hash == hydra_system_hash { status = "1"; } return Ok(( StatusCode::OK, format!("nixos_current_system_is_sync{{}} {}\n", status) )); }