/
main.go
147 lines (126 loc) · 3.36 KB
/
main.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
package main
// based on https://github.com/y-matsuwitter/mackerel-fluentd
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
mp "github.com/mackerelio/go-mackerel-plugin"
)
var Options struct {
Host string
Port int
Tempfile string
}
type Plugin struct {
Target string
Metrics []FluentMetric
Err error
}
type FluentMetric struct {
Id string `json:"plugin_id"`
Category string `json:"plugin_category"`
Type string `json:"type"`
Config map[string]string `json:"config"`
Output bool `json:"output_plugin"`
RetryCount int64 `json:"retry_count"`
BufferQueueLength int64 `json:"buffer_queue_length"`
BufferTotalQueuedSize int64 `json:"buffer_total_queued_size"`
}
type FluentMetrics struct {
Metrics []FluentMetric `json:"plugins"`
}
func (p Plugin) fetchFluentMetrics() ([]FluentMetric, error) {
resp, err := http.Get(p.Target)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var metrics FluentMetrics
err = json.Unmarshal(body, &metrics)
if err != nil {
return nil, err
}
return metrics.Metrics, nil
}
func (p *Plugin) Prepare() {
p.Metrics, p.Err = p.fetchFluentMetrics()
}
func (p Plugin) FetchMetrics() (map[string]float64, error) {
if p.Err != nil {
return nil, p.Err
}
results := make(map[string]float64)
for _, metric := range p.Metrics {
if isEnableFluentMetric(metric) {
results["retry."+metric.Id] = float64(metric.RetryCount)
results["queue."+metric.Id] = float64(metric.BufferQueueLength)
results["size."+metric.Id] = float64(metric.BufferTotalQueuedSize)
}
}
return results, nil
}
func (p Plugin) GraphDefinition() map[string](mp.Graphs) {
metrics := make([]mp.Metrics, 0)
for _, metric := range p.Metrics {
if isEnableFluentMetric(metric) {
metrics = append(metrics, mp.Metrics{
Name: "retry." + metric.Id,
Label: "Retry Count " + metric.Id,
Diff: false,
})
metrics = append(metrics, mp.Metrics{
Name: "queue." + metric.Id,
Label: "Queue Length " + metric.Id,
Diff: false,
})
metrics = append(metrics, mp.Metrics{
Name: "size." + metric.Id,
Label: "Buffer Size " + metric.Id,
Diff: false,
})
}
}
graphs := make(map[string]mp.Graphs)
graphs["fluentd.buffer"] = mp.Graphs{
Label: "Fluentd Buffer",
Metrics: metrics,
}
return graphs
}
func isEnableFluentMetric(m FluentMetric) bool {
if !m.Output {
return false
}
if strings.HasPrefix(m.Id, "object:") {
return false
}
return true
}
func main() {
flag.StringVar(&Options.Host, "host", "localhost", "fluentd monitor_agent host")
flag.IntVar(&Options.Port, "port", 24220, "fluentd monitor_agent port")
flag.StringVar(&Options.Tempfile, "tempfile", "", "Temp file name")
flag.Parse()
plugin := Plugin{
Target: fmt.Sprintf("http://%s:%d/api/plugins.json", Options.Host, Options.Port),
}
plugin.Prepare()
helper := mp.NewMackerelPlugin(plugin)
if Options.Tempfile != "" {
helper.Tempfile = Options.Tempfile
} else {
helper.Tempfile = fmt.Sprintf("/tmp/mackerel-plugin-fluentd-%s-%d", Options.Host, Options.Port)
}
if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
helper.OutputDefinitions()
} else {
helper.OutputValues()
}
}