implemented check for anonymous volumes

This commit is contained in:
2023-11-17 16:53:56 +01:00
parent 66280fdbde
commit a519a09725
24 changed files with 177 additions and 42 deletions

View File

@@ -0,0 +1,21 @@
# Health Check for Docker Containers
## Description
This Ansible role is designed to ensure the health of Docker containers running on a system. It includes a script that checks for unhealthy or exited Docker containers and sets up a systemd service and timer to regularly execute this check.
## Files
- `vars/main.yml`: Variable definitions for the script's directory.
- `handlers/main.yml`: Handlers to reload and restart the systemd service and timer.
- `files/health-docker-container.sh`: The script that checks the container health.
- `tasks/main.yml`: Tasks to create necessary directories, copy scripts, and create systemd service and timer.
- `templates/health-docker-container.service.j2`: Systemd service template.
- `templates/health-docker-container.timer.j2`: Systemd timer template.
- `meta/main.yml`: Meta information declaring dependencies for the role.
## Usage
To use this role, include it in your playbook and set the `path_administrator_scripts` variable to the desired path for the health check scripts.
Ensure that the `systemd_notifier` dependency is satisfied for error notifications.

View File

@@ -0,0 +1,20 @@
#!/bin/sh
docker_ps_grep_unhealthy="$(docker ps --filter health=unhealthy --format '{{.Names}}')"
docker_ps_grep_exited="$(docker ps --filter status=exited --format '{{.Names}}')"
exitcode=0
if [ ! -z "$docker_ps_grep_unhealthy" ]
then
echo "Some docker containers are unhealthy: $docker_ps_grep_unhealthy"
exitcode=1
fi
if [ ! -z "$docker_ps_grep_exited" ]
then
echo "Some docker containers exited: $docker_ps_grep_exited"
exitcode=2
fi
if [ "$exitcode" -ne "0" ]
then
exit $exitcode
fi
echo "All docker containers are healthy."
exit

View File

@@ -0,0 +1,11 @@
- name: "reload health-docker-container.service"
systemd:
name: health-docker-container.service
enabled: yes
daemon_reload: yes
- name: "restart health-docker-container.timer"
systemd:
name: health-docker-container.timer
state: restarted
enabled: yes
daemon_reload: yes

View File

@@ -0,0 +1,2 @@
dependencies:
- systemd_notifier

View File

@@ -0,0 +1,22 @@
- name: "create {{health_docker_container_folder}}"
file:
path: "{{health_docker_container_folder}}"
state: directory
mode: 0755
- name: create health-docker-container.sh
copy:
src: health-docker-container.sh
dest: "{{health_docker_container_folder}}health-docker-container.sh"
- name: create health-docker-container.service
template: src=health-docker-container.service.j2 dest=/etc/systemd/system/health-docker-container.service
notify: reload health-docker-container.service
- name: create health-docker-container.timer
template:
src: health-docker-container.timer.j2
dest: "/etc/systemd/system/health-docker-container.timer"
register: health_docker_container_timer
changed_when: health_docker_container_timer.changed or activate_all_timers | default(false) | bool
notify: restart health-docker-container.timer

View File

@@ -0,0 +1,7 @@
[Unit]
Description=Checking docker health
OnFailure=systemd-notifier@%n.service
[Service]
Type=oneshot
ExecStart=/bin/bash {{health_docker_container_folder}}health-docker-container.sh

View File

@@ -0,0 +1,10 @@
[Unit]
Description=starts health-docker-container.service
[Timer]
OnCalendar={{on_calendar_health_docker_container}}
RandomizedDelaySec={{randomized_delay_sec}}
Persistent=false
[Install]
WantedBy=timers.target

View File

@@ -0,0 +1 @@
health_docker_container_folder: "{{path_administrator_scripts}}health-docker-container/"