Skip to content

Commit

Permalink
Add sphinx statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
svaroqui committed Jan 2, 2018
1 parent 365e65b commit e24435e
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cluster/prov_opensvc.go
Expand Up @@ -653,6 +653,14 @@ show_disabled = false
conf = conf + cluster.GetPodDockerSphinxTemplate(collector, pod)
conf = conf + cluster.GetPodPackageTemplate(collector, pod)
conf = conf + cluster.GetProxiesEnv(collector, servers, agent, prx)

conf = conf + `[task0]
schedule = ` + cluster.conf.ProvSphinxCron + `
command = ` + collector.ProvFSPath + `/{svcname}_pod01/init/reindex.sh
user = root
`

log.Println(conf)
return conf, nil
}
Expand Down
3 changes: 3 additions & 0 deletions cluster/prx.go
Expand Up @@ -346,6 +346,9 @@ func (cluster *Cluster) refreshProxies() {
if cluster.conf.HaproxyOn && pr.Type == proxyHaproxy {
cluster.refreshHaproxy(pr)
}
if cluster.conf.SphinxOn && pr.Type == proxySphinx {
cluster.refreshSphinx(pr)
}
if cluster.conf.GraphiteMetrics {
cluster.SendProxyStats(pr)
}
Expand Down
86 changes: 86 additions & 0 deletions cluster/prx_sphinx.go
@@ -0,0 +1,86 @@
// 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"

"github.com/signal18/replication-manager/sphinx"
"github.com/signal18/replication-manager/state"
)

func connectSphinx(proxy *Proxy) (sphinx.SphinxSQL, error) {
sphinx := sphinx.SphinxSQL{
User: proxy.User,
Password: proxy.Pass,
Host: proxy.Host,
Port: proxy.Port,
}

var err error
err = sphinx.Connect()
if err != nil {
return sphinx, err
}
return sphinx, nil
}

func (cluster *Cluster) initSphinx(proxy *Proxy) {
if cluster.conf.SphinxOn == false {
return
}

sphinx, err := connectSphinx(proxy)
if err != nil {
cluster.sme.AddState("ERR00051", state.State{ErrType: "ERROR", ErrDesc: fmt.Sprintf(clusterError["ERR00051"], err), ErrFrom: "MON"})
return
}
defer sphinx.Connection.Close()

}

func (cluster *Cluster) refreshSphinx(proxy *Proxy) {
if cluster.conf.SphinxOn == false {
return
}

sphinx, err := connectSphinx(proxy)
if err != nil {
cluster.sme.AddState("ERR00051", state.State{ErrType: "ERROR", ErrDesc: fmt.Sprintf(clusterError["ERR00051"], err), ErrFrom: "MON"})
return
}
defer sphinx.Connection.Close()
proxy.Version = sphinx.GetVersion()

proxy.BackendsWrite = nil
proxy.BackendsRead = nil

status, err := sphinx.GetStatus()
var bke = Backend{
Host: cluster.conf.ProvProxRouteAddr,
Port: cluster.conf.ProvProxRoutePort,
Status: "UP",
PrxName: "",
PrxStatus: "UP",
PrxConnections: status["CONNECTIONS"],
PrxByteIn: "0",
PrxByteOut: "0",
PrxLatency: status["AVG_QUERY_WALL"],
}
if err == nil {
proxy.BackendsWrite = append(proxy.BackendsRead, bke)
}
}

