From 3edb48e45346aeeef7b9d73aa1cec4e9076957e4 Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Tue, 22 Aug 2023 09:16:23 +0200 Subject: [PATCH] [Zabbix] Add simple Zabbix scripts --- Linux/zabbix/check_service.sh | 27 ++++++++++++++++++++++ Linux/zabbix/cleanup_disk.sh | 43 +++++++++++++++++++++++++++++++++++ Linux/zabbix/cleanup_swap.sh | 10 ++++++++ 3 files changed, 80 insertions(+) create mode 100755 Linux/zabbix/check_service.sh create mode 100644 Linux/zabbix/cleanup_disk.sh create mode 100644 Linux/zabbix/cleanup_swap.sh diff --git a/Linux/zabbix/check_service.sh b/Linux/zabbix/check_service.sh new file mode 100755 index 0000000..05545a6 --- /dev/null +++ b/Linux/zabbix/check_service.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Find service name from argument +monitorService=$1 + +# Check if $monitorService is empty +if [ -z "$monitorService" ]; then + read -p "Please pass me the service name: " monitorService +fi + + +# Define the check_service function +function check_service { + + # Check if the service is active and report so + if systemctl is-active --quiet $monitorService; then + echo "$monitorService is running" + + # If the service is not running, exit with an error. + else + echo "$monitorService is not running" + exit 1 + fi +} + +# Excecute the check_service function +check_service diff --git a/Linux/zabbix/cleanup_disk.sh b/Linux/zabbix/cleanup_disk.sh new file mode 100644 index 0000000..fa23e86 --- /dev/null +++ b/Linux/zabbix/cleanup_disk.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# Prune container images +if [ -x "$(command -v docker)" ]; then + echo "Pruning using Docker" + sudo docker image prune -af + +elif [ -x "$(command -v podman)" ]; then + echo "Pruning using Podman" + sudo podman image prune -af + +else + echo "No container images to prune" +fi + + +# Remove all disabled snaps +if [ -x "$(command -v snap)" ]; then + echo "Remove all deactivated snaps" + sudo snap list --all | \ + awk '/disabled/{print $1, $3}' | \ + while read snapname revision; do \ + sudo snap remove "$snapname" --revision="$revision"; + done +else + echo "No snaps found" +fi + + +# Flush and rotate all logs +if [ -x "$(command -v journalctl)" ]; then + echo "Vacuuming logs using journalctl" + sudo journalctl \ + --flush \ + --rotate \ + --vacuum-time=1s +else + echo "JournalCTL not found" +fi + +# remove zipped logs +echo "Delete zipped logs" +sudo rm -rf /var/log/*.gz diff --git a/Linux/zabbix/cleanup_swap.sh b/Linux/zabbix/cleanup_swap.sh new file mode 100644 index 0000000..3dd30e5 --- /dev/null +++ b/Linux/zabbix/cleanup_swap.sh @@ -0,0 +1,10 @@ +# Clear swapusage +free_memory=`free -m | awk 'NR==2{print $7}'` +used_swap=`free -m | awk 'NR==3{print $3}'` + +if [ $free_memory -gt $used_swap ]; then + sudo swapoff -a && sudo swapon -a + echo "Swap usage cleared!" +else + echo "Cannot clear swapusage, not enough memory!" +fi2