-
Notifications
You must be signed in to change notification settings - Fork 0
/
mackerel-fluentd.go
106 lines (91 loc) · 2.31 KB
/
mackerel-fluentd.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
mp "github.com/mackerelio/go-mackerel-plugin"
)
type FluentdMetrics struct {
Target string
Tempfile string
plugins []FluentdPluginMetrics
err error
}
type FluentdPluginMetrics struct {
RetryCount uint64 `json:"retry_count"`
OutputPlugin bool `json:"output_plugin"`
Config map[string]string `json:"config"`
Type string `json:"type"`
PluginCategory string `json:"plugin_category"`
PluginID string `json:"plugin_id"`
}
type FluentMonitorJSON struct {
Plugins []FluentdPluginMetrics `json:"plugins"`
}
func (f FluentdMetrics) fetch() ([]FluentdPluginMetrics, error) {
return []FluentdPluginMetrics{}, nil
}
func (f *FluentdMetrics) Init() {
resp, err := http.Get(f.Target)
if err != nil {
f.err = err
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
f.err = err
return
}
var j FluentMonitorJSON
err = json.Unmarshal(body, &j)
f.plugins = j.Plugins
f.err = err
}
func (f FluentdMetrics) FetchMetrics() (map[string]float64, error) {
metrics := map[string]float64{}
for _, plugin := range f.plugins {
metrics[plugin.PluginID] = float64(plugin.RetryCount)
}
return metrics, f.err
}
func (f FluentdMetrics) GraphDefinition() map[string](mp.Graphs) {
metrics := [](mp.Metrics){}
for _, p := range f.plugins {
metrics = append(metrics, mp.Metrics{
Name: p.PluginID,
Label: p.PluginID,
Diff: false})
}
return map[string](mp.Graphs){
"fluentd.retry": mp.Graphs{
Label: "Fluentd retry count",
Unit: "integer",
Metrics: metrics,
},
}
}
func main() {
host := flag.String("host", "localhost", "fluentd monitor_agent port")
port := flag.String("port", "24220", "fluentd monitor_agent port")
tempFile := flag.String("tempfile", "", "Temp file name")
flag.Parse()
f := FluentdMetrics{
Target: fmt.Sprintf("http://%s:%s/api/plugins.json", *host, *port),
Tempfile: *tempFile,
}
f.Init()
helper := mp.NewMackerelPlugin(f)
if *tempFile != "" {
helper.Tempfile = *tempFile
} else {
helper.Tempfile = fmt.Sprintf("/tmp/mackerel-plugin-fluentd-%s-%s", *host, *port)
}
if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
helper.OutputDefinitions()
} else {
helper.OutputValues()
}
}