-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmotd
155 lines (132 loc) · 4.81 KB
/
motd
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
#Heading
source /etc/motd/colors.txt
echo -e "${CYA}"
/usr/bin/env figlet -f roman -w 100 "$(hostname)"
echo -e "${YEL}OS info:
$NC Distro......: $YEL`cat /etc/*release | grep "PRETTY_NAME" | cut -d "=" -f 2- | sed 's/"//g'`
$NC Kernel......: $YEL`uname -sr`
"
# Last login
echo -e " ${YEL}Last login..........: ${NC}${PUR}$(last | head -1 | cut -c 40-55 | xargs)${NC} from ${PUR}$(last | head -1 | cut -c 23-39 | xargs)${NC}"
# System uptime
echo -e " ${YEL}Uptime..............:${NC} ${PUR}$(uptime -p | sed 's/^up.//')${NC}"
# Free memory
echo -e " ${YEL}Memory..............:${NC} Used: ${PUR}$(free -m | grep Mem: | xargs | cut -f3 -d' ')${NC} MB | Free: ${PUR}$(free -m | grep Mem: | xargs | cut -f7 -d' ')${NC} MB | Total: ${PUR}$(free -m | grep Mem: | xargs | cut -f2 -d' ')${NC} MB"
# Current Logged in SSH Users
echo -e " ${YEL}SSH Logins..........:${NC} There are currently ${PUR}$(who | wc -l)${NC} users logged in"
#Process Check
COLUMNS=7
# colors
green="\e[1;32m"
red="\e[1;31m"
undim="\e[0m"
services=( "sshd" "smbd" "fail2ban" "docker" "ufw" "clamav-freshclam" )
# 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"
echo ""
echo -e "${YEL} Services:${NC}"
printf "$out" | column -ts $',' | sed -e 's/^/ /'
echo ""
#Dockekr Containers (optional)
#dImages=$(docker images -q | wc -l)
# set column width
# COLUMNS=5
# colors
# green="\e[1;32m"
# red="\e[1;31m"
# undim="\e[0m"
# mapfile -t containers < <(docker ps -a --format '{{.Names}}\t{{.Status}}' | sort -k1 | awk '{ print $1,$2 }')
# out=""
# for i in "${!containers[@]}"; do
# IFS=" " read name status <<< ${containers[i]}
# # color green if service is active, else red
# if [[ "${status}" == "Up" ]]; then
# out+="${NC}${name}:,${green}${status,,}${undim},"
# else
# out+="${RED}${name}:,${red}${status,,}${undim},"
# fi
# # insert \n every $COLUMNS column
# if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
# out+="\n"
# fi
# done
# out+="\n"
# echo -e "${YEL} Docker Status:"
# printf "$out" | column -ts $',' | sed -e 's/^/ /'
# echo ""
# echo -e "${YEL} $dImages ${NC}Docker Images on Local Machine"
# echo ""
#Fail2Ban Status
logfile='/var/log/fail2ban.log*'
mapfile -t lines < <(grep -hioP '(\[[a-z-]+\]) ?(?:restore)? (ban|unban)' $logfile | sort | uniq -c)
jails=($(printf -- '%s\n' "${lines[@]}" | grep -oP '\[\K[^\]]+' | sort | uniq))
out=""
for jail in ${jails[@]}; do
bans=$(printf -- '%s\n' "${lines[@]}" | grep -iP "[[:digit:]]+ \[$jail\] ban" | awk '{print $1}')
restores=$(printf -- '%s\n' "${lines[@]}" | grep -iP "[[:digit:]]+ \[$jail\] restore ban" | awk '{print $1}')
unbans=$(printf -- '%s\n' "${lines[@]}" | grep -iP "[[:digit:]]+ \[$jail\] unban" | awk '{print $1}')
bans=${bans:-0} # default value
restores=${restores:-0} # default value
unbans=${unbans:-0} # default value
bans=$(($bans+$restores))
diff=$(($bans-$unbans))
out+=$(printf "$jail, %+3s bans, %+3s unbans, %+3s active" $bans $unbans $diff)"\n"
done
echo -e "${YEL} Fail2ban Status (Monthly)${NC}:"
printf "$out" | column -ts $',' | sed -e 's/^/ /'
echo ""
#Disk Usage
# config
max_usage=90
bar_width=50
# colors
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+3)
echo -e "${YEL} Disk usage:${NC}"
for line in "${dfs[@]}"; do
# get disk usage
usage=$(echo " $line" | awk '{print $2}' | sed 's/%//')
used_width=$((($usage*$bar_width)/100))
# color is GRN if usage < max_usage, else RED
if [ "${usage}" -ge "${max_usage}" ]; then
color=$RED
else
color=$GRN
fi
# print GRN/RED bar until used_width
bar="[${color}"
for ((i=0; i<$used_width; i++)); do
bar+="="
done
# print dimmmed bar until end
bar+="${NC}${dim}"
for ((i=$used_width; i<$bar_width; i++)); do
bar+="="
done
bar+="${undim}]"
# print usage line & bar
echo -e "${line}" | awk '{ printf("%-31s%+3s used out of %+4sB\n", $1, $2, $3); }' | sed -e 's/^/ /'
echo -e "${bar}" | sed -e 's/^/ /'
done