You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.5 KiB

#!/bin/bash
# Enter your zipcode here for local weather.
zipcode=20001
# Add the services you want to monitor here as an array
servicelist=('apache2' 'gitea' 'postfix')
# Colors are nice
blk=$(tput setaf 0)
red=$(tput setaf 1)
grn=$(tput setaf 2)
ylw=$(tput setaf 3)
blu=$(tput setaf 4)
cyn=$(tput setaf 6)
wht=$(tput setaf 7)
und=$(tput smul)
bld=$(tput bold)
# Uncoloring is important
rst=$(tput sgr0)
# Get the number of columns and rows to center the
# last updated time on the bottom of the window.
cols=$(tput cols)
rows=$(tput lines)
# trap ctrl-c and call ctrl_c()
# https://rimuhosting.com/knowledgebase/linux/misc/trapping-ctrl-c-in-bash
trap ctrl_c INT
function ctrl_c() {
#clear
tput cnorm
echo "${rst}${red}CTRL+C Detected, exiting.${rst}"
exit
}
function dothething() {
# Grab the size of the window again as the window size
# may have changed since the script was started.
cols=$(tput cols)
rows=$(tput lines)
clear
updatedstring="Last updated: `date \"+%F %X\"`"
# Sometimes this may not complete due to wttr.in running
# out of available queries to their weather provider.
# This should only last for a day or so.
curl wttr.in/${zipcode}?1QnuFA
# Make sure the updated line is at the bottom of the screen
down=$rows
# Do some math to center the string horizontally
let over="$cols/2-${#updatedstring}/2"
# Move the cursor over to the correct starting location
tput cup $down $over
# Print the last time the weather was updated
echo -n "${rst}${ylw}${updatedstring}${rst}"
# Reset the cursor back to 0,0
tput cup 0 0
}
# This be where we check the services
function servicecheck(){
# If we're not monitoring any services, return
[ ${#servicelist[@]} -eq 0 ] && return
# These are the allowed rows
# TODO? determine lines from after weather print?
servicerow=(15 16 17 18 19)
# These are the allowed columns
# TODO? allow column count instead of static assignments?
servicecol=(1 25 50)
# Loop over the number of services that we're monitoring
# https://askubuntu.com/a/1340735
for index in $(seq 0 $((${#servicelist[@]}-1))); do
# Determine the position of the current service
# we go across and then down
# TODO? allow for variable columns?
let echocol="${servicecol[${index}%3]}"
let echorow="${servicerow[${index}/3]}"
# Put our cursor where we can print
tput cup $echorow $echocol
# Clear the variables we use
unset echocol echorow
# Do the actual service check. is-active returns
# active or inactive. Easy peasy
servicestatus=`systemctl is-active ${servicelist[${index}]}`
# Print out the status, green with a dot (\U25CF) if it's online
# and red with a square (\U25A0) if it's stopped
[ "${servicestatus}" == "active" ] && echo -en "${grn}\U25CF ${servicelist[${index}]}${rst}" || echo -en "${red}\U25A0 ${servicelist[${index}]}${rst}"
done
tput cup 0 0
}
# Clear the screen and let's get going
clear
# Make the cursor invisible for the terminal
tput civis
# Programmers are great at naming things
dothething
# Loop forever. Use ctrl+c to exit.
while true; do
# We want to make sure we only check once per hour
# in order to not overwhelm the wttr.in service.
# Octal fix: https://stackoverflow.com/a/24777667
if [ `date +%M` -eq "00" ]; then
# Don't forget to check the seconds or it'll run
# the command every second of the 0th minute.
if [ `date +%S` -eq "00" ]; then
dothething
fi
fi
# I don't think checking every second is a problem... yet...
servicecheck
# Sleep for a second so we're not hammering the system
# with date checks.
sleep 1
done