-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemInfo
172 lines (144 loc) · 7.14 KB
/
SystemInfo
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
# Define a function to print the progress bar
printBar() {
input=$1
color=$2
roundedInput=$(awk -v x=$input 'BEGIN{printf "%3.0f\n", x/2}')
output=""
coloredEquals=$(awk -v percent=$input 'BEGIN{printf "%d\n", percent / 100 * 50}')
for ((i=1;i<=$coloredEquals;i++))
do
output=$output"="
done
for ((i=1;i<=$((50 - $coloredEquals));i++))
do
output+=" "
done
echo -e "${color}${output}\033[0m"
}
lengthWithoutEscapes() {
echo -en "$1" | sed 's/\x1b\[[0-9;]*m//g' | wc -c
}
# Function to print a line with proper alignment
printLine() {
line="$1"
underline="$2"
length=$(lengthWithoutEscapes "$line")
spaces=$((68 - length))
padding=$(printf '%*s' $spaces)
if [ "$underline" == "underline" ]; then
echo -e "│\033[4m$line\033[0m$padding│"
else
echo -e "│$line$padding│"
fi
#echo "│$line$padding│"
}
# Fetch system information
CURRENT_USER=$(whoami)
CURRENT_TIME=$(date +"%Y-%m-%d,%T")
VERSION=$(cat /etc/*-release | grep PRETTY_NAME | grep -v grep | awk -F'=' '{print $2}' | sed -e 's/^"//' -e 's/"$//')
KERNEL=$(uname -sr)
UPTIME=$(uptime -p | sed 's/up //' | sed 's/\([0-9]\+\) days/\\033[95m\1\\033[0m days /' | sed 's/\([0-9]\+\) day/\\033[95m\1\\033[0m day /' | sed 's/\([0-9]\+\) hours/\\033[95m\1\\033[0m hours /' | sed 's/\([0-9]\+\) hour/\\033[95m\1\\033[0m hour /' | sed 's/\([0-9]\+\) minutes/\\033[95m\1\\033[0m minutes /' | sed 's/\([0-9]\+\) minute/\\033[95m\1\\033[0m minute /' | sed 's/ $//')
IPADDR=$(hostname -I | awk '{print $1}')
HOSTNAME=$(hostname)
CPU=$(lscpu | grep -w 'Model name' | awk -F':' '{ print $2 }' | head -n 1 | awk '{$1=$1};1')
MEMUSED=$(free -m | awk 'NR==2{print $3}')
MEMTOTAL=$(free -m | awk 'NR==2{print $2}')
#MEMPERCENT=$(awk "BEGIN {print ($MEMUSED/$MEMTOTAL * 100)}")
MEMPERCENT=$(awk "BEGIN {printf \"%.2f\", $MEMUSED/$MEMTOTAL * 100}")
MEMPERCENT_INT=${MEMPERCENT%.*}
if [ $MEMPERCENT_INT -lt 50 ]; then
MEMCOLOR="\033[92m" # Green
elif [ $MEMPERCENT_INT -lt 70 ]; then
MEMCOLOR="\033[93m" # Orange
else
MEMCOLOR="\033[91m" # Red
fi
MEMORY="${MEMUSED}MB / ${MEMTOTAL}MB (${MEMCOLOR}${MEMPERCENT}%\033[0m Used)"
SWAPUSED=$(free -m | awk 'NR==3{print $3}')
SWAPTOTAL=$(free -m | awk 'NR==3{print $2}')
SWAPPERCENT=$(awk "BEGIN {printf \"%.2f\", $SWAPUSED/$SWAPTOTAL * 100}")
SWAPPERCENT_INT=${SWAPPERCENT%.*}
if [ $SWAPPERCENT_INT -lt 50 ]; then
SWAPCOLOR="\033[92m" # Green
elif [ $SWAPPERCENT_INT -lt 70 ]; then
SWAPCOLOR="\033[93m" # Orange
else
SWAPCOLOR="\033[91m" # Red
fi
SWAP="${SWAPUSED}MB / ${SWAPTOTAL}MB (${SWAPCOLOR}${SWAPPERCENT}%\033[0m Used)"
VCPU=$(nproc)
LOADAVG=$(cat /proc/loadavg | awk '{print $1" "$2" "$3}')
setColor() {
LOADVAL=$1
RATIO=$(awk -v loadval="$LOADVAL" -v vcpu="$VCPU" 'BEGIN {print loadval / vcpu}')
if awk -v ratio="$RATIO" 'BEGIN {exit !(ratio < 0.4)}'; then
echo "\033[92m" # Green
elif awk -v ratio="$RATIO" 'BEGIN {exit !(ratio < 0.7)}'; then
echo "\033[93m" # Orange
else
echo "\033[91m" # Red
fi
}
LOAD1=$(awk '{print $1}' /proc/loadavg)
LOAD5=$(awk '{print $2}' /proc/loadavg)
LOAD15=$(awk '{print $3}' /proc/loadavg)
COLOR1=$(setColor $LOAD1)
COLOR5=$(setColor $LOAD5)
COLOR15=$(setColor $LOAD15)
#LOAD_STRING=" Load avg : ($COLOR1${LOAD1}\033[0m(1m) $COLOR5${LOAD5}\033[0m(5m) $COLOR15${LOAD15}\033[0m(15m))"
LOAD_STRING=" Load avg : $COLOR1${LOAD1}\033[0m(1m) $COLOR5${LOAD5}\033[0m(5m) $COLOR15${LOAD15}\033[0m(15m)"
ROOT_PROCESSES=$(ps -U root --no-headers | wc -l)
USER_PROCESSES=$(ps -U root --no-headers -N 2>/dev/null | wc -l)
TOTAL_PROCESSES=$(($ROOT_PROCESSES + $USER_PROCESSES))
USERS_LOGGED_ON=$(who --count | tail -1 | awk -F'=' '{print $2}')
LAST_BOOT=$(uptime -s | sed 's/[[:space:]]/, /g' | sed '$ s/, $//g')
LAST_LOGIN=$(last -i | egrep -v 'logged|reboot|tty' | head -1 | awk -F' ' '{print $1" ("$3") at "$6" "$5" "$7}')
# Print the information in the desired format
echo "┌────────────────────────────────────────────────────────────────────┐"
printLine " "
#printLine " [System Info] "
echo -e "$(printLine " [System Info]" "underline")"
printLine " "
printLine " Current Time : $CURRENT_TIME"
printLine " Version : $VERSION"
printLine " Kernel : $KERNEL"
echo -e "$(printLine " Uptime : $UPTIME")"
printLine " Ipaddr : $IPADDR"
printLine " Hostname : $HOSTNAME"
printLine " "
printLine " Cpu : $CPU"
echo -e "$(printLine " Memory : $MEMORY")"
echo -e "$(printLine " SWAP : $SWAP")"
#printLine " Load avg : $(echo $LOADAVG | awk '{print $1"(1m) "$2"(5m) "$3"(15m)" }')"
echo -e "$(printLine "$LOAD_STRING")"
echo -e "$(printLine " Processes : ${ROOT_PROCESSES}(root) ${USER_PROCESSES}(user) ${TOTAL_PROCESSES}(total)")"
printLine " Users Logged on : ${USERS_LOGGED_ON} users"
printLine " "
printLine " Last Boot : $LAST_BOOT"
printLine " Last Login : $LAST_LOGIN"
printLine " "
#printLine " [Filesystem Info] "
echo -e "$(printLine " [Filesystem Info]" "underline")"
printLine " "
# Fetch and print filesystem information
MAX_MOUNT_LENGTH=$(df -x tmpfs -x devtmpfs -x vfat -x overlay --output=target | tail -n +2 | awk '{ if (length($0) > max) max = length($0) } END { print max }')
df -h -x tmpfs -x devtmpfs -x vfat -x overlay --output=target,size,used,pcent | tail -n +2 | while read -r line; do
MOUNT_POINT=$(echo $line | awk '{print $1}')
SIZE=$(echo $line | awk '{print $2}')
USED=$(echo $line | awk '{print $3}')
PERCENT_USED=$(echo $line | awk '{print $4}' | sed 's/%//')
MOUNT_SPACES=$(printf '%*s' $((MAX_MOUNT_LENGTH - ${#MOUNT_POINT})))
coloredEquals=$(awk -v percent=$PERCENT_USED 'BEGIN{printf "%d\n", percent / 100 * 50}')
if awk "BEGIN {exit !($PERCENT_USED < 50)}"; then
PERCENT_COLOR="\033[32m"
elif awk "BEGIN {exit !(($PERCENT_USED >= 50) && ($PERCENT_USED < 70))}"; then
PERCENT_COLOR="\033[33m"
else
PERCENT_COLOR="\033[31m"
fi
echo -e "$(printLine " Mounted: ${MOUNT_POINT}${MOUNT_SPACES} ${USED} / ${SIZE} (${PERCENT_COLOR}${PERCENT_USED}%\033[0m Used)")"
echo -e "$(printLine " [$(printBar $PERCENT_USED $PERCENT_COLOR)]")"
done
printLine " "
echo "└────────────────────────────────────────────────────────────────────┘"