When alerts are based on metric data, like CPU or memory utilization, Prometheus is the obvious tool for pushing alerts into Alertmanager. There are other examples where the required alert is based on Boolean conditions, like "is DNS working". In such instances, is there a "best practice" method for generating these alerts?
Using the above DNS example, I could use a script:-
#!/usr/bin/env bash
function alert {
/usr/local/bin/amtool alert add \
alertname=resolveFail \
instance=$(hostname -s) \
severity=warning \
--annotation=summary='DNS resolve failure'
}
if ! /usr/bin/host $HOSTNAME 127.0.0.1 &> /dev/null
then
alert
fi
A slight alternative would be to use curl
instead of amtool
and push to the Alertmanager API. A third option would be to modify the above script to create a metric for the Node Exporter Textfile Collector:-
#!/usr/bin/env bash
TEXTFILE_COLLECTOR_DIR=/var/lib/node_exporter/textfile_collector/
if /usr/bin/host $HOSTNAME 127.0.0.1 &> /dev/null
then
STATE=1
else
STATE=0
fi
echo "node_dns_resolving $STATE" > $TEXTFILE_COLLECTOR_DIR/dns.prom.$$
mv "$TEXTFILE_COLLECTOR_DIR/dns.prom.$$" "$TEXTFILE_COLLECTOR_DIR/dns.prom"
I'd be interested to hear recommendations for these (or maybe other) methods.