forked from signal18/replication-manager
/
srv_snd.go
120 lines (106 loc) · 4.3 KB
/
srv_snd.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
// replication-manager - Replication Manager Monitoring and CLI for MariaDB and MySQL
// Copyright 2017 Signal 18 SARL
// Authors: Guillaume Lefranc <guillaume@signal18.io>
// Stephane Varoqui <svaroqui@gmail.com>
// This source code is licensed under the GNU General Public License, version 3.
// Redistribution/Reuse of this code is permitted under the GNU v3 license, as
// an additional term, ALL code must carry the original Author(s) credit in comment form.
// See LICENSE in this directory for the integral text.
package cluster
import (
"fmt"
"os/exec"
"strconv"
"strings"
"time"
"github.com/signal18/replication-manager/alert"
"github.com/signal18/replication-manager/dbhelper"
"github.com/signal18/replication-manager/graphite"
)
func (server *ServerMonitor) SendDatabaseStats(slaveStatus *dbhelper.SlaveStatus) error {
graph, err := graphite.NewGraphite(server.ClusterGroup.conf.GraphiteCarbonHost, server.ClusterGroup.conf.GraphiteCarbonPort)
if err != nil {
return err
}
replacer := strings.NewReplacer("`", "", "?", "", " ", "_", ".", "-", "(", "-", ")", "-", "/", "_", "<", "-", "'", "-", "\"", "-")
hostname := replacer.Replace(server.Variables["HOSTNAME"])
var metrics = make([]graphite.Metric, 3)
if server.IsSlave {
metrics[0] = graphite.NewMetric(fmt.Sprintf("mysql.%s.mysql_slave_status_seconds_behind_master", hostname), fmt.Sprintf("%d", slaveStatus.SecondsBehindMaster.Int64), time.Now().Unix())
metrics[1] = graphite.NewMetric(fmt.Sprintf("mysql.%s.mysql_slave_status_exec_master_log_pos", hostname), fmt.Sprintf("%s", slaveStatus.ExecMasterLogPos.String), time.Now().Unix())
metrics[2] = graphite.NewMetric(fmt.Sprintf("mysql.%s.mysql_slave_status_read_master_log_pos", hostname), fmt.Sprintf("%s", slaveStatus.ReadMasterLogPos.String), time.Now().Unix())
//metrics[3] = graphite.NewMetric(fmt.Sprintf("mysql.%s.mysql_slave_relay_log_pos", hostname), fmt.Sprintf("%d", slaveStatus.re), time.Now().Unix())
}
graph.SendMetrics(metrics)
isNumeric := func(s string) bool {
_, err := strconv.ParseFloat(s, 64)
return err == nil
}
var globalstatusmetrics = make([]graphite.Metric, len(server.Status))
i := 0
for k, v := range server.Status {
if isNumeric(v) {
globalstatusmetrics[i] = graphite.NewMetric(fmt.Sprintf("mysql.%s.mysql_global_status_%s", hostname, strings.ToLower(k)), v, time.Now().Unix())
}
i++
}
graph.SendMetrics(globalstatusmetrics)
var globalvariablesmetrics = make([]graphite.Metric, len(server.Variables))
i = 0
for k, v := range server.Variables {
if isNumeric(v) {
globalvariablesmetrics[i] = graphite.NewMetric(fmt.Sprintf("mysql.%s.mysql_global_variables_%s", hostname, strings.ToLower(k)), v, time.Now().Unix())
}
i++
}
graph.SendMetrics(globalvariablesmetrics)
var globalinnodbengine = make([]graphite.Metric, len(server.EngineInnoDB))
i = 0
for k, v := range server.EngineInnoDB {
if isNumeric(v) {
globalinnodbengine[i] = graphite.NewMetric(fmt.Sprintf("mysql.%s.engine_innodb_%s", hostname, strings.ToLower(k)), v, time.Now().Unix())
}
i++
}
graph.SendMetrics(globalinnodbengine)
var queries = make([]graphite.Metric, len(server.Queries))
i = 0
for k, v := range server.Queries {
if isNumeric(v) {
label := replacer.Replace(k)
if len(label) > 198 {
label = label[0:198]
}
queries[i] = graphite.NewMetric(fmt.Sprintf("mysql.%s.pfs.%s", hostname, label), v, time.Now().Unix())
}
i++
}
graph.SendMetrics(queries)
graph.Disconnect()
return nil
}
func (server *ServerMonitor) SendAlert() error {
if server.ClusterGroup.conf.MailTo != "" {
a := alert.Alert{
From: server.ClusterGroup.conf.MailFrom,
To: server.ClusterGroup.conf.MailTo,
Type: server.State,
Origin: server.URL,
Destination: server.ClusterGroup.conf.MailSMTPAddr,
}
err := a.Email()
if err != nil {
server.ClusterGroup.LogPrintf("ERROR", "Could not send mail alert: %s ", err)
}
}
if server.ClusterGroup.conf.AlertScript != "" {
server.ClusterGroup.LogPrintf("INFO", "Calling alert script")
var out []byte
out, err := exec.Command(server.ClusterGroup.conf.AlertScript, server.URL, server.PrevState, server.State).CombinedOutput()
if err != nil {
server.ClusterGroup.LogPrintf("ERROR", "%s", err)
}
server.ClusterGroup.LogPrintf("INFO", "Alert script complete:", string(out))
}
return nil
}