Skip to content

Commit

Permalink
Imported old go-prom-irc into the project
Browse files Browse the repository at this point in the history
  • Loading branch information
fleaz committed Apr 14, 2018
1 parent 2f83ce7 commit 388dbb4
Showing 1 changed file with 154 additions and 4 deletions.
158 changes: 154 additions & 4 deletions prometheus.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,166 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"time"

"github.com/spf13/viper"
)

type Alert struct {
Labels map[string]interface{} `json:"labels"`
Annotations map[string]interface{} `json:"annotations"`
StartsAt string `json:"startsAt"`
EndsAt string `json:"endsAt"`
}

type Notification struct {
Version string `json:"version"`
GroupKey string `json:"groupKey"`
Status string `json:"status"`
Receiver string `json:"receiver"`
GroupLables map[string]interface{} `json:"groupLabels"`
CommonLabels map[string]interface{} `json:"commonLabels"`
CommonAnnotations map[string]interface{} `json:"commonAnnotations"`
ExternalURL string `json:"externalURL"`
Alerts []Alert `json:"alerts"`
}

type NotificationContext struct {
Alert *Alert
Notification *Notification
InstanceCount int
Status string
ColorStart string
ColorEnd string
}

type Instance struct {
Name string
Value string
}

func SortAlerts(alerts []Alert) (firing, resolved []Alert) {
for _, alert := range alerts {
tStart, _ := time.Parse(time.RFC3339, alert.StartsAt)
tEnd, _ := time.Parse(time.RFC3339, alert.EndsAt)
if tEnd.After(tStart) {
resolved = append(resolved, alert)
} else {
firing = append(firing, alert)
}
}
return
}

func getColorcode(status string) string {
switch status {
case "firing":
return "\x0305"
case "resolved":
return "\x0303"
default:
return "\x0300"
}
}

func prometheusHandler(w http.ResponseWriter, r *http.Request, c *viper.Viper) {
fmt.Println("Got http event for /prometheus")
var event IRCMessage
event.Messages = append(event.Messages, "prometheus rocks")
event.Channel = c.GetString("channel")
messageChannel <- event

const firingTemplateString = "[{{ .ColorStart }}{{ .Status }}{{ .ColorEnd }}:{{ .InstanceCount }}] {{ .Alert.Labels.alertname}} - {{ .Alert.Annotations.description}}"
const resolvedTemplateString = "[{{ .ColorStart }}{{ .Status }}{{ .ColorEnd }}:{{ .InstanceCount }}] {{ .Alert.Labels.alertname}}"
const hostListTemplateString = "→ {{range $i, $instance := . }}{{if $i}}, {{end}}{{$instance.Name}}{{if $instance.Value}} ({{$instance.Value}}){{end}}{{end}}"

firingTemplate, err := template.New("notification").Parse(firingTemplateString)
if err != nil {
log.Fatalf("Failed to parse template: %v", err)
}

resolvedTemplate, err := template.New("notification").Parse(resolvedTemplateString)
if err != nil {
log.Fatalf("Failed to parse template: %v", err)
}

hostListTemplate, err := template.New("notification").Parse(hostListTemplateString)
if err != nil {
log.Fatalf("Failed to parse template: %v", err)
}

defer r.Body.Close()
decoder := json.NewDecoder(r.Body)

var notification Notification

if err := decoder.Decode(&notification); err != nil {
log.Println(err)
return
}

body, err := json.Marshal(&notification)

if err != nil {
log.Println(err)
return
}
log.Printf("JSON: %v", string(body))

var sortedAlerts = make(map[string][]Alert)
sortedAlerts["firing"], sortedAlerts["resolved"] = SortAlerts(notification.Alerts)

var instance Instance
var instanceList []Instance
var buf bytes.Buffer

for alertStatus, alertList := range sortedAlerts {
// Clear buffer
buf.Reset()
// Clear InstanceList
instanceList = instanceList[:0]

for _, alert := range alertList {
name := alert.Labels["instance"].(string)
// TODO: Add hostname shortening
value, ok := alert.Annotations["value"].(string)
if ok {
instance = Instance{Name: name, Value: value}
} else {
instance = Instance{Name: name}
}
instanceList = append(instanceList, instance)
}

context := NotificationContext{
Alert: &notification.Alerts[0],
Notification: &notification,
Status: strings.ToUpper(alertStatus),
InstanceCount: len(instanceList),
ColorStart: getColorcode(alertStatus),
ColorEnd: "\x03",
}

if context.InstanceCount > 0 {
// Sort instances
//sort.Strings(instanceList)
if strings.Compare(alertStatus, "firing") == 0 {
_ = firingTemplate.Execute(&buf, &context)
} else {
_ = resolvedTemplate.Execute(&buf, &context)
}
bot.Privmsg(*channel, buf.String())
buf.Reset()
_ = hostListTemplate.Execute(&buf, &instanceList)
bot.Privmsg(*channel, buf.String())
// var event IRCMessage
// event.Messages = append(event.Messages, "prometheus rocks")
// event.Channel = c.GetString("channel")
// messageChannel <- event
}
}

}

0 comments on commit 388dbb4

Please sign in to comment.