Example #1
0
func dbMon(mc *MgoCmd, req *cmdproto.MgoRequest) {
	atomic.StoreInt32(&mc.dbMonState, 1)
	stop := false

	pingCount := 5
	errCount := 0
	var err error

	ticker := time.NewTicker(time.Second * 2)

	defer func() {
		ticker.Stop()
		atomic.StoreInt32(&mc.dbMonState, 0)
	}()

	dialInfo := &mgo.DialInfo{Addrs: mc.MgoAddrs, Timeout: (10 * time.Second)}
	mgoSession, err := mgo.DialWithInfo(dialInfo)
	mgoSession.SetSocketTimeout(time.Second * 10)
	addrs, _ := net.InterfaceAddrs()
	if err != nil {
		cmdlog.EPrintln(err.Error())
		cmds.SendMail("mongod", fmt.Sprintf("[%s]---%+v---mongod ping failed, please notice!", time.Now().UTC().String(), addrs))
		return
	}

	for {
		select {
		case <-ticker.C:
			for i := 0; i < pingCount; i++ {
				err = mgoSession.Ping()
				if err != nil {
					cmdlog.EPrintln(err.Error())
					errCount++
				}
			}
			if errCount == pingCount {
				for i := 0; i < 5; i++ {
					cmds.SendMail("mongod", fmt.Sprintf("[%s]---%+v---mongod ping failed, please notice!", time.Now().UTC().String(), addrs))
				}
				errCount = 0
				stop = true
			}
		}
		if stop == true {
			break
		}
	}
}
Example #2
0
func diskMon(sc *SystemCmd) {
	ticker := time.NewTicker(time.Second * 10)
	atomic.StoreInt32(&sc.diskMonState, 1)
	defer func() {
		ticker.Stop()
		atomic.StoreInt32(&sc.diskMonState, 0)
	}()

	stop := false
	addrs, _ := net.InterfaceAddrs()
	for {
		select {
		case <-ticker.C:
			diskInfo := cmds.DiskUsage("/")
			diskLeft := int(float64(diskInfo.Free) / float64(diskInfo.All) * 100)
			if diskLeft < sc.DiskLeftNotify {
				for i := 0; i < 5; i++ {
					cmds.SendMail("Disk Usage", fmt.Sprintf("[%s]---(%+v)---Disk left %d%%, please clear mongo data!", time.Now().UTC().String(), addrs, diskLeft))
				}
				stop = true
			}
		}
		if stop == true {
			break
		}
	}

	return
}