forked from mackerelio/mackerel-agent-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apache2.go
203 lines (181 loc) · 5.18 KB
/
apache2.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
199
200
201
202
203
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"github.com/codegangsta/cli"
mp "github.com/mackerelio/go-mackerel-plugin"
)
// metric value structure
var graphdef map[string](mp.Graphs) = map[string](mp.Graphs){
"apache2.workers": mp.Graphs{
Label: "Apache Workers",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "busy_workers", Label: "Busy Workers", Diff: false, Stacked: true},
mp.Metrics{Name: "idle_workers", Label: "Idle Workers", Diff: false, Stacked: true},
},
},
"apache2.bytes": mp.Graphs{
Label: "Apache Bytes",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "bytes_sent", Label: "Bytes Sent", Diff: false},
},
},
"apache2.cpu": mp.Graphs{
Label: "Apache CPU Load",
Unit: "float",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "cpu_load", Label: "CPU Load", Diff: false},
},
},
"apache2.req": mp.Graphs{
Label: "Apache Requests",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "requests", Label: "Requests", Diff: false},
},
},
"apache2.scoreboard": mp.Graphs{
Label: "Apache Scoreboard",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "score-_", Label: "Waiting for connection", Diff: false, Stacked: true},
mp.Metrics{Name: "score-S", Label: "Starting up", Diff: false, Stacked: true},
mp.Metrics{Name: "score-R", Label: "Reading request", Diff: false, Stacked: true},
mp.Metrics{Name: "scpre-W", Label: "Sending reply", Diff: false, Stacked: true},
mp.Metrics{Name: "score-K", Label: "Keepalive", Diff: false, Stacked: true},
mp.Metrics{Name: "score-D", Label: "DNS lookup", Diff: false, Stacked: true},
mp.Metrics{Name: "score-C", Label: "Closing connection", Diff: false, Stacked: true},
mp.Metrics{Name: "score-L", Label: "Logging", Diff: false, Stacked: true},
mp.Metrics{Name: "score-G", Label: "Gracefully finishing", Diff: false, Stacked: true},
mp.Metrics{Name: "score-I", Label: "Idle cleanup", Diff: false, Stacked: true},
mp.Metrics{Name: "score-.", Label: "Open slot", Diff: false, Stacked: true},
},
},
}
// for fetching metrics
type Apache2Plugin struct {
Host string
Port uint16
Path string
Tempfile string
}
// Graph definition
func (c Apache2Plugin) GraphDefinition() map[string](mp.Graphs) {
return graphdef
}
// main function
func doMain(c *cli.Context) {
var apache2 Apache2Plugin
apache2.Host = c.String("http_host")
apache2.Port = uint16(c.Int("http_port"))
apache2.Path = c.String("status_page")
apache2.Tempfile = c.String("tempfile")
helper := mp.NewMackerelPlugin(apache2)
if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
helper.OutputDefinitions()
} else {
helper.OutputValues()
}
}
// fetch metrics
func (c Apache2Plugin) FetchMetrics() (map[string]float64, error) {
data, err := getApache2Metrics(c.Host, c.Port, c.Path)
if err != nil {
return nil, err
}
stat := make(map[string]float64)
err_stat := parseApache2Status(data, &stat)
if err_stat != nil {
return nil, err_stat
}
err_score := parseApache2Scoreboard(data, &stat)
if err_score != nil {
return nil, err_score
}
return stat, nil
}
// parsing scoreboard from server-status?auto
func parseApache2Scoreboard(str string, p *map[string]float64) error {
for _, line := range strings.Split(str, "\n") {
matched, err := regexp.MatchString("Scoreboard(.*)", line)
if err != nil {
return err
}
if !matched {
continue
}
record := strings.Split(line, ":")
for _, sb := range strings.Split(strings.Trim(record[1], " "), "") {
name := fmt.Sprintf("score-%s", sb)
c, assert := (*p)[name]
if !assert {
c = 0
}
(*p)[name] = c + 1
}
return nil
}
return errors.New("Scoreboard data is not found.")
}
// parsing metrics from server-status?auto
func parseApache2Status(str string, p *map[string]float64) error {
Params := map[string]string{
"Total Accesses": "requests",
"Total kBytes": "bytes_sent",
"CPULoad": "cpu_load",
"BusyWorkers": "busy_workers",
"IdleWorkers": "idle_workers"}
for _, line := range strings.Split(str, "\n") {
record := strings.Split(line, ":")
_, assert := Params[record[0]]
if !assert {
continue
}
var err_parse error
(*p)[Params[record[0]]], err_parse = strconv.ParseFloat(strings.Trim(record[1], " "), 64)
if err_parse != nil {
return err_parse
}
}
if len(*p) == 0 {
return errors.New("Status data not found.")
}
return nil
}
// Getting apache2 status from server-status module data.
func getApache2Metrics(host string, port uint16, path string) (string, error) {
uri := "http://" + host + ":" + strconv.FormatUint(uint64(port), 10) + path
resp, err := http.Get(uri)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", errors.New(fmt.Sprintf("HTTP status error: %d", resp.StatusCode))
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return "", err
}
return string(body[:]), nil
}
// main
func main() {
app := cli.NewApp()
app.Name = "apache2_metrics"
app.Version = Version
app.Usage = "Get metrics from apache2."
app.Author = "Yuichiro Saito"
app.Email = "saito@heartbeats.jp"
app.Flags = Flags
app.Action = doMain
app.Run(os.Args)
}