From 08385e02fbdec70c245be29661f7fa7d2f6a9c5f Mon Sep 17 00:00:00 2001 From: jonas Date: Sat, 1 Apr 2023 02:08:39 +0200 Subject: [PATCH] first commit --- .env.example | 4 +++ .gitignore | 1 + README.md | 19 ++++++++++++ inc/bash-colors.sh | 6 ++++ inc/main-inc.sh | 38 +++++++++++++++++++++++ inc/output.sh | 42 ++++++++++++++++++++++++++ inc/sensor-scripts/iperf3.sh | 55 ++++++++++++++++++++++++++++++++++ inc/sensor-scripts/ping.sh | 37 +++++++++++++++++++++++ inc/sensor-scripts/tcp-port.sh | 26 ++++++++++++++++ main.sh | 40 +++++++++++++++++++++++++ sensors.d/00-example.sh | 9 ++++++ sensors.d/iperf-example.sh | 3 ++ sensors.d/ping-1.1.1.1.sh | 3 ++ sensors.d/ping-8.8.8.8.sh | 3 ++ sensors.d/ping-router.sh | 3 ++ sensors.d/tcp-example.sh | 4 +++ 16 files changed, 293 insertions(+) create mode 100644 .env.example create mode 100644 .gitignore create mode 100644 README.md create mode 100644 inc/bash-colors.sh create mode 100644 inc/main-inc.sh create mode 100644 inc/output.sh create mode 100644 inc/sensor-scripts/iperf3.sh create mode 100644 inc/sensor-scripts/ping.sh create mode 100644 inc/sensor-scripts/tcp-port.sh create mode 100644 main.sh create mode 100644 sensors.d/00-example.sh create mode 100644 sensors.d/iperf-example.sh create mode 100644 sensors.d/ping-1.1.1.1.sh create mode 100644 sensors.d/ping-8.8.8.8.sh create mode 100644 sensors.d/ping-router.sh create mode 100644 sensors.d/tcp-example.sh 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 [