#!/bin/bash # Title......: web # Description: A menu for monitoring my website # Author.....: Mitchell Johnston - uid 0 # Contact....: mitch@crn.hopto.org # Updated....: Mon 13 May 2024 12:38:28 PM CDT #---------------------------------- : ' Changes: Thu May 02 2024 Cleaned up monitoring loop Fri Feb 23 2024 Set quote to display at center line of screen Wed Feb 21 2024 modified ss to resolve names Fri Oct 13 2023 switched to "lolcat" for vnstat Added grc to sar line for cleaner display Thu Oct 12 2023 modified "vnstat -hg" line in default monitor to add some color ' # variables #---------------------------------- [ "$1" == "-D" ] && DEBUG=1 && shift 1 # -D to turn on debug mode PS4='$SECONDS $ROWS: ' # debug prompt DOW=$(date +%a) # day of week: Thu TODAY=$(date +%m/%d) # month/day: 03/25 DOM=$(date +%d) # day of month: 25 OS=$(uname -s) # OS type: SunOS Linux NAME=${0##*/} # name of the script TMOUT=30 # time before web log is displayed ACCESS='/var/log/lighttpd/access.log' BASE='/var/www' export PATH="$PATH:/home/mitch/bin" export GREP_COLORS='ms=01;31' # Colors - uncomment if needed R=$(tput setaf 1) # red BR=$(tput setaf 1; tput bold) # bold red G=$(tput setaf 2) # green BG=$(tput setaf 2; tput bold) # bold green Y=$(tput setaf 3) # yellow BY=$(tput setaf 3; tput bold) # bold yellow B=$(tput setaf 4) # blue BM=$(tput setaf 5; tput bold) # bold magenta C=$(tput setaf 6) # cyan BC=$(tput setaf 6; tput bold) # bold cyan BL=$(tput setaf 7; tput bold) # bold light grey BLD=$(tput bold) # bold N=$(tput sgr0) # normal SIT=$(tput sitm) # italics RIT=$(tput ritm) # remove italics UL=$(tput smul) # turn underline on NL=$(tput rmul) # turn underline off RV=$(tput rev) # turn on reverse mode ROWS=$(tput lines) COLS=$(tput cols) # functions #---------------------------------- bl(){ ## write a blank line # Use: bl [ "$DEBUG" == "1" ] && set -x echo "" } html(){ ## mark up code vim -f +"syn on" +"colorscheme termschool" +"set nonu" +"set foldenable!" +"set nospell" +"run! syntax/2html.vim" +"wq" +"q" $1 } center(){ ## center test on a line # Use: center "string" {optional fill character} [[ $# == 0 ]] && return 1 declare -i TERM_COLS="$(tput cols)" declare -i str_len="${#1}" [[ $str_len -ge $TERM_COLS ]] && { echo "$1"; return 0; } declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))" [[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" " filler="" for (( i = 0; i < filler_len; i++ )); do filler="${filler}${ch}" done printf "%s%s%s" "$filler" "$1" "$filler" [[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}" printf "\n" return 0 } log(){ ## creates a basic log entry $LOG must be defined # Use: log {entry} [ "$DEBUG" == "1" ] && set -x logger -i -t "$NAME" "$*" } pause(){ ## simple pause routine # Use: pause {optional number of seconds} or "-nt" for no time out [ "$DEBUG" == "1" ] && set -x [ "$1" == "-nt" ] && TMOUT="" && shift echo "$BY"; if [ $# -gt 0 ] then read -t $1 -r -p "${C}Hit any key (${BY}$1${C} second timeout)${N}" -n 1 FOO; else read -r -p "${C}Hit any key${N}" -n 1 FOO; fi; bl } xtitle(){ ## set window title # Use: xtitle "Text to display" printf "\033]0;%s\007" "$* 🌀${HOSTNAME}" } version(){ ## display version and change history grep -E '^# Updated' $0 echo " " sed -n "/' Changes/,/^ *$/p" <$0 |grep -E -v 'sed -n|exit 0|}' exit 0 } # setup #---------------------------------- # this provides a quick way to edit all my scripts on the fly if [ "$1" == "-E" ] then vim $0 sed -i -e "7s/.*/# Updated....: $(date)/" $0 log "Updated $0" html $0 cp $0 /var/www/unix mv $0.html /var/www/unix cp $0 /var/gopher/scripts exit fi # display help if needed if [ "$1" == "-h" ] || [ "$1" == "--help" ] then fmt -s -w $(tput cols) </dev/null || sudo apt install bash -qyy command -v goaccess >/dev/null || sudo apt install goaccess -yyq command -v grc >/dev/null || sudo apt install grc -yyq command -v httping >/dev/null || sudo apt install httping -yyq command -v iptraf-ng >/dev/null || sudo apt install iptraf-ng -yyq command -v lnav >/dev/null || sudo apt install lnav -yyq command -v lolcat >/dev/null || sudo apt install lolcat -yyq # main #--------------------------- [ "$DEBUG" == 1 ] && set -x while : do if [ $# -lt 1 ] # check to see if a command line argument is passed then clear xtitle "Web Menu" echo ",----[${BC}Web${N}]" echo " ${BC}$(cat ~/.host) / $(fclock) ${N} ${C}[Web Menu]${N} 4. 404 report b. ${BR}Block IP${N} e. Edit lighttp.conf g. GoAccess h. Httping l. View logs Q. Query web log s. Server status S. ${BY}Server control${N} t. Trafic monitor ${C}-----------------------${N} ${BR}q. Quit${N} " |boxes -d boxquote |\grep -v '\,' read -r -p "${C}Selection:${N}" -n 1 CHOICE else CHOICE=$(echo "$1" |cut -d'-' -f2) [ "$1" == "-E" ] && CHOICE=E fi case $CHOICE in 4) # 404 report clear bat --style grid /var/log/lighttpd/error.log pause grep 404 $ACCESS | awk '{print $7}' | grep -E 'mp3$|html$|pdf$'|sort |uniq -c | sort -n|bat --style grid # 404 check on mp4 files cd /var/www || exit 1 echo "$NAME: Checking mp4 files" for MP4 in $(grep src media.md |cut -d\" -f2|egrep -v '100\%|preload') do PNG=$(echo "$MP4"|sed s/mp4/png/1) [ ! -f "${BASE}${MP4}" ] && echo "Missing: $MP4" [ ! -f "${BASE}${PNG}" ] && echo "Missing: $PNG" done for MP4 in $(grep src health.md |cut -d\" -f2|egrep -v '100\%|preload') do PNG=$(echo "$MP4"|sed s/mp4/png/1) [ ! -f "${BASE}${MP4}" ] && echo "Missing: $MP4" [ ! -f "${BASE}${PNG}" ] && echo "Missing: $PNG" done pause ;; b) # block an IP IP=$(curl -q ifconfig.me) xtitle "web: Blockem" clear echo "External IP: ${BY}$IP${N}" if [ "$#" -eq 2 ] then IP="$2" else read -r -p "${BR}IP:${BY}" IP fi echo "$N" if [ $(grep -c "$IP" /etc/iptables/rules.v4) -eq 0 ] then sudo iptables -t filter -I INPUT 1 -p tcp -m tcp -s $IP -j REJECT sudo bash -c '/sbin/iptables-save > /etc/iptables/rules.v4' log "Blocked $IP" fi grep -C 3 "$IP" /etc/iptables/rules.v4| sed s#$IP#${BR}$IP${N}#1 # To unblock an entry: # sudo vi /etc/iptables/rules.v4 # sudo bash -c '/sbin/iptables-restore < /etc/iptables/rules.v4' ;; e) # edit conf clear sudo vim /etc/lighttpd/lighttpd.conf log "edit /etc/lighttpd/lighttpd.conf" pause ;; g) # goaccess xtitle "web: GoAccess" cd /var/log/lighttpd || exit 1 if [ -f access.log.2.gz ] then zcat access.log.*.gz |goaccess --config-file=/etc/goaccess/goaccess.conf --enable-panel=GEO_LOCATION access.log access.log.1 --log-format=COMBINED -e DeviceDHCP.Home -e "209.188.75.60" -e "65.23.183.188" -e server9.hobohost.com --ignore-crawlers else goaccess --config-file=/etc/goaccess/goaccess.conf --enable-panel=GEO_LOCATION access.log access.log.1 --log-format=COMBINED -e DeviceDHCP.Home -e "209.188.75.60" -e "65.23.183.188" -e server9.hobohost.com --ignore-crawlers fi cd - >/dev/null || exit 1 ;; h) # httping xtitle "web: httping" clear echo -n "${BY}Web server:${N} " read -r OPTION httping "${OPTION:=crn.hopto.org}" -c 20 -S -Y -Z -s --offset-yellow 370 --offset-red 380 -K pause ;; l) # view logs lnav /var/log/lighttpd/ /var/log/ ;; t) # Traffic xtitle "Web: Traffic" clear sudo iptraf-ng -i wlp1s0 ;; s) # server status xtitle "Web: status" clear sudo service lighttpd status pause ;; S) # start/stop server xtitle "Web: Control" clear echo -n "${G}start${N}, ${R}stop${N}, status, or ${BY}reload${N}: " read -r OPTION log "running: service lighttpd $OPTION" [ "$OPTION" != "status" ] && sudo service lighttpd "$OPTION" tput cup $ROWS $[$COLS/2];spinit sleep 7;clear sudo service lighttpd status sudo systemctl status netfilter-persistent.service sudo /sbin/iptables-save ;; Q) # Query log xtitle "Web: Query" clear FZF_DEFAULT_OPTS="-m --prompt='▶' --pointer='→' --marker='♡' --ansi";cat /var/log/lighttpd/access.log | fzf -m pause ;; q) # quit clear echo "${G}Bye${N}" exit ;; !) # run command xtitle "Web: cmd" clear echo "${C}Hit return with no command to end.${N}" while : do echo -n "[menu] $PWD $ " read -r EXEC [ -z "$EXEC" ] && break eval "$EXEC" done pause ;; =) # reset term and restart script reset exec $0 ;; *) # Monitoring loop # setup trap "break" 2 # main loop while :; do # clean up any trash ROWS=$(tput lines) # in case term size change COLS=$(tput cols) # " " clear;echo $N # Hardware xtitle "Web: Hardware" clear neofetch --ascii_distro linuxmint_small echo " ";tput cup $ROWS $[$COLS/2];spinit sleep 30; # Logs timeout --foreground 3m lnav /var/log/lighttpd/ /var/log/; # sar clear xtitle "Web: SAR" if [ $(date +%H) -lt 2 ] then grc sar; else grc sar -s $(date -d "2 hours ago" +%H):00:00; fi echo " ";tput cup $ROWS $[$COLS/2];spinit sleep 30; # Network clear xtitle "Web: Network" netmon echo " ";tput cup $ROWS $[$COLS/2];spinit sleep 30; clear # connections xtitle "Web: Connections" echo "$BG" center "-| Connections |-" echo " " echo -n "$BC" grc w echo " " grc ss -4tr src 192.168/16 echo " ";tput cup $ROWS $[$COLS/2];spinit sleep 30; # stats clear xtitle "Web: vnstat -s" echo -n "$N" vnstat -i $(ifchk) | lolcat echo " ";tput cup $ROWS $[$COLS/2];spinit sleep 15; clear xtitle "Web: vnstat -hg" vnstat -hg |lolcat echo " ";tput cup $ROWS $[$COLS/2];spinit sleep 15; # Bible quote clear xtitle "Web: Quote" tput cup $(($(tput lines) / 2)) # go to center line center "$(quote)" # Man does not live by bread alone... echo " ";tput cup $ROWS $[$COLS/2];spinit sleep 30 # bottom, center [ "$(date +%H)" -eq 14 ] && [ "$(date +%M)" -gt 30 ] && exit # auto shutdown done ;; esac [ $# -gt 0 ] && exit done