From 7f49a42ddb0ba78fbae2a72c901c4f1a7cc08362 Mon Sep 17 00:00:00 2001 From: Pat Riehecky Date: Tue, 7 Oct 2025 15:07:25 -0500 Subject: [PATCH] Add optional timestamp to metric output Fixes #18 --- cvmfs-client-prometheus.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cvmfs-client-prometheus.sh b/cvmfs-client-prometheus.sh index 5af1403..97a1d7a 100755 --- a/cvmfs-client-prometheus.sh +++ b/cvmfs-client-prometheus.sh @@ -2,6 +2,7 @@ HTTP_HEADER='FALSE' USE_NON_STANDARD_MOUNTPOINTS='FALSE' +EPOCHTIME='' TMPFILE=$(mktemp) @@ -46,6 +47,7 @@ usage() { echo '' >&2 echo ' --http: add the HTTP protocol header to the output' >&2 echo ' --non-standard-mountpoints: use cvmfs_config status instead of findmnt to discover repositories' >&2 + echo ' --timestamp: add a timestamp to each metric' >&2 echo '' >&2 echo 'NOTE: The user running this script must have read access' >&2 echo ' to the CVMFS cache files!' >&2 @@ -608,7 +610,7 @@ for cmd in attr bc cvmfs_config cvmfs_talk grep; do done ############################################################# -args=$(getopt --options 'h' --longoptions 'help,http,non-standard-mountpoints' -- "$@") +args=$(getopt --options 'h' --longoptions 'help,http,non-standard-mountpoints,timestamp' -- "$@") eval set -- "$args" for arg in $@; do @@ -628,6 +630,11 @@ for arg in $@; do USE_NON_STANDARD_MOUNTPOINTS='TRUE' shift ;; + --timestamp) + # Add metric timestamp with a leading space to simplify adding + EPOCHTIME=" $(date +%s)" + shift + ;; -h | --help) # get help shift @@ -665,6 +672,12 @@ if check_cvmfs_version_exact; then postprocess_metrics_for_2132 fi +# Add timestamp to all metrics if requested +if [[ -n "${EPOCHTIME}" ]]; then + sed "/^#/! s/\$/${EPOCHTIME}/" "${TMPFILE}" > "${TMPFILE}.time" + mv "${TMPFILE}.time" "${TMPFILE}" +fi + if [[ "${HTTP_HEADER}" == 'TRUE' ]]; then content_length=$(stat --printf="%s" "${TMPFILE}") echo -ne "HTTP/1.1 200 OK\r\n"