- yaml & json supported

This commit is contained in:
Matej Kramny
2017-02-04 21:48:27 -08:00
parent 36bf228599
commit d43eca4b7d
4 changed files with 63 additions and 38 deletions

View File

@@ -8,11 +8,14 @@ import (
"net/url" "net/url"
"os" "os"
"os/signal" "os/signal"
"strings"
"sync" "sync"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
cachet "github.com/castawaylabs/cachet-monitor" cachet "github.com/castawaylabs/cachet-monitor"
docopt "github.com/docopt/docopt-go" docopt "github.com/docopt/docopt-go"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
) )
const usage = `cachet-monitor const usage = `cachet-monitor
@@ -162,25 +165,31 @@ func getConfiguration(path string) (*cachet.CachetMonitor, error) {
} }
} }
if err = json.Unmarshal(data, &cfg); err != nil { if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
err = yaml.Unmarshal(data, &cfg)
} else {
err = json.Unmarshal(data, &cfg)
}
if err != nil {
logrus.Warnf("Unable to parse configuration file") logrus.Warnf("Unable to parse configuration file")
} }
cfg.Monitors = make([]cachet.MonitorInterface, len(cfg.RawMonitors)) cfg.Monitors = make([]cachet.MonitorInterface, len(cfg.RawMonitors))
for index, rawMonitor := range cfg.RawMonitors { for index, rawMonitor := range cfg.RawMonitors {
var abstract cachet.AbstractMonitor var abstract cachet.AbstractMonitor
if err := json.Unmarshal(rawMonitor, &abstract); err != nil {
logrus.Errorf("Unable to unmarshal monitor (index: %d): %v", index, err)
continue
}
var t cachet.MonitorInterface var t cachet.MonitorInterface
var err error var err error
switch abstract.Type { monType := ""
if t, ok := rawMonitor["type"].(string); ok {
monType = t
}
switch monType {
case "http", "": case "http", "":
var s cachet.HTTPMonitor var s cachet.HTTPMonitor
err = json.Unmarshal(rawMonitor, &s) err = mapstructure.Decode(rawMonitor, &s)
t = &s t = &s
case "dns": case "dns":
// t = cachet.DNSMonitor // t = cachet.DNSMonitor

View File

@@ -1,22 +1,20 @@
package cachet package cachet
import ( import (
"fmt"
"net" "net"
"os" "os"
"strings"
"time" "time"
"encoding/json"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
) )
type CachetMonitor struct { type CachetMonitor struct {
SystemName string `json:"system_name"` SystemName string `json:"system_name"`
API CachetAPI `json:"api"` API CachetAPI `json:"api"`
RawMonitors []json.RawMessage `json:"monitors"` RawMonitors []map[string]interface{} `json:"monitors" yaml:"monitors"`
Monitors []MonitorInterface `json:"-"` Monitors []MonitorInterface `json:"-" yaml:"-"`
} }
// Validate configuration // Validate configuration
@@ -28,7 +26,6 @@ func (cfg *CachetMonitor) Validate() bool {
cfg.SystemName = getHostname() cfg.SystemName = getHostname()
} }
fmt.Println(cfg.API)
if len(cfg.API.Token) == 0 || len(cfg.API.URL) == 0 { 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") logrus.Warnf("API URL or API Token missing.\nGet help at https://github.com/castawaylabs/cachet-monitor")
valid = false valid = false
@@ -39,8 +36,9 @@ func (cfg *CachetMonitor) Validate() bool {
valid = false valid = false
} }
for _, monitor := range cfg.Monitors { for index, monitor := range cfg.Monitors {
if errs := monitor.Validate(); len(errs) > 0 { if errs := monitor.Validate(); len(errs) > 0 {
logrus.Warnf("Monitor validation errors (index %d): %v", index, "\n - "+strings.Join(errs, "\n - "))
valid = false valid = false
} }
} }

32
http.go
View File

@@ -10,15 +10,33 @@ import (
"time" "time"
) )
type HTTPMonitor struct { // // Investigating template
*AbstractMonitor // var HTTPTemplate = MessageTemplate{
// Subject: `{{ .Name }} - {{ .config.SystemName }}`,
// Message: `{{ .Name }} check **failed** - {{ .now }}
Method string `json:"method"` // {{ .lastFailReason }}`,
ExpectedStatusCode int `json:"expected_status_code"` // }
Headers map[string]string `json:"headers"`
// // Fixed template
// var HTTPTemplate = MessageTemplate{
// Subject: `{{ .Name }} - {{ .config.SystemName }}`,
// Message: `**Resolved** - {{ .now }}
// - - -
// {{ .incident.Message }}`,
// }
type HTTPMonitor struct {
AbstractMonitor `mapstructure:",squash"`
Method string
ExpectedStatusCode int `mapstructure:"expected_status_code"`
Headers map[string]string
// compiled to Regexp // compiled to Regexp
ExpectedBody string `json:"expected_body"` ExpectedBody string `mapstructure:"expected_body"`
bodyRegexp *regexp.Regexp bodyRegexp *regexp.Regexp
} }
@@ -116,7 +134,7 @@ func (monitor *HTTPMonitor) Validate() []string {
return errs return errs
} }
func (mon *HTTPMonitor) GetMonitor() *AbstractMonitor { func (mon *HTTPMonitor) GetMonitor() AbstractMonitor {
return mon.AbstractMonitor return mon.AbstractMonitor
} }

View File

@@ -15,34 +15,34 @@ const HistorySize = 10
type MonitorInterface interface { type MonitorInterface interface {
do() bool do() bool
Validate() []string Validate() []string
GetMonitor() *AbstractMonitor GetMonitor() AbstractMonitor
} }
// AbstractMonitor data model // AbstractMonitor data model
type AbstractMonitor struct { type AbstractMonitor struct {
Name string `json:"name"` Name string
Target string `json:"target"` Target string
// (default)http, tcp, dns, icmp // (default)http, tcp, dns, icmp
Type string `json:"type"` Type string
// defaults true // defaults true
Strict bool `json:"strict"` Strict bool
Interval time.Duration `json:"interval"` Interval time.Duration
Timeout time.Duration `json:"timeout"` Timeout time.Duration
MetricID int `json:"metric_id"` MetricID int `mapstructure:"metric_id"`
ComponentID int `json:"component_id"` ComponentID int `mapstructure:"component_id"`
// Templating stuff // Templating stuff
Template struct { Template struct {
Investigating MessageTemplate `json:"investigating"` Investigating MessageTemplate
Fixed MessageTemplate `json:"fixed"` Fixed MessageTemplate
} `json:"template"` }
// Threshold = percentage // Threshold = percentage
Threshold float32 `json:"threshold"` Threshold float32
history []bool history []bool
lastFailReason string lastFailReason string
@@ -59,7 +59,7 @@ func (mon *AbstractMonitor) do() bool {
func (mon *AbstractMonitor) Validate() []string { func (mon *AbstractMonitor) Validate() []string {
return []string{} return []string{}
} }
func (mon *AbstractMonitor) GetMonitor() *AbstractMonitor { func (mon AbstractMonitor) GetMonitor() AbstractMonitor {
return mon return mon
} }