-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskel.go
108 lines (87 loc) · 2.87 KB
/
skel.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
package skel
import (
"encoding/json"
"fmt"
"os"
"github.com/Sirupsen/logrus"
)
const NAME string = "skel"
const PROVIDER string = "skel" //we might want to make this an env tied to nginx version or app name maybe...
const PROTOCOL_VERSION string = "1"
//SkelConfig is the keeper of the config
type SkelConfig struct {
SkelHost string
}
// InventoryData is the data type for inventory data produced by a plugin data
// source and emitted to the agent's inventory data store
type InventoryData map[string]interface{}
// MetricData is the data type for events produced by a plugin data source and
// emitted to the agent's metrics data store
type MetricData map[string]interface{}
// EventData is the data type for single shot events
type EventData map[string]interface{}
// PluginData defines the format of the output JSON that plugins will return
type PluginData struct {
Name string `json:"name"`
ProtocolVersion string `json:"protocol_version"`
PluginVersion string `json:"plugin_version"`
Metrics []MetricData `json:"metrics"`
Inventory map[string]InventoryData `json:"inventory"`
Events []EventData `json:"events"`
Status string `json:"status"`
}
// OutputJSON takes an object and prints it as a JSON string to the stdout.
// If the pretty attribute is set to true, the JSON will be idented for easy reading.
func OutputJSON(data interface{}, pretty bool) error {
var output []byte
var err error
if pretty {
output, err = json.MarshalIndent(data, "", "\t")
} else {
output, err = json.Marshal(data)
}
if err != nil {
return fmt.Errorf("Error outputting JSON: %s", err)
}
if string(output) == "null" {
fmt.Println("[]")
} else {
fmt.Println(string(output))
}
return nil
}
func Run(log *logrus.Logger, prettyPrint bool, version string) {
// Initialize the output structure
var data = PluginData{
Name: NAME,
ProtocolVersion: PROTOCOL_VERSION,
PluginVersion: version,
Inventory: make(map[string]InventoryData),
Metrics: make([]MetricData, 0),
Events: make([]EventData, 0),
}
var config = SkelConfig{
SkelHost: os.Getenv("KEY"),
}
validateConfig(log, config)
var metric = getMetric(log, config)
data.Metrics = append(data.Metrics, metric)
fatalIfErr(log, OutputJSON(data, prettyPrint))
}
func getMetric(log *logrus.Logger, config SkelConfig) map[string]interface{} {
return map[string]interface{}{
"event_type": "LoadBalancerSample",
"provider": PROVIDER,
"skel.stat": 1,
}
}
func validateConfig(log *logrus.Logger, config SkelConfig) {
if config.SkelHost == "" {
log.Fatal("Config Yaml is missing values. Please check the config to continue")
}
}
func fatalIfErr(log *logrus.Logger, err error) {
if err != nil {
log.WithError(err).Fatal("can't continue")
}
}