Browse Source

refactor(alert): refactor alert functions

- Refactored the alert functions to improve code readability and maintainability.
- Renamed variables for better clarity and consistency.
- Updated function names to be more descriptive of their purpose.
- Reorganized the order of function definitions for better organization.
pull/4345/head
Daniel Gibbs 2 years ago
parent
commit
00a6b222dc
No known key found for this signature in database GPG Key ID: 2076B128385E8C55
  1. 87
      lgsm/modules/alert.sh
  2. 8
      lgsm/modules/alert_discord.sh
  3. 4
      lgsm/modules/alert_email.sh
  4. 4
      lgsm/modules/alert_gotify.sh
  5. 4
      lgsm/modules/alert_ifttt.sh
  6. 4
      lgsm/modules/alert_pushbullet.sh
  7. 2
      lgsm/modules/alert_pushover.sh
  8. 2
      lgsm/modules/alert_rocketchat.sh
  9. 2
      lgsm/modules/alert_slack.sh
  10. 2
      lgsm/modules/alert_telegram.sh
  11. 2
      lgsm/modules/info_messages.sh

87
lgsm/modules/alert.sh

@ -28,12 +28,12 @@ fn_alert_log() {
} }
fn_alert_test() { fn_alert_test() {
fn_script_log_info "Sending test alert" fn_script_log_info "Sending alert: Testing LinuxGSM Alert. No action to be taken"
alertsubject="Alert - ${selfname} - Test" alerttitle="Alert - ${selfname} - Test"
alertemoji="🚧" alertemoji="🚧"
alertsound="1" alertsound="1"
alerturl="not enabled" alerturl="not enabled"
alertbody="Testing LinuxGSM Alert. No action to be taken." alertmessage="Testing LinuxGSM Alert. No action to be taken."
# Green # Green
alertcolourhex="#cdcd00" alertcolourhex="#cdcd00"
alertcolourdec="13487360" alertcolourdec="13487360"
@ -41,36 +41,36 @@ fn_alert_test() {
# Running command manually # Running command manually
fn_alert_stopped() { fn_alert_stopped() {
fn_script_log_info "Sending alert: Stopped" fn_script_log_info "Sending alert: ${selfname} has stopped"
alertsubject="Alert - ${selfname} - Stopped" alerttitle="Alert - ${selfname} - Stopped"
alertemoji="❌" alertemoji="❌"
alertsound="1" alertsound="1"
alerturl="not enabled" alerturl="not enabled"
alertbody="${selfname} has stopped" alertmessage="${selfname} has stopped."
# Red # Red
alertcolourhex="#cd0000" alertcolourhex="#cd0000"
alertcolourdec="13434880" alertcolourdec="13434880"
} }
fn_alert_started() { fn_alert_started() {
fn_script_log_info "Sending alert: Started" fn_script_log_info "Sending alert: ${selfname} has started"
alertsubject="Alert - ${selfname} - Started" alerttitle="Alert - ${selfname} - Started"
alertemoji="✅" alertemoji="✅"
alertsound="1" alertsound="1"
alerturl="not enabled" alerturl="not enabled"
alertbody="${selfname} has started" alertmessage="${selfname} has started."
# Green # Green
alertcolourhex="#00cd00" alertcolourhex="#00cd00"
alertcolourdec="52480" alertcolourdec="52480"
} }
fn_alert_restarted() { fn_alert_restarted() {
fn_script_log_info "Sending alert: Restarted" fn_script_log_info "Sending alert: ${selfname} has restarted"
alertsubject="Alert - ${selfname} - Restarted" alerttitle="Alert - ${selfname} - Restarted"
alertemoji="↺" alertemoji="↺"
alertsound="1" alertsound="1"
alerturl="not enabled" alerturl="not enabled"
alertbody="${selfname} has restarted" alertmessage="${selfname} has restarted"
# Green # Green
alertcolourhex="#00cd00" alertcolourhex="#00cd00"
alertcolourdec="52480" alertcolourdec="52480"
@ -78,24 +78,24 @@ fn_alert_restarted() {
# Failed monitor checks # Failed monitor checks
fn_alert_monitor_session() { fn_alert_monitor_session() {
fn_script_log_info "Sending alert: Restarted: ${executable} not running" fn_script_log_info "Sending alert: ${selfname} is not running. Game server has been restarted"
alertsubject="Alert - ${selfname} - Restarted" alerttitle="Alert - ${selfname} - Restarted"
alertemoji="🚨" alertemoji="🚨"
alertsound="2" alertsound="2"
alerturl="not enabled" alerturl="not enabled"
alertbody="${selfname} ${executable} not running" alertmessage="${selfname} is not running. Game server has been restarted."
# Red # Red
alertcolourhex="#cd0000" alertcolourhex="#cd0000"
alertcolourdec="13434880" alertcolourdec="13434880"
} }
fn_alert_monitor_query() { fn_alert_monitor_query() {
fn_script_log_info "Sending alert: Restarted: ${selfname}" fn_script_log_info "Sending alert: Unable to query ${selfname}. Game server has been restarted"
alertsubject="Alert - ${selfname} - Restarted" alerttitle="Alert - ${selfname} - Restarted"
alertemoji="🚨" alertemoji="🚨"
alertsound="2" alertsound="2"
alerturl="not enabled" alerturl="not enabled"
alertbody="Unable to query: ${selfname}" alertmessage="Unable to query ${selfname}. Game server has been restarted."
# Red # Red
alertcolourhex="#cd0000" alertcolourhex="#cd0000"
alertcolourdec="13434880" alertcolourdec="13434880"
@ -103,72 +103,85 @@ fn_alert_monitor_query() {
# Update alerts # Update alerts
fn_alert_update() { fn_alert_update() {
fn_script_log_info "Sending alert: Updated" fn_script_log_info "Sending alert: ${selfname} has received a game server update: ${localbuild}"
alertsubject="Alert - ${selfname} - Updated" alerttitle="Alert - ${selfname} - Updated"
alertemoji="🎮" alertemoji="🎉"
alertsound="1" alertsound="1"
alerturl="not enabled" alerturl="not enabled"
alertbody="${gamename} received update: ${remotebuildversion}" alertmessage="${selfname} has received a game server update: ${localbuild}."
# Green # Green
alertcolourhex="#00cd00" alertcolourhex="#00cd00"
alertcolourdec="52480" alertcolourdec="52480"
} }
fn_alert_check_update() { fn_alert_check_update() {
fn_script_log_info "Sending alert: Update available" fn_script_log_info "Sending alert: ${gamename} update available: ${remotebuildversion}"
alertsubject="Alert - ${selfname} - Update available" alerttitle="Alert - ${selfname} - Update available"
alertemoji="🎮" alertemoji="🎮"
alertsound="1" alertsound="1"
alerturl="not enabled" alerturl="not enabled"
alertbody="${gamename} update available: ${remotebuildversion}" alertmessage="${gamename} update available: ${remotebuildversion}"
# Blue # Blue
alertcolourhex="#1e90ff" alertcolourhex="#1e90ff"
alertcolourdec="2003199" alertcolourdec="2003199"
} }
fn_alert_update_linuxgsm() {
fn_script_log_info "Sending alert: ${selfname} has received an LinuxGSM update"
alerttitle="Alert - ${selfname} - LinuxGSM updated"
alertemoji="🎉"
alertsound="1"
alerturl="not enabled"
alertbody="${gamename} update available"
alertmessage="${selfname} has received an LinuxGSM update."
# Green
alertcolourhex="#00cd00"
alertcolourdec="52480"
}
fn_alert_permissions() { fn_alert_permissions() {
fn_script_log_info "Sending alert: Permissions error" fn_script_log_info "Sending alert: ${selfname} has permissions issues"
alertsubject="Alert - ${selfname}: Permissions error" alerttitle="Alert - ${selfname}: Permissions error"
alertemoji="❗" alertemoji="❗"
alertsound="2" alertsound="2"
alerturl="not enabled" alerturl="not enabled"
alertbody="${selfname} has permissions issues" alertmessage="${selfname} has permissions issues"
# Red # Red
alertcolourhex="#cd0000" alertcolourhex="#cd0000"
alertcolourdec="13434880" alertcolourdec="13434880"
} }
fn_alert_config() { fn_alert_config() {
fn_script_log_info "Sending alert: New _default.cfg" fn_script_log_info "Sending alert: ${selfname} has received a new _default.cfg"
alertsubject="Alert - ${selfname} - New _default.cfg" alerttitle="Alert - ${selfname} - New _default.cfg"
alertemoji="🎮" alertemoji="🎮"
alertsound="1" alertsound="1"
alerturl="not enabled" alerturl="not enabled"
alertbody="${selfname} has received a new _default.cfg. Check file for changes." alertmessage="${selfname} has received a new _default.cfg."
# Blue # Blue
alertcolourhex="#1e90ff" alertcolourhex="#1e90ff"
alertcolourdec="2003199" alertcolourdec="2003199"
} }
fn_alert_wipe() { fn_alert_wipe() {
fn_script_log_info "Sending alert: Wipe" fn_script_log_info "Sending alert: ${selfname} has been wiped"
alertsubject="Alert - ${selfname} - Wipe" alerttitle="Alert - ${selfname} - Wipe"
alertemoji="🧹" alertemoji="🧹"
alertsound="1" alertsound="1"
alerturl="not enabled" alerturl="not enabled"
alertbody="${selfname} has been wiped" alertmessage="${selfname} has been wiped"
# Blue # Blue
alertcolourhex="#1e90ff" alertcolourhex="#1e90ff"
alertcolourdec="2003199" alertcolourdec="2003199"
} }
fn_alert_info() { fn_alert_info() {
fn_script_log_info "Sending alert: Info" fn_script_log_info "Sending alert: ${selfname} info"
alertsubject="Alert - ${selfname} - Info" alerttitle="Alert - ${selfname} - Info"
alertemoji="📄" alertemoji="📄"
alertsound="1" alertsound="1"
alerturl="not enabled" alerturl="not enabled"
alertbody="${selfname} info" alertmessage="${selfname} info"
# Blue # Blue
alertcolourhex="#1e90ff" alertcolourhex="#1e90ff"
alertcolourdec="2003199" alertcolourdec="2003199"

8
lgsm/modules/alert_discord.sh

@ -16,13 +16,13 @@ jsonshortinfo=$(
"embeds": [ "embeds": [
{ {
"author": { "author": {
"name": "${alertemoji} ${alertsubject} ${alertemoji}", "name": "${alertemoji} ${alerttitle} ${alertemoji}",
"url": "", "url": "",
"icon_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg" "icon_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg"
}, },
"title": "${servername}", "title": "${servername}",
"url": "", "url": "",
"description": "${alertbody} \n More info: ${alerturl}", "description": "${alertmessage} \n More info: ${alerturl}",
"color": "${alertcolourdec}", "color": "${alertcolourdec}",
"type": "content", "type": "content",
"thumbnail": { "thumbnail": {
@ -63,13 +63,13 @@ jsonshortnoinfo=$(
"embeds": [ "embeds": [
{ {
"author": { "author": {
"name": "${alertemoji} ${alertsubject} ${alertemoji}", "name": "${alertemoji} ${alerttitle} ${alertemoji}",
"url": "", "url": "",
"icon_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg" "icon_url": "https://raw.githubusercontent.com/${githubuser}/${githubrepo}/${githubbranch}/lgsm/data/alert_discord_logo.jpg"
}, },
"title": "${servername}", "title": "${servername}",
"url": "", "url": "",
"description": "${alertbody}", "description": "${alertmessage}",
"color": "${alertcolourdec}", "color": "${alertcolourdec}",
"type": "content", "type": "content",
"thumbnail": { "thumbnail": {

4
lgsm/modules/alert_email.sh

@ -10,9 +10,9 @@ moduleselfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
fn_print_dots "Sending Email alert: ${email}" fn_print_dots "Sending Email alert: ${email}"
if [ -n "${emailfrom}" ]; then if [ -n "${emailfrom}" ]; then
mail -s "${alertsubject}" -r "${emailfrom}" "${email}" < "${alertlog}" mail -s "${alerttitle}" -r "${emailfrom}" "${email}" < "${alertlog}"
else else
mail -s "${alertsubject}" "${email}" < "${alertlog}" mail -s "${alerttitle}" "${email}" < "${alertlog}"
fi fi
exitcode=$? exitcode=$?
if [ "${exitcode}" == "0" ]; then if [ "${exitcode}" == "0" ]; then

4
lgsm/modules/alert_gotify.sh

@ -10,8 +10,8 @@ module_selfname="$(basename "$(readlink -f "${BASH_SOURCE[0]}")")"
json=$( json=$(
cat << EOF cat << EOF
{ {
"title": "${alertemoji} ${alertsubject} ${alertemoji}", "title": "${alertemoji} ${alerttitle} ${alertemoji}",
"message": "Server name\n${servername}\n\nMessage\n${alertbody}\n\nGame\n${gamename}\n\nServer IP\n${alertip}:${port}\n\nHostname\n${HOSTNAME}\n\nMore info\n${alerturl}", "message": "Server name\n${servername}\n\nMessage\n${alertmessage}\n\nGame\n${gamename}\n\nServer IP\n${alertip}:${port}\n\nHostname\n${HOSTNAME}\n\nMore info\n${alerturl}",
"priority": 5 "priority": 5
} }
EOF EOF

4
lgsm/modules/alert_ifttt.sh

@ -11,8 +11,8 @@ json=$(
cat << EOF cat << EOF
{ {
"value1": "${selfname}", "value1": "${selfname}",
"value2": "${alertemoji} ${alertsubject} ${alertemoji}", "value2": "${alertemoji} ${alerttitle} ${alertemoji}",
"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}" "value3": "Message: \n${alertmessage}\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 EOF
) )

4
lgsm/modules/alert_pushbullet.sh

@ -12,8 +12,8 @@ json=$(
{ {
"channel_tag": "${channeltag}", "channel_tag": "${channeltag}",
"type": "note", "type": "note",
"title": "${alertemoji} ${alertsubject} ${alertemoji}", "title": "${alertemoji} ${alerttitle} ${alertemoji}",
"body": "Server name\n${servername}\n\nMessage\n${alertbody}\n\nGame\n${gamename}\n\nServer IP\n${alertip}:${port}\n\nHostname\n${HOSTNAME}\n\nMore info\n${alerturl}" "body": "Server name\n${servername}\n\nMessage\n${alertmessage}\n\nGame\n${gamename}\n\nServer IP\n${alertip}:${port}\n\nHostname\n${HOSTNAME}\n\nMore info\n${alerturl}"
} }
EOF EOF
) )

2
lgsm/modules/alert_pushover.sh

@ -22,7 +22,7 @@ else
alertpriority="0" alertpriority="0"
fi fi
pushoversend=$(curl --connect-timeout 10 -sS -F token="${pushovertoken}" -F user="${pushoveruserkey}" -F html="1" -F sound="${alertsound}" -F priority="${alertpriority}" -F title="${alertemoji} ${alertsubject} ${alertemoji}" -F message=" <b>Server name</b><br>${servername}<br><br><b>Message</b><br>${alertbody}<br><br><b>Game</b><br>${gamename}<br><br><b>Server IP</b><br><a href='https://www.gametracker.com/server_info/${alertip}:${port}'>${alertip}:${port}</a><br><br><b>Hostname</b><br>${HOSTNAME}<br><br><b>More info</b><br><a href='${alerturl}'>${alerturl}</a>" "https://api.pushover.net/1/messages.json" | grep errors) pushoversend=$(curl --connect-timeout 10 -sS -F token="${pushovertoken}" -F user="${pushoveruserkey}" -F html="1" -F sound="${alertsound}" -F priority="${alertpriority}" -F title="${alertemoji} ${alerttitle} ${alertemoji}" -F message=" <b>Server name</b><br>${servername}<br><br><b>Message</b><br>${alertmessage}<br><br><b>Game</b><br>${gamename}<br><br><b>Server IP</b><br><a href='https://www.gametracker.com/server_info/${alertip}:${port}'>${alertip}:${port}</a><br><br><b>Hostname</b><br>${HOSTNAME}<br><br><b>More info</b><br><a href='${alerturl}'>${alerturl}</a>" "https://api.pushover.net/1/messages.json" | grep errors)
if [ -n "${pushoversend}" ]; then if [ -n "${pushoversend}" ]; then
fn_print_fail_nl "Sending Pushover alert: ${pushoversend}" fn_print_fail_nl "Sending Pushover alert: ${pushoversend}"

2
lgsm/modules/alert_rocketchat.sh

@ -11,7 +11,7 @@ json=$(
cat << EOF cat << EOF
{ {
"alias": "LinuxGSM", "alias": "LinuxGSM",
"text": "*${alertemoji} ${alertsubject} ${alertemoji}* \n *${servername}* \n ${alertbody} \n More info: ${alerturl}", "text": "*${alertemoji} ${alerttitle} ${alertemoji}* \n *${servername}* \n ${alertmessage} \n More info: ${alerturl}",
"attachments": [ "attachments": [
{ {
"fields": [ "fields": [

2
lgsm/modules/alert_slack.sh

@ -25,7 +25,7 @@ json=$(
"type": "section", "type": "section",
"text": { "text": {
"type": "mrkdwn", "type": "mrkdwn",
"text": "*${alertemoji} ${alertsubject}* \n ${alertbody}" "text": "*${alertemoji} ${alerttitle}* \n ${alertmessage}"
} }
}, },
{ {

2
lgsm/modules/alert_telegram.sh

@ -12,7 +12,7 @@ json=$(
{ {
"chat_id": "${telegramchatid}", "chat_id": "${telegramchatid}",
"parse_mode": "HTML", "parse_mode": "HTML",
"text": "<b>${alertemoji} ${alertsubject} ${alertemoji}</b>\n\n<b>Server name</b>\n${servername}\n\n<b>Message</b>\n${alertbody}\n\n<b>Game</b>\n${gamename}\n\n<b>Server IP</b>\n<a href='https://www.gametracker.com/server_info/${alertip}:${port}'>${alertip}:${port}</a>\n\n<b>Hostname</b>\n${HOSTNAME}\n\n<b>More info</b>\n<a href='${alerturl}'>${alerturl}</a>", "text": "<b>${alertemoji} ${alerttitle} ${alertemoji}</b>\n\n<b>Server name</b>\n${servername}\n\n<b>Message</b>\n${alertmessage}\n\n<b>Game</b>\n${gamename}\n\n<b>Server IP</b>\n<a href='https://www.gametracker.com/server_info/${alertip}:${port}'>${alertip}:${port}</a>\n\n<b>Hostname</b>\n${HOSTNAME}\n\n<b>More info</b>\n<a href='${alerturl}'>${alerturl}</a>",
"disable_web_page_preview": "yes" "disable_web_page_preview": "yes"
} }
EOF EOF

2
lgsm/modules/info_messages.sh

@ -51,7 +51,7 @@ fn_info_message_head() {
echo -e "${bold}${lightyellow}Alert Summary${default}" echo -e "${bold}${lightyellow}Alert Summary${default}"
fn_messages_separator fn_messages_separator
echo -e "Message" echo -e "Message"
echo -e "${alertbody}" echo -e "${alertmessage}"
echo -e "" echo -e ""
echo -e "Game" echo -e "Game"
echo -e "${gamename}" echo -e "${gamename}"

Loading…
Cancel
Save