-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.sh
64 lines (55 loc) · 1.53 KB
/
report.sh
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
#author: salman sk
# Script Header
# This script generates a daily system report including SSH logins, CPU and memory usage, and tool logs.
# Usage: Run this script daily using a cron job or manually.
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPORT_DIR="$SCRIPT_DIR/reports"
LOG_DIR="$SCRIPT_DIR/logs"
REPORT_FILE="$REPORT_DIR/report_$(date '+%Y-%m-%d').txt"
mkdir -p "$REPORT_DIR"
mkdir -p "$LOG_DIR"
# Check if necessary commands are available
for cmd in last top free awk grep; do
if ! command -v "$cmd" &>/dev/null; then
echo "Error: Command '$cmd' is not available. Please install it to proceed."
exit 1
fi
done
{
echo "Daily System Report - $(date)"
echo "============================="
echo ""
# SSH Logins
echo "SSH Logins:"
last -a | grep 'ssh' | head -n 20
echo ""
# CPU Usage
echo "CPU Usage:"
top -bn1 | grep "Cpu(s)" | awk '{print "CPU Load: " $2 + $4 "%"}'
echo ""
# Memory Usage
echo "Memory Usage:"
free -h | awk '/^Mem:/ {print "Total: " $2 ", Used: " $3 ", Free: " $4}'
echo ""
# Tool Logs
echo "Tool Logs:"
if [ -d "$LOG_DIR" ]; then
log_files=("$LOG_DIR"/*.log)
if [ "${#log_files[@]}" -gt 0 ]; then
for log in "${log_files[@]}"; do
echo "Log File: $(basename "$log")"
tail -n 20 "$log"
echo ""
done
else
echo "No logs found in $LOG_DIR."
fi
else
echo "Log directory $LOG_DIR does not exist."
fi
} > "$REPORT_FILE"
# Report completion
echo "Report generated at $REPORT_FILE"
exit 0