Created
December 24, 2022 08:22
-
-
Save pd12bbf7608ae1/29da2f1c6131fa32b68bca048ac15a58 to your computer and use it in GitHub Desktop.
Get TG Notice with cfworkers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------------------------------------ | |
# custom notifications | |
# | |
# enable/disable sending custom notifications | |
SEND_CUSTOM="YES" | |
# if a role's recipients are not configured, use the following. | |
# (empty = do not send a notification for unconfigured roles) | |
DEFAULT_RECIPIENT_CUSTOM="sysadmin" | |
# The custom_sender() is a custom function to do whatever you need to do | |
custom_sender() { | |
# variables you can use: | |
# ${host} the host generated this event | |
# ${url_host} same as ${host} but URL encoded | |
# ${unique_id} the unique id of this event | |
# ${alarm_id} the unique id of the alarm that generated this event | |
# ${event_id} the incremental id of the event, for this alarm id | |
# ${when} the timestamp this event occurred | |
# ${name} the name of the alarm, as given in netdata health.d entries | |
# ${url_name} same as ${name} but URL encoded | |
# ${chart} the name of the chart (type.id) | |
# ${url_chart} same as ${chart} but URL encoded | |
# ${family} the family of the chart | |
# ${url_family} same as ${family} but URL encoded | |
# ${status} the current status : REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL | |
# ${old_status} the previous status: REMOVED, UNINITIALIZED, UNDEFINED, CLEAR, WARNING, CRITICAL | |
# ${value} the current value of the alarm | |
# ${old_value} the previous value of the alarm | |
# ${src} the line number and file the alarm has been configured | |
# ${duration} the duration in seconds of the previous alarm state | |
# ${duration_txt} same as ${duration} for humans | |
# ${non_clear_duration} the total duration in seconds this is/was non-clear | |
# ${non_clear_duration_txt} same as ${non_clear_duration} for humans | |
# ${units} the units of the value | |
# ${info} a short description of the alarm | |
# ${value_string} friendly value (with units) | |
# ${old_value_string} friendly old value (with units) | |
# ${image} the URL of an image to represent the status of the alarm | |
# ${color} a color in #AABBCC format for the alarm | |
# ${goto_url} the URL the user can click to see the netdata dashboard | |
# ${calc_expression} the expression evaluated to provide the value for the alarm | |
# ${calc_param_values} the value of the variables in the evaluated expression | |
# ${total_warnings} the total number of alarms in WARNING state on the host | |
# ${total_critical} the total number of alarms in CRITICAL state on the host | |
# these are more human friendly: | |
# ${alarm} like "name = value units" | |
# ${status_message} like "needs attention", "recovered", "is critical" | |
# ${severity} like "Escalated to CRITICAL", "Recovered from WARNING" | |
# ${raised_for} like "(alarm was raised for 10 minutes)" | |
# example human readable SMS | |
# local msg="${host} ${status_message}: ${alarm} ${raised_for}" | |
# limit it to 160 characters and encode it for use in a URL | |
# urlencode "${msg:0:160}" >/dev/null; msg="${REPLY}" | |
# a space separated list of the recipients to send alarms to | |
# to="${1}" | |
# Sample send SMS to an imaginary SMS gateway accessible via HTTPS | |
#for phone in ${to}; do | |
# httpcode=$(docurl -X POST \ | |
# --data-urlencode "From=XXX" \ | |
# --data-urlencode "To=${phone}" \ | |
# --data-urlencode "Body=${msg}" \ | |
# -u "${accountsid}:${accounttoken}" \ | |
# https://domain.website.com/) | |
# | |
# if [ "${httpcode}" = "200" ]; then | |
# info "sent custom notification ${msg} to ${phone}" | |
# sent=$((sent + 1)) | |
# else | |
# error "failed to send custom notification ${msg} to ${phone} with HTTP error code ${httpcode}." | |
# fi | |
#done | |
# info "not sending custom notification to ${to}, for ${status} of '${host}.${chart}.${name}' - custom_sender() is not configured." | |
local bottoken="" | |
local chatids="" | |
local apiEndpoint="" | |
local apiAuthorization="" | |
local message="${host} ${status_message} - <b>${name//_/ }</b> | |
${chart} (${family}) | |
<a href=\"${goto_url}\">${alarm}</a> | |
<i>${info}</i>" | |
local httpcode sent=0 chatid emoji disableNotification="" | |
if [ "${status}" = "CLEAR" ]; then disableNotification="--data-urlencode disable_notification=true"; fi | |
case "${status}" in | |
WARNING) emoji="⚠️" ;; | |
CRITICAL) emoji="🔴" ;; | |
CLEAR) emoji="✅" ;; | |
*) emoji="⚪️" ;; | |
esac | |
if [ -n "${bottoken}" ] && [ -n "${chatids}" ] && [ -n "${message}" ]; then | |
for chatid in ${chatids}; do | |
# https://core.telegram.org/bots/api#sendmessage | |
httpcode=$(docurl \ | |
-H "apihost: api.telegram.org"\ | |
-H "apiAuthorization: ${apiAuthorization}"\ | |
${disableNotification} \ | |
--data-urlencode "parse_mode=HTML" \ | |
--data-urlencode "disable_web_page_preview=true" \ | |
--data-urlencode "text=${emoji} ${message}" \ | |
"https://${apiEndpoint}/bot${bottoken}/sendMessage?chat_id=${chatid}") | |
if [ "${httpcode}" = "200" ]; then | |
info "sent telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}'" | |
sent=$((sent + 1)) | |
elif [ "${httpcode}" = "401" ]; then | |
error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}': Wrong bot token." | |
else | |
error "failed to send telegram notification for: ${host} ${chart}.${name} is ${status} to '${chatid}' with HTTP response status code ${httpcode}." | |
fi | |
done | |
[ ${sent} -gt 0 ] && return 0 | |
fi | |
return 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment