-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonitoring-example.sh
More file actions
49 lines (44 loc) · 1.07 KB
/
monitoring-example.sh
File metadata and controls
49 lines (44 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
# Copyright (c) 2025-2026 Marc Allgeier (fidpa)
# SPDX-License-Identifier: MIT
# https://github.com/fidpa/server-scripts-cli
# ---
# deployment: manual
# service: none
# status: active
# type: check
# requires_root: false
# ---
#
# Monitoring Example Script
# Demonstrates system metrics collection
#
# Usage:
# ./monitoring-example.sh [--json]
set -uo pipefail
# Parse arguments
OUTPUT_FORMAT="text"
if [[ "${1:-}" == "--json" ]]; then
OUTPUT_FORMAT="json"
fi
# Collect metrics
CPU_LOAD=$(uptime | awk -F'load average:' '{print $2}' | awk '{print $1}' | tr -d ',')
MEMORY_USED=$(free -m | awk '/Mem:/ {printf "%.0f", $3/$2 * 100}')
DISK_USED=$(df -h / | awk 'NR==2 {print $5}' | tr -d '%')
if [[ "$OUTPUT_FORMAT" == "json" ]]; then
cat << EOF
{
"cpu_load": "$CPU_LOAD",
"memory_percent": $MEMORY_USED,
"disk_percent": $DISK_USED,
"timestamp": "$(date -Is)"
}
EOF
else
echo "=== System Monitoring ==="
echo "CPU Load: $CPU_LOAD"
echo "Memory Used: ${MEMORY_USED}%"
echo "Disk Used: ${DISK_USED}%"
echo "Timestamp: $(date -Is)"
fi
exit 0