check_systemd_service 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/bash
  2. # Copyright (C) 2016 Mohamed El Morabity <melmorabity@fedoraproject.com>
  3. #
  4. # This module is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU General Public License as published by the Free Software
  6. # Foundation, either version 3 of the License, or (at your option) any later
  7. # version.
  8. #
  9. # This software is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License along with
  14. # this program. If not, see <http://www.gnu.org/licenses/>.
  15. PLUGINDIR=$(dirname $0)
  16. . $PLUGINDIR/utils.sh
  17. if [ $# -ne 1 ]; then
  18. echo "Usage: ${0##*/} <service name>" >&2
  19. exit $STATE_UNKNOWN
  20. fi
  21. service=$1
  22. status=$(systemctl is-enabled $service 2>/dev/null)
  23. r=$?
  24. if [ -z "$status" ]; then
  25. echo "ERROR: service $service doesn't exist"
  26. exit $STATE_CRITICAL
  27. fi
  28. if [ $r -ne 0 ]; then
  29. echo "ERROR: service $service is $status"
  30. exit $STATE_CRITICAL
  31. fi
  32. systemctl --quiet is-active $service
  33. if [ $? -ne 0 ]; then
  34. echo "ERROR: service $service is not running"
  35. exit $STATE_CRITICAL
  36. fi
  37. echo "OK: service $service is running"
  38. exit $STATE_OK