Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE][Alert] Add support for MQTT #3560

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lgsm/config-default/config-lgsm/rustserver/_default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ telegramtoken="accesstoken"
telegramchatid=""
curlcustomstring=""

mqttalert="off"
mqtthost="mqtt.example.com"
mqttport="1883"
mqttuser="someuser"
mqttpassword="somepassword"
mqtttopic="/lgsm/rustserver"

## Updating | https://docs.linuxgsm.com/commands/update
updateonstart="off"

Expand Down
22 changes: 22 additions & 0 deletions lgsm/functions/alert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,25 @@ elif [ -z "${telegramchatid}" ]&&[ "${function_selfname}" == "command_test_alert
echo " * https://docs.linuxgsm.com/alerts/telegram"
fn_script_error "Telegram chat id not set."
fi

if [ "${mqttalert}" == "on" ]&&[ -n "${mqtthost}" ]; then
alert_mqtt.sh
elif [ "${mqttalert}" != "on" ]&&[ -n "${function_selfname}" == "command_test_alert.sh"]; then
fn_print_warn_nl "MQTT Messages not enabled"
fn_script_log_warn "MQTT Messages not enabled"
elif [ -z "${mqtthost}" ]&&[ "${function_selfname}" == "command_test_alert.sh" ]; then
fn_print_error_nl "MQTT host not set"
fn_script_error "MQTT host not set"
elif [ -z "${mqttport}" ]&&[ "${function_selfname}" == "command_test_alert.sh" ]; then
fn_print_error_nl "MQTT port not set"
fn_script_error "MQTT port not set"
elif [ -z "${mqttuser}" ]&&[ "${function_selfname}" == "command_test_alert.sh" ]; then
fn_print_error_nl "MQTT User not set"
fn_script_error "MQTT User not set"
elif [ -z "${mqttpassword}" ]&&[ "${function_selfname}" == "command_test_alert.sh" ]; then
fn_print_error_nl "MQTT Password not set"
fn_script_error "MQTT Password not set"
elif [ -z "${mqtttopic}" ]&&[ "${function_selfname}" == "command_test_alert.sh" ]; then
fn_print_error_nl "MQTT Topic not set"
fn_script_error "MQTT Topic not set"
fi
30 changes: 30 additions & 0 deletions lgsm/functions/alert_mqtt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
# LinuxGSM alert_ifttt.sh function
# Author: Daniel Gibbs
# Website: https://linuxgsm.com
# Description: Sends IFTTT alert.

local commandname="ALERT"
local commandaction="Alert"
local function_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"

json=$(cat <<EOF
{
"value1": "${servicename}",
"value2": "${alertsubject}",
"value3": "Message\n${alertbody}\n\nGame\n${gamename}\n\nServer name\n${servername}\n\nHostname\n${HOSTNAME}\n\nServer IP\n${alertip}:${port}\n\nMore info\n${alerturl}"
}
EOF
)

fn_print_dots "Sending MQTT alert"
#iftttsend=$(${curlpath} -sSL -H "Content-Type: application/json" -X POST -d """${json}""" "https://maker.ifttt.com/trigger/${iftttevent}/with/key/${ifttttoken}" | grep "Bad Request")
mqttsend=$(${mqttpubpath} -h "${mqtthost}" -p "${mqttport}" -P "${mqttpassword}" -u "${mqttuser}" -t "${mqtttopic}" -m "${json}") | grep "Bad Request"

if [ -n "${mqttsend}" ]; then
fn_print_fail_nl "Sending MQTT alert: ${pushbulletsend}"
fn_script_log_fatal "Sending MQTT alert: ${pushbulletsend}"
else
fn_print_ok_nl "Sending MQTT alert"
fn_script_log_pass "Sent MQTT alert"
fi
8 changes: 7 additions & 1 deletion lgsm/functions/core_dl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,10 @@ curlpath=$(command -v curl 2>/dev/null)
if [ "$(basename "${curlpath}")" != "curl" ]; then
echo "[ FAIL ] Curl is not installed"
exit 1
fi
fi

#Defines mqttpubpath path.
mqttpubpath=$(command -v mosquitto_pub2>/dev/null)
if [ "$(basename "${mqttpubpath}")" != "mosquitto_pub" ]; then
echo "[ INFO ] mosquitto_pub is not installed. MQTT alerts not available"
fi
5 changes: 5 additions & 0 deletions lgsm/functions/core_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ functionfile="${FUNCNAME}"
fn_fetch_function
}

alert_mqtt.sh(){
functionfile="${FUNCNAME}"
fn_fetch_function
}

# Logs

logs.sh(){
Expand Down