Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service Age not status #31

Open
otheus opened this issue Mar 26, 2021 · 1 comment
Open

Service Age not status #31

otheus opened this issue Mar 26, 2021 · 1 comment

Comments

@otheus
Copy link

otheus commented Mar 26, 2021

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
Copy link
Author

otheus commented Mar 26, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant