|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/getevo/evo/lib/gpath" |
| 6 | + "gopkg.in/yaml.v3" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "regexp" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +var defaults = Watcher{ |
| 18 | + Interval: 1 * time.Second, |
| 19 | + Log: true, |
| 20 | +} |
| 21 | + |
| 22 | +var configDir = "/etc/watchdog/conf.d" |
| 23 | + |
| 24 | +func main() { |
| 25 | + |
| 26 | + fmt.Println() |
| 27 | + |
| 28 | + Install() |
| 29 | + err := filepath.Walk(configDir, |
| 30 | + func(path string, info os.FileInfo, err error) error { |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + filename, _ := filepath.Abs(path) |
| 36 | + yamlFile, err := ioutil.ReadFile(filename) |
| 37 | + if err != nil { |
| 38 | + panic(err) |
| 39 | + } |
| 40 | + watcher := defaults |
| 41 | + err = yaml.Unmarshal(yamlFile, &watcher) |
| 42 | + if err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | + if watcher.Log && watcher.LogPath == "" { |
| 46 | + watcher.LogPath = path + ".log" |
| 47 | + } |
| 48 | + if watcher.Name == "" { |
| 49 | + watcher.Name = filepath.Base(path) |
| 50 | + } |
| 51 | + fmt.Printf("Watcher %s created \n", watcher.Name) |
| 52 | + createWatcher(watcher) |
| 53 | + return nil |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + log.Println(err) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func Install() { |
| 61 | + if !gpath.IsDirExist(configDir) { |
| 62 | + gpath.MakePath(configDir) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func createWatcher(config Watcher) { |
| 67 | + l, err := os.OpenFile(config.LogPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
| 68 | + if err != nil { |
| 69 | + log.Println(err) |
| 70 | + } |
| 71 | + var regexes = []*regexp.Regexp{} |
| 72 | + for _, item := range config.Regex { |
| 73 | + regexes = append(regexes, regexp.MustCompile(item)) |
| 74 | + } |
| 75 | + go func() { |
| 76 | + for { |
| 77 | + for _, command := range config.Command { |
| 78 | + out, err := exec.Command("bash", "-c", command).Output() |
| 79 | + if err != nil { |
| 80 | + if config.Log { |
| 81 | + l.WriteString(string(out)) |
| 82 | + } |
| 83 | + log.Println(err) |
| 84 | + continue |
| 85 | + } |
| 86 | + |
| 87 | + fmt.Println(string(out)) |
| 88 | + for _, regex := range regexes { |
| 89 | + if regex.Match(out) && config.OnMatch != "" { |
| 90 | + exec.Command("bash", "-c", config.OnMatch).Output() |
| 91 | + continue |
| 92 | + } else { |
| 93 | + exec.Command("bash", "-c", config.Else).Output() |
| 94 | + continue |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + for _, item := range config.Contains { |
| 99 | + if strings.Contains(string(out), item) && config.OnMatch != "" { |
| 100 | + exec.Command("bash", "-c", config.OnMatch).Output() |
| 101 | + continue |
| 102 | + } else { |
| 103 | + exec.Command("bash", "-c", config.Else).Output() |
| 104 | + continue |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + time.Sleep(config.Interval) |
| 109 | + |
| 110 | + } |
| 111 | + } |
| 112 | + }() |
| 113 | +} |
0 commit comments