-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathheartbeat.go
89 lines (73 loc) · 2.14 KB
/
heartbeat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"time"
)
func heartbeatsList(config *Config) ([]string, error) {
hbDirPath := path.Clean(config.configPath + "/scripts/heartbeats/")
stat, err := os.Stat(hbDirPath)
if err != nil {
return nil, fmt.Errorf("invalid 'heartbeats' directory '%s': %s", hbDirPath, err)
}
if !stat.Mode().IsDir() {
return nil, fmt.Errorf("is not a directory '%s'", hbDirPath)
}
scripts, err := filepath.Glob(hbDirPath + "/*")
if err != nil {
return nil, fmt.Errorf("error listing '%s' directory: %s", hbDirPath, err)
}
for _, scriptPath := range scripts {
stat, err := os.Stat(scriptPath)
if err != nil {
return nil, fmt.Errorf("invalid 'script' file '%s': %s", scriptPath, err)
}
if !stat.Mode().IsRegular() {
return nil, fmt.Errorf("is not a regular 'script' file '%s'", scriptPath)
}
_, err = ioutil.ReadFile(scriptPath)
if err != nil {
return nil, fmt.Errorf("error reading script file '%s': %s", scriptPath, err)
}
}
return scripts, nil
}
func heartbeatExecute(script string) {
varMap := make(map[string]interface{})
varMap["NOSEE_SRV"] = GlobalConfig.Name
varMap["VERSION"] = NoseeVersion
varMap["DATETIME"] = time.Now().Format(time.RFC3339)
varMap["STARTTIME"] = appStartTime.Format(time.RFC3339)
varMap["UPTIME"] = (int)(time.Since(appStartTime).Seconds())
cmd := exec.Command(script)
env := os.Environ()
for key, val := range varMap {
env = append(env, fmt.Sprintf("%s=%s", key, InterfaceValueToString(val)))
}
cmd.Env = env
if cmdOut, err := cmd.CombinedOutput(); err != nil {
Warning.Printf("error running heartbeat '%s': %s: %s", script, err, bytes.TrimSpace(cmdOut))
} else {
Trace.Printf("heartbeat '%s' OK: %s", script, bytes.TrimSpace(cmdOut))
}
}
func heartbeatsExecute(scripts []string) {
for _, script := range scripts {
heartbeatExecute(script)
}
}
func heartbeatsSchedule(scripts []string, delay time.Duration) {
go func() {
for {
heartbeatsExecute(scripts)
Info.Printf("heartbeat, %d scripts", len(scripts))
// should check total exec duration and compare to delay, here!
time.Sleep(delay)
}
}()
}