From 546874eea8dc5d1c8efe7ceb11a2ec43dc59cc8b Mon Sep 17 00:00:00 2001 From: WhatTheMike Date: Fri, 23 Dec 2022 14:29:17 +0000 Subject: [PATCH] Added script to monitor upsc status and reboot as necessary --- playbooks/nut-server.yaml | 15 +++++++++++++++ templates/nutmon.sh.j2 | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 templates/nutmon.sh.j2 diff --git a/playbooks/nut-server.yaml b/playbooks/nut-server.yaml index 5ec1cf8..9c20d81 100644 --- a/playbooks/nut-server.yaml +++ b/playbooks/nut-server.yaml @@ -136,6 +136,21 @@ src: /root/ansible/templates/nut-telegraf.conf.j2 become: true become_method: sudo + - name: Verify monitoring template is deployed + ansible.builtin.template: + dest: /home/pi/nutmon.sh + src: /root/ansible/templates/nutmon.sh.j2 + mode: '0755' + + - name: Ensure monitoring script is scheduled + ansible.builtin.cron: + name: "Check on the UPS" + minute: "*/5" + job: "/home/pi/nutmon.sh" + user: root + state: present + become: true + become_method: sudo - name: Enable and restart services ansible.builtin.service: diff --git a/templates/nutmon.sh.j2 b/templates/nutmon.sh.j2 new file mode 100644 index 0000000..2a10670 --- /dev/null +++ b/templates/nutmon.sh.j2 @@ -0,0 +1,22 @@ +#!/bin/bash + +upsname="{{ upsname }}" +retries=5 +retry_delay=30 + +# Check dependencies +which upsc >/dev/null || exit 1 + +upsstat=1 # just to bootstrap the loop + +while [ $upsstat -ne 0 ]; do + upsc $upsname + upsstat=$? + if [ $upsstat -eq 1 ]; then + let retries=retries-1 + if [ $retries -le 0 ]; then + reboot + fi + sleep $retry_delay + fi +done