Skip to content

Commit

Permalink
Merge pull request #70 from say-paul/check-service
Browse files Browse the repository at this point in the history
Monitor services
  • Loading branch information
LorbusChris authored Jul 15, 2022
2 parents f4b0529 + 33a44ab commit f946b66
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
5 changes: 4 additions & 1 deletion etc/greenboot/greenboot.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ GREENBOOT_WATCHDOG_CHECK_ENABLED=true
### This variable is the number of hours after an upgrade that we consider
### the new deployment as culprit of reboot.
### It has to be a positive integer. Defaults to 24 (hours).
# GREENBOOT_WATCHDOG_GRACE_PERIOD=24
# GREENBOOT_WATCHDOG_GRACE_PERIOD=24

## Service monitor
# GREENBOOT_MONITOR_SERVICES="sshd NetworkManager"
5 changes: 5 additions & 0 deletions greenboot.spec
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ install -DpZm 0755 usr/lib/greenboot/check/wanted.d/* %{buildroot}%{_prefix}/lib
%systemd_post greenboot-grub2-set-success.service
%systemd_post greenboot-rpm-ostree-grub2-check-fallback.service
%systemd_post redboot-auto-reboot.service
%systemd_post greenboot-service-monitor.service

%post default-health-checks
%systemd_post greenboot-loading-message.service
Expand All @@ -102,6 +103,7 @@ install -DpZm 0755 usr/lib/greenboot/check/wanted.d/* %{buildroot}%{_prefix}/lib
%systemd_preun greenboot-grub2-set-counter.service
%systemd_preun greenboot-grub2-set-success.service
%systemd_preun greenboot-rpm-ostree-grub2-check-fallback.service
%systemd_preun greenboot-service-monitor.service

%preun default-health-checks
%systemd_preun greenboot-loading-message.service
Expand All @@ -116,6 +118,7 @@ install -DpZm 0755 usr/lib/greenboot/check/wanted.d/* %{buildroot}%{_prefix}/lib
%systemd_postun greenboot-grub2-set-counter.service
%systemd_postun greenboot-grub2-set-success.service
%systemd_postun greenboot-rpm-ostree-grub2-check-fallback.service
%systemd_postun greenboot-service-monitor.service

%postun default-health-checks
%systemd_postun greenboot-loading-message.service
Expand Down Expand Up @@ -156,6 +159,8 @@ install -DpZm 0755 usr/lib/greenboot/check/wanted.d/* %{buildroot}%{_prefix}/lib
%{_unitdir}/greenboot-rpm-ostree-grub2-check-fallback.service
%{_libexecdir}/%{name}/redboot-auto-reboot
%{_unitdir}/redboot-auto-reboot.service
%{_libexecdir}/%{name}/greenboot-service-monitor
%{_unitdir}/greenboot-service-monitor.service

%files default-health-checks
%{_prefix}/lib/%{name}/check/required.d/01_repository_dns_check.sh
Expand Down
21 changes: 21 additions & 0 deletions usr/lib/systemd/system/greenboot-service-monitor.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# SPDX-License-Identifier: LGPL-2.1+
#
# This file is part of greenboot.
#
# greenboot is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.

[Unit]
Description=Monitor user services
Before=boot-complete.target
OnFailure=redboot.target
After=default.target graphical.target multi-user.target

[Service]
Type=oneshot
ExecStart=/usr/libexec/greenboot/greenboot-service-monitor

[Install]
RequiredBy=boot-complete.target
28 changes: 28 additions & 0 deletions usr/libexec/greenboot/greenboot-service-monitor
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
set -eo pipefail
GREENBOOT_CONFIGURATION_FILE=/etc/greenboot/greenboot.conf
service_started=true
if test -f "$GREENBOOT_CONFIGURATION_FILE"; then
source $GREENBOOT_CONFIGURATION_FILE
fi

if [ -n "$GREENBOOT_MONITOR_SERVICES" ]; then
services=($GREENBOOT_MONITOR_SERVICES)
else
echo "GREENBOOT_MONITOR_SERVICE not found, using default value"
services=( sshd NetworkManager )
fi

# check all the services listed
for service in "${services[@]}"; do
rc="$(systemctl show -p ActiveState --value $service)"
if [[ $rc != "active" ]]; then
echo "<0>${service} is ${rc}"
service_started=false
fi
done

# fail if any of the monitored services failed. This will make boot-complete.target fail.
if ! $service_started ; then
exit 1
fi

0 comments on commit f946b66

Please sign in to comment.