-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathloggers.go
More file actions
88 lines (72 loc) · 2.13 KB
/
loggers.go
File metadata and controls
88 lines (72 loc) · 2.13 KB
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
func loggersList(config *Config) ([]string, error) {
lgDirPath := path.Clean(config.configPath + "/scripts/loggers/")
stat, err := os.Stat(lgDirPath)
if err != nil {
return nil, fmt.Errorf("invalid 'loggers' directory '%s': %s", lgDirPath, err)
}
if !stat.Mode().IsDir() {
return nil, fmt.Errorf("is not a directory '%s'", lgDirPath)
}
scripts, err := filepath.Glob(lgDirPath + "/*")
if err != nil {
return nil, fmt.Errorf("error listing '%s' directory: %s", lgDirPath, 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 loggersExec(run *Run) {
varMap := make(map[string]interface{})
varMap["NOSEE_SRV"] = GlobalConfig.Name
varMap["VERSION"] = NoseeVersion
varMap["HOST_NAME"] = run.Host.Name
varMap["HOST_FILE"] = run.Host.Filename
varMap["CLASSES"] = strings.Join(run.Host.Classes, ",")
var valuesBuff bytes.Buffer
for _, result := range run.TaskResults {
for key, val := range result.Values {
// df.toml;DISK_FULLEST_PERC;27
str := fmt.Sprintf("%s;%s;%s\n", result.Task.Probe.Filename, key, val)
valuesBuff.WriteString(str)
}
}
go func() {
for _, script := range globalLogers {
cmd := exec.Command(script)
// we inject Values thru stdin:
cmd.Stdin = strings.NewReader(valuesBuff.String())
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 logger '%s': %s: %s", script, err, bytes.TrimSpace(cmdOut))
} else {
Trace.Printf("logger '%s' OK: %s", script, bytes.TrimSpace(cmdOut))
}
}
}()
}