Files
cachet-monitor/config.go
Christoph Eck 79676357f3 slack: webhookurl and sendSlack
- GetComponentStatus fix
- get main url from api definition for slack post
- fix run send slack
2020-01-27 18:36:25 +01:00

97 lines
2.3 KiB
Go

package cachet
import (
"net"
"os"
"strings"
"time"
"github.com/sirupsen/logrus"
)
type CachetMonitor struct {
SystemName string `json:"system_name" yaml:"system_name"`
DateFormat string `json:"date_format" yaml:"date_format"`
SlackWebhook string `json:"slack_webhook" yaml:"slack_webhook"`
API CachetAPI `json:"api"`
RawMonitors []map[string]interface{} `json:"monitors" yaml:"monitors"`
Monitors []MonitorInterface `json:"-" yaml:"-"`
Immediate bool `json:"-" yaml:"-"`
Restarted bool `json:"-" yaml:"-"`
}
// Validate configuration
func (cfg *CachetMonitor) Validate() bool {
valid := true
if len(cfg.SystemName) == 0 {
// get hostname
cfg.SystemName = getHostname()
}
if len(cfg.DateFormat) == 0 {
cfg.DateFormat = DefaultTimeFormat
}
if len(cfg.API.Token) == 0 || len(cfg.API.URL) == 0 {
logrus.Warnf("API URL or API Token missing.\nGet help at https://github.com/castawaylabs/cachet-monitor")
valid = false
}
if len(cfg.Monitors) == 0 {
logrus.Warnf("No monitors defined!\nSee help for example configuration")
valid = false
}
for index, monitor := range cfg.Monitors {
if errs := monitor.Validate(); len(errs) > 0 {
logrus.Warnf("Monitor validation errors (index %d): %v", index, "\n - "+strings.Join(errs, "\n - "))
valid = false
}
}
return valid
}
// getHostname returns id of the current system
func getHostname() string {
hostname, err := os.Hostname()
if err == nil && len(hostname) > 0 {
return hostname
}
addrs, err := net.InterfaceAddrs()
if err != nil || len(addrs) == 0 {
return "unknown"
}
return addrs[0].String()
}
func getMs() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}
func GetMonitorType(t string) string {
if len(t) == 0 {
return "http"
}
return strings.ToLower(t)
}
func getTemplateData(monitor *AbstractMonitor) map[string]interface{} {
return map[string]interface{}{
"SystemName": monitor.config.SystemName,
"API": monitor.config.API,
"Monitor": monitor,
"now": time.Now().Format(monitor.config.DateFormat),
}
}
func MainUrl(cfg *CachetMonitor) string {
var url = cfg.API.URL
var index = strings.Index(url,"/api/")
return url[0:index]
}