this basically works but it alerts too much for now. Must fix that.
ci/woodpecker/push/woodpecker Pipeline was successful
Details
ci/woodpecker/push/woodpecker Pipeline was successful
Details
This commit is contained in:
parent
6d5cbcaa14
commit
ccc908525b
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
62
main.go
62
main.go
|
|
@ -1,11 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
|
@ -16,7 +18,20 @@ type ContainerInfo struct {
|
|||
Ignores []string
|
||||
}
|
||||
|
||||
var notifiedChanges = struct {
|
||||
sync.RWMutex
|
||||
changes map[string]string
|
||||
}{changes: make(map[string]string)}
|
||||
|
||||
func main() {
|
||||
// Check if GLITCHTIP_DSN environment variable is set
|
||||
glitchtipDSN := os.Getenv("GLITCHTIP_DSN")
|
||||
if glitchtipDSN == "" {
|
||||
log.Fatal("GLITCHTIP_DSN environment variable is not set")
|
||||
}
|
||||
|
||||
log.Println("Starting Oculus...")
|
||||
|
||||
for {
|
||||
containers, err := getContainers()
|
||||
if err != nil {
|
||||
|
|
@ -26,6 +41,7 @@ func main() {
|
|||
}
|
||||
|
||||
for _, container := range containers {
|
||||
log.Printf("Monitoring container: %s (%s) every %s", container.Name, container.ID, container.Interval)
|
||||
go monitorContainer(container)
|
||||
}
|
||||
|
||||
|
|
@ -35,6 +51,7 @@ func main() {
|
|||
}
|
||||
|
||||
func getContainers() ([]ContainerInfo, error) {
|
||||
log.Println("Fetching container list...")
|
||||
output, err := exec.Command("docker", "ps", "--format", "{{.ID}} {{.Label \"oculus.enable\"}} {{.Label \"oculus.interval\"}} {{.Label \"oculus.cname\"}} {{.Label \"oculus.ignores\"}}").Output()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -81,11 +98,13 @@ func getContainers() ([]ContainerInfo, error) {
|
|||
})
|
||||
}
|
||||
|
||||
log.Printf("Found %d containers to monitor.", len(containers))
|
||||
return containers, nil
|
||||
}
|
||||
|
||||
func monitorContainer(container ContainerInfo) {
|
||||
for {
|
||||
log.Printf("Checking diffs for container: %s (%s)", container.Name, container.ID)
|
||||
checkDiff(container)
|
||||
time.Sleep(container.Interval)
|
||||
}
|
||||
|
|
@ -106,20 +125,39 @@ func checkDiff(container ContainerInfo) {
|
|||
}
|
||||
|
||||
if diffOutput != "" {
|
||||
// Write diff output to a file
|
||||
filename := fmt.Sprintf("%s.diff", container.Name)
|
||||
err = writeToFile(filename, diffOutput)
|
||||
if err != nil {
|
||||
log.Printf("Error writing diff to file for container %s: %v", container.ID, err)
|
||||
}
|
||||
diffHash := fmt.Sprintf("%x", md5.Sum([]byte(diffOutput)))
|
||||
|
||||
// Send notification using go-glitch
|
||||
cmd = exec.Command("go-glitch")
|
||||
cmd.Stdin = strings.NewReader(diffOutput)
|
||||
err = cmd.Run()
|
||||
if err != nil {
|
||||
log.Printf("Error sending notification for container %s: %v", container.ID, err)
|
||||
notifiedChanges.RLock()
|
||||
lastNotifiedHash, notified := notifiedChanges.changes[container.ID]
|
||||
notifiedChanges.RUnlock()
|
||||
|
||||
if !notified || lastNotifiedHash != diffHash {
|
||||
log.Printf("Writing diff output for container: %s (%s)", container.Name, container.ID)
|
||||
// Write diff output to a file
|
||||
filename := fmt.Sprintf("%s.diff", container.Name)
|
||||
err = writeToFile(filename, diffOutput)
|
||||
if err != nil {
|
||||
log.Printf("Error writing diff to file for container %s: %v", container.ID, err)
|
||||
}
|
||||
|
||||
// Send notification using go-glitch
|
||||
log.Printf("Sending notification for container: %s (%s)", container.Name, container.ID)
|
||||
cmd = exec.Command("go-glitch")
|
||||
cmd.Stdin = strings.NewReader(diffOutput)
|
||||
output, err = cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("Error sending notification for container %s: %v", container.ID, err)
|
||||
log.Printf("go-glitch output: %s", output)
|
||||
} else {
|
||||
notifiedChanges.Lock()
|
||||
notifiedChanges.changes[container.ID] = diffHash
|
||||
notifiedChanges.Unlock()
|
||||
}
|
||||
} else {
|
||||
log.Printf("No new changes detected for container: %s (%s)", container.Name, container.ID)
|
||||
}
|
||||
} else {
|
||||
log.Printf("No significant changes detected for container: %s (%s)", container.Name, container.ID)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
A /newfile2.txt
|
||||
A /newfile3.txt
|
||||
A /newfile.txt
|
||||
Loading…
Reference in New Issue