add node_exporter role
This commit is contained in:
36
roles/node_exporter/files/apt-exporter.pl
Normal file
36
roles/node_exporter/files/apt-exporter.pl
Normal 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")
|
||||
}
|
42
roles/node_exporter/files/promcat.sh
Normal file
42
roles/node_exporter/files/promcat.sh
Normal 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
|
Reference in New Issue
Block a user