Skip to content

Commit 384a6d0

Browse files
committed
First push
0 parents  commit 384a6d0

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
3+
# IntelliJ project files
4+
.idea
5+
*.iml
6+
out
7+
gen
8+
### Go template
9+
# Binaries for programs and plugins
10+
*.exe
11+
*.exe~
12+
*.dll
13+
*.so
14+
*.dylib
15+
16+
# Test binary, built with `go test -c`
17+
*.test
18+
19+
# Output of the go coverage tool, specifically when used with LiteIDE
20+
*.out
21+
22+
# Dependency directories (remove the comment below to include it)
23+
# vendor/
24+
25+
# SqlLite Databases
26+
*.sqlite*
27+
28+
29+
# vscode
30+
.vscode

main.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
}

model.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "time"
4+
5+
type Watcher struct {
6+
Name string `yaml:"name"`
7+
Command []string `yaml:"command"`
8+
Regex []string `yaml:"regex"`
9+
Contains []string `yaml:"contains"`
10+
OnMatch string `yaml:"onmatch"`
11+
Else string `yaml:"else"`
12+
Interval time.Duration `yaml:"interval"`
13+
Log bool `yaml:"log"`
14+
LogPath string `yaml:"path"`
15+
}

0 commit comments

Comments
 (0)