func doDump(expInvPath, discoveryPath string) (err error) { inv, err := w.NewInventory(discoveryPath) if err != nil { return } err = inv.Reload() if err != nil { return } expInv := w.NewExpectedInventory() for mangledName, list := range inv.Inventory { expInv[mangledName] = w.ExpectedInventoryEntry{ Red: len(list) - 2, Yellow: len(list) - 1, } } expInvBytes, err := json.MarshalIndent(expInv, "", " ") if err != nil { return } f, err := os.Create(expInvPath) if err != nil { return } _, err = f.Write(expInvBytes) if err != nil { return } return }
func doWatch(expInvPath, discoveryPath string, scanWait int, pagerDutyKeyPath string) (err error) { pdf, err := os.Open(pagerDutyKeyPath) if err != nil { return } json.NewDecoder(pdf).Decode(&pagerdutyConfig) expInv, err := w.LoadExpectedInventoryFromFile(expInvPath) if err != nil { return } inv, err := w.NewInventory(discoveryPath) if err != nil { return } var lastGoodInv *w.Inventory var urgentIncidentId string var notifIncidentId string var urgentIncident w.Incident = nil var notifyIncident w.Incident = nil for { time.Sleep(time.Duration(scanWait) * time.Second) err = inv.Reload() if err != nil { return err } health := expInv.GetSystemHealth(inv) // New style of code, driven by interfaces if len(health.Red) == 0 && urgentIncident != nil { urgentIncident.Resolve() } if len(health.Yellow) == 0 && notifyIncident != nil { notifyIncident.Resolve() } var actionsToMissingIdents map[string]w.InventoryDiffEntry if health.IsDegraded() { scamp.Error.Printf("system is in a degraded state") if lastGoodInv == nil { scamp.Error.Printf("we've never seen a healthy state. watchdog was restarted during outage?") continue } actionsToMissingIdents = lastGoodInv.Diff(inv) } else { lastGoodInv = inv.Clone() continue } if len(health.Red) > 0 /* && urgentIncidentId == "" */ { red := make(map[string]w.InventoryDiffEntry) for _, redAction := range health.Red { red[redAction] = actionsToMissingIdents[redAction] } urgentIncident = w.NewPagerdutyIncident(pagerdutyConfig.UrgentKey, red) } if len(health.Yellow) > 0 /* && notifIncidentId == "" */ { } if urgentIncidentId != "" { scamp.Error.Printf("we have an open urgent incident") } if notifIncidentId != "" { scamp.Error.Printf("we have an open notification incident") } } return }