This commit is contained in:
A. Feldmann
2018-03-14 09:48:49 +01:00
committed by Alexander Trost
parent 3c343fb0a2
commit 33bb722e06
5 changed files with 43 additions and 0 deletions

View File

@@ -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)
}

View File

@@ -17,6 +17,7 @@ type CachetMonitor struct {
Monitors []MonitorInterface `json:"-" yaml:"-"`
Immediate bool `json:"-" yaml:"-"`
Restarted bool `json:"-" yaml:"-"`
}
// Validate configuration

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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