From a0920cdbc36162d3ae7ba1eebd1549aa00a66598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Thu, 9 Jan 2025 13:13:27 -0500 Subject: [PATCH] add helper script to extract timestamps from files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a pretty simple tool, but we've found this useful to check some diagnostics. We've also found alternatives like: https://github.com/Puppet-Finland/prometheus-file-age-metrics/blob/main/create-file-age-metrics.sh ... which gives you a metric like "out of date files", which didn't like, because the threshold lives in the script instead of the alerting layer (which we prefer). The closest thing to this is the filestat exporter: https://github.com/michael-doubez/filestat_exporter ... which provides way more metrics and seems overengineered for our purpose. Signed-off-by: Antoine Beaupré --- path-timestamp.sh | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 path-timestamp.sh diff --git a/path-timestamp.sh b/path-timestamp.sh new file mode 100644 index 0000000..b16caea --- /dev/null +++ b/path-timestamp.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +# Expose timestamp of given path +# +# This will provide the last modification timestamp (technically, +# stat(1)'s %Y parameter), which is a number of seconds since the +# epoch, once per provided path. It will claim the timestamp is zero +# if stat(1) fails to extract the timestamp for any reason. +# +# Usage: add this to crontab: +# +# */5 * * * * prometheus path-timestamp.sh /var/lib/prometheus | sponge /var/lib/node_exporter/path-timestamp.prom +# +# Author: Antoine Beaupré + +echo "# HELP node_path_modification_timestamp_seconds Last change timestamp" +echo "# TYPE node_path_modification_timestamp_seconds gauge" +for path in "$@"; do + printf 'node_path_modification_timestamp_seconds{path="%s"} ' "$path" + stat -c %Y "$path" 2>/dev/null || echo 0 +done