通过golang编写并发程序监控系统中文件大小并通过prometheus告警
package main import ( "fmt" "io/ioutil" "os" "path/filepath" "sync" ) const ( maxFileSize int64 = 10 * 1024 * 1024 metricsName string = "node_filesystem_file_size" nodeFilesystemFileSize string = "/host/prom-textfile/node-filesystem-file-size.prom" ) type FileInfo struct { Path string Size int64 MountPoint string FileName string } func main() { dirs := []string{"/web", "/var/log", "/data"} wg := &sync.WaitGroup{} fileChan := make(chan FileInfo) for _, dir := range dirs { wg.Add(1) go func(dir string) { defer wg.Done() err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() && info.Size() > maxFileSize { fileChan <- FileInfo{ FileName: info.Name(), MountPoint: dir, Path: path, Size: info.Size(), } } return nil }) if err != nil { fmt.Printf("Error walking directory %s: %s\n", dir, err) } }(dir) } if err := ioutil.WriteFile(nodeFilesystemFileSize, []byte{}, 0644); err != nil { fmt.Printf("清空文件内容失败: %s\n", err) } go func() { for fileInfo := range fileChan { writeCollectorFile(fileInfo) } }() wg.Wait() close(fileChan) } func writeCollectorFile(fileInfo FileInfo) { f, err := os.OpenFile(nodeFilesystemFileSize, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) if err != nil { fmt.Printf("打开文件失败: %s\n", err) return } defer f.Close() _, err = f.WriteString(fmt.Sprintf("%s{path=\"%s\", mountPoint=\"%s\", fileName=\"%s\"} %d\n", metricsName, fileInfo.Path, fileInfo.MountPoint, fileInfo.FileName, fileInfo.Size)) if err != nil { fmt.Printf("写入文件失败: %s\n", err) } }
效果如下: