scripts

custom scripts and utils
git clone git://git.pyratebeard.net/scripts.git
Log | Files | Refs | README

ahoy (9185B)


      1 #!/usr/bin/env bash
      2 #            ██
      3 #           ░██                ██   ██
      4 #   ██████  ░██       ██████  ░░██ ██
      5 #  ░░░░░░██ ░██████  ██░░░░██  ░░███
      6 #   ███████ ░██░░░██░██   ░██   ░██
      7 #  ██░░░░██ ░██  ░██░██   ░██   ██
      8 # ░░████████░██  ░██░░██████   ██
      9 #  ░░░░░░░░ ░░   ░░  ░░░░░░   ░░
     10 #
     11 #  author ▓▒ pyratebeard
     12 #    code ▓▒ https://git.pyratebeard.net/
     13 #
     14 # TODO
     15 # * add rkhunter check
     16 # * add clamav check
     17 # * server details
     18 
     19 basename="${0##*/}"
     20 
     21 # colours
     22 black="\e[30m"
     23 red="\e[31m"
     24 green="\e[32m"
     25 yellow="\e[33m"
     26 blue="\e[34m"
     27 magenta="\e[35m"
     28 cyan="\e[36m"
     29 white="\e[37m"
     30 light_black="\e[1;30m"
     31 light_red="\e[1;31m"
     32 light_green="\e[1;32m"
     33 light_yellow="\e[1;33m"
     34 light_blue="\e[1;34m"
     35 light_magenta="\e[1;35m"
     36 light_cyan="\e[1;36m"
     37 light_white="\e[1;37m"
     38 lighter_black="\e[38;5;241m"
     39 reset="\e[0m"
     40 
     41 display_tasks=0
     42 
     43 usage() {
     44 	echo "Usage: ${basename} [-b] [-h] [-m] [-t] [-r] [-s <hostname>] [-w]
     45 
     46 	-b: backups
     47 	-h: help (this page)
     48 	-m: mail
     49 	-t: tasking
     50 	-r: rss
     51 	-s: show <hostname> details, use 'all' to show all
     52 	-w: weather
     53 "
     54 }
     55 
     56 heading() {
     57 	header="$(echo ${1} | sed -e 's/\(.\)/\1 /g')"
     58 	echo -e "${lighter_black}├┄ ${light_white}${header}${reset}"
     59 }
     60 
     61 tasking() {
     62 	heading ${FUNCNAME[0]}
     63 	total_tasks=$(cat $HOME/.todo | wc -l)
     64 
     65 	echo -e "${lighter_black}│${reset}you have ${light_green}${total_tasks}${reset} task(s)"
     66 }
     67 
     68 # check nightly backups
     69 backups() {
     70 	heading ${FUNCNAME[0]}
     71 	local backup_success=0
     72 	local remote_backup_success=0
     73 	local borg_backup_success=0
     74 	local failed=""
     75 	local remote_failed=""
     76 	local borg_failed=""
     77 	_repos=$(find /home/pyratebeard/lib/dedup/* -maxdepth 1 -type d -prune -printf '%f\n')
     78 	for r in ${_repos} ; do
     79 		_latest=$(find /home/pyratebeard/lib/dedup/${r}/archive -type f \
     80 					-exec stat -c '%X %n' {} \; | \
     81 					sort -nr | \
     82 					awk -F\/ 'NR==1 {print $NF}')
     83 		if [[ "${_latest}" -lt $(date --date="yesterday" +%Y%m%d) ]] ; then
     84 			backup_success=$(( backup_success + 1 ))
     85 			failed+="${r} "
     86 		fi
     87 #		_remote_latest=$(rclone ls backblaze:securededup/$r/archive | \
     88 #					tail -n1 | \
     89 #					awk '{print $NF}')
     90 #		if [[ "${_remote_latest}" -lt $(date --date="yesterday" +%Y%m%d) ]] ; then
     91 #			remote_backup_success=$(( remote_backup_success + 1 ))
     92 #			remote_failed+="${r} "
     93 #		fi
     94 	done
     95 	[[ ${backup_success} -gt 0 ]] \
     96 		&& echo -e "${lighter_black}│${reset}frak! local dedup of ${failed}${red}failed${reset}" \
     97 		|| echo -e "${lighter_black}│${reset}schway! local dedup ${light_green}successful${reset}"
     98 
     99 #	[[ ${remote_backup_success} -gt 0 ]] \
    100 #		&& echo -e "${lighter_black}│${reset}frak! dedup sync of ${failed}${red}failed${reset}" \
    101 #		|| echo -e "${lighter_black}│${reset}schway! dedup sync ${light_green}successful${reset}"
    102 	
    103 #	for b in $(find /media/backup/borg-* -maxdepth 1 -type d -prune -printf '%f\n') ; do
    104 #		cut_b=${b##*-}
    105 #		_latest=$(BORG_PASSPHRASE=$(pass borg/${cut_b}) borg list /media/backup/${b} | tail -n 1 | awk '{print $1}')
    106 #		#echo ${cut_b} - ${_latest} - $(date --date="yesterday" +%Y%m%d)
    107 #		if [[ "${_latest}" -lt $(date --date="yesterday" +%Y%m%d) ]] ; then
    108 #			borg_backup_success=$(( borg_backup_success + 1 ))
    109 #			borg_failed+="${cut_b} "
    110 #			echo -e "${lighter_black}│${reset}frak! borg of ${cut_b} is ${red}over a day old${reset}"
    111 #		else
    112 #			echo -e "${lighter_black}│${reset}schway! borg of ${cut_b} is ${light_green}up to date${reset}"
    113 #		fi
    114 #	done
    115 #	[[ ${borg_backup_success} -gt 0 ]] \
    116 #		&& echo -e "${lighter_black}│${reset}borg of ${borg_failed[@]}is ${red}over a day old${reset}" \
    117 #		|| echo -e "${lighter_black}│${reset}schway! borg ${light_green}successful${reset}"
    118 }
    119 
    120 # weather in current location
    121 weather() {
    122 	heading ${FUNCNAME[0]}
    123 	if [ -f /tmp/weather ] && [[ $(cat /tmp/weather) != "" ]] ; then
    124 		wttr=$(cat /tmp/weather | awk -F\: '{print $2}' | cut -c 2-)
    125 		echo -e "${lighter_black}│${reset}${wttr}"
    126 	else
    127 		$HOME/.local/bin/weather
    128 		wttr=$(cat /tmp/weather | awk -F\: '{print $2}' | cut -c 2-)
    129 		echo -e "${lighter_black}│${reset}${wttr}"
    130 	fi
    131 }
    132 
    133 # you've got mail
    134 mail() {
    135 	heading ${FUNCNAME[0]}
    136 	local -r mail_dir="$HOME/.local/var/mbox"
    137 
    138 	local -r pyratebeard_mail=$(find "${mail_dir}"/pyratebeard/INBOX/new/ -type f | wc -l)
    139 	local -r unread_mail=$(find "${mail_dir}"/pyratebeard/INBOX/cur/ -type f ! -name "*S" | wc -l)
    140 	echo -e "${lighter_black}│${reset}you have ${light_green}${pyratebeard_mail}${reset} new mail(s)"
    141 	echo -e "${lighter_black}│${reset}you have ${light_green}${unread_mail}${reset} unread mail(s)"
    142 }
    143 
    144 # not currently used but keeping for future reference
    145 getnews() {
    146 	local -r api_key=$(pass websites/newsapi/key)
    147 	curl 'https://newsapi.org/v2/top-headlines' -s -G \
    148 	-d sources="$1" \
    149 	-d pageSize=5 \
    150 	-d apiKey="${api_key}" | jq -r 'def default: "\u001b[0m"; def grey: "\u001b[1;30m"; .articles[] | .title, grey + .url, "" + default' | tr -d '\t'
    151 }
    152 
    153 rss() {
    154 	heading ${FUNCNAME[0]}
    155 	if [ -f ~/.local/share/newsboat/cache.db.lock ] ; then
    156 		echo -e "${lighter_black}│${reset}${light_black}waiting for newsboat refresh...${reset}" 
    157 		sleep 10
    158 	fi
    159 	local unread_count
    160 	unread_count=$(sqlite3 ~/.local/share/newsboat/cache.db "select count(unread) from rss_item where unread = 1;")
    161 	local queued_pods
    162 	queued_pods=$(wc -l ~/.local/share/newsboat/queue | awk '{print $1}')
    163 	echo -e "${lighter_black}│${reset}there are ${light_green}${unread_count}${reset} unread items"
    164 	echo -e "${lighter_black}│${reset}you have ${light_green}${queued_pods}${reset} queued podcasts"
    165 }
    166 
    167 servers() {
    168 	echo -e "${cyan}
    169     ┐─┐┬─┐┬─┐┐ ┬┬─┐┬─┐┐─┐
    170     └─┐├─ │┬┘│┌┘├─ │┬┘└─┐
    171     ──┘┴─┘┆└┘└┘ ┴─┘┆└┘──┘
    172 ${reset}"
    173 	local server="$1"
    174 	if [ "${server}" == "all" ] ; then
    175 		for node in blacksun grimoire pyratetube mordhaus ; do
    176 			heading "${node}"
    177 			uptime=$(ssh -q "${node}" "uptime -p | cut -b4- | sed 's/\(,\|ear\|eek\(s\)\|ay\(s\)\|our\(s\)\|inute\(s\)\)//g'")
    178 			echo -e "uptime is ${uptime}"
    179 			echo
    180 		done
    181 	else
    182 		heading "${server}"
    183 		uptime=$(ssh -q "${server}" "uptime -p | cut -b4- | sed 's/\(,\|ear\|eek(s)\|ay(s)\|our(s)\|inute(s)\)//g'")
    184 
    185 		echo -e "uptime is ${uptime}"
    186 		echo
    187 	fi
    188 }
    189 
    190 main() {
    191 # 	echo -e "${blue}│${magenta} ┳━┓┳ ┳┏━┓┓ ┳
    192 #${blue}│${magenta} ┃━┫┃━┫┃ ┃┗┏┛
    193 #${blue}│${magenta} ┛ ┇┇ ┻┛━┛ ┇
    194 #${blue}└──┄┄────┄┄───┐${yellow}
    195 #  $(whoami) ${blue}│
    196 #┌──┄┄────┄┄───┘${reset}"
    197 #${lighter_black}┊ ${light_blue}██    ▄  █ ████▄ ▀▄    ▄ ▄
    198 #${lighter_black}┊ ${light_blue}█ █  █   █ █   █   █  █ █
    199 #${lighter_black}│ ${light_blue}▓▄▄█ ▓▓▀▀█ ▓   █    ▀█ █
    200 #${lighter_black}┊ ${light_blue}▓  ▒ ▓   ▓ ▀▓▓▒▒    ▓  ▒
    201 #${lighter_black}│ ${light_blue}   ▒    ▒         ▄▀
    202 #${lighter_black}├───${light_blue}▒${lighter_black}────${light_blue}▀${lighter_black}┄┄ ${light_yellow}$(whoami)${light_blue} ▀
    203 #${lighter_black}│ ${light_blue} ▀${reset}"
    204 	echo -e "
    205 ${lighter_black}┊ ${light_blue} ██  █ █ ██  █ █ ${reset}${light_black}   ▄█████▄
    206 ${lighter_black}┊ ${light_blue} █ █ █ █ █ █ █ █ ${reset}${light_black}  ▄███████▄
    207 ${lighter_black}│ ${light_blue} ▓ █ ▓ █ ▓ █ ▓ █ ${reset}${light_black} ░██ ░█ ░██
    208 ${lighter_black}┊ ${light_blue} ▓▓▓ ▓▓▓ ▓ ▓  ▓  ${reset}${light_black} ░░███████
    209 ${lighter_black}│ ${light_blue} ▒ ▒ ▒ ▒ ▒▒▒  ▒  ${reset}${light_black}  ░░█░█░█
    210 ${lighter_black}├────${light_blue}▒${lighter_black}─${light_blue}▒${lighter_black}┄ ${light_yellow}$(whoami) ${reset}${light_black}░ ░ ░
    211 ${lighter_black}│    ${light_blue}▒ ▒${reset}"
    212 #     ┳━┓┳ ┳┏━┓┓ ┳
    213 #     ┃━┫┃━┫┃ ┃┗┏┛
    214 #     ┛ ┇┇ ┻┛━┛ ┇
    215 #      ┳━┓┳ ┳┏━┓o
    216 #      ┃━┫┃━┫┃ ┃┃
    217 #      ┛ ┇┇ ┻┛━┛┇
    218 #	 かいぞく
    219 # ░█▀█░█░█░█▀█░█░█░
    220 # ░█▀█░█▀█░█░█░░█░░
    221 # ░▀░▀░▀░▀░▀▀▀░░▀░░
    222 #
    223 	#echo -e "  greetings, ${white}\033[7m $(whoami) ${reset}\n"
    224 
    225 	tasking
    226 	echo -e "${lighter_black}│${reset}"
    227 	backups
    228 	echo -e "${lighter_black}│${reset}"
    229 	weather
    230 	echo -e "${lighter_black}│${reset}"
    231 	mail
    232 	echo -e "${lighter_black}│${reset}"
    233 	rss
    234 	echo -e "${lighter_black}└──┄┄────┄┄${reset}"
    235 }
    236 
    237 if [ $# -eq 0 ] ; then
    238 	main
    239 else
    240 	while getopts ":bhmtrws:" opt ; do
    241 		case ${opt} in
    242 			b) backups ;;
    243 			h) usage ;;
    244 			m) mail ;;
    245 			t) display_tasks=1 ; tasking ;;
    246 			r) rss ;;
    247 			w) weather ;;
    248 			s) servers "${OPTARG}" ;;
    249 			*) usage ;;
    250 		esac
    251 	done
    252 fi