forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.go
executable file
·96 lines (79 loc) · 2.13 KB
/
data.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
package stubstatus
import (
"bufio"
"fmt"
"io"
"regexp"
"strconv"
"github.com/elastic/beats/libbeat/common"
)
var (
activeRe = regexp.MustCompile("Active connections: (\\d+)")
requestRe = regexp.MustCompile("\\s(\\d+)\\s+(\\d+)\\s+(\\d+)")
connRe = regexp.MustCompile("Reading: (\\d+) Writing: (\\d+) Waiting: (\\d+)")
)
// Map body to MapStr
func eventMapping(m *MetricSet, body io.ReadCloser, hostname string, metricset string) (common.MapStr, error) {
// Nginx stub status sample:
// Active connections: 1
// server accepts handled requests
// 7 7 19
// Reading: 0 Writing: 1 Waiting: 0
var (
active int
accepts int
handled int
dropped int
requests int
current int
reading int
writing int
waiting int
)
scanner := bufio.NewScanner(body)
// Parse active connections.
scanner.Scan()
matches := activeRe.FindStringSubmatch(scanner.Text())
if matches == nil {
return nil, fmt.Errorf("cannot parse active connections from Nginx stub status")
}
active, _ = strconv.Atoi(matches[1])
// Skip request status headers.
scanner.Scan()
// Parse request status.
scanner.Scan()
matches = requestRe.FindStringSubmatch(scanner.Text())
if matches == nil {
return nil, fmt.Errorf("cannot parse request status from Nginx stub status")
}
accepts, _ = strconv.Atoi(matches[1])
handled, _ = strconv.Atoi(matches[2])
requests, _ = strconv.Atoi(matches[3])
// Derived request status.
dropped = accepts - handled
current = requests - m.previousNumRequests
// Kept for next run.
m.previousNumRequests = requests
// Parse connection status.
scanner.Scan()
matches = connRe.FindStringSubmatch(scanner.Text())
if matches == nil {
return nil, fmt.Errorf("cannot parse connection status from Nginx stub status")
}
reading, _ = strconv.Atoi(matches[1])
writing, _ = strconv.Atoi(matches[2])
waiting, _ = strconv.Atoi(matches[3])
event := common.MapStr{
"hostname": hostname,
"active": active,
"accepts": accepts,
"handled": handled,
"dropped": dropped,
"requests": requests,
"current": current,
"reading": reading,
"writing": writing,
"waiting": waiting,
}
return event, nil
}