func (cluster *Cluster) setMaintenanceSphinx(proxy *Proxy, host string, port string) {
if cluster.conf.SphinxOn == false {
return
}

}
1 change: 1 addition & 0 deletions config/config.go
Expand Up @@ -251,6 +251,7 @@ type Config struct {
ProvSphinxDiskDevice string `mapstructure:"prov-sphinx-disk-device"`
ProvSphinxDiskType string `mapstructure:"prov-sphinx-disk-type"`
ProvSphinxTags string `mapstructure:"prov-sphinx-tags"`
ProvSphinxCron string `mapstructure:"prov-sphinx-reindex-schedule"`
ProvSphinxType string `mapstructure:"prov-sphinx-service-type"`
ProvSSLCa string `mapstructure:"prov-tls-server-ca"`
ProvSSLCert string `mapstructure:"prov-tls-server-cert"`
Expand Down
1 change: 1 addition & 0 deletions server.go
Expand Up @@ -401,6 +401,7 @@ func init() {
monitorCmd.Flags().StringVar(&conf.ProvSphinxMem, "prov-sphinx-memory", "256", "Memory in M for micro service VM")
monitorCmd.Flags().StringVar(&conf.ProvSphinxDisk, "prov-sphinx-disk-size", "20g", "Disk in g for micro service VM")
monitorCmd.Flags().StringVar(&conf.ProvSphinxCores, "prov-sphinx-cpu-cores", "1", "Number of cpu cores for the micro service VM")
monitorCmd.Flags().StringVar(&conf.ProvSphinxCron, "prov-sphinx-reindex-schedule", "@5", "task time to 5 minutes for index rotation")
monitorCmd.Flags().StringVar(&conf.ProvSSLCa, "prov-tls-server-ca", "", "server TLS ca")
monitorCmd.Flags().StringVar(&conf.ProvSSLCert, "prov-tls-server-cert", "", "server TLS cert")
monitorCmd.Flags().StringVar(&conf.ProvSSLKey, "prov-tls-server-key", "", "server TLS key")
Expand Down
108 changes: 108 additions & 0 deletions sphinx/sphinx.go
@@ -0,0 +1,108 @@
package sphinx

import (
"errors"
"fmt"
"time"

"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)

type SphinxSQL struct {
Connection *sqlx.DB
User string
Password string
Port string
Host string
}

func (sphinxql *SphinxSQL) Connect() error {
SphinxConfig := mysql.Config{
User: sphinxql.User,
Passwd: sphinxql.Password,
Net: "tcp",
Addr: fmt.Sprintf("%s:%s", sphinxql.Host, sphinxql.Port),
Timeout: time.Second * 5,
ReadTimeout: time.Second * 15,
}

var err error
sphinxql.Connection, err = sqlx.Connect("mysql", SphinxConfig.FormatDSN())
if err != nil {
return fmt.Errorf("Could not connect to SphinxQL (%s)", err)
}
return nil
}

func (sphinxql *SphinxSQL) GetVariables() (map[string]string, error) {
type Variable struct {
Variable_name string
Value string
}

vars := make(map[string]string)
rows, err := sphinxql.Connection.Queryx("SHOW VARIABLES")
if err != nil {
return nil, errors.New("Could not get status variables")
}
for rows.Next() {
var v Variable
err := rows.Scan(&v.Variable_name, &v.Value)
if err != nil {
return nil, errors.New("Could not get results from status scan")
}
vars[v.Variable_name] = v.Value
}
return vars, nil
}

func (sphinxql *SphinxSQL) GetStatus() (map[string]string, error) {
type Variable struct {
Variable_name string
Value string
}

vars := make(map[string]string)
rows, err := sphinxql.Connection.Queryx("SHOW STATUS")
if err != nil {
return nil, errors.New("Could not get status variables")
}
for rows.Next() {
var v Variable
err := rows.Scan(&v.Variable_name, &v.Value)
if err != nil {
return nil, errors.New("Could not get results from status scan")
}
vars[v.Variable_name] = v.Value
}
return vars, nil

}

func (sphinxql *SphinxSQL) GetVersion() string {
var version string
return version
}

func (sphinxql *SphinxSQL) GetIndexes() (map[string]string, error) {
type Indexes struct {
Index string
Type string
}

vars := make(map[string]string)
rows, err := sphinxql.Connection.Queryx("SHOW TABLES")
if err != nil {
return nil, errors.New("Could not get status variables")
}
for rows.Next() {
var v Indexes
err := rows.Scan(&v.Index, &v.Type)
if err != nil {
return nil, errors.New("Could not get results from status scan")
}
vars[v.Index] = v.Type
}
return vars, nil
}

0 comments on commit e24435e

Please sign in to comment.