func getCurrOps() { // Set the value of currentOperator on Monim start conn, err := cim.NewCimConnection(alertBaseHost, "alertbase01", fmt.Sprintf("alertbase01-%d", time.Now().UnixNano())) if err != nil { fmt.Println("getCurrOps : Cannot connect to alertbase ; err = ", err) currentOperator = "NA" return } defer conn.Close() res, err := conn.RunCommand("getallopsjson") if err != nil { fmt.Println("getCurrOps : Cannot run getallopsjson cmd in alertbase via cim ; err =", err) currentOperator = "NA" return } var opslst []alertbaseutil.OPSROW err = json.Unmarshal([]byte(res), &opslst) if err != nil { fmt.Println("getCurrOps : Error decoding alertbase output for cmd = getallopsjson; err =", err) currentOperator = "NA" } else if len(opslst) == 0 { currentOperator = "NA" // No one has ever takenover yet } else { currentOperator = opslst[0].Opsname } }
func getAlertsJson(w http.ResponseWriter, r *http.Request, usr string) { conn, err := cim.NewCimConnection(alertBaseHost, "alertbase01", fmt.Sprintf("alertbase01-%d", time.Now().UnixNano())) if err != nil { http.Error(w, "Monim cannot connect to alertbase", 500) return } defer conn.Close() res, err := conn.RunCommand("getalljson") if err != nil { http.Error(w, "Error running cim command in alertbase", 500) return } w.Write([]byte(res)) }
func addOps(w http.ResponseWriter, r *http.Request, usr string) { currentOperator = usr csvline := fmt.Sprintf("%s,%d", usr, int64(time.Now().UnixNano()/1000000)) conn, err := cim.NewCimConnection(alertBaseHost, "alertbase01", fmt.Sprintf("alertbase01-%d", time.Now().UnixNano())) if err != nil { http.Error(w, "Monim cannot connect to alertbase", 500) return } defer conn.Close() res, err := conn.RunCommand("addops " + csvline) if err != nil || res != "done" { http.Error(w, "Error running cim command in alertbase", 500) return } mailer(opsdl, fmt.Sprintf("%s (%s) has taken over", username[usr], phonenumber[usr])) }
func editAlert(w http.ResponseWriter, r *http.Request, usr string) { editkvplist := r.FormValue("editkvplist") if editkvplist == "" { return } editkvplist, _ = url.QueryUnescape(editkvplist) log.Println("Got editalert req from", usr, "editkvplist =", editkvplist) conn, err := cim.NewCimConnection(alertBaseHost, "alertbase01", fmt.Sprintf("alertbase01-%d", time.Now().UnixNano())) if err != nil { http.Error(w, "Monim cannot connect to alertbase", 500) return } defer conn.Close() res, err := conn.RunCommand("editalert " + editkvplist) if err != nil || res != "done" { http.Error(w, "Error running cim command in alertbase", 500) return } alert := new(alertbaseutil.ROW) alert.SetFromKVL(editkvplist) if alert.Status == "owned" || alert.Status == "closed" { owner := usr if alert.Owner != "" { owner = alert.Owner } closed := "" if alert.Status == "closed" { closed = "closed" } kk := alert.GetKey() if openat2sent2[kk] { mailer(openat2esc[kk], fmt.Sprintf("Alert %s @%s '%s'", closed, owner, alert.Subject)) } else if openat2sent1[kk] { mailer(email[currentOperator], fmt.Sprintf("Alert %s @%s '%s'", closed, owner, alert.Subject)) } } }
func init_connection() (*cim.CimConnection, error) { conn, err := cim.NewCimConnection(opt.Host_, "alertbase01", fmt.Sprintf("alertbase01-%d", time.Now().UnixNano())) return conn, err }
func alertMailer() { conn, err := cim.NewCimConnection(alertBaseHost, "alertbase01", fmt.Sprintf("alertbase01-%d", time.Now().UnixNano())) if err != nil { log.Println("alertMailer : Cannot connect to alertbase; err =", err) conn = nil } defer func() { if conn != nil { conn.Close() } }() res := "" lastsentesc1 := map[int64]time.Time{} lastsentesc2 := map[int64]time.Time{} alertlst := []alertbaseutil.ROW{} lastcleanupat := time.Now() for { if conn == nil { log.Println("alertMailer : conn is nil, so trying to reconnect to alertbase") conn, err = cim.NewCimConnection(alertBaseHost, "alertbase01", fmt.Sprintf("alertbase01-%d", time.Now().UnixNano())) if err != nil { log.Println("alertMailer : Cannot connect to alertbase ; err =", err) conn = nil time.Sleep(time.Minute) } } if conn != nil && err == nil { res, err = conn.RunCommand("getalljson") if err != nil { log.Println("alertMailer : Error executing 'getalljson' cmd on alertbase via cim, will try reconnecting...") conn.Close() conn = nil time.Sleep(time.Minute) } else { err = json.Unmarshal([]byte(res), &alertlst) if err != nil { log.Println("alertMailer : Error decoding alertbase output for cmd = 'getalljson'; err =", err) } } } for _, alert := range alertlst { kk := alert.GetKey() openat2esc[kk] = alert.Escalate if alert.Status != "open" { continue } openat := alert.GetOpenat() lim1 := openat.Add(time.Duration(genutil.ToInt(alert.Escalatemin1, 10000)) * time.Minute) lim2 := openat.Add(time.Duration(genutil.ToInt(alert.Escalatemin2, 10000)) * time.Minute) now := time.Now() if now.After(lim2) && (now.Sub(lastsentesc2[kk]) >= 5*time.Hour) { mailer(alert.Escalate, fmt.Sprintf("The alert '%s' unowned over %s mins", alert.Subject, alert.Escalatemin2)) lastsentesc2[kk] = now openat2sent2[kk] = true } else if now.After(lim1) && now.Before(lim2) && (now.Sub(lastsentesc1[kk]) >= 10*time.Minute) { mailer(email[currentOperator], fmt.Sprintf("The alert '%s' unowned over %s mins", alert.Subject, alert.Escalatemin1)) lastsentesc1[kk] = now openat2sent1[kk] = true } } time.Sleep(time.Minute) //------------- Polls alertbase every ~ 1min now := time.Now() if now.Sub(lastcleanupat) >= 24*time.Hour { // -- cleanup every day // Time for cleaning up old entries in the lastsentesc* and openat2* maps lastcleanupat = now kkset := map[int64]bool{} for kk, _ := range lastsentesc1 { kkset[kk] = true } for kk, _ := range lastsentesc2 { kkset[kk] = true } for kk, _ := range openat2esc { kkset[kk] = true } for kk, _ := range openat2sent1 { kkset[kk] = true } for kk, _ := range openat2sent2 { kkset[kk] = true } lim := int64(now.Add(7*24*time.Hour).UnixNano() / 1000000) for kk, _ := range kkset { if kk <= lim { delete(lastsentesc1, kk) delete(lastsentesc2, kk) delete(openat2esc, kk) delete(openat2sent1, kk) delete(openat2sent2, kk) } } } } }