forked from mackerelio/mackerel-agent-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-opcache.go
149 lines (130 loc) · 3.85 KB
/
php-opcache.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
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"github.com/codegangsta/cli"
mp "github.com/mackerelio/go-mackerel-plugin"
)
var graphdef = map[string](mp.Graphs){
"php-opcache.memory_size": mp.Graphs{
Label: "PHP OPCache Memory Size",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "used_memory", Label: "Used Memory", Diff: false, Stacked: false},
mp.Metrics{Name: "free_memory", Label: "Free Memory", Diff: false, Stacked: false},
mp.Metrics{Name: "wasted_memory", Label: "Wasted Memory", Diff: false, Stacked: false},
},
},
"php-opcache.memory": mp.Graphs{
Label: "PHP OPCache Memory Statistics",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "opcache_hit_rate", Label: "OPCache hit rate", Diff: false, Stacked: false},
mp.Metrics{Name: "current_wasted_percentage", Label: "Used Memory", Diff: false, Stacked: false},
},
},
"php-opcache.cache_size": mp.Graphs{
Label: "PHP OPCache Cache Size",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "num_cached_scripts", Label: "Cached Script", Diff: true, Stacked: false},
mp.Metrics{Name: "num_cached_keys", Label: "Num Cached Key", Diff: true, Stacked: false},
mp.Metrics{Name: "max_cached_keys", Label: "Max Cached Key", Diff: true, Stacked: false},
},
},
"php-opcache.stats": mp.Graphs{
Label: "PHP OPCache Cache Statistics",
Unit: "integer",
Metrics: [](mp.Metrics){
mp.Metrics{Name: "hits", Label: "Hits", Diff: true, Stacked: false},
mp.Metrics{Name: "misses", Label: "Misses", Diff: true, Stacked: false},
mp.Metrics{Name: "blacklist_misses", Label: "Blacklist Misses", Diff: true, Stacked: false},
},
},
}
// PhpOpcachePlugin mackerel plugin for php-opcache
type PhpOpcachePlugin struct {
Host string
Port uint16
Path string
Tempfile string
}
// GraphDefinition interface for mackerelplugin
func (c PhpOpcachePlugin) GraphDefinition() map[string](mp.Graphs) {
return graphdef
}
// FetchMetrics interface for mackerelplugin
func (c PhpOpcachePlugin) FetchMetrics() (map[string]float64, error) {
data, err := getPhpOpcacheMetrics(c.Host, c.Port, c.Path)
if err != nil {
return nil, err
}
stat := make(map[string]float64)
errStat := parsePhpOpcacheStatus(data, &stat)
if errStat != nil {
return nil, errStat
}
return stat, nil
}
func parsePhpOpcacheStatus(str string, p *map[string]float64) error {
for _, line := range strings.Split(str, "\n") {
record := strings.Split(line, ":")
if len(record) != 2 {
continue
}
var errParse error
(*p)[record[0]], errParse = strconv.ParseFloat(strings.Trim(record[1], " "), 64)
if errParse != nil {
return errParse
}
}
if len(*p) == 0 {
return errors.New("Status data not found.")
}
return nil
}
func getPhpOpcacheMetrics(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 "", fmt.Errorf("HTTP status error: %d", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return "", err
}
return string(body[:]), nil
}
func doMain(c *cli.Context) {
var phpopcache PhpOpcachePlugin
phpopcache.Host = c.String("http_host")
phpopcache.Port = uint16(c.Int("http_port"))
phpopcache.Path = c.String("status_page")
helper := mp.NewMackerelPlugin(phpopcache)
helper.Tempfile = c.String("tempfile")
if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" {
helper.OutputDefinitions()
} else {
helper.OutputValues()
}
}
func main() {
app := cli.NewApp()
app.Name = "php-opcache_metrics"
app.Version = version
app.Usage = "Get metrics from php-opcache."
app.Author = "Yuichiro Mukai"
app.Email = "y.iky917@gmail.com"
app.Flags = flags
app.Action = doMain
app.Run(os.Args)
}