From eefdd975704181800e295b808f75c8be3fcefcfe Mon Sep 17 00:00:00 2001 From: Ali Date: Mon, 5 Jan 2026 08:57:28 +0800 Subject: [PATCH] Updated pm-task. --- pm-task | 3 ++ storage-monitor.sh | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 storage-monitor.sh diff --git a/pm-task b/pm-task index 4dbf849..176f2d6 100755 --- a/pm-task +++ b/pm-task @@ -187,6 +187,9 @@ if [ $1 ]; then up|start) docker compose up -d ;; + upgrade) + docker compose pull && docker compose up -d --remove-orphans --force-recreate + ;; --help) subHelp ;; diff --git a/storage-monitor.sh b/storage-monitor.sh new file mode 100755 index 0000000..a87ba7c --- /dev/null +++ b/storage-monitor.sh @@ -0,0 +1,70 @@ +#!/bin/bash +# This script monitors disk space and sends an alert if usage exceeds a specified threshold. +# Usage: ./storage-monitor.sh [mount-point] [threshold] [url_to_call] + +# Check if correct number of arguments provided +if [ $# -lt 2 ]; then + echo "Usage: $0 [mount-point] [threshold] [url_to_call]" + echo "Example: $0 / 80 https://example.com/alert" + echo "Threshold should be a percentage (0-100)" + exit 1 +fi + +# Assign arguments to variables +MOUNT_POINT="$1" +THRESHOLD="$2" +URL_TO_CALL="$3" + +# Validate that mount point exists +if [ ! -d "$MOUNT_POINT" ]; then + echo "Error: Mount point '$MOUNT_POINT' does not exist" + exit 1 +fi + +# Validate threshold is a number between 0 and 100 +if ! [[ "$THRESHOLD" =~ ^[0-9]+$ ]] || [ "$THRESHOLD" -lt 0 ] || [ "$THRESHOLD" -gt 100 ]; then + echo "Error: Threshold must be a number between 0 and 100" + exit 1 +fi + +# Get disk usage percentage (remove % sign) +USAGE=$(df "$MOUNT_POINT" | awk 'NR==2 {print $5}' | sed 's/%//') + +# Check if we got a valid usage value +if ! [[ "$USAGE" =~ ^[0-9]+$ ]]; then + echo "Error: Could not determine disk usage for '$MOUNT_POINT'" + exit 1 +fi + +# Display current usage +echo "Current disk usage for $MOUNT_POINT: $USAGE%" + +# Check if usage exceeds threshold +if [ "$USAGE" -gt "$THRESHOLD" ]; then + echo "ALERT: Disk usage ($USAGE%) exceeds threshold ($THRESHOLD%)" + + # Prepare alert message + MESSAGE="ALERT: Disk usage on $MOUNT_POINT is at $USAGE%, exceeding threshold of $THRESHOLD%" + + # If URL is provided, send HTTP request + if [ -n "$URL_TO_CALL" ]; then + echo "Sending alert to: $URL_TO_CALL" + + # Try to send alert using curl (most common) + if command -v curl &> /dev/null; then + curl -X POST -d "message=$MESSAGE" "$URL_TO_CALL" 2>/dev/null + # Fallback to wget if curl is not available + elif command -v wget &> /dev/null; then + wget --post-data="message=$MESSAGE" "$URL_TO_CALL" -O /dev/null 2>/dev/null + else + echo "Warning: Neither curl nor wget is available to send HTTP request" + fi + fi + + # Exit with error code to indicate alert condition + exit 2 +else + echo "Disk usage ($USAGE%) is within acceptable limits (threshold: $THRESHOLD%)" + exit 0 +fi +# Usage: ./storage-monitor.sh [mount-point] [threshold] [url_to_call] \ No newline at end of file