diff --git a/plugins/inputs/fluentd/README.md b/plugins/inputs/fluentd/README.md index a8b5782b5054d..70222a62a4720 100644 --- a/plugins/inputs/fluentd/README.md +++ b/plugins/inputs/fluentd/README.md @@ -8,19 +8,18 @@ This plugin understands data provided by /api/plugin.json resource (/api/config. ```toml # Read metrics exposed by fluentd in_monitor plugin [[inputs.fluentd]] - ## This plugin only reads information exposed by fluentd using /api/plugins.json. - ## Tested using 'fluentd' version '0.14.9' - ## - ## Endpoint: - ## - only one URI is allowed - ## - https is not supported - endpoint = "http://localhost:24220/api/plugins.json" - - ## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent) - exclude = [ - "monitor_agent", - "dummy", - ] + ## This plugin reads information exposed by fluentd (using /api/plugins.json endpoint). + ## + ## Endpoint: + ## - only one URI is allowed + ## - https is not supported + endpoint = "http://localhost:24220/api/plugins.json" + + ## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent) + exclude = [ + "monitor_agent", + "dummy", + ] ``` ### Measurements & Fields: diff --git a/plugins/inputs/fluentd/fluentd.go b/plugins/inputs/fluentd/fluentd.go index 156660d41917d..963bc14002028 100644 --- a/plugins/inputs/fluentd/fluentd.go +++ b/plugins/inputs/fluentd/fluentd.go @@ -6,7 +6,6 @@ import ( "io/ioutil" "net/http" "net/url" - "reflect" "time" "github.com/influxdata/telegraf" @@ -17,18 +16,18 @@ const ( measurement = "fluentd" description = "Read metrics exposed by fluentd in_monitor plugin" sampleConfig = ` - ## This plugin only reads information exposed by fluentd using /api/plugins.json. - ## - ## Endpoint: - ## - only one URI is allowed - ## - https is not supported - endpoint = "http://localhost:24220/api/plugins.json" - - ## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent) - exclude = [ - "monitor_agent", - "dummy", - ] + ## This plugin reads information exposed by fluentd (using /api/plugins.json endpoint). + ## + ## Endpoint: + ## - only one URI is allowed + ## - https is not supported + endpoint = "http://localhost:24220/api/plugins.json" + + ## Define which plugins have to be excluded (based on "type" field - e.g. monitor_agent) + exclude = [ + "monitor_agent", + "dummy", + ] ` ) @@ -39,6 +38,10 @@ type Fluentd struct { client *http.Client } +type endpointInfo struct { + Payload []pluginData `json:"plugins"` +} + type pluginData struct { PluginID string `json:"plugin_id"` PluginType string `json:"type"` @@ -55,53 +58,19 @@ type pluginData struct { // Returns: // pluginData: slice that contains parsed plugins // error: error that may have occurred -func parse(data []byte) ([]pluginData, error) { - var ( - pdPoint pluginData - pdPointArray []pluginData - parsed map[string]interface{} - err error - ) - - if err = json.Unmarshal(data, &parsed); err != nil { - return pdPointArray, err - } - - switch parsed["plugins"].(type) { - case []interface{}: - // Iterate through all plugins in array - for _, plugin := range parsed["plugins"].([]interface{}) { - - tmpInterface := make(map[string]interface{}) - - // Go through all fields in plugin - for name, value := range plugin.(map[string]interface{}) { - - tags := reflect.ValueOf(pdPoint) - // Iterate through pluginData structure and assign field in case - // when we have field that name is coresponing with field tagged in JSON structure - for i := 0; i < tags.Type().NumField(); i++ { - if tag, ok := tags.Type().Field(i).Tag.Lookup("json"); ok { - if tag == name && value != nil { - tmpInterface[tag] = value - } - } - } - } +func parse(data []byte) (datapointArray []pluginData, err error) { + var endpointData endpointInfo - // Marshal each plugin and Unmarshal it to fit into pluginData structure - tmpByte, err := json.Marshal(tmpInterface) - if err = json.Unmarshal(tmpByte, &pdPoint); err != nil { - return pdPointArray, fmt.Errorf("Processing JSON structure") - } + if err = json.Unmarshal(data, &endpointData); err != nil { + err = fmt.Errorf("Processing JSON structure") + return + } - pdPointArray = append(pdPointArray, pdPoint) - } - default: - return pdPointArray, fmt.Errorf("Unknown JSON structure") + for _, point := range endpointData.Payload { + datapointArray = append(datapointArray, point) } - return pdPointArray, err + return } // Description - display description