From 37699c642778bc0d4d920bbf6c5688e26c1a967f Mon Sep 17 00:00:00 2001 From: Pavel Muhortov Date: Wed, 13 Dec 2023 01:24:40 +0300 Subject: [PATCH] added ds18b20-over-ssh.sh --- README.md | 78 +++++++++++++++++++++++++++++++++++++++++---- ds18b20-over-ssh.sh | 61 +++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 ds18b20-over-ssh.sh diff --git a/README.md b/README.md index 3ab13a5..372b284 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Small tools needed to solve immediate tasks independently or as part of a projec * [`build-python`.sh](https://git.hmp.today/pavel.muhortov/utils#build-python-sh) * [`cert-info`.sh](https://git.hmp.today/pavel.muhortov/utils#cert-info-sh) +* [`ds18b20-over-ssh`.sh](https://git.hmp.today/pavel.muhortov/utils#ds18b20-over-ssh-sh) * [`sendmail`.py](https://git.hmp.today/pavel.muhortov/utils#sendmail-py) * [`simplewc`.py](https://git.hmp.today/pavel.muhortov/utils#simplewc-py) @@ -75,7 +76,7 @@ bash <(curl -s https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/cert bash <(wget -qO- https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/cert-info.sh) www.hmp.today ``` -### `Example usage in Zabbix` +### `Example usage cert-info.sh in Zabbix` ```bash # download @@ -92,19 +93,82 @@ sudo chmod +x /usr/lib/zabbix/externalscripts/cert-info.sh |Execute on |Zabbix server (proxy)| |Commands |/usr/lib/zabbix/externalscripts/cert-info.sh {$URL}| |Description|Load certificate info from {$URL} host macro| -`Add` `Zabbix Server` - `Configuration` - `Hosts` - `"hostname"` - `Macros` - `Add` -| PARAMETER | VALUE | -|-----------|--------------| -|Macro |{$URL}| -|Value |hostname:port| -`Update` +| MACRO | VALUE | +|-------|--------------| +|{$URL} |hostname:port | `Zabbix Server` - `Monitoring` - `Hosts` - `"hostname"` - `Check SSL certificate` ____ +## `ds18b20-over-ssh`.sh + +**Description:** +> Get ds18b20 sensor value. + +**Dependencies:** +> +> * [bash](https://www.gnu.org/software/bash/) (tested versions: 5.1.16 on [Ubuntu 22.04](https://wiki.ubuntu.com/JammyJellyfish/ReleaseNotes)) +> * [sshpass](https://sourceforge.net/projects/sshpass/) (tested version 1.0.9 on [Ubuntu 22.04](https://wiki.ubuntu.com/JammyJellyfish/ReleaseNotes)) + +| POSITION | PARAMETERS | DESCRIPTION | DEFAULT | +|-----------|--------------|------------------------|---------------| +| 1 |**[host]**|ssh host|**REQUIRED**| +| 2 |**[port]**|ssh port|**REQUIRED**| +| 3 |**[user]**|ssh username|**REQUIRED**| +| 4 |**[pass]**|ssh password|**REQUIRED**| +| 5 |**[node]**|ds18b20 node|**REQUIRED**| + +### `Example usage ds18b20-over-ssh.sh in terminal with make the script executable` + +```bash +# download +sudo wget https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/ds18b20-over-ssh.sh -O /usr/local/bin/ds18b20-over-ssh.sh +sudo chmod +x /usr/local/bin/ds18b20-over-ssh.sh +# check ds18b20 value +ds18b20-over-ssh.sh host.domain.zone 22 root password 28-0a1b2c3d4e5f +``` + +### `Example usage ds18b20-over-ssh.sh in terminal without download` + +```bash +bash <(curl -s https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/ds18b20-over-ssh.sh) host.domain.zone 22 root password 28-0a1b2c3d4e5f +bash <(wget -qO- https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/ds18b20-over-ssh.sh) host.domain.zone 22 root password 28-0a1b2c3d4e5f +``` + +### `Example usage ds18b20-over-ssh.sh in Zabbix` + +```bash +# download +sudo wget https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/ds18b20-over-ssh.sh -O /usr/lib/zabbix/externalscripts/ds18b20-over-ssh.sh +sudo chmod +x /usr/lib/zabbix/externalscripts/ds18b20-over-ssh.sh +``` + +`Zabbix Server` - `Administration` - `Scripts` - `Create scripts` +| PARAMETER | VALUE | +|-----------|--------------| +|Name |Check ds18b20 over SSH| +|Scope |Manual host action| +|Type |Script| +|Execute on |Zabbix server (proxy)| +|Commands |/usr/lib/zabbix/externalscripts/ds18b20-over-ssh.sh {$HOST} {$PORT} {$USER} {$PASS} {$NODE}| +|Description|Get temperature from {$NODE} sensor over SSH| + +`Zabbix Server` - `Configuration` - `Hosts` - `"hostname"` - `Macros` - `Add` +| MACRO | VALUE | +|-------|-----------------| +|{$HOST}|host.domain.zone | +|{$PORT}|22 | +|{$USER}|root | +|{$PASS}|password | +|{$NODE}|28-0a1b2c3d4e5f | + +`Zabbix Server` - `Monitoring` - `Hosts` - `"hostname"` - `Check ds18b20 over SSH` + +____ + ## `sendmail`.py **Description:** diff --git a/ds18b20-over-ssh.sh b/ds18b20-over-ssh.sh new file mode 100644 index 0000000..0d5a470 --- /dev/null +++ b/ds18b20-over-ssh.sh @@ -0,0 +1,61 @@ +#!/usr/bin/env bash + +# DESCRIPTION: +# get ds18b20 sensor value +# +# DEPENDENCIES: +# - sshpass +# +# PARAMETERS: +# 1: ssh host +# 2: ssh port +# 3: ssh username +# 4: ssh password +# 5: ds18b20 node +# +# FUNCTIONS: +# +# +# VARIABLES: +# + +if [ -z "${1}" ] || \ + [ -z "${2}" ] || \ + [ -z "${3}" ] || \ + [ -z "${4}" ] || \ + [ -z "${5}" ]; then + printf "%s\n" \ + "Usage example: ${0} 'host' 'port' 'username' 'password' 'node'" + + exit 1 +fi + +if ! command -v sshpass &> /dev/null; then + printf "%s\n" "Not found dependencies" + + exit 1 +fi + +host=${1} +port=${2} +user=${3} +pass=${4} +node=${5} + +# +# MAIN: +# + +temp=$(sshpass -p "${pass}" ssh "${user}@${host}" -p "${port}" \ + -o "StrictHostKeyChecking=no" \ + -o "UserKnownHostsFile=/dev/null" \ + -o "LogLevel=ERROR" \ + "cat /sys/bus/w1/devices/${node}/temperature" + ) +if [ "${temp}" -lt 0 ]; then + printf "%s\n" "$(("${temp}" / 1000)).$((-1 * "${temp}" % 1000))" +else + printf "%s\n" "$(("${temp}" / 1000)).$(("${temp}" % 1000))" +fi + +exit 0