This is a Webhook for Grafana SMS Wrapper
This Go code creates a server that listens for incoming Grafana webhook alerts and processes them by sending formatted messages to a configured API. Below is a breakdown of the code.
The program uses various Go packages:
bytes
: To handle byte slices, particularly for request bodies.encoding/json
: For JSON encoding/decoding.fmt
: For formatted I/O.io/ioutil
: To read HTTP responses.log
: For logging errors and other messages.net/http
: For HTTP server and client functionalities.os
: To interact with the operating system, particularly for reading the config file.
Holds the configuration details, including:
- Server settings:
IP
: Server's IP address.Port
: Server's port.Path
: Server's endpoint path for receiving webhook requests.
- PhoneNumbers: List of phone numbers to which alerts will be sent.
- URL: The API URL to which alerts will be forwarded.
- UserName: Username for API authentication.
- Password: Password for API authentication.
Defines the structure of the incoming Grafana alert payload, which includes:
- Alerts: A list of alerts, each containing:
Title
: Title of the alert.Annotations
: Contains aDescription
field with additional details about the alert.
- Reads Configuration: The configuration file (
config.json
) is loaded using thereadConfig
function. - Sets up HTTP Server: The server listens for HTTP POST requests at the path defined in the config (
config.Server.Path
). - Handles Incoming Requests:
- On receiving a POST request, it decodes the incoming JSON payload (Grafana alert data).
- For each alert, it formats a message by combining the
title
anddescription
. - The message is sent to each phone number in the configuration via the
sendMessage
function. - If successful, the server responds with an HTTP 200 status and a success message. Otherwise, it returns an error.
Reads the config.json
file and returns a Config
structure. If there is an issue reading or decoding the file, an error is returned.
Sends the formatted alert message to the specified API URL for each phone number. The function:
- Constructs a JSON body containing the message and phone number.
- Marshals the body into JSON.
- Sends an HTTP POST request to the API with custom headers (
UserName
andPassword
for authentication). - Logs the request and the response for debugging.
- If the API responds with a non-OK status, an error is returned.
- Server Start: The server listens at the IP and port defined in the configuration.
- Handling Webhook: When a Grafana webhook is triggered, the server decodes the payload and sends alerts to each phone number via an API call.
- API Communication: Each alert is sent to an external API (as defined in the
config.json
) using the username and password for authentication. - Error Handling: The program handles various errors:
- Invalid JSON in the incoming request.
- Issues reading or parsing the configuration file.
- Errors when sending messages (e.g., network issues, API errors).
- Non-OK HTTP responses from the API are logged and returned as errors.
- A Grafana alert is triggered and sent to the server's webhook endpoint (e.g.,
http://<server_ip>:<port>/alert
). - The server decodes the alert's JSON payload and extracts the title and description of the alert.
- The server sends the alert message (formatted with the title and description) to each phone number defined in the configuration by making an HTTP request to the destination API.
- The server responds to Grafana with a success message if all messages are sent successfully or an error if something fails.
This program acts as a bridge that receives Grafana alerts and forwards them as notifications (via API calls) to a set of phone numbers defined in the configuration file.