mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Add initial datadog widget
This commit is contained in:
34
datadog/client.go
Normal file
34
datadog/client.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package datadog
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
datadog "github.com/zorkian/go-datadog-api"
|
||||
)
|
||||
|
||||
// Monitors returns a list of newrelic monitors
|
||||
func Monitors() ([]datadog.Monitor, error) {
|
||||
client := datadog.NewClient(apiKey(), applicationKey())
|
||||
|
||||
monitors, err := client.GetMonitorsByTags(wtf.ToStrs(wtf.Config.UList("wtf.mods.datadog.monitors.tags")))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return monitors, nil
|
||||
}
|
||||
|
||||
func apiKey() string {
|
||||
return wtf.Config.UString(
|
||||
"wtf.mods.datadog.apiKey",
|
||||
os.Getenv("WTF_DATADOG_API_KEY"),
|
||||
)
|
||||
}
|
||||
|
||||
func applicationKey() string {
|
||||
return wtf.Config.UString(
|
||||
"wtf.mods.datadog.applicationKey",
|
||||
os.Getenv("WTF_DATADOG_APPLICATION_KEY"),
|
||||
)
|
||||
}
|
||||
73
datadog/widget.go
Normal file
73
datadog/widget.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package datadog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/senorprogrammer/wtf/wtf"
|
||||
datadog "github.com/zorkian/go-datadog-api"
|
||||
)
|
||||
|
||||
type Widget struct {
|
||||
wtf.TextWidget
|
||||
}
|
||||
|
||||
func NewWidget() *Widget {
|
||||
widget := Widget{
|
||||
TextWidget: wtf.NewTextWidget("Datadog", "datadog", false),
|
||||
}
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
monitors, monitorErr := Monitors()
|
||||
|
||||
widget.UpdateRefreshedAt()
|
||||
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s", widget.Name)))
|
||||
widget.View.Clear()
|
||||
|
||||
var content string
|
||||
if monitorErr != nil {
|
||||
widget.View.SetWrap(true)
|
||||
content = monitorErr.Error()
|
||||
} else {
|
||||
widget.View.SetWrap(false)
|
||||
content = widget.contentFrom(monitors)
|
||||
}
|
||||
|
||||
widget.View.SetText(content)
|
||||
}
|
||||
|
||||
/* -------------------- Unexported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) contentFrom(monitors []datadog.Monitor) string {
|
||||
var str string
|
||||
|
||||
triggeredMonitors := []datadog.Monitor{}
|
||||
|
||||
for _, monitor := range monitors {
|
||||
state := *monitor.OverallState
|
||||
switch state {
|
||||
case "Alert":
|
||||
triggeredMonitors = append(triggeredMonitors, monitor)
|
||||
}
|
||||
}
|
||||
if len(triggeredMonitors) > 0 {
|
||||
str = str + fmt.Sprintf(
|
||||
" %s\n",
|
||||
"[red]Triggered Monitors[white]",
|
||||
)
|
||||
for _, triggeredMonitor := range triggeredMonitors {
|
||||
str = str + fmt.Sprintf("[red] %s\n", *triggeredMonitor.Name)
|
||||
}
|
||||
} else {
|
||||
str = str + fmt.Sprintf(
|
||||
" %s\n",
|
||||
"[green]No Triggered Monitors[white]",
|
||||
)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
Reference in New Issue
Block a user