MogiePete / zabbix-systemd-service-monitoring

Simple Zabbix Template to discover, monitor, and alert on systemd services.
GNU General Public License v2.0
91 stars 41 forks source link

Service Age not status #31

Open otheus opened 3 years ago

otheus commented 3 years ago

It would be in most circumstances better to get the actual timestamp of the last time the service was restarted. Unfortunately, Zabbix' custom params does not know how to handle exception data other than making the service unavilable, so 0 probably makes sense. HEre is my implementation, based on your check-status script:

service="$1"
if [[ -z "$service" ]]; then
        # Assume they mean the system itself
        date +%s -d "$(uptime --since)"
else
        hrt=$(systemctl show "$service" --property=ActiveEnterTimestamp | awk -F= '{print $2}')
        if [[ -z $hrt ]] || [[ "$hrt" = 0 ]]; then
                echo 0
        else
                date +%s -d "$hrt"
        fi
fi
otheus commented 3 years ago

Also, I refactored the other script into a nicer more efficient pipeline, but it shaves only 1% of execution time at best. I then did it in perl for a 1.01% speed-up! Here's the SH pipeline version.

extract_enabled_service_names() {
        awk -F'[. \t]+' '$2 == "service" && $3 ~ "^(generated|enabled)" {print $1}'
}

include_whitelist_only() {
        if [[ -r /etc/zabbix/service_discovery_whitelist ]] ; then
            grep -E -f /etc/zabbix/service_discovery_whitelist
        else cat; fi
}
exclude_blacklist() {
        if [[ -r /etc/zabbix/service_discovery_blacklist ]] ; then
            grep -Ev -f /etc/zabbix/service_discovery_blacklist
        else cat; fi
}
convert_to_json() {
        awk 'BEGIN { print "{\"data\":["; } NR>1 { printf(",");} { printf("{\"{#SERVICE}\": \"%s\"}",$0) } END { print "]}" } '
}

systemctl list-unit-files |
        extract_enabled_service_names |
        include_whitelist_only |
        exclude_blacklist |
        convert_to_json