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

Fix trigger in Zabbix 4.0 LTS, add support to systemd 219 (CentOS 7) #37

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ Installation
------------

* Server
* Import template `Template_App_systemd_Services_v4.xml` file if you have Zabbix 4
* Import template `Template_App_systemd_Services_v5.xml` file if you have Zabbix 5
* Import template `Template_systemd_Services_v4.xml` file if you have Zabbix 4
* Import template `Template_systemd_Services_v5.xml` file if you have Zabbix 5
* Link template to host
* Agent
* Place the following files inside `/etc/zabbix/`:
* `service_discovery_blacklist` or `service_discovery_whitelist`
* Place the following file inside `/usr/local/bin/`:
* `zbx_service_restart_check.sh`
* `zbx_service_discovery.sh`
* If you have an old systemd version (before 230), adopt the file `_old` version instead, removing its `_old` suffix
* Set executable permissions on both scripts:
* `chmod +x /usr/local/bin/zbx*.sh`
* If running SELinux run restorecon on the two scripts in `/usr/local/bin` e.g.:
Expand Down Expand Up @@ -82,6 +83,13 @@ zabbix_agentd -t "systemd.service.status[sshd]"
zabbix_agentd -t "systemd.service.restart[sshd]"
```

The `status[SERVICENAME]` will return:

* `[t|1]` if that service is running
* `[t|0]` if that service is NOT running

The `restart[SERVICENAME]` will return the most recent restart date.

License
-------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
<tags/>
</trigger_prototype>
<trigger_prototype>
<expression>{SystemD service monitoring template:systemd.service.status[{#SERVICE}].last()}&lt;&gt;0</expression>
<expression>{SystemD service monitoring template:systemd.service.status[{#SERVICE}].last()}=0</expression>
Copy link
Contributor Author

@valerio-bozzolan valerio-bozzolan Feb 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now Zabbix 4 checks the last()=0. Note that Zabbix 5 also checks that:

I don't know why v4 was last()<>0. Probably the related v4 script had different return values than the current version. By the way now it's correct (1 = running, 0 = stopped).

<recovery_mode>0</recovery_mode>
<recovery_expression/>
<name>{#SERVICE} service not running</name>
Expand Down
File renamed without changes.
Empty file modified usr/local/bin/zbx_service_discovery.sh
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions usr/local/bin/zbx_service_restart_check.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ uptime=$(date +%s -d "$(systemctl show --value -p ActiveEnterTimestamp sysinit.t
if [[ $(( now - uptime)) -lt 180 ]]; then
echo 0
else
# note that the next line requires systemd version 230 because of "--value"
service_start=$(systemctl show --value -p ActiveEnterTimestamp "$service")
service_start_as_epoch=$(date -d "$service_start" +%s)

Expand Down
31 changes: 31 additions & 0 deletions usr/local/bin/zbx_service_restart_check_old.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
#
# Version supporting old systemd versions (before 230)
# For example it supports systemd 219
#

service="$1"
now=$(date +%s)

uptime_value_raw=$(systemctl show -p ActiveEnterTimestamp sysinit.target)

# parse the value that is something like "ActiveEnterTimestamp=SOMETHING"
# https://unix.stackexchange.com/q/728424/85666
uptime_value=$(echo "$uptime_value_raw" | cut -d"=" -f2)

# Don't alert if the server has just been restarted
uptime=$(date +%s -d "$uptime_value")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just extracted the variable

if [[ $(( now - uptime)) -lt 180 ]]; then
echo 0
else
# avoid to use the "--value" since it was introduced only in systemd version 230
service_start_raw=$(systemctl show -p ActiveEnterTimestamp "$service")

# parse the value that is something like "ActiveEnterTimestamp=SOMETHING"
# https://unix.stackexchange.com/q/728424/85666
service_start=$(echo "$service_start_raw" | cut -d"=" -f2)

service_start_as_epoch=$(date -d "$service_start" +%s)

[[ $(( now - service_start_as_epoch )) -lt 180 ]] && echo 1 || echo 0
fi