-
Notifications
You must be signed in to change notification settings - Fork 13
/
nagios.go
84 lines (73 loc) · 2.71 KB
/
nagios.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
package main
import (
"errors"
"os/exec"
"path/filepath"
"strings"
)
// GetNagiosTasks sends tasks to dump Nagios data for each project that contain
// a Nagios pod. It is an error if no projects contain a Nagios pod.
func GetNagiosTasks(tasks chan<- Task, runner Runner, projects []string) {
foundANagiosPod := false
for _, p := range projects {
pods, err := getResourceNamesBySubstr(p, "pod", "nagios")
if err != nil {
tasks <- NewError(err)
continue
}
for _, pod := range pods {
foundANagiosPod = true
tasks <- GetNagiosStatusData(runner, p, pod)
tasks <- GetNagiosHistoricalData(runner, p, pod)
}
}
if !foundANagiosPod {
tasks <- NewError(errors.New("a Nagios pod could not be found in any project. For a more thorough analysis, please ensure Nagios is running in all RHMAP projects"))
}
}
// GetNagiosStatusData is a task factory for tasks that fetch Nagios status from
// the given pod in project.
func GetNagiosStatusData(r Runner, project, pod string) Task {
return func() error {
cmd := exec.Command("oc", "-n", project, "exec", pod, "--", "cat", "/var/log/nagios/status.dat")
fname := pod + "_status.dat"
path := filepath.Join("projects", project, "nagios", fname)
return MarkErrorAsIgnorable(r.Run(cmd, path))
}
}
// GetNagiosHistoricalData is a task factory for tasks that fetch Nagios
// archives from the given pod in project.
func GetNagiosHistoricalData(r Runner, project, pod string) Task {
return func() error {
cmd := exec.Command("oc", "-n", project, "exec", pod, "--", "tar", "-c", "-C", "/var/log/nagios", "archives")
fname := pod + "_history.tar"
path := filepath.Join("projects", project, "nagios", fname)
return MarkErrorAsIgnorable(r.Run(cmd, path))
}
}
// ResourceMatchFactory is an interface of a factory which will take a project name,
// a resource type, and substring and return any of the requested resources in that
// project which contain the substring in their name.
type ResourceMatchFactory func(project, resource, substr string) ([]string, error)
// getResourceNamesBySubstr returns a list of names for the provided resource type that contain
// the provided string, in the provided project.
func getResourceNamesBySubstr(project, resource, substr string) ([]string, error) {
// FIXME: take runner as argument.
runner := simpleRunner{}
resources, err := GetResourceNames(runner, project, resource)
if err != nil {
return nil, err
}
filtered := resources[:0]
for _, resource := range resources {
if strings.Contains(resource, substr) {
filtered = append(filtered, resource)
}
}
return filtered, nil
}
// FIXME: get rid of this, use a DumpRunner.
type simpleRunner struct{}
func (simpleRunner) Run(cmd *exec.Cmd, path string) error {
return cmd.Run()
}