From 70473782b9a3c09d70418b80cb89871dd5fab857 Mon Sep 17 00:00:00 2001 From: jonas Date: Sat, 1 Apr 2023 16:51:07 +0200 Subject: [PATCH] Updates feature list, added ssh exitcode sensor --- README.md | 14 ++++++-------- inc/sensor-scripts/ssh.sh | 28 ++++++++++++++++++++++++++++ sensors.d/ssh-test.sh | 3 +++ 3 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 inc/sensor-scripts/ssh.sh create mode 100644 sensors.d/ssh-test.sh diff --git a/README.md b/README.md index 58f0a1b..7dabeed 100644 --- a/README.md +++ b/README.md @@ -9,14 +9,12 @@ processing in other applications. - 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) - - [x] Speedtest Internet (int, Download Bandwith in Mb/s) - - [x] 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) + - [x] Ping + - [x] TCP Port open + - [x] SSH command (exit code) + - [ ] ~~SSH command (output)~~ + - [x] Speedtest Internet + - [x] Speedtest IPerf3 ## Important ToDos: - [ ] Add systemd file diff --git a/inc/sensor-scripts/ssh.sh b/inc/sensor-scripts/ssh.sh new file mode 100644 index 0000000..095a64f --- /dev/null +++ b/inc/sensor-scripts/ssh.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +# Usage: doSshExitCode [] +function doSshExitCodeTest { + COMMAND_NAME="${1}" + CONNECTION_STRING="${2}" + + if [ $# -eq 3 ]; then + SSH_COMMAND="${3}" + else + SSH_EXTRA_ARGS="${3}" + SSH_COMMAND="${4}" + fi + + ssh -q \ + -o BatchMode=yes \ + -o StrictHostKeyChecking=no \ + ${CONNECTION_STRING} \ + ${SSH_EXTRA_ARGS} \ + "${SSH_COMMAND}" | \ + while read -r LINE; do :; done + EXIT_CODE=${PIPESTATUS[0]} + + writeData \ + ssh-exitcode \ + "sourceHost=$(hostname -f),targetHost=${CONNECTION_STRING}" \ + "exit_code=${EXIT_CODE},command_name=\"${COMMAND_NAME}\",last_line=\"${LINE}\"" +} \ No newline at end of file diff --git a/sensors.d/ssh-test.sh b/sensors.d/ssh-test.sh new file mode 100644 index 0000000..4c4dae2 --- /dev/null +++ b/sensors.d/ssh-test.sh @@ -0,0 +1,3 @@ +INTERVAL=180 + +doSshExitCodeTest "example-ssh-test" "user@host" "-i ~/.ssh/id_rsa" "/opt/script/do-something.sh" \ No newline at end of file