forked from mackerelio/mackerel-agent-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
198 lines (170 loc) · 3.76 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
mp "github.com/mackerelio/go-mackerel-plugin-helper"
)
type mailq struct {
command string
args []string
line int
pattern string
}
var mailqFormats = map[string]mailq{
"postfix": {
command: "postqueue",
args: []string{"-p"},
line: -1,
pattern: `-- \d+ Kbytes in (\d+) Requests\.`,
},
"qmail": {
command: "qmail-qstat",
pattern: `messages in queue: (\d+)`,
},
"exim": {
command: "exim",
args: []string{"-bpc"},
pattern: `(\d+)`,
},
}
type plugin struct {
path string
mailq mailq
keyPrefix, labelPrefix string
}
func (format *mailq) parse(rd io.Reader) (count uint64, err error) {
l, err := getNthLine(rd, format.line)
if err != nil {
return
}
m := regexp.MustCompile(format.pattern).FindStringSubmatch(l)
if m != nil {
count, err = strconv.ParseUint(m[1], 10, 64)
}
return
}
func (p *plugin) fetchMailqCount() (count uint64, err error) {
var path string
if p.path != "" {
path = p.path
} else {
path, err = exec.LookPath(p.mailq.command)
if err != nil {
return
}
}
cmd := exec.Cmd{
Path: path,
Args: append([]string{p.mailq.command}, p.mailq.args...),
}
if p.path != "" {
cmd.Path = p.path
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return
}
err = cmd.Start()
if err != nil {
return
}
count, err = p.mailq.parse(stdout)
if err != nil {
cmd.Wait()
return
}
err = cmd.Wait()
return
}
func (p *plugin) FetchMetrics() (map[string]interface{}, error) {
count, err := p.fetchMailqCount()
if err != nil {
return nil, err
}
return map[string]interface{}{"count": count}, nil
}
func (p *plugin) GraphDefinition() map[string]mp.Graphs {
return map[string]mp.Graphs{
p.keyPrefix: {
Label: p.labelPrefix + " Count",
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "count", Label: "count", Type: "uint64"},
},
},
}
}
func getNthLine(rd io.Reader, nth int) (string, error) {
var result string
brd := bufio.NewReader(rd)
if nth >= 0 { // get nth-first line (0 is the first line)
for i := 0; ; i++ {
s, err := brd.ReadString('\n')
if err == nil || err == io.EOF {
if i == nth {
result = strings.TrimRight(s, "\n")
}
if err == io.EOF {
break
}
} else {
return "", err
}
}
} else { // get nth-last line (-1 is the last line, -2 is the second last line)
buffer := make([]string, -nth)
i := 0
for {
s, err := brd.ReadString('\n')
if err == nil || err == io.EOF {
if s != "" {
buffer[i%-nth] = strings.TrimRight(s, "\n")
i++
}
if err == io.EOF {
result = buffer[i%-nth]
break
}
} else {
return "", err
}
}
}
return result, nil
}
func main() {
var mtas []string
for k := range mailqFormats {
mtas = append(mtas, k)
}
mta := flag.String("mta", "", fmt.Sprintf("type of MTA (one of %v)", mtas))
flag.StringVar(mta, "M", "", "shorthand for -mta")
command := flag.String("command", "", "path to queue-printing command (guessed by -M flag if not given)")
flag.StringVar(command, "c", "", "shorthand for -command")
tempfile := flag.String("tempfile", "/tmp/mackerel-plugin-mailq", "path to tempfile")
keyPrefix := flag.String("metric-key-prefix", "mailq", "prefix to metric key")
labelPrefix := flag.String("metric-label-prefix", "Mailq", "prefix to metric label")
flag.Parse()
if format, ok := mailqFormats[*mta]; *mta == "" || !ok {
fmt.Fprintf(os.Stderr, "Unknown MTA: %s\n", *mta)
flag.PrintDefaults()
os.Exit(1)
} else {
plugin := &plugin{
path: *command,
mailq: format,
keyPrefix: *keyPrefix,
labelPrefix: *labelPrefix,
}
helper := mp.NewMackerelPlugin(plugin)
helper.Tempfile = *tempfile
helper.Run()
}
}