#!/bin/bash # I, Ian Kelling, follow the GNU license recommendations at # https://www.gnu.org/licenses/license-recommendations.en.html. They # recommend that small programs, < 300 lines, be licensed under the # Apache License 2.0. This file contains or is part of one or more small # programs. If a small program grows beyond 300 lines, I plan to switch # its license to GPL. # Copyright 2024 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # requires 1.4.0 to run with multiple units due to bug before that: # https://github.com/prometheus/node_exporter/pull/2475 set -eE -o pipefail trap 'echo "$0:$LINENO:error: \"$BASH_COMMAND\" returned $?" >&2' ERR if [[ ! $SERVICE_RESULT ]]; then echo "$0: error, no SERVICE_RESULT var." exit 1 fi ser_name="$1" if [[ ! $ser_name ]]; then echo "$0: error, no arg service name passed." exit 1 fi dir=/var/lib/prometheus/node-exporter if [[ ! -e $dir ]]; then exit 0 fi f=$dir/${ser_name}-result.prom ftmp=$f.$$ write_count=false if [[ -s $f ]]; then # https://www.freedesktop.org/software/systemd/man/systemd.exec.html if [[ $SERVICE_RESULT != success ]]; then write_count=true read -r _ count <$f case $count in ''|*[!0-9]*) count=0 ;; *) count=$(( count + 1 )) ;; esac fi else count=0 write_count=true fi if $write_count; then printf 'node_systemd_unit_result_fail_count{name="%s"} %s\n' "$ser_name" "$count" >$ftmp mv $ftmp $f fi