Skip to content

Commit

Permalink
Merge pull request #1 from natrontech/feature-retries
Browse files Browse the repository at this point in the history
feat: add GET_RETIRES variable
  • Loading branch information
svengerber authored Jul 5, 2023
2 parents 34aca97 + 77597a8 commit 984d83a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 33 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The binary requires the following environment variables:
| Variable | Description |
| --- | --- |
| `UPTIME_KUMA_URL`| The URL to your Uptime Kuma instance. This URL should be the push monitor URL. |
| `GET_RETRIES`| How often a GET request should be retried if it fails. Default is 3. |

### Example with kube-prometheus-stack
Configure a push monitor in your Uptime Kuma instance.
Expand All @@ -36,7 +37,11 @@ metadata:
namespace: monitoring
type: Opaque
stringData:
# Required
UPTIME_KUMA_URL: "https://uptime-kuma.example.com/api/push/XXXXXXX"
# Optional
GET_RETRIES: 3

```
Add this middleware as a sidecar container to your Alertmanager deployment.
Use the secret as an environment variable.
Expand Down
83 changes: 54 additions & 29 deletions cmd/pusher/pusher.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,68 @@
package main

import (
"os"
"net/http"
"net/url"
"log"

"github.com/gofiber/fiber/v2"
"github.com/natrontech/alertmanager-uptime-kuma-push/internal/util"
"log"
"net/http"
"net/url"
"os"
"strconv"
"time"

"github.com/gofiber/fiber/v2"
"github.com/natrontech/alertmanager-uptime-kuma-push/internal/util"
)

func init() {

// Log with timestamp
log.SetFlags(log.LstdFlags)
// Log with timestamp
log.SetFlags(log.LstdFlags)

if err := util.LoadEnv(); err != nil {
if err := util.LoadEnv(); err != nil {
log.Println("Error loading environment variables:", err)
os.Exit(1)
}
}
}

func main() {
app := fiber.New()

app.All("/push", func(c *fiber.Ctx) error {

// Only log the host of the URL
url, _ := url.Parse(util.UptimeKumaURL)
log.Println("Sending HTTP Get request to", url.Host)

_ , err := http.Get(util.UptimeKumaURL)
if err != nil {
log.Println("Error sending HTTP request to uptime kuma url", err)
}

return c.SendString("OK")
})

app.Listen(":8081")
}
app := fiber.New()

app.All("/push", func(c *fiber.Ctx) error {

// Set the timeout duration
client := &http.Client{
Timeout: 3 * time.Second,
}

// Loop over the number of retries
for i := 0; i < util.Retries; i++ {

// Log trying again if it's not the first try
if i > 0 {
log.Println("Try " + strconv.Itoa(i) + " of " + strconv.Itoa(util.Retries) + " failed, trying again")
}

// Only log the host of the URL
url, _ := url.Parse(util.UptimeKumaURL)
log.Println("Sending HTTP GET request to", url.Host)

_, err := client.Get(util.UptimeKumaURL)
if err != nil {
log.Println("Error sending HTTP request to uptime kuma url", err)
}

// Exit the loop if there is no error
if err == nil {
break
}

// Log if it was the last try
if i == util.Retries-1 {
log.Println("Try " + strconv.Itoa(i+1) + " of " + strconv.Itoa(util.Retries) + " failed")
}

}
return c.SendString("OK")
})

app.Listen(":8081")
}
29 changes: 25 additions & 4 deletions internal/util/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package util

import (
"errors"
"os"
"fmt"
"log"
"net/url"
"os"
"strconv"
)

var (
UptimeKumaURL string
UptimeKumaURL string
Retries int
)

// LoadEnv loads OS environment variables
Expand All @@ -17,17 +21,34 @@ func LoadEnv() error {
return errors.New("UPTIME_KUMA_URL not set")
}

// Get the value of GET_RETRIES environment variable
getRetriesStr := os.Getenv("GET_RETRIES")

// Check if GET_RETRIES is not set, set it to 3
if getRetriesStr == "" {
Retries = 3
log.Println("GET_RETRIES not set, defaulting to 3")
} else {
// Convert the GET_RETRIES value to an int
getRetries, err := strconv.Atoi(getRetriesStr)
if err != nil {
return fmt.Errorf("error converting GET_RETRIES to int: %w", err)
}
Retries = getRetries
}

// Validate uptime kuma url
uri, err := url.ParseRequestURI(UptimeKumaURL)
if err != nil {
return errors.New("UPTIME_KUMA_URL is not a valid URL")
}

switch uri.Scheme {
switch uri.Scheme {
case "http":
case "https":
default:
return errors.New("UPTIME_KUMA_URL has invalid scheme (http or https only)")
}

return nil
}
}

0 comments on commit 984d83a

Please sign in to comment.