Esempio n. 1
0
func configLogger() {
	cfg, err := utils.GetMainConfig()
	if err == nil {
		level := logging.INFO
		if cfg.LogLevel != "" {
			switch strings.ToUpper(cfg.LogLevel) {
			case "DEBUG":
				level = logging.DEBUG
			case "INFO":
				level = logging.INFO
			case "CRITICAL":
				level = logging.CRITICAL
			case "ERROR":
				level = logging.ERROR
			case "WARNING":
				level = logging.WARNING
			case "NOTICE":
				level = logging.NOTICE
			}
		}
		if cfg.LogFile != "" {
			logging.InitSimpleFileLogger(cfg.LogFile, level)
		} else {
			logging.InitLogger(level)
		}
		return
	}
	logging.InitLogger(logging.DEBUG)
}
Esempio n. 2
0
func DefaultBuildClient() *redis.Client {
	port := ":6379"
	if cfg, err := utils.GetMainConfig(); err == nil {
		port = cfg.RedisAddr
	}
	return redis.NewTCPClient(&redis.Options{
		Addr: port,
	})
}
Esempio n. 3
0
func fromBigFile(clue router.Message) bool {
	limit := 0.001
	if cfg, err := utils.GetMainConfig(); err == nil {
		limit = cfg.SingleFileDiskOccupyLimit
	}
	file := clue.FileName
	fs := syscall.Statfs_t{}
	syscall.Statfs(file, &fs)
	if percentage := float64(clue.Size) / (float64(fs.Bsize) * float64(fs.Blocks)); !math.IsInf(percentage, 1) && percentage > limit {
		return true
	}
	return false
}
Esempio n. 4
0
func ScanAbnormal(queue *list.List) {
	c := time.Tick(time.Second)
	action := "info"
	if mcfg, err := utils.GetMainConfig(); err == nil {
		action = mcfg.Action
	}
	for _ = range c {
		erase_list := []string{}
		for {
			if ele := queue.Back(); ele != nil {
				value := queue.Remove(ele)
				msg := value.(router.Message)
				if fromWhiteList(msg) {
					logging.Infof("%v is on  white list,pass.", msg.FileName)
				} else if fromBigFile(msg) {
					if canEraseInstant(msg.FileName) {
						erase_list = append(erase_list, msg.FileName)
					} else {
						logging.Warningf("Big file found and I dare not del.(%v:%v)", msg.FileName, msg.Size)
					}
				} else if can_del, ok := fromBigDirectory(msg); ok {
					if yes, _ := canErase(can_del...); len(yes) > 0 {
						erase_list = append(erase_list, yes...)
					}
				} else {
					logging.Debugf("You're good %s(%s), let you go.", msg.FileName, watchman.HumanReadable(msg.Event))
				}
			} else {
				break
			}
		}
		if len(erase_list) > 0 {
			logging.Infof("[%s] Remove %v", action, erase_list)
			switch action {
			case "info":
				printState(erase_list...)
			case "remove":
				erase(erase_list...)
			}
		}
	}
}
Esempio n. 5
0
func fromBigDirectory(clue router.Message) ([]string, bool) {
	level := 1
	dir := filepath.Dir(clue.FileName)
	if _, err := strconv.Atoi(filepath.Base(dir)); err == nil {
		dir = filepath.Dir(dir)
		level = 2
	}
	tl := utils.GetExpiredDate(dir)
	timelimit := time.Now().AddDate(0, 0, -tl)
	limit := 0.001
	if cfg, err := utils.GetMainConfig(); err == nil {
		limit = cfg.TrivialFilesOccupyLimit
	}
	list, total := utils.Find(dir, level, 1000000, timelimit)
	fs := syscall.Statfs_t{}
	syscall.Statfs(dir, &fs)
	if percentage := float64(total) / (float64(fs.Bsize) * float64(fs.Blocks)); !math.IsInf(percentage, 1) && percentage > limit {
		return list, true
	}
	return []string{}, false
}