From 0be2ef61f6f5c4eefbd6f73b92641e3b1a0665fa Mon Sep 17 00:00:00 2001 From: spleenftw <57947900+Spleenftw@users.noreply.github.com> Date: Fri, 23 Feb 2024 00:28:24 +0100 Subject: [PATCH] Create check_pivpn.sh Scripts to auto-update the openvpn_exporter "service_openvpn" metrics --- check_pivpn.sh | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 check_pivpn.sh diff --git a/check_pivpn.sh b/check_pivpn.sh new file mode 100644 index 0000000..5c8b7c2 --- /dev/null +++ b/check_pivpn.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# /usr/local/bin/check_pivpn.sh + +# Set the initial status +status="" + +# Function to restart OpenVPN service +restart_openvpn() { + systemctl restart openvpn_exporter +} + +# Check if OpenVPN service is active +if systemctl is-active --quiet openvpn; then + status="ACTIVE" +else + status="NON-ACTIVE" +fi + +# Display the initial status +echo "Service is $status" + +# Continuously monitor the status and restart if it changes +while true; do + # Check if OpenVPN service is active + if systemctl is-active --quiet openvpn; then + new_status="ACTIVE" + else + new_status="NON-ACTIVE" + fi + + # Check if the status has changed + if [ "$new_status" != "$status" ]; then + status="$new_status" + echo "Service is now $status" + + # Restart OpenVPN service + restart_openvpn + fi + + # Sleep for a while before checking again + sleep 5 +done