Compare commits
7 Commits
5aed8cf0ec
...
delte
| Author | SHA1 | Date | |
|---|---|---|---|
| 6c006f6384 | |||
| 7b674a8262 | |||
| 19120d4d0a | |||
| b89910f649 | |||
| c0cb8db954 | |||
| 3c3936e677 | |||
| de00a92b88 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -450,7 +450,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "nixos-exporter"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"reqwest",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "nixos-exporter"
|
||||
version = "0.1.0"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
|
||||
240
src/main.rs
240
src/main.rs
@@ -1,7 +1,7 @@
|
||||
use axum::{
|
||||
extract::Query,
|
||||
extract::{State, Query},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
response::IntoResponse,
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
@@ -10,28 +10,71 @@ use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(Clone, PartialEq)]
|
||||
enum OperationMode {
|
||||
None,
|
||||
Exporter,
|
||||
Validator,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
listen: String,
|
||||
operationmode: OperationMode,
|
||||
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"),
|
||||
operationmode: OperationMode::None,
|
||||
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.operationmode == OperationMode::None {
|
||||
println!("operationmode is not set");
|
||||
valid = false;
|
||||
}
|
||||
if self.prometheus_url == String::from("") && self.operationmode == OperationMode::Validator {
|
||||
println!("Prometheus url is not specified");
|
||||
valid = false;
|
||||
}
|
||||
if self.hydra_url == String::from("") && self.operationmode == OperationMode::Validator {
|
||||
println!("Hydra url is not specified");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_nix_store_path(path: std::path::PathBuf) -> Result<(String, String), String> {
|
||||
let (hash, name) = path.file_name()
|
||||
.ok_or_else(String::default)?
|
||||
let (hash, name) = path.iter().nth(3)
|
||||
.ok_or_else(|| String::from("Can't read store path name"))?
|
||||
.to_str()
|
||||
.ok_or_else(String::default)?
|
||||
.ok_or_else(|| String::from("Failed converting store path name to string"))?
|
||||
.split_once("-")
|
||||
.ok_or_else(String::default)?;
|
||||
.ok_or_else(|| String::from("Failed splitting store path name for hash and name"))?;
|
||||
return Ok((hash.to_string(), name.to_string()));
|
||||
}
|
||||
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let mut listen = String::from("[::]:9152");
|
||||
let mut operationmode = OperationMode::Exporter;
|
||||
let mut app_state = AppState::new();
|
||||
|
||||
let mut args = std::env::args();
|
||||
args.next();
|
||||
let name = args.next().unwrap();
|
||||
loop {
|
||||
let arg = if let Some(arg) = args.next() {
|
||||
arg
|
||||
@@ -47,13 +90,25 @@ async fn main() {
|
||||
std::process::exit(0);
|
||||
}
|
||||
"--listen" => {
|
||||
listen = args.next().unwrap();
|
||||
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();
|
||||
}
|
||||
"exporter" => {
|
||||
operationmode = OperationMode::Exporter;
|
||||
app_state.operationmode = OperationMode::Exporter;
|
||||
}
|
||||
"validator" => {
|
||||
operationmode = OperationMode::Validator;
|
||||
app_state.operationmode = OperationMode::Validator;
|
||||
}
|
||||
unknown => {
|
||||
println!("unknown option: {}", unknown);
|
||||
@@ -62,16 +117,25 @@ async fn main() {
|
||||
}
|
||||
}
|
||||
|
||||
let mut app = Router::new();
|
||||
if operationmode == OperationMode::Exporter {
|
||||
println!("Running NixOS Exporter in Exporter mode");
|
||||
app = app.route("/metrics", get(metrics));
|
||||
} else if operationmode == OperationMode::Validator {
|
||||
println!("Running NixOS Exporter in Validator mode");
|
||||
app = app.route("/metrics", get(check));
|
||||
if !app_state.clone().is_valid() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let addr = SocketAddr::from_str(&listen).unwrap();
|
||||
let mut app = Router::new();
|
||||
if app_state.operationmode == OperationMode::Exporter {
|
||||
println!("Running NixOS Exporter in Exporter mode");
|
||||
app = app.route("/metrics", get(metrics));
|
||||
} else if app_state.operationmode == OperationMode::Validator {
|
||||
println!("Running NixOS Exporter in Validator mode");
|
||||
app = app.route("/metrics", get(check));
|
||||
} else {
|
||||
println!("Run mode not specified, do {} --help", name);
|
||||
std::process::exit(1);
|
||||
};
|
||||
|
||||
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())
|
||||
@@ -79,110 +143,78 @@ async fn main() {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn get_current_system() -> Result<(String, String), String> {
|
||||
let symlink = match std::fs::read_link("/run/current-system") {
|
||||
Ok(symlink) => symlink,
|
||||
Err(err) => return Err(err.to_string()),
|
||||
};
|
||||
fn parse_symlink(path: String) -> Result<(String, String), String> {
|
||||
let symlink = std::fs::read_link(path).map_err(|err| err.to_string())?;
|
||||
|
||||
let (hash, name) = parse_nix_store_path(symlink)?;
|
||||
|
||||
Ok((String::from(hash), String::from(name)))
|
||||
}
|
||||
|
||||
async fn metrics() -> Response {
|
||||
let current_system = get_current_system();
|
||||
let (hash, name) = match current_system {
|
||||
Ok((hash, name)) => (hash, name),
|
||||
Err(err) => {
|
||||
println!("failed: {}", err);
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, "").into_response();
|
||||
}
|
||||
};
|
||||
fn gen_prometheus_metric(path: String, infix: String) -> Result<String, String> {
|
||||
let (hash, name) = parse_symlink(path).map_err(|err| err)?;
|
||||
|
||||
(
|
||||
StatusCode::OK,
|
||||
format!("nixos_current_system_hash{{hash=\"{}\"}} 1\nnixos_current_system_name{{name=\"{}\"}} 1\n", hash, name)
|
||||
).into_response()
|
||||
return Ok(format!("nixos_{}_hash{{hash=\"{}\"}} 1\nnixos_{}_name{{name=\"{}\"}} 1\n", infix, hash, infix, name));
|
||||
}
|
||||
|
||||
async fn check(Query(params): Query<HashMap<String, String>>) -> Response {
|
||||
let target = match params.get("target") {
|
||||
Some(target) => target,
|
||||
None => {
|
||||
return (StatusCode::NOT_FOUND, "specify target").into_response();
|
||||
},
|
||||
};
|
||||
async fn metrics() -> Result<(StatusCode, impl IntoResponse), (StatusCode, impl IntoResponse)> {
|
||||
let mut out = String::new();
|
||||
out.push_str(&gen_prometheus_metric(String::from("/run/current-system"), String::from("current_system"))
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?);
|
||||
out.push_str(&gen_prometheus_metric(String::from("/run/current-system/kernel"), String::from("current_system_kernel"))
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?);
|
||||
out.push_str(&gen_prometheus_metric(String::from("/run/booted-system"), String::from("booted_system"))
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?);
|
||||
out.push_str(&gen_prometheus_metric(String::from("/run/booted-system/kernel"), String::from("booted_system_kernel"))
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err))?);
|
||||
return Ok((
|
||||
StatusCode::OK,
|
||||
out,
|
||||
));
|
||||
}
|
||||
|
||||
async fn check(State(app_state): State<AppState>, Query(params): Query<HashMap<String, String>>) -> 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 = match client.get(format!("https://prometheus.monitoring.clerie.de/api/v1/query?query=nixos_nixos_current_system_hash{{job=%22nixos-exporter%22,instance=%22{}.mon.clerie.de:9152%22}}", target))
|
||||
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 {
|
||||
Ok(req) => req,
|
||||
Err(_) => {
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, "Promehteus can't get reached").into_response();
|
||||
},
|
||||
};
|
||||
.send().await
|
||||
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Promehteus can't get reached"))?;
|
||||
|
||||
|
||||
match prometheus_req.status() {
|
||||
reqwest::StatusCode::OK => (),
|
||||
_ => {
|
||||
return (StatusCode::NOT_FOUND, "Target does not exist in Hydra").into_response();
|
||||
},
|
||||
if prometheus_req.status() != reqwest::StatusCode::OK {
|
||||
return Err((StatusCode::NOT_FOUND, "Target does not exist in Hydra"));
|
||||
}
|
||||
|
||||
let prometheus_body = match prometheus_req.json::<serde_json::Value>().await {
|
||||
Ok(body) => body,
|
||||
Err(_) => {
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Hydra").into_response();
|
||||
},
|
||||
};
|
||||
let prometheus_body = prometheus_req.json::<serde_json::Value>().await
|
||||
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Hydra"))?;
|
||||
|
||||
let current_system_hash = match prometheus_body["data"]["result"][0]["metric"]["hash"].as_str() {
|
||||
Some(nix_store_path) => nix_store_path,
|
||||
_ => {
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, "No buildoutput found in Hydra").into_response();
|
||||
},
|
||||
};
|
||||
let current_system_hash = prometheus_body["data"]["result"][0]["metric"]["hash"].as_str()
|
||||
.ok_or_else(|| (StatusCode::INTERNAL_SERVER_ERROR, "No buildoutput found in Hydra"))?;
|
||||
|
||||
let hydra_req = match client.get(format!("https://hydra.clerie.de/job/nixfiles/nixfiles/nixosConfigurations.{}/latest", target))
|
||||
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 {
|
||||
Ok(req) => req,
|
||||
Err(_) => {
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, "Hydra can't get reached").into_response();
|
||||
},
|
||||
};
|
||||
.send().await
|
||||
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Hydra can't get reached"))?;
|
||||
|
||||
match hydra_req.status() {
|
||||
reqwest::StatusCode::OK => (),
|
||||
_ => {
|
||||
return (StatusCode::NOT_FOUND, "Target does not exist in Hydra").into_response();
|
||||
},
|
||||
if hydra_req.status() != reqwest::StatusCode::OK {
|
||||
return Err((StatusCode::NOT_FOUND, "Target does not exist in Hydra"));
|
||||
}
|
||||
|
||||
let hydra_body = match hydra_req.json::<serde_json::Value>().await {
|
||||
Ok(body) => body,
|
||||
Err(_) => {
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Hydra").into_response();
|
||||
},
|
||||
};
|
||||
let hydra_body = hydra_req.json::<serde_json::Value>().await
|
||||
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid response from Hydra"))?;
|
||||
|
||||
let nix_store_path = match hydra_body["buildoutputs"]["out"]["path"].as_str() {
|
||||
Some(nix_store_path) => nix_store_path,
|
||||
_ => {
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, "No buildoutput found in Hydra").into_response();
|
||||
},
|
||||
};
|
||||
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, _) = match parse_nix_store_path(std::path::PathBuf::from(nix_store_path)) {
|
||||
Ok((hash, name)) => (hash, name),
|
||||
Err(_) => {
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, "Invalid store path returned by Hydra").into_response();
|
||||
},
|
||||
};
|
||||
let (hydra_system_hash, _) = parse_nix_store_path(std::path::PathBuf::from(nix_store_path))
|
||||
.map_err(|_err| (StatusCode::INTERNAL_SERVER_ERROR, "Invalid store path returned by Hydra"))?;
|
||||
|
||||
let mut status = "0";
|
||||
|
||||
@@ -190,8 +222,8 @@ async fn check(Query(params): Query<HashMap<String, String>>) -> Response {
|
||||
status = "1";
|
||||
}
|
||||
|
||||
return (
|
||||
return Ok((
|
||||
StatusCode::OK,
|
||||
format!("nixos_current_system_valid{{target=\"{}.net.clerie.de:9152\"}} {}\n", target, status)
|
||||
).into_response();
|
||||
format!("nixos_current_system_is_sync{{}} {}\n", status)
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user