示例#1
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
}
示例#2
0
func diskMon(mc *MgoCmd, req *cmdproto.MgoRequest) {
	ticker := time.NewTicker(mc.DiskCheckInterval.Duration)
	stop := false
	collDays := 0
	collName := ""
	percent := 100
	var err1 error
	atomic.StoreInt32(&mc.diskMonState, 1)
	for {
		select {
		case <-mc.stopDiskMonCh:
			stop = true
		case <-ticker.C:
			//check if need to lru.
			ds := cmds.DiskUsage("/")
			percent = int(float64(ds.Free) / float64(ds.All) * 100)
			if percent < mc.MgoLRUGate {
				//cmdlog.Printf("mongodb is lru...\n")
				//do delete collections.
				names, err := mc.mgoSession.DB(req.DB).CollectionNames()
				if err != nil {
					cmdlog.EPrintln(err.Error())
					continue
				}
				resNames := filterCollNames(names)
				for _, v := range resNames {
					collDays = len(v)
					for i := 0; i < collDays/3; i++ {
						collName = v[i]
						//cmdlog.Printf("mongodb drop collection %s\n", collName)
						err1 = mc.mgoSession.DB(req.DB).C(collName).DropCollection()
						if err1 != nil {
							cmdlog.EPrintln(err1.Error())
						}
					}
				}
			}
		}
		if stop == true {
			ticker.Stop()
			atomic.StoreInt32(&mc.diskMonState, 0)
			break
		}
	}
}