reworked log and logError for pipes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
FG_RED="\e[31m"
|
||||
BG_RED="\e[41m\e[97m"
|
||||
FG_BLUE="\e[34m"
|
||||
FG_YELLOW="\e[33m"
|
||||
|
||||
@@ -10,9 +10,22 @@ source output.sh
|
||||
|
||||
# Include all sensor scripts
|
||||
if [ -d "sensor-scripts" ]; then
|
||||
for script in $(ls sensor-scripts/*.sh 2>/dev/null); do
|
||||
log "Loading sensor function $(basename ${script} .sh)"
|
||||
source "${script}"
|
||||
for SCRIPT in $(ls sensor-scripts/*.sh 2>/dev/null); do
|
||||
log "Loading sensor function $(basename ${SCRIPT} .sh)"
|
||||
|
||||
TMP_LOG=$(mktemp /tmp/tmp.sf-XXXXXXXX.log)
|
||||
source "${SCRIPT}" >${TMP_LOG} 2>&1
|
||||
|
||||
if [ $? -ne 0 ] ; then
|
||||
cat ${TMP_LOG} | logError
|
||||
logError "An error occured loading sensor function $(basename ${SCRIPT} .sh)"
|
||||
|
||||
rm ${TMP_LOG}
|
||||
exit 1
|
||||
else
|
||||
cat ${TMP_LOG} | log
|
||||
rm ${TMP_LOG}
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
@@ -7,36 +7,78 @@ function _now {
|
||||
}
|
||||
|
||||
# Write log to stdout
|
||||
# Usage: log <text>
|
||||
# Usage with tag: log -t <tag> <text>
|
||||
# Usage with pipe: log -p [<tag>]
|
||||
function log {
|
||||
echo "[ $(_now) :: INFO ] $@"
|
||||
}
|
||||
if ! [ "${1}" ] || [ "${1}" = "-p" ]; then
|
||||
# Read from pipe
|
||||
PREFIX="PIPE"
|
||||
[ "${2}" = "" ] || PREFIX="${2}"
|
||||
|
||||
# Log with custom tag
|
||||
function logAdv {
|
||||
TAG="${1}"
|
||||
shift
|
||||
while read -r LINE; do
|
||||
echo -e "${FG_YELLOW}[ $(_now) :: ${PREFIX} ] ${LINE}${C_RESET}"
|
||||
done
|
||||
else
|
||||
# Read from arguments
|
||||
PREFIX="INFO"
|
||||
[ "${1}" = "-t" ] && ! [ "${2}" = "" ] && PREFIX="${2}"
|
||||
shift 2
|
||||
|
||||
echo -e "${FG_BLUE}[ $(_now) :: ${TAG} ] ${@}${C_RESET}"
|
||||
}
|
||||
|
||||
# Log tagged for sensor scripts, can be muted with main.sh -qs
|
||||
function logScript {
|
||||
if ! [ "${SCRIPTS_QUIET}" = "yes" ]; then
|
||||
echo -e "[ $(_now) :: SENS ] ${@}"
|
||||
echo "${@}" | while read -r LINE; do
|
||||
echo "[ $(_now) :: ${PREFIX} ] ${LINE}"
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
# Write error to stderr
|
||||
# Write log to stderr
|
||||
# Usage: logError <text>
|
||||
# Usage with tag: logError -t <tag> <text>
|
||||
# Usage with pipe: logError -p [<tag>]
|
||||
function logError {
|
||||
echo -e "${BG_RED}[ $(_now) :: FAIL ] ${@}${C_RESET}" >&2
|
||||
if ! [ "${1}" ] || [ "${1}" = "-p" ]; then
|
||||
# Read from pipe
|
||||
PREFIX="FAIL"
|
||||
[ "${2}" = "" ] || PREFIX="${2}"
|
||||
|
||||
while read -r LINE; do
|
||||
echo -e "${BG_RED}[ $(_now) :: ${PREFIX} ] ${LINE}${C_RESET}" >&2
|
||||
done
|
||||
else
|
||||
# Read from arguments
|
||||
PREFIX="FAIL"
|
||||
[ "${1}" = "-t" ] && ! [ "${2}" = "" ] && PREFIX="${2}"
|
||||
shift 2
|
||||
|
||||
echo "${@}" | while read -r LINE; do
|
||||
echo -e "${BG_RED}[ $(_now) :: ${PREFIX} ] ${LINE}${C_RESET}" >&2
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
# Write pipe input to stdout
|
||||
function logPipe {
|
||||
PREFIX="PIPE"
|
||||
[ "${1}" = "" ] || PREFIX="${@}"
|
||||
# Log tagged for sensor scripts, can be muted with the -qs arg on main.sh
|
||||
# Usage: logScript <text>
|
||||
# Usage with tag: logScript -t <tag> <text>
|
||||
# Usage with pipe: logScript -p [<tag>]
|
||||
function logScript {
|
||||
if ! [ "${SCRIPTS_QUIET}" = "yes" ]; then
|
||||
if ! [ "${1}" ] || [ "${1}" = "-p" ]; then
|
||||
# Read from pipe
|
||||
PREFIX="SENS"
|
||||
[ "${2}" = "" ] || PREFIX="${2}"
|
||||
|
||||
while read -r LINE; do
|
||||
echo -e "${FG_YELLOW}[ $(_now) :: ${PREFIX} ] ${LINE}${C_RESET}"
|
||||
done
|
||||
while read -r LINE; do
|
||||
echo -e "[ $(_now) :: ${PREFIX} ] ${LINE}"
|
||||
done
|
||||
else
|
||||
# Read from arguments
|
||||
PREFIX="SENS"
|
||||
[ "${1}" = "-t" ] && ! [ "${2}" = "" ] && PREFIX="${2}"
|
||||
shift 2
|
||||
|
||||
echo "${@}" | while read -r LINE; do
|
||||
echo "[ $(_now) :: ${PREFIX} ] ${LINE}"
|
||||
done
|
||||
fi
|
||||
fi
|
||||
}
|
||||
25
inc/sensor-scripts/speedtest.sh
Normal file
25
inc/sensor-scripts/speedtest.sh
Normal file
@@ -0,0 +1,25 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Usage: doSpeedtest [<interface>] [<speedtest-server-id>]
|
||||
function doSpeedtest {
|
||||
if ! [ "${1}" = "" ]; then
|
||||
INTERFACE=${1}
|
||||
INTERFACE_ARG="-I ${INTERFACE}"
|
||||
INTERFACE_TEXT=" from NIC '${INTERFACE}'"
|
||||
fi
|
||||
|
||||
if ! [ "${2}" = "" ]; then
|
||||
SERVER_ID=${2}
|
||||
SERVER_ARG="-s ${SERVER_ID}"
|
||||
SERVER_ID_TEXT=" to server ${SERVER_ID}"
|
||||
fi
|
||||
|
||||
logScript "Doing internet speedtest${INTERFACE_TEXT}${SERVER_ID_TEXT}"
|
||||
|
||||
if ! OUT=$(speedtest -f json); then
|
||||
logError "Speedtest failed"
|
||||
logError "${OUT}"
|
||||
else
|
||||
echo
|
||||
fi
|
||||
}
|
||||
Reference in New Issue
Block a user