Skip to content

Commit

Permalink
VERSION 4.1.0
Browse files Browse the repository at this point in the history
1. 支持转发数据到 n9e 的 transfer
  • Loading branch information
freedomkk-qfeng committed Mar 23, 2020
1 parent 2761f50 commit 605e058
Show file tree
Hide file tree
Showing 9 changed files with 385 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

*.exe
cfg.json

var/app.pid
Expand Down
1 change: 1 addition & 0 deletions cfg.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
},
"transfer": {
"enabled": true,
"n9eMode":false,
"addr": "127.0.0.1:8433",
"interval": 300,
"timeout": 1000
Expand Down
14 changes: 9 additions & 5 deletions cron/collector.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package cron

import (
"github.com/gaochao1/swcollector/funcs"
"github.com/gaochao1/swcollector/g"
"github.com/open-falcon/common/model"
"log"
"math"
"time"

"github.com/gaochao1/swcollector/funcs"
"github.com/gaochao1/swcollector/g"
"github.com/open-falcon/common/model"
)

func Collect() {
Expand Down Expand Up @@ -66,8 +67,11 @@ func MetricToTransfer(sec int64, fns []func() []*model.MetricValue) {
mvsSend = mvs[n*(i-1) : (n*(i-1))+int(mod)]
}
time.Sleep(100 * time.Millisecond)

go g.SendToTransfer(mvsSend)
if g.Config().Transfer.N9eMode {
go g.N9ePush(mvsSend)
} else {
go g.SendToTransfer(mvsSend)
}
}

endTime := time.Now()
Expand Down
9 changes: 4 additions & 5 deletions funcs/swsystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package funcs

import (
"log"
"strconv"

"github.com/gaochao1/sw"
"github.com/gaochao1/swcollector/g"
Expand Down Expand Up @@ -38,17 +39,15 @@ func swSystemInfo(ip string, ch chan SwSystem) {
var swSystem SwSystem
swSystem.Ip = ip

//ping timeout.Millisecond
timeout := 1000
pingCount := 1
timeout := g.Config().Switch.PingTimeout * g.Config().Switch.PingRetry

ping, err := sw.PingStatSummary(ip, pingCount, timeout)
ping, err := sw.PingRtt(ip, timeout, fastPingMode)
if err != nil {
log.Println(err)
ch <- swSystem
return
} else {
swSystem.Ping = ping["max"]
swSystem.Ping = strconv.FormatFloat(ping, 'f', 2, 64)

uptime, err := sw.SysUpTime(ip, g.Config().Switch.Community, timeout)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions g/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type SwitchConfig struct {

type TransferConfig struct {
Enabled bool `json:"enabled"`
N9eMode bool `json:"n9eMode"`
Addr string `json:"addr"`
Interval int `json:"interval"`
Timeout int `json:"timeout"`
Expand Down
3 changes: 2 additions & 1 deletion g/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
// 4.0.6.1 fix channal closed bug
// 4.0.6.2 fix Vendor bug;add remote config api
// 4.0.6.3 fix bugs
// 4.1.0 support n9e transfer mode
const (
VERSION = "4.0.6.3"
VERSION = "4.1.0"
COLLECT_INTERVAL = time.Second
)
60 changes: 60 additions & 0 deletions g/push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package g

import (
"bufio"
"io"
"log"
"net"
"net/rpc"
"reflect"
"time"

"github.com/didi/nightingale/src/dataobj"

"github.com/open-falcon/common/model"
"github.com/ugorji/go/codec"
)

func N9ePush(items []*model.MetricValue) {
var mh codec.MsgpackHandle
mh.MapType = reflect.TypeOf(map[string]interface{}(nil))

addr := config.Transfer.Addr
retry := 0
for {
conn, err := net.DialTimeout("tcp", addr, time.Millisecond*3000)
if err != nil {
log.Println("dial transfer err:", err)
continue
}

var bufconn = struct { // bufconn here is a buffered io.ReadWriteCloser
io.Closer
*bufio.Reader
*bufio.Writer
}{conn, bufio.NewReader(conn), bufio.NewWriter(conn)}

rpcCodec := codec.MsgpackSpecRpc.ClientCodec(bufconn, &mh)
client := rpc.NewClientWithCodec(rpcCodec)

var reply dataobj.TransferResp
err = client.Call("Transfer.Push", items, &reply)
client.Close()
if err != nil {
log.Println(err)
continue
} else {
if reply.Msg != "ok" {
log.Println("some item push err", reply)
}
return
}
time.Sleep(time.Millisecond * 500)

retry += 1
if retry == 3 {
retry = 0
break
}
}
}
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/gaochao1/swcollector

go 1.14

require (
github.com/alouca/gologger v0.0.0-20120904114645-7d4b7291de9c // indirect
github.com/didi/nightingale v1.0.1
github.com/freedomkk-qfeng/go-fastping v0.0.0-20160109021039-d7bb493dee3e // indirect
github.com/gaochao1/gosnmp v0.0.0-20150630013918-783a67a067fd
github.com/gaochao1/sw v0.0.0-20200323064703-ada7b15352e3
github.com/open-falcon/common v0.0.0-20160912145637-b9ba65549217
github.com/toolkits/file v0.0.0-20160325033739-a5b3c5147e07
github.com/toolkits/net v0.0.0-20160910085801-3f39ab6fe3ce
github.com/toolkits/slice v0.0.0-20141116085117-e44a80af2484
github.com/toolkits/time v0.0.0-20160524122720-c274716e8d7f // indirect
github.com/ugorji/go/codec v1.1.7
)
290 changes: 290 additions & 0 deletions go.sum

Large diffs are not rendered by default.

0 comments on commit 605e058

Please sign in to comment.