一些有用的显示服务器信息的脚本

ssh登录服务器的时候最先看到的便是一些欢迎的信息, 但默认的欢迎信息可以说是毫无用处, 我们便可以自行修改来展示一些有用的信息.

image-20231230235121076

motd

motd, 全称Message Of The Day, 是Linux中发送问候消息的功能, 每次任意用户登录时都会触发motd服务的功能, 这个功能的脚本几乎都是使用root 权限来启动的

脚本位置在/etc/update-motd.d, 不想执行某些脚本, 可以直接取消执行权限

image-20231230235518657

10-hostname-color

类似于logo banner之类的东西, 比如Spring Boot项目启动后就会显示Spring Boot的logo

1
2
#!/bin/bash
/usr/bin/figlet "$(hostname)" | /usr/bin/lolcat -f

10-uname

1
2
#!/bin/sh
uname -snrvm

20-sysinfo

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
#!/bin/bash

# get load averages
IFS=" " read LOAD1 LOAD5 LOAD15 <<<$(cat /proc/loadavg | awk '{ print $1,$2,$3 }')
# get free memory
IFS=" " read USED AVAIL TOTAL <<<$(free -htm | sed -n '2p' | awk {'print $3,$7,$2'})
# get processes
PROCESS=`ps -eo user=|sort|uniq -c | awk '{ print $2 " " $1 }'`
PROCESS_ALL=`echo "$PROCESS"| awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'`
PROCESS_ROOT=`echo "$PROCESS"| grep root | awk {'print $2'}`
PROCESS_USER=`echo "$PROCESS"| grep -v root | awk {'print $2'} | awk '{ SUM += $1} END { print SUM }'`
# get processors
PROCESSOR_NAME=`grep "model name" /proc/cpuinfo | cut -d ' ' -f3- | awk {'print $0'} | head -1`
PROCESSOR_COUNT=`grep -ioP 'processor\t:' /proc/cpuinfo | wc -l`

W="\e[0;39m"
G="\e[1;32m"

echo -e "
${W}system info:
$W Distro......: $W`cat /etc/*release | grep "PRETTY_NAME" | cut -d "=" -f 2- | sed 's/"//g'`
$W Kernel......: $W`uname -sr`
$W Uptime......: $W`uptime -p`
$W Load........: $G$LOAD1$W (1m), $G$LOAD5$W (5m), $G$LOAD15$W (15m)
$W Processes...:$W $G$PROCESS_ROOT$W (root), $G$PROCESS_USER$W (user), $G$PROCESS_ALL$W (total)
$W CPU.........: $W$PROCESSOR_NAME ($G$PROCESSOR_COUNT$W vCPU)
$W Memory......: $G$USED$W used, $G$AVAIL$W avail, $G$TOTAL$W total$W "

30-gpu-utility

1
2
3
4
5
6
7
8
#!/bin/bash
printf "\ngpu utility:\n"
GPUNUM=`nvidia-smi -q|grep Attached|awk '{print $4}'`
for i in $(seq 0 $(($GPUNUM-1)))
do
echo "GPU$i Utilization: "`nvidia-smi -q -i $i -d Utilization|sed -n "11p"|awk '{print $3$4}'`\
"Memory Used: "`nvidia-smi -q -i $i -d Utilization|sed -n "12p"|awk '{print $3$4}'`;
done

35-diskspace

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
#!/bin/bash

# config
max_usage=90
bar_width=50
# colors
white="\e[39m"
green="\e[1;32m"
red="\e[1;31m"
dim="\e[2m"
undim="\e[0m"

# disk usage: ignore zfs, squashfs & tmpfs
mapfile -t dfs < <(df -H -x zfs -x squashfs -x tmpfs -x devtmpfs -x overlay --output=target,pcent,size | tail -n+2)
printf "\ndisk usage:\n"

for line in "${dfs[@]}"; do
# get disk usage
usage=$(echo "$line" | awk '{print $2}' | sed 's/%//')
used_width=$((($usage*$bar_width)/100))
# color is green if usage < max_usage, else red
if [ "${usage}" -ge "${max_usage}" ]; then
color=$red
else
color=$green
fi
# print green/red bar until used_width
bar="[${color}"
for ((i=0; i<$used_width; i++)); do
bar+="="
done
# print dimmmed bar until end
bar+="${white}${dim}"
for ((i=$used_width; i<$bar_width; i++)); do
bar+="="
done
bar+="${undim}]"
# print usage line & bar
echo "${line}" | awk '{ printf("%-31s%+3s used out of %+4s\n", $1, $2, $3); }' | sed -e 's/^/ /'
echo -e "${bar}" | sed -e 's/^/ /'
done

40-services

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
#!/bin/bash

# set column width
COLUMNS=3
# colors
green="\e[1;32m"
red="\e[1;31m"
undim="\e[0m"

services=("ssh" "docker" "apache2" "nginx" "mysql" "networking" "ufw" "xrdp")
# sort services
IFS=$'\n' services=($(sort <<<"${services[*]}"))
unset IFS

service_status=()
# get status of all services
for service in "${services[@]}"; do
service_status+=($(systemctl is-active "$service"))
done

out=""
for i in ${!services[@]}; do
# color green if service is active, else red
if [[ "${service_status[$i]}" == "active" ]]; then
out+="${services[$i]}:,${green}${service_status[$i]}${undim},"
else
out+="${services[$i]}:,${red}${service_status[$i]}${undim},"
fi
# insert \n every $COLUMNS column
if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
out+="\n"
fi
done
out+="\n"

printf "\nservices:\n"
printf "$out" | column -ts $',' | sed -e 's/^/ /'