add node_exporter role

This commit is contained in:
2022-08-30 07:49:00 -05:00
parent 3e982b9729
commit d5fd90a9e9
13 changed files with 398 additions and 0 deletions

View File

@ -0,0 +1,36 @@
#!/usr/bin/env perl
use strict;
use warnings;
my $cmd = "apt-get --just-print dist-upgrade";
my %metrics;
open(my $fh, '-|', $cmd) or die $!;
while(my $line = <$fh>) {
if ($line =~ /Inst \S+ \S+ \(\S+ (.+) \[(\S+)\]\)/) {
my $k = sprintf("apt_upgrades_pending{origin=\"%s\", arch=\"%s\"}", $1, $2);
if (!exists $metrics{$k}) {
$metrics{$k} = 1;
} else {
$metrics{$k}++;
}
}
}
if (%metrics) {
# print apt metrics
while(my($k, $v) = each %metrics) {
printf("%s %d\n", $k, $v)
}
}
else {
print("apt_upgrades_pending{origin=\"\",arch=\"\"} 0\n");
}
# print reboot required metric
if (-e "/var/run/reboot-required") {
print("node_reboot_required 1\n")
}
else {
print("node_reboot_required 0\n")
}

View File

@ -0,0 +1,42 @@
#!/bin/bash
function usage { printf "Usage: %s FILE\n" "$(basename "$0")" >&2; exit 1; }
while getopts "h" opt; do
case "${opt}" in
*)
usage
;;
esac
done
shift $((OPTIND-1))
FILE="$1"
if [ -z "${FILE}" ]; then
usage
exit 1
fi
if command -v sponge > /dev/null; then
( echo "# promcat (sponge)" ; cat /dev/stdin ) | sponge "${FILE}"
else
TEMP=$(mktemp --suffix .prom)
function finish {
if [ -f "${TEMP}" ]; then
rm -f "${TEMP}"
fi
}
trap finish EXIT
echo "# promcat (mktemp, mv)" > "${TEMP}"
cat /dev/stdin >> "${TEMP}"
if [ ! -s "${TEMP}" ] || grep -q '^[[:space:]]*$' "${TEMP}" ; then
printf "%s is empty\n" "${TEMP}" >&2
exit 1
else
mv "${TEMP}" "${FILE}"
fi
fi