-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv
More file actions
106 lines (87 loc) · 3.9 KB
/
Copy pathcsv
File metadata and controls
106 lines (87 loc) · 3.9 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
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# Helper function for headers with clear separation
print_header() {
echo
echo "====== $1 ======"
}
# Capture date and time
DATE=$(date '+%Y-%m-%d %H:%M:%S')
TIMESTAMP=$(date '+%Y%m%d_%H%M%S')
OUTPUT_DIR="/tmp/CPU_Utilization"
OUTPUT_FILE="$OUTPUT_DIR/CPU_Utilization_$TIMESTAMP.csv"
# Create directory if not exists
[ ! -d "$OUTPUT_DIR" ] && mkdir -p "$OUTPUT_DIR"
# Capture general system information
HOSTNAME=$(hostname)
CPU_USAGE_TOP=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}')
CPU_USAGE_VMSTAT=$(vmstat 1 2 | tail -1 | awk '{print $13+$14}')
MPSTAT_OUTPUT=$(mpstat -P ALL 1 1)
IOSTAT_OUTPUT=$(iostat -c 1 1)
PROC_STAT=$(cat /proc/stat | grep '^cpu ' | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}')
DISK_USAGE=$(df -h)
MEMORY_USAGE=$(free -h | grep "Mem:" | awk '{print "Total: "$2", Used: "$3", Free: "$4}')
UPTIME_P=$(uptime -p)
UPTIME_S=$(uptime -s)
LOAD_AVG=$(uptime | awk -F'load average:' '{ print $2 }')
TCP_CONNECTIONS=$(ss -t | wc -l)
RUNNING_PROCESSES=$(ps -e --no-headers | wc -l)
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)
{
echo "Report generated on:,[$DATE],for Hostname:,$HOSTNAME"
print_header "Top 5 Memory-Consuming Processes"
echo "PID,Memory (%),Command"
ps aux --sort=-%mem | awk '{print $2, $4, substr($0, index($0, $11))}' | head -n 6 | while read -r pid mem cmd; do
echo "$pid,$mem,\"$(echo "$cmd" | cut -c 1-1000)\""
done
echo "*****"
print_header "Top 5 CPU-Consuming Processes"
echo "PID,CPU (%),Command"
ps aux --sort=-%cpu | awk '{print $2, $3, substr($0, index($0, $11))}' | head -n 6 | while read -r pid cpu cmd; do
echo "$pid,$cpu,\"$(echo "$cmd" | cut -c 1-1000)\""
done
echo "*****"
print_header "Additional System Information"
echo "Metric,Value"
echo "CPU Utilization (top),$CPU_USAGE_TOP%"
echo "CPU Utilization (vmstat),$CPU_USAGE_VMSTAT%"
echo "Memory Usage,$MEMORY_USAGE"
echo "System Uptime and Load Average,\"$(uptime)\""
echo "TCP Connections,$TCP_CONNECTIONS"
echo "Running Processes,$RUNNING_PROCESSES"
# print_header "Per-Core CPU Utilization"
# echo "$MPSTAT_OUTPUT"
# print_header "Detailed CPU Stats"
# echo "$IOSTAT_OUTPUT"
print_header "Disk Usage (All Mounted Filesystems)"
echo "Filesystem,Size,Used,Available,Use%,Mounted on"
df -h | awk 'NR>1 {print $1 "," $2 "," $3 "," $4 "," $5 "," $6}'
echo "Uptime (Human-Readable),$UPTIME_P"
echo "System Start Time,$UPTIME_S"
} | tee "$OUTPUT_FILE"
echo
print_header "Top 5 FIDs by Total CPU Utilization"
echo "No.,FID,Total CPU (%)"
echo "$TOP_FID_CPU" | nl -w2 -s'. ' | awk '{print $1 "," $2 "," $3}'
echo
read -p "Select an FID by entering the corresponding number: " FID_SELECTION
SELECTED_FID=$(echo "$TOP_FID_CPU" | sed -n "${FID_SELECTION}p" | awk '{print $1}')
print_header "Top 10 CPU-consuming processes for FID '$SELECTED_FID'"
# Display truncated command line for readability and save full command to file
echo "PID,CPU (%),Command" >> "$OUTPUT_FILE"
ps -u "$SELECTED_FID" --sort=-%cpu -o pid,%cpu,cmd | head -n 10 | while read -r pid cpu cmd; do
echo "$pid,$cpu,\"$(echo "$cmd" | cut -c 1-1000)\""
echo "$pid,$cpu,\"$cmd\"" >> "$OUTPUT_FILE" # Save full command to file in CSV format
done
print_header "End of Report"
echo "Detailed information saved to: $OUTPUT_FILE"
# Prompt to send the file via email
ORIGINAL_USER=$(who | head -n 1 | awk '{print $1}')
USER_EMAIL="${ORIGINAL_USER}@xyz.com"
read -p "Do you want to send the report to your email address ($USER_EMAIL)? (y/n): " SEND_EMAIL
if [[ $SEND_EMAIL == [Yy]* ]]; then
echo "Sending report to $USER_EMAIL..."
mail -s "CPU Utilization Report" -a "$OUTPUT_FILE" "$USER_EMAIL" <<< "Please find the attached CPU utilization report."
echo "Report sent to $USER_EMAIL."
else
echo "Report not sent."
fi