Skip to content

Commit

Permalink
TASK: Upload first set of scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
d-Rickyy-b committed Nov 9, 2019
1 parent c69536a commit 7372e99
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 10-header
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
# This script generates a ascii art heading
figlet -f big "Title"
35 changes: 35 additions & 0 deletions 20-sysinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# This script prints basic system information
# Most of the script was created by yboetz
# https://github.com/yboetz/motd/blob/master/20-sysinfo

# get load averages
IFS=" " read LOAD1 LOAD5 LOAD15 <<<$(cat /proc/loadavg | awk '{ print $1,$2,$3 }')
# get free memory
IFS=" " read USED FREE TOTAL <<<$(free -htm | grep "Mem" | awk {'print $3,$4,$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` `uname -v | sed -e 's/^.*(/(/'`
$W Uptime......: $W`uptime -p`
$W Current time: $W`date`
$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 Online users: $W`who|wc -l`
$W CPU.........: $W$PROCESSOR_NAME ($G$PROCESSOR_COUNT$W vCPU)
$W Memory......: $G$USED$W used, $G$FREE$W free, $G$TOTAL$W total$W"

58 changes: 58 additions & 0 deletions 30-diskspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/bin/bash
# This script displays the usage of specified disks
# Base version used for this script: https://github.com/Heholord/FalconStats/blob/master/scripts/discspace.sh


clear="\e[39\e[0m"
dim="\e[2m"

# The parameter maxDiskUsage indicates when the bar should be displayed as red
maxDiskUsage=80

# The parameter barWidth indicates how long the disk usage bar should be
barWidth=50

# mountpoints contains all the directories which devices should be monitored
# Examples: mountpoints="/ /boot /home" - this will monitor the devices mounted
mountpoints="/"

# Make space above
echo ""
echo "disk usage:"

for point in ${mountpoints}; do
# Get the output of df and add spaces after the newlines
line=$(df -hl "${point}"|sed -e ':a;N;$!ba;s/\n/\n /g')
usagePercent=$(echo "$line"|tail -n1|awk '{print $5;}'|sed 's/%//')
usedBarWidth=$((($usagePercent*$barWidth)/100))
barContent=""
color="\e[32m"

# Color the bar red in case it's bigger than the speicified value
if [ "${usagePercent}" -ge "${maxDiskUsage}" ]; then
color="\e[31m"
fi
barContent="${color}"

# Build the usage bar
for sep in $(seq 1 $usedBarWidth); do
barContent="${barContent}="
done

# Rest of the unused space in white
barContent="${barContent}${clear}${dim}"
for sep in $(seq 1 $(($barWidth-$usedBarWidth))); do
barContent="${barContent}="
done
bar="[${barContent}${clear}]"

# Print the column/title bar
if [ "${titeled}" == "" ]; then
echo " ${line}"
titeled="1"
else
echo " ${line}"|tail -n1
fi
echo -e " ${bar}"
done

41 changes: 41 additions & 0 deletions 40-services
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
# This script displays the status of a list of specified services
# Base version used for this script: https://github.com/yboetz/motd/blob/master/40-services


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

services=("fail2ban" "ufw" "docker" "apache2")
# 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/^/ /'

22 changes: 22 additions & 0 deletions 50-fail2ban
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
# This script lists all bans and unbans from fail2ban
# It sometimes is not very accurate in calculating the still active bans
# Taken from: https://github.com/yboetz/motd/blob/master/50-fail2ban

logfile='/var/log/fail2ban.log*'
mapfile -t lines < <(grep -hioP '(\[[a-z-]+\]) (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}')
unbans=$(printf -- '%s\n' "${lines[@]}" | grep -iP "[[:digit:]]+ \[$jail\] unban" | awk '{print $1}')
bans=${bans:-0} # default value
unbans=${unbans:-0} # default value
diff=$(($bans-$unbans))
out+=$(printf "$jail, %+3s bans, %+3s unbans, %+3s active" $bans $unbans $diff)"\n"
done

printf "\nfail2ban status (monthly):\n"
printf "$out" | column -ts $',' | sed -e 's/^/ /'

32 changes: 32 additions & 0 deletions 60-docker
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
# This script lists all docker containers and their status
# Taken from https://github.com/yboetz/motd/blob/master/60-docker

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

mapfile -t containers < <(docker ps -a --format '{{.Names}}\t{{.Status}}' | 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+="${name}:,${green}${status,,}${undim},"
else
out+="${name}:,${red}${status,,}${undim},"
fi
# insert \n every $COLUMNS column
if [ $((($i+1) % $COLUMNS)) -eq 0 ]; then
out+="\n"
fi
done
out+="\n\n"

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

8 changes: 8 additions & 0 deletions 80-loggedinusers
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
# This scripts prints out all logged in users

echo ""
echo "logged in users:"
echo " $(who|sed -e ':a;N;$!ba;s/\n/\n /g')"
echo ""

0 comments on commit 7372e99

Please sign in to comment.