-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil-lite.sh
110 lines (86 loc) · 2.3 KB
/
util-lite.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
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
107
108
109
110
#!/bin/bash
cpu_symbol="="
ram_symbol="="
swap_symbol="="
generate_cpu_graph() {
cpu_percentage="$1"
cpu_cores="$2"
cpu_graph_symbol="$3"
cpu_percentage=${cpu_percentage%\%}
cpu_blocks=$((cpu_percentage / 2))
free_blocks=$((50 - cpu_blocks))
graph="["
for ((i = 0; i < cpu_blocks; i++)); do
graph+="$cpu_graph_symbol"
done
for ((i = 0; i < free_blocks; i++)); do
graph+=" "
done
graph+="] ${cpu_percentage}%"
echo ""
echo "CPU Cores: $cpu_cores"
echo "CPU: ${cpu_percentage}%"
# Set color based on CPU percentage
if (( cpu_percentage >= 70 )); then
echo -e "CPU Usage: \033[0;31m$graph\033[0m"
elif (( cpu_percentage >= 40 )); then
echo -e "CPU Usage: \033[0;33m$graph\033[0m"
else
echo -e "CPU Usage: \033[0;32m$graph\033[0m"
fi
}
generate_usage_graph() {
used="$1"
total="$2"
symbol="$3"
label="$4"
used_percentage=$((used * 100 / total))
used_blocks=$((used_percentage / 2))
free_blocks=$((50 - used_blocks))
graph="["
for ((i = 0; i < used_blocks; i++)); do
graph+="$symbol"
done
for ((i = 0; i < free_blocks; i++)); do
graph+=" "
done
graph+="] $used_percentage%"
echo ""
echo "$label: ${used}MB / ${total}MB"
# Set color based on usage percentage
if (( used_percentage >= 70 )); then
echo -e "${label} Usage: \033[0;31m$graph\033[0m"
elif (( used_percentage >= 40 )); then
echo -e "${label} Usage: \033[0;33m$graph\033[0m"
else
echo -e "${label} Usage: \033[0;32m$graph\033[0m"
fi
}
while getopts "c:r:s:" opt; do
case "$opt" in
c)
cpu_symbol="$OPTARG"
;;
r)
ram_symbol="$OPTARG"
;;
s)
swap_symbol="$OPTARG"
;;
esac
done
while true; do
ram_info=$(free -m | awk 'NR==2{print $2, $3}')
total_ram=$(echo "$ram_info" | awk '{print $1}')
used_ram=$(echo "$ram_info" | awk '{print $2}')
swap_info=$(free -m | awk 'NR==3{print $2, $3}')
total_swap=$(echo "$swap_info" | awk '{print $1}')
used_swap=$(echo "$swap_info" | awk '{print $2}')
cpu_usage=$(top -b -n 1 | awk '/%Cpu/{print 100 - $8"%"}')
cpu_cores=$(nproc)
clear
generate_usage_graph "$used_ram" "$total_ram" "$ram_symbol" "RAM"
generate_usage_graph "$used_swap" "$total_swap" "$swap_symbol" "Swap"
generate_cpu_graph "$cpu_usage" "$cpu_cores" "$cpu_symbol"
sleep 1
done