-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShell
More file actions
95 lines (76 loc) · 3.09 KB
/
Copy pathShell
File metadata and controls
95 lines (76 loc) · 3.09 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# 1. Capture date and time
DATE=$(date '+%Y-%m-%d %H:%M:%S')
TIMESTAMP=$(date '+%Y%m%d_%H%M%S')
# 2. Define directory and file path
OUTPUT_DIR="/tmp/CPU_Utilization"
OUTPUT_FILE="$OUTPUT_DIR/CPU_Utilization_$TIMESTAMP.txt"
# 3. Check if the directory exists, if not create it
if [ ! -d "$OUTPUT_DIR" ]; then
mkdir -p "$OUTPUT_DIR"
fi
# 4. Capture hostname
HOSTNAME=$(hostname)
# 5. Capture CPU utilization using 'top' command
CPU_USAGE_TOP=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}')
# 6. Capture CPU utilization using 'vmstat' command
CPU_USAGE_VMSTAT=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}')
# 7. Capture per-core CPU usage using 'mpstat' command (from sysstat package)
MPSTAT_OUTPUT=$(mpstat -P ALL 1 1)
# 8. Capture detailed CPU statistics using 'iostat' command
IOSTAT_OUTPUT=$(iostat -c 1 1)
# 9. Read CPU utilization data directly from /proc/stat file
PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}')
# 10. Capture disk usage of the root '/' partition using 'df -h'
DISK_USAGE=$(df -h / | grep / | awk '{print $5}')
# 11. Capture memory usage using 'free' command
MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}')
# 12. Capture system uptime and load average using 'uptime'
UPTIME=$(uptime -p)
LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }')
# 13. Capture number of active TCP connections using 'ss' (if installed) or 'netstat'
TCP_CONNECTIONS=$(ss -t | wc -l)
# 14. Capture number of running processes using 'ps'
RUNNING_PROCESSES=$(ps -e --no-headers | wc -l)
# 15. Capture top 5 memory-hogging processes
TOP_MEMORY_PROCESSES=$(ps aux --sort=-%mem | head -n 6)
# 16. Capture top 5 CPU-hogging processes
TOP_CPU_PROCESSES=$(ps aux --sort=-%cpu | head -n 6)
# 17. Calculate top 5 FIDs by total CPU utilization
TOP_FID_CPU=$(ps -eo user,%cpu --sort=-%cpu | awk '{cpu[$1]+=$2} END {for (u in cpu) print u, cpu[u]}' | sort -k2 -nr | head -n 5)
# 18. Write the information to both the output file and the console using 'tee'
{
echo "[$DATE] Hostname: $HOSTNAME"
echo "CPU Utilization (top): $CPU_USAGE_TOP%"
echo "CPU Utilization (vmstat): $CPU_USAGE_VMSTAT%"
echo "Disk Usage: $DISK_USAGE"
echo
echo "Memory Usage:"
echo "$MEMORY_USAGE"
echo
echo "Uptime: $UPTIME"
echo "Load Average: $LOAD_AVG"
echo
echo "Active TCP Connections: $TCP_CONNECTIONS"
echo "Number of Running Processes: $RUNNING_PROCESSES"
echo
echo "Top 5 Memory-Hogging Processes:"
echo "$TOP_MEMORY_PROCESSES"
echo
echo "Top 5 CPU-Hogging Processes:"
echo "$TOP_CPU_PROCESSES"
echo
echo "Per-core CPU Utilization (mpstat):"
echo "$MPSTAT_OUTPUT"
echo
echo "Detailed CPU stats (iostat):"
echo "$IOSTAT_OUTPUT"
echo
echo "CPU Usage from /proc/stat: $PROC_STAT%"
echo
echo "Top 5 FIDs by Total CPU Utilization:"
echo "FID, Total CPU%"
echo "$TOP_FID_CPU"
} | tee "$OUTPUT_FILE"
# Optional message to confirm that the output has been saved
echo "CPU utilization, memory, and other system information saved to $OUTPUT_FILE"