commit 08385e02fbdec70c245be29661f7fa7d2f6a9c5f Author: jonas Date: Sat Apr 1 02:08:39 2023 +0200 first commit diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..5600164 --- /dev/null +++ b/.env.example @@ -0,0 +1,4 @@ +INFLUX_SERVER=127.0.0.1 +INFLUX_ORG=my-org +INFLUX_BUCKET=customer-xyz +INFLUX_TOKEN="" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2eea525 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..43d488b --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Bash based logging Script for InfluxDB + +## Short description: +This script logs various inputs and sensors to test reachability and read out +data from devices on the network. The data will be stored in an InfluxDB for +processing in other applications. + +## Reasonable features to include: +- Modular design with sensors being run from a sensors.d directory +- Common functions and variables passed to the sensors for ease of programming +- Sensor types: + - [x] Ping (int, latency) + - [x] TCP Port (Bool, open or not) + - [ ] SSH command (int, return code) + - [ ] SSH command (string, output) + - [ ] Speedtest Internet (int, Download Bandwith in Mb/s) + - [ ] Speedtest Internet (int, Upload Bandwidth in Mb/s) + - [x] Speedtest IPerf (int, Download Bandwidth in Mb/s) + - [x] Speedtest IPerf (int, Upload Bandwith in Mb/s) \ No newline at end of file diff --git a/inc/bash-colors.sh b/inc/bash-colors.sh new file mode 100644 index 0000000..6c73acf --- /dev/null +++ b/inc/bash-colors.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +BG_RED="\e[41m\e[97m" +FG_BLUE="\e[34m" +FG_YELLOW="\e[33m" +C_RESET="\e[0m" \ No newline at end of file diff --git a/inc/main-inc.sh b/inc/main-inc.sh new file mode 100644 index 0000000..7a8fbb6 --- /dev/null +++ b/inc/main-inc.sh @@ -0,0 +1,38 @@ +#!/bin/bash +pushd $(dirname ${BASH_SOURCE[0]}) >/dev/null + +# Include configuration +source ../.env +export INFLUX_TOKEN + +# Include other functions +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}" + done +fi + +# Test if influxdb2 CLI is installed +if ! which influx >/dev/null; then + logError "Influx CLI not found!" + exit 1 +fi + +# Write data to influx DB +function writeData { + MEASURE="$1" + TAGS="$2" + FIELDS="$3" + + influx write \ + -b ${INFLUX_BUCKET} \ + -o ${INFLUX_ORG} \ + -p s \ + "${MEASURE},${TAGS} ${FIELDS}" +} + +popd >/dev/null \ No newline at end of file diff --git a/inc/output.sh b/inc/output.sh new file mode 100644 index 0000000..034d824 --- /dev/null +++ b/inc/output.sh @@ -0,0 +1,42 @@ +#!/bin/bash +source "bash-colors.sh" + +# Echo formatted time right now +function _now { + date +"%y/%m/%d %H:%M:%S" +} + +# Write log to stdout +function log { + echo "[ $(_now) :: INFO ] $@" +} + +# Log with custom tag +function logAdv { + TAG="${1}" + shift + + 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 ] ${@}" + fi +} + +# Write error to stderr +function logError { + echo -e "${BG_RED}[ $(_now) :: FAIL ] ${@}${C_RESET}" >&2 +} + +# Write pipe input to stdout +function logPipe { + PREFIX="PIPE" + [ "${1}" = "" ] || PREFIX="${@}" + + while read -r LINE; do + echo -e "${FG_YELLOW}[ $(_now) :: ${PREFIX} ] ${LINE}${C_RESET}" + done +} \ No newline at end of file diff --git a/inc/sensor-scripts/iperf3.sh b/inc/sensor-scripts/iperf3.sh new file mode 100644 index 0000000..65d27ed --- /dev/null +++ b/inc/sensor-scripts/iperf3.sh @@ -0,0 +1,55 @@ +#!/bin/bash +SCRIPT_NAME=$(basename ${BASH_SOURCE[0]}) + +if ! which jq >/dev/null; then + logError "${SCRIPT_NAME}: Did not find jq in path!" + return +elif ! which iperf3 >/dev/null; then + logError "${SCRIPT_NAME}: Did not find iperf3 in path!" + return +fi + +# Usage: doIperfTest [