From 33bb722e06fe22b9853377a3fc62e6aec542807b Mon Sep 17 00:00:00 2001 From: "A. Feldmann" Date: Wed, 14 Mar 2018 09:48:49 +0100 Subject: [PATCH] go fmt --- cli/main.go | 5 +++++ config.go | 1 + incident.go | 26 ++++++++++++++++++++++++++ monitor.go | 10 ++++++++++ readme.md | 1 + 5 files changed, 43 insertions(+) diff --git a/cli/main.go b/cli/main.go index 2ac47ce..2fb525b 100644 --- a/cli/main.go +++ b/cli/main.go @@ -38,6 +38,7 @@ Options: -h --help Show this screen. --version Show version --immediate Tick immediately (by default waits for first defined interval) + --restarted Get open incidents before start monitoring (if monitor died or restarted) Environment varaibles: CACHET_API override API url from configuration @@ -58,6 +59,10 @@ func main() { cfg.Immediate = immediate.(bool) } + if restarted, ok := arguments["--restarted"]; ok { + cfg.Restarted = restarted.(bool) + } + if name := arguments["--name"]; name != nil { cfg.SystemName = name.(string) } diff --git a/config.go b/config.go index af2a05c..31e7012 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,7 @@ type CachetMonitor struct { Monitors []MonitorInterface `json:"-" yaml:"-"` Immediate bool `json:"-" yaml:"-"` + Restarted bool `json:"-" yaml:"-"` } // Validate configuration diff --git a/incident.go b/incident.go index dd7a6b1..e2bf655 100644 --- a/incident.go +++ b/incident.go @@ -21,6 +21,32 @@ type Incident struct { ComponentStatus int `json:"component_status"` } +//Get the last still open incident +func (mon *AbstractMonitor) Get(cfg *CachetMonitor) (*Incident, error) { + requestType := "GET" + requestURL := fmt.Sprintf("/incidents?component_id=%d", mon.ComponentID) + _, body, err := cfg.API.NewRequest(requestType, requestURL, nil) + if err != nil { + return nil, err + } + data := make([]Incident, 0) + if err := json.Unmarshal(body.Data, &data); err != nil { + return nil, fmt.Errorf("Cannot parse incident body: %v, %v", err, string(body.Data)) + } + //filter out resolved incidents + openIncidents := make([]Incident, 0) + for _, i := range data { + if i.Status < 4 { + openIncidents = append(openIncidents, i) + } + } + if len(openIncidents) == 0 { + return nil, nil + } + return &openIncidents[0], nil + +} + // Send - Create or Update incident func (incident *Incident) Send(cfg *CachetMonitor) error { switch incident.Status { diff --git a/monitor.go b/monitor.go index f7cbba6..9e09657 100644 --- a/monitor.go +++ b/monitor.go @@ -118,6 +118,16 @@ func (mon *AbstractMonitor) ClockStart(cfg *CachetMonitor, iface MonitorInterfac mon.tick(iface) } + if cfg.Restarted { + initialIncident, err := mon.Get(cfg) + if err != nil { + logrus.Warn("could not fetch initial incident: %v", err) + } + if initialIncident != nil { + mon.incident = initialIncident + } + } + ticker := time.NewTicker(mon.Interval * time.Second) for { select { diff --git a/readme.md b/readme.md index e40e248..7041068 100644 --- a/readme.md +++ b/readme.md @@ -111,6 +111,7 @@ Options: -h --help Show this screen. --version Show version --immediate Tick immediately (by default waits for first defined interval) + --restarted Get open incidents before start monitoring (if monitor died or restarted) Environment varaibles: CACHET_API override API url from configuration