-
Notifications
You must be signed in to change notification settings - Fork 21
/
registry.go
157 lines (120 loc) · 3.39 KB
/
registry.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
package plugins
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/amonapp/amonagent/internal/settings"
)
// ConfiguredPlugin - XXX
type ConfiguredPlugin struct {
Name string
Plugin Plugin
}
// PluginConfig - XXX
type PluginConfig struct {
Path string
Name string
}
// PluginConfigPath - XXX
var PluginConfigPath = path.Join(settings.ConfigPath, "plugins-enabled")
// GetConfigPath - Simple function that generates the plugin config path for the current distro
func GetConfigPath(plugin string) (PluginConfig, error) {
config := PluginConfig{}
// On Linux /etc/opt/amonagent/plugins-enabled/plugin.conf
var pluginPath = path.Join(PluginConfigPath, strings.Join([]string{plugin, "conf"}, "."))
config.Path = pluginPath
config.Name = plugin
return config, nil
}
// ReadPluginConfig - Reads the file from the expected path and returns it as bytes
func ReadPluginConfig(plugin string) ([]byte, error) {
c, err := GetConfigPath(plugin)
if err != nil {
log.WithFields(log.Fields{
"plugin": plugin,
"path": c.Path,
"error": err,
}).Error("Can't read config file")
}
file, e := ioutil.ReadFile(c.Path)
if e != nil {
return nil, e
}
return file, nil
}
// UmarshalPluginConfig - Converts bytes to interface
func UmarshalPluginConfig(plugin string) (interface{}, error) {
configFile, err := ReadPluginConfig(plugin)
if err != nil {
log.WithFields(log.Fields{
"plugin": plugin,
"error": err,
}).Error("Can't read config file")
}
var data map[string]interface{}
if err := json.Unmarshal(configFile, &data); err != nil {
log.WithFields(log.Fields{
"plugin": plugin,
"error": err,
}).Error("Can't unmarshal config file")
}
return data, nil
}
// GetAllEnabledPlugins - XXX
func GetAllEnabledPlugins() ([]PluginConfig, error) {
fileList := []PluginConfig{}
if _, err := os.Stat(PluginConfigPath); os.IsNotExist(err) {
if err != nil {
if os.IsNotExist(err) {
// Plugin config directory doesn't exist for some reason. Create
PluginDirErr := os.MkdirAll(PluginConfigPath, os.ModePerm)
if PluginDirErr != nil {
fmt.Printf("Plugin config directory doesn't exist: %s\n", PluginConfigPath)
}
}
return fileList, err
}
}
filepath.Walk(PluginConfigPath, func(path string, f os.FileInfo, err error) error {
if !f.IsDir() {
// Only files ending with .conf
fileName := strings.Split(f.Name(), ".conf")
if len(fileName) == 2 {
// Go over the list of all available(loaded) plugins and add the config only if it is for an existing plugin
for name := range Plugins {
if name == fileName[0] {
f := PluginConfig{Path: path, Name: fileName[0]}
fileList = append(fileList, f)
}
}
}
}
return nil
})
return fileList, nil
}
// Plugin - XXX
type Plugin interface {
// Description returns a one-sentence description on the Plugin
Description() string
SampleConfig() string
// Collects all the metrics and returns a struct with the results
Collect() (interface{}, error)
// Start starts the service - Optional
Start() error
// Stop stops the services and closes any necessary channels and connections - Optional
Stop()
}
// PluginRegistry - XXX
type PluginRegistry func() Plugin
// Plugins - XXX
var Plugins = map[string]PluginRegistry{}
// Add - XXX
func Add(name string, registry PluginRegistry) {
Plugins[name] = registry
}