Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

代码结构调整和增加多MySQL实例支持 #4

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,48 @@

mymon(MySQL-Monitor) -- MySQL数据库运行状态数据采集脚本,采集包括global status, global variables, slave status等。

由github.com/open-falcon/mymon 修改而来,
支持配置多个MySQL实例
## Installation

```bash
# set $GOPATH and $GOROOT

mkdir -p $GOPATH/src/github.com/open-falcon
cd $GOPATH/src/github.com/open-falcon
git clone https://github.com/open-falcon/mymon.git
mkdir -p $GOPATH/src/github.com/coraldane
cd $GOPATH/src/github.com/coraldane
git clone https://github.com/coraldane/mymon.git

cd mymon
go get ./...
go build -o mymon
control build

echo '* * * * * cd $GOPATH/src/github.com/open-falcon/mymon && ./mymon -c etc/mon.cfg' > /etc/cron.d/mymon

```

## Configuration

```
[default]
log_file=mymon.log # 日志路径和文件名
# Panic 0
# Fatal 1
# Error 2
# Warn 3
# Info 4
# Debug 5
log_level=4 # 日志级别

falcon_client=http://127.0.0.1:1988/v1/push # falcon agent连接地址

#自定义endpoint
endpoint=127.0.0.1 #若不设置则使用OS的hostname

[mysql]
user=root # 数据库用户名
password= # 数据库密码
host=127.0.0.1 # 数据库连接地址
port=3306 # 数据库端口
{
"log_level": "debug",
"interval": 60,
"falcon_client": "http://127.0.0.1:1988/v1/push",
"db_server_list": [
{
"endpoint": "机器名1",
"host": "host地址1",
"port": 3306,
"user": "$user",
"passwd": "$pass"
},
{
"endpoint": "机器名2",
"host": "host地址2",
"port": 3306,
"user": "$user",
"passwd": "$pass"
}
]
}
```

## MySQL metrics
Expand All @@ -52,4 +54,4 @@ echo '* * * * * cd $GOPATH/src/github.com/open-falcon/mymon && ./mymon -c etc/mo
## Contributors

- libin 微信:libin_cc 邮件:[email protected]

- coraldane 邮件:[email protected]
16 changes: 16 additions & 0 deletions cfg.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"log_level": "debug",
"interval": 60,
"connect_timeout": 5,
"max_idle": 10,
"falcon_client": "http://127.0.0.1:1989/v1/push",
"db_server_list": [
{
"endpoint": "mixuan1",
"host": "idao.me",
"port": 3306,
"user": "viewer",
"passwd": "mixuan"
}
]
}
118 changes: 118 additions & 0 deletions control
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/bin/bash

WORKSPACE=$(cd $(dirname $0)/; pwd)
cd $WORKSPACE

mkdir -p var

app=mysql-monitor
conf=cfg.json
pidfile=var/app.pid
logfile=var/app.log

function check_pid() {
if [ -f $pidfile ];then
pid=`cat $pidfile`
if [ -n $pid ]; then
running=`ps -p $pid|grep -v "PID TTY" |wc -l`
return $running
fi
fi
return 0
}

function start() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo -n "$app now is running already, pid="
cat $pidfile
return 1
fi

if ! [ -f $conf ];then
echo "Config file $conf doesn't exist, creating one."
cp cfg.example.json $conf
fi

nohup ./$app -c $conf &>> $logfile &
echo $! > $pidfile
echo "$app started..., pid=$!"
}

function stop() {
pid=`cat $pidfile`
kill $pid
echo "$app stoped..."
}

function restart() {
stop
sleep 1
start
}

function status() {
check_pid
running=$?
if [ $running -gt 0 ];then
echo started
else
echo stoped
fi
}

function tailf() {
tail -f $logfile
}

function build() {
go build -o $app main.go
if [ $? -ne 0 ]; then
exit $?
fi
./$app -v
}

function pack() {
build
git log -1 --pretty=%h > gitversion
version=`./$app -v`

file_list="control cfg.example.json $app"
echo "...tar $app-$version.tar.gz <= $file_list"
tar zcf $app-$version.tar.gz gitversion $file_list
}

