Skip to content

Commit

Permalink
Update main.go (#2)
Browse files Browse the repository at this point in the history
* Update main.go

* Update alertmanager-receiver-46elks.service

* Delete api.go.tmpl

* Update main.go

* Create alertmanager-receiver-46elks.env

* Create README.md

* mod stuff

* Update README.md
  • Loading branch information
SoundGoof authored Apr 4, 2023
1 parent 34c2730 commit ecc0c1c
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 24 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# alertmanager-receiver-46elks
SMS sender using the 46elks API for Prometheus Alertmanager

* compile program using 1.19 or better: go build main.go
* copy program to /usr/local/bin/
* copy env file to /etc/sysconfig
* alter env file to suide your needs
* copy servicefile to /etc/systemd/system/
* reload system do to read service file: systemctl daemon-reload
* enable service: systemctl enable alertmanager-receiver-46elks.service
* start service: systemctl start alertmanager-receiver-46elks.service

4 changes: 4 additions & 0 deletions alertmanager-receiver-46elks.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
API_PORT=1025
API_SENDER=sms_sender_name
API_USERNAME=your_username
API_PASSWORD=your_password
1 change: 1 addition & 0 deletions alertmanager-receiver-46elks.service
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Description=Alertmanager Receiver 46elks
ExecStart=/usr/local/bin/alertmanager-receiver-46elks
User=nobody
WorkingDirectory=/tmp
EnvironmentFile=/etc/sysconfig/alertmanager-receiver-46elks.env

[Install]
WantedBy=multi-user.target
21 changes: 0 additions & 21 deletions api.go.tmpl

This file was deleted.

5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module main.go

go 1.19

require gopkg.in/yaml.v2 v2.4.0
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
32 changes: 29 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ import (

var (
m = sync.Mutex{}
username = ""
password = ""
sender = ""
port = ""

)

type Alert struct {
Expand Down Expand Up @@ -122,15 +127,15 @@ func handleAlert(w http.ResponseWriter, r *http.Request, alert *Alert) {
log.Printf("Message: %s", message)

data := url.Values{
"from": {"KamelNet"},
"from": {sender},
"to": {to},
"message": {message},
}

req, err := http.NewRequest("POST", "https://api.46elks.com/a1/sms", bytes.NewBufferString(data.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
req.SetBasicAuth(apiUsername, apiPassword)
req.SetBasicAuth(username, password)

client := &http.Client{}
resp, err := client.Do(req)
Expand All @@ -157,6 +162,27 @@ func handleAlert(w http.ResponseWriter, r *http.Request, alert *Alert) {

func main() {
log.Printf("Running")

// Read environment variables for username and password
username := os.Getenv("API_USERNAME")
if username == "" {
log.Fatal("Environment variable API_USERNAME is not set")
}
password := os.Getenv("API_PASSWORD")
if password == "" {
log.Fatal("Environment variable API_PASSWORD is not set")
}
sender := os.Getenv("API_SENDER")
if sender == "" {
log.Fatal("Environment variable API_SENDER is not set")
}

// Read the environment variable for port, and use the default port 1025 if not set
port := os.Getenv("API_PORT")
if port == "" {
port = "1025"
}

// Ask soundgoof why the port 1025 was chosen
log.Fatal(http.ListenAndServe(":1025", http.HandlerFunc(handle)))
log.Fatal(http.ListenAndServe(":"+port, http.HandlerFunc(handle)))
}

0 comments on commit ecc0c1c

Please sign in to comment.