-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt_upsc_script.sh
53 lines (43 loc) · 1.82 KB
/
mqtt_upsc_script.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
################################################################################
# This script retrieves UPS (Uninterruptible Power Supply) information using the
# upsc command, converts it to JSON format, and publishes it to an MQTT broker.
#
# Usage: ./mqtt_upsc_script.sh <device_name>
#
# The script requires the upsc command to be installed, and it uses mosquitto_pub
# to publish the JSON output to an MQTT broker. Configuration details are kept
# in the 'config.sh' file. Make sure to set the appropriate MQTT broker settings
# in the 'config.sh' file before running the script.
################################################################################
# Set environment variable for the directory
UPS_JSON_PUBLISHER_DIR="/root/ups-json-publisher"
# Check if the script was called with a device name as an argument
if [ $# -ne 1 ]; then
echo "Usage: $0 <device_name>"
exit 1
fi
# Extract device name from the argument
device_name=$1
# Execute the `upsc` command and store the output in a variable
upsc_output=$(upsc "$device_name" 2>/dev/null)
# Check if the device is reachable
if [ -z "$upsc_output" ]; then
echo "The device '$device_name' could not be reached."
exit 1
fi
# Convert output to JSON format
json_output="{"
while IFS=: read -r key value; do
key=$(echo "$key" | sed 's/^[ \t]*//;s/[ \t]*$//')
value=$(echo "$value" | sed 's/^[ \t]*//;s/[ \t]*$//')
json_output+="\"$key\":\"$value\","
done <<< "$upsc_output"
json_output="${json_output%,}" # Remove the trailing comma
json_output+="}"
# Display JSON output
echo "$json_output"
# Load configuration variables
source "$UPS_JSON_PUBLISHER_DIR/config.sh"
# Publish JSON output to MQTT broker
echo "$json_output" | mosquitto_pub -h "$mqtt_broker" -p "$mqtt_port" -u "$mqtt_username" -P "$mqtt_password" -i "$mqtt_client_id" -t "$mqtt_topic" -l