-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
143 lines (119 loc) · 3.32 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
package main
import (
"fmt"
"os"
"strconv"
zsend "github.com/blacked/go-zabbix"
docopt "github.com/docopt/docopt-go"
hierr "github.com/reconquest/hierr-go"
)
var (
stats []map[string]string
version = "[manual build]"
queryGlobal = "SHOW GLOBAL STATUS"
queryGalera = "SHOW GLOBAL STATUS LIKE 'wsrep_%'"
queryProcess = "SHOW PROCESSLIST"
querySlave = "SHOW SLAVE STATUS"
)
func main() {
usage := `zabbix-agent-extension-mysql
Usage:
zabbix-agent-extension-mysql [options] [<zabbix options>] [<mysql options>]
Options:
--discovery Run low-level discovery for detect galera/slave.
--type <type> Type of statistic global/process/slave/galera
send to zabbix [default: global].
Zabbix options:
-z --zabbix <zabbix> Hostname or IP address of zabbix server
[default: 127.0.0.1].
-p --port <port> Port of zabbix server [default: 10051].
--zabbix-prefix <prefix> Add part of your prefix for key [default: None].
-o --hostname <hostname> Host to send metrics to [default: ` + obtainHostname() + `]
MySQL options:
-n --network <net> Network type unix or tcp [default: tcp].
-m --mysql <dsn> MySQL DSN socket or host:port
[default: localhost:3306].
--user <user> MySQL user [default: zabbix].
--pass <password> MySQL password [default: zabbix].
Misc options:
--version Show version.
-h --help Show this screen.
`
args, err := docopt.ParseArgs(usage, nil, version)
if err != nil {
fmt.Println(hierr.Errorf(err, "can't parse docopt"))
os.Exit(1)
}
zabbix := args["--zabbix"].(string)
port, err := strconv.Atoi(args["--port"].(string))
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
zabbixPrefix := args["--zabbix-prefix"].(string)
if zabbixPrefix == "None" {
zabbixPrefix = "mysql"
} else {
zabbixPrefix = fmt.Sprintf("%s.%s", zabbixPrefix, "mysql")
}
hostname := args["--hostname"].(string)
network := args["--network"].(string)
mysqlDSN := args["--mysql"].(string)
user := args["--user"].(string)
password := args["--pass"].(string)
statsType := args["--type"].(string)
dsn := fmt.Sprintf("%s:%s@%s(%s)/", user, password, network, mysqlDSN)
if args["--discovery"].(bool) {
err = discovery(dsn)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}
var (
metrics []*zsend.Metric
filterMetrics []string
)
switch statsType {
case "galera":
stats, err = getGlobalStats(queryGalera, dsn)
filterMetrics = galeraMetrics
case "process":
stats, err = getStats(queryProcess, dsn)
stats = calcProcessStats(stats)
filterMetrics = processMetrics
case "slave":
stats, err = getStats(querySlave, dsn)
filterMetrics = slaveMetrics
default:
stats, err = getGlobalStats(queryGlobal, dsn)
filterMetrics = globalMetrics
}
if err != nil {
fmt.Println(err)
os.Exit(1)
}
metrics = createMetrics(
hostname,
stats[0],
statsType,
filterMetrics,
metrics,
zabbixPrefix,
)
packet := zsend.NewPacket(metrics)
sender := zsend.NewSender(
zabbix,
port,
)
sender.Send(packet)
fmt.Println("OK")
}
func obtainHostname() string {
hostname, err := os.Hostname()
if err != nil {
panic(err)
}
return hostname
}