| Server IP : 142.11.234.102 / Your IP : 216.73.217.78 Web Server : Apache System : Linux dal-shared-66.hostwindsdns.com 4.18.0-513.24.1.lve.1.el8.x86_64 #1 SMP Thu May 9 15:10:09 UTC 2024 x86_64 User : krnuyqrm ( 1183) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /lib/python2.7/site-packages/hw_cpbackup/util/ |
Upload File : |
"""
Handles the formatting of webhook alerts to the Google Chat API.
Messages are formed based on the type, username, and hostname passed from the calller.
An alert is sent to the webook via JSON payload POST request.
Google Chat API ref: https://developers.google.com/chat/api
"""
import socket
import requests
def send_alert(username='', hostname=socket.gethostname(), message=None):
"""Alert sender"""
endpoint = "https://chat.googleapis.com/v1/spaces/AAAAFHtTWnQ/messages?key=AIzaSyDdI0hCZtE6vySjMm-WEfRq3CPzqKqqsHI&token=MYBjN03H1TyHBjTrkZE4bKVFSCDOTLwktnEqkG7QPOQ%3D"
if not isinstance(message, str):
raise Exception('Undefined alert condition')
elif message == '':
raise Exception('Condition cannot be empty')
# craft json body
request_body = {
"text": "<users/all> ",
"cards": [
{
"header": {
"title": "Errors detected in Object Storage",
"subtitle": "Investigate and manually ensure backup.",
"imageUrl": "https://lh4.googleusercontent.com/proxy/lmaJGx8fX3alebQxcvE_EbRJnEviLkdyBAc0N-mjCMZm2uNIIZxkT9Y8UX9cZQVdGbP0zgumdkys1_RAD_awT-xaKFRxw1yD8e33gsSxEhAN7_YJANmdNxEyDZoBOCp8IDp2H4Y"
},
"sections": [
{
"widgets": [
{
"keyValue": {
"topLabel": "Condition",
"content": message
}
},
{
"keyValue": {
"topLabel": "Server",
"content": hostname
}
}
]
}
]
}
]
}
if username:
username_widget = {
"keyValue": {
"topLabel": "Username",
"content": username
}
}
request_body['cards'][0]['sections'][0]['widgets'].append(username_widget)
# send
# TODO: make this configurable so we can quickly turn alerts on/off
# without needing to comment out this line, rebuild, and redeploy the agent
# every time we need to temporarily disable alerts.
requests.post(endpoint, json=request_body)