function packbin() {
build
git log -1 --pretty=%h > gitversion
version=`./$app -v`
tar zcvf $app-bin-$version.tar.gz $app gitversion
}

function help() {
echo "$0 build|pack|start|stop|restart|status|tail"
}

if [ "$1" == "" ]; then
help
elif [ "$1" == "stop" ];then
stop
elif [ "$1" == "start" ];then
start
elif [ "$1" == "restart" ];then
restart
elif [ "$1" == "status" ];then
status
elif [ "$1" == "tail" ];then
tailf
elif [ "$1" == "build" ];then
build
elif [ "$1" == "pack" ];then
pack
elif [ "$1" == "packbin" ];then
packbin
else
help
fi
24 changes: 24 additions & 0 deletions cron/heartbeat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cron

import (
"github.com/coraldane/mymon/g"
"github.com/toolkits/logger"
"time"
)

func Heartbeat(server *g.DBServer) {
time.Sleep(10 * time.Second)
SleepRandomDuration()
for {
heartbeat(server)
d := time.Duration(g.Config().Interval) * time.Second
time.Sleep(d)
}
}

func heartbeat(server *g.DBServer) {
err := FetchData(server)
if err != nil {
logger.Errorln(err)
}
}
15 changes: 15 additions & 0 deletions cron/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cron

import (
"math/rand"
"time"
)

func SleepRandomDuration() {
ns := int64(30) * 1000000000
// 以当前时间为随机数种子,如果所有conf-agent在同一时间启动,系统时间是相同的,那么随机种子就是一样的
// 问题不大,批量ssh去启动conf-agent的话也是一个顺次的过程
r := rand.New(rand.NewSource(time.Now().UnixNano()))
d := time.Duration(r.Int63n(ns)) * time.Nanosecond
time.Sleep(d)
}
89 changes: 89 additions & 0 deletions cron/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cron

import (
"bytes"
"encoding/json"
"github.com/coraldane/mymon/g"
"github.com/coraldane/mymon/job"
"github.com/coraldane/mymon/models"
"github.com/toolkits/logger"
"io/ioutil"
"net/http"
)

func FetchData(server *g.DBServer) (err error) {
defer func() {
MysqlAlive(server, err == nil)
}()

data := make([]*models.MetaData, 0)
globalStatus, err := job.GlobalStatus(server)
if err != nil {
logger.Errorln("get GlobalStatus error", err)
return
}
data = append(data, globalStatus...)

globalVars, err := job.GlobalVariables(server)
if err != nil {
logger.Errorln("get GlobalVariables error", err)
return
}
data = append(data, globalVars...)

innodbState, err := job.InnodbStatus(server)
if err != nil {
logger.Errorln("get InnodbStatus error", err)
return
}
data = append(data, innodbState...)

slaveState, err := job.SlaveStatus(server)
if err != nil {
logger.Errorln("get SlaveStatus error", err)
return
}
data = append(data, slaveState...)

msg, err := sendData(data)
if err != nil {
logger.Errorln("sendData error", err)
return
}
logger.Info("Send response %s: %s", server.String(), string(msg))
return
}

func MysqlAlive(server *g.DBServer, ok bool) {
data := models.NewMetric("mysql_alive_local", server)
if ok {
data.SetValue(1)
}
msg, err := sendData([]*models.MetaData{data})
if err != nil {
logger.Error("Send alive data failed: %v", err)
return
}
logger.Info("Alive data response %s: %s", server.String(), string(msg))
}

func sendData(data []*models.MetaData) ([]byte, error) {
bs, err := json.Marshal(data)
if err != nil {
return nil, err
}

strUrl := g.Config().FalconClient
logger.Debug("Send to %s, size: %d", strUrl, len(data))
for _, m := range data {
logger.Debug("%s", m)
}

res, err := http.Post(strUrl, "Content-Type: application/json", bytes.NewBuffer(bs))
if err != nil {
return nil, err
}

defer res.Body.Close()
return ioutil.ReadAll(res.Body)
}
Loading