Пример #1
0
func (self *ROW) GetKey() int64 {
	return genutil.ToInt(self.Openat, int64(-1))
}
Пример #2
0
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)
				}
			}
		}
	}
}