6
6
"io/ioutil"
7
7
"net/http"
8
8
"net/url"
9
- "reflect"
10
9
"time"
11
10
12
11
"github.com/influxdata/telegraf"
@@ -17,18 +16,18 @@ const (
17
16
measurement = "fluentd"
18
17
description = "Read metrics exposed by fluentd in_monitor plugin"
19
18
sampleConfig = `
20
- ## This plugin only reads information exposed by fluentd using /api/plugins.json.
21
- ##
22
- ## Endpoint:
23
- ## - only one URI is allowed
24
- ## - https is not supported
25
- endpoint = "http://localhost:24220/api/plugins.json"
26
-
27
- ## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
28
- exclude = [
29
- "monitor_agent",
30
- "dummy",
31
- ]
19
+ ## This plugin reads information exposed by fluentd ( using /api/plugins.json endpoint) .
20
+ ##
21
+ ## Endpoint:
22
+ ## - only one URI is allowed
23
+ ## - https is not supported
24
+ endpoint = "http://localhost:24220/api/plugins.json"
25
+
26
+ ## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent)
27
+ exclude = [
28
+ "monitor_agent",
29
+ "dummy",
30
+ ]
32
31
`
33
32
)
34
33
@@ -39,6 +38,10 @@ type Fluentd struct {
39
38
client * http.Client
40
39
}
41
40
41
+ type endpointInfo struct {
42
+ Payload []pluginData `json:"plugins"`
43
+ }
44
+
42
45
type pluginData struct {
43
46
PluginID string `json:"plugin_id"`
44
47
PluginType string `json:"type"`
@@ -55,53 +58,19 @@ type pluginData struct {
55
58
// Returns:
56
59
// pluginData: slice that contains parsed plugins
57
60
// error: error that may have occurred
58
- func parse (data []byte ) ([]pluginData , error ) {
59
- var (
60
- pdPoint pluginData
61
- pdPointArray []pluginData
62
- parsed map [string ]interface {}
63
- err error
64
- )
65
-
66
- if err = json .Unmarshal (data , & parsed ); err != nil {
67
- return pdPointArray , err
68
- }
69
-
70
- switch parsed ["plugins" ].(type ) {
71
- case []interface {}:
72
- // Iterate through all plugins in array
73
- for _ , plugin := range parsed ["plugins" ].([]interface {}) {
74
-
75
- tmpInterface := make (map [string ]interface {})
76
-
77
- // Go through all fields in plugin
78
- for name , value := range plugin .(map [string ]interface {}) {
79
-
80
- tags := reflect .ValueOf (pdPoint )
81
- // Iterate through pluginData structure and assign field in case
82
- // when we have field that name is coresponing with field tagged in JSON structure
83
- for i := 0 ; i < tags .Type ().NumField (); i ++ {
84
- if tag , ok := tags .Type ().Field (i ).Tag .Lookup ("json" ); ok {
85
- if tag == name && value != nil {
86
- tmpInterface [tag ] = value
87
- }
88
- }
89
- }
90
- }
61
+ func parse (data []byte ) (datapointArray []pluginData , err error ) {
62
+ var endpointData endpointInfo
91
63
92
- // Marshal each plugin and Unmarshal it to fit into pluginData structure
93
- tmpByte , err := json .Marshal (tmpInterface )
94
- if err = json .Unmarshal (tmpByte , & pdPoint ); err != nil {
95
- return pdPointArray , fmt .Errorf ("Processing JSON structure" )
96
- }
64
+ if err = json .Unmarshal (data , & endpointData ); err != nil {
65
+ err = fmt .Errorf ("Processing JSON structure" )
66
+ return
67
+ }
97
68
98
- pdPointArray = append (pdPointArray , pdPoint )
99
- }
100
- default :
101
- return pdPointArray , fmt .Errorf ("Unknown JSON structure" )
69
+ for _ , point := range endpointData .Payload {
70
+ datapointArray = append (datapointArray , point )
102
71
}
103
72
104
- return pdPointArray , err
73
+ return
105
74
}
106
75
107
76
// Description - display description
0 commit comments