-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.go
164 lines (137 loc) · 3.95 KB
/
module.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"errors"
"io/ioutil"
"log"
"path/filepath"
)
type Modules struct {
InputModules []Module
TransformModules []Module
OutputModules []Module
InputResponseChan chan *ModuleMetrics
InputChannels []chan int
}
type Module interface {
Init(config *Config, moduleConfig *ModuleConfig) error
Name() string
TearDown() error
}
type TransformModule interface {
TransformMetrics(metrics []Metric) ([]Metric, error)
}
type InputModule interface {
GetMetrics() (*ModuleMetrics, error)
}
type OutputModule interface {
SendMetrics([]*ModuleMetrics) error
}
func getModules(config Config) Modules {
files, err := ioutil.ReadDir(config.ConfigPath)
if err != nil {
log.Fatalf("Problem loading modules: %v", err)
}
modules := Modules{}
modules.InputResponseChan = make(chan *ModuleMetrics, len(files)*2)
for _, file := range files {
if file.IsDir() {
log.Printf("Found subdirectory %s in config path %s", file.Name(), config.ConfigPath)
continue
}
extension := file.Name()[len(file.Name())-4:]
if extension != "yaml" {
log.Printf("Found non yaml extension %s in config path %s", extension, config.ConfigPath)
continue
}
fullPath := filepath.Join(config.ConfigPath, file.Name())
moduleConfig := parseModuleConfig(fullPath)
if moduleConfig.Enabled {
module := getModule(moduleConfig.Name)
log.Printf("Module %s enabled: %v", moduleConfig.Name, moduleConfig)
switch v := module.(type) {
default:
log.Fatalf("unexpected type %T", v)
case InputModule:
modules.InputModules = append(modules.InputModules, module)
requestChan, err := InitInputModule(module, modules.InputResponseChan)
if err != nil {
log.Fatalf("Failed to initialize input module: %v", err)
}
modules.InputChannels = append(modules.InputChannels, requestChan)
case TransformModule:
modules.TransformModules = append(modules.TransformModules, module)
case OutputModule:
modules.OutputModules = append(modules.OutputModules, module)
}
module.Init(&config, &moduleConfig)
}
}
return modules
}
// InitInputModule creates a channel for making requests on and aggregates the response on the
// responseChan that is passed in. It returns the request channel.
func InitInputModule(module Module, responseChan chan *ModuleMetrics) (chan int, error) {
inputModule, ok := module.(InputModule)
if !ok {
return nil, errors.New("Not an input module")
}
requestChan := make(chan int)
// Create a goroutine to listen for requests on the request channel
go func(module InputModule, moduleName string, requestChan chan int, responseChan chan *ModuleMetrics) {
for _ = range requestChan {
metrics, err := module.GetMetrics()
if err != nil {
log.Printf("Failed to retrieve %s metrics: %v", moduleName, err)
} else {
responseChan <- metrics
}
}
}(inputModule, module.Name(), requestChan, responseChan)
return requestChan, nil
}
func getModule(name string) Module {
switch name {
case CpuModuleName:
return &CPUInputModule{}
case MemoryModuleName:
return &MemoryInputModule{}
case NetworkModuleName:
return &NetworkInputModule{}
case GraphiteModuleName:
return &GraphiteOutputModule{}
case DiskspaceModuleName:
return &DiskspaceInputModule{}
case DiskusageModuleName:
return &DiskusageInputModule{}
case LoadModuleName:
return &LoadInputModule{}
case ProcessessModuleName:
return &ProcessesInputModule{}
case InternalModuleName:
return &InternalInputModule{}
case RedisModuleName:
return &RedisInputModule{}
default:
log.Fatalf("Invalid module: %s", name)
}
return nil
}
func tearDownModules(modules *Modules) {
//close all channels
for _, c := range modules.InputChannels {
close(c)
}
close(modules.InputResponseChan)
// input modules
for _, e := range modules.InputModules {
e.TearDown()
}
// transform modules
for _, e := range modules.TransformModules {
e.TearDown()
}
// output modules
for _, e := range modules.OutputModules {
e.TearDown()
}
}