Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
freedomkk-qfeng committed Jan 9, 2021
1 parent fb53eb6 commit 68af7e7
Show file tree
Hide file tree
Showing 27 changed files with 2,351 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.exe
etc/probe.local.yml
etc/address.local.yml
logs/
88 changes: 87 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,88 @@
# n9e-probe
n9e-probe

## 功能
ping 和 http get 请求探测
适配 [nightingale](https://github.com/didi/nightingale)

### 指标
#### ping
|metric|说明|
|--|--|
|ping.latency|ping 请求的延迟,单位是毫秒。-1 表示 ping 不通|

|tag|说明|
|--|--|
|ip|探测的目标 ip|
|region|如果配置了,则插入 region tag|

#### url
|metric|说明|
|--|--|
|url.latency|http 请求的延迟,单位是毫秒。-1 表示无法访问|
|url.cert|证书探测。1正常,-1不正常。http 站点则是0|
|url.status_code|返回的状态码|

|tag|说明|
|--|--|
|host|目标 host|
|scheme|目标 scheme|
|path|目标的 path|
|region|如果配置了,则插入 region tag|

### 配置
#### probe.yml
```
logger:
dir: logs/
level: INFO
keepHours: 24
probe:
# 如果需要区分来自不同区域的探针,可以通过在配置 region 来插入 tag
#region: default
timeout: 5 # 探测的超时时间,单位是秒
limit: 10 # 并发限制
interval: 30 # 请求的间隔
headers: # 插入到 http 请求中的 headers,可以多条
user-agent: Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36 Edg/87.0.664.66
ping:
107: # n9e 节点上的 nid 号
- 114.114.114.114 # 要探测的 ip 地址列表
- 114.114.115.115
url:
107: # n9e 节点上的 nid 号
- https://www.baidu.com # 要探测的 ip 地址列表
- https://www.sjtu.edu.cn/
- https://bbs.ngacn.cc
- https://www.163.com
```

## 编译
```
# cd /home
# git clone https://github.com/shanghai-edu/n9e-probe.git
# cd n9e-probe
# ./control build
```
也可以直接在 release 中下载打包好的二进制
## 运行
### 支持 `systemctl` 的操作系统,如 `CentOS7`
执行 `install.sh` 脚本即可,`systemctl` 将托管运行

```
# ./install.sh
Created symlink from /etc/systemd/system/multi-user.target.wants/agent.service to /usr/lib/systemd/system/agent.service.
```
后续可通过 `systemctl start/stop/restart probe` 来进行服务管理

注意如果没有安装在 `/home` 路径上,则需要修改 `service/agent.service` 中的相关路径,否则 `systemctl` 注册时会找不到

### 不支持 systemctl 的操作系统
执行 `./control start` 启动即可
```
# ./control start
probe started
```
后续可通过 `./control start/stop/restart` 来进行服务管理
142 changes: 142 additions & 0 deletions config/address/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package address

import (
"fmt"
"os"
"path"
"strconv"
"strings"
"sync"

"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/runner"
)

type Module struct {
HTTP string `yaml:"http"`
RPC string `yaml:"rpc"`
Addresses []string `yaml:"addresses"`
}

var (
lock sync.Once
mods map[string]Module
)

func GetHTTPListen(mod string) string {
return getMod(mod).HTTP
}

func GetHTTPPort(mod string) int {
return convPort(mod, getMod(mod).HTTP, "http")
}

func GetRPCListen(mod string) string {
return getMod(mod).RPC
}

func GetRPCPort(mod string) int {
return convPort(mod, getMod(mod).RPC, "rpc")
}

func convPort(module, listen, portType string) int {
splitChar := ":"
if IsIPv6(listen) {
splitChar = "]:"
}
port, err := strconv.Atoi(strings.Split(listen, splitChar)[1])
if err != nil {
fmt.Printf("%s.%s invalid", module, portType)
os.Exit(1)
}

return port
}

func GetHTTPAddresses(mod string) []string {
modConf := getMod(mod)

count := len(modConf.Addresses)
if count == 0 {
return []string{}
}

port := convPort(mod, modConf.HTTP, "http")

addresses := make([]string, count)
for i := 0; i < count; i++ {
addresses[i] = fmt.Sprintf("%s:%d", modConf.Addresses[i], port)
}

return addresses
}

func GetAddresses(mod string) []string {
modConf := getMod(mod)
return modConf.Addresses
}

func GetRPCAddresses(mod string) []string {
modConf := getMod(mod)

count := len(modConf.Addresses)
if count == 0 {
return []string{}
}

port := convPort(mod, modConf.RPC, "rpc")

addresses := make([]string, count)
for i := 0; i < count; i++ {
addresses[i] = fmt.Sprintf("%s:%d", modConf.Addresses[i], port)
}

return addresses
}

func getMod(modKey string) Module {
lock.Do(func() {
parseConf()
})

mod, has := mods[modKey]
if !has {
fmt.Printf("module(%s) configuration section not found", modKey)
os.Exit(1)
}

return mod
}

func parseConf() {
conf := getConf()

var c map[string]Module
err := file.ReadYaml(conf, &c)
if err != nil {
fmt.Println("cannot parse file:", conf)
os.Exit(1)
}

mods = c
}

func getConf() string {
conf := path.Join(runner.Cwd, "etc", "address.local.yml")
if file.IsExist(conf) {
return conf
}

conf = path.Join(runner.Cwd, "etc", "address.yml")
if file.IsExist(conf) {
return conf
}

fmt.Println("configuration file address.[local.]yml not found")
os.Exit(1)
return ""
}

func IsIPv6(address string) bool {
return strings.Count(address, ":") >= 2
}
62 changes: 62 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package config

import (
"bytes"
"fmt"
"sync"

"github.com/spf13/viper"
"github.com/toolkits/pkg/file"
)

type ConfYaml struct {
Logger LoggerSection `yaml:"logger"`
Probe probeSection `yaml:"probe"`
Ping map[string][]string `yaml:"ping"`
Url map[string][]string `yaml:"url"`
}

type probeSection struct {
Region string `yaml:"region"`
Timeout int64 `yaml:"timeout"`
Limit int64 `yaml:"limit"`
Interval int `yaml:"interval"`
Headers map[string]string `yaml:"headers"`
}

var (
Config *ConfYaml
lock = new(sync.RWMutex)
Endpoint string
Cwd string
)

// Get configuration file
func Get() *ConfYaml {
lock.RLock()
defer lock.RUnlock()
return Config
}

func Parse(conf string) error {
bs, err := file.ReadBytes(conf)
if err != nil {
return fmt.Errorf("cannot read yml[%s]: %v", conf, err)
}

lock.Lock()
defer lock.Unlock()

viper.SetConfigType("yaml")
err = viper.ReadConfig(bytes.NewBuffer(bs))
if err != nil {
return fmt.Errorf("cannot read yml[%s]: %v", conf, err)
}

err = viper.Unmarshal(&Config)
if err != nil {
return fmt.Errorf("Unmarshal %v", err)
}

return nil
}
3 changes: 3 additions & 0 deletions config/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package config

const Version = "0.1.0"
27 changes: 27 additions & 0 deletions config/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package config

import (
"fmt"
"os"

"github.com/toolkits/pkg/logger"
)

type LoggerSection struct {
Dir string `json:"dir"`
Level string `json:"level"`
KeepHours uint `json:"keepHours"`
}

func InitLog(l LoggerSection) {

lb, err := logger.NewFileBackend(l.Dir)
if err != nil {
fmt.Println("cannot init logger:", err)
os.Exit(1)
}
lb.SetRotateByHour(true)
lb.SetKeepHours(l.KeepHours)

logger.SetLogging(l.Level, lb)
}
9 changes: 9 additions & 0 deletions etc/address.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
transfer:
http: 0.0.0.0:8008
rpc: 0.0.0.0:8009
addresses:
- 192.168.0.100

probe:
http: 127.0.0.1:2059
25 changes: 25 additions & 0 deletions etc/probe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
logger:
dir: logs/
level: INFO
keepHours: 24

probe:
# 如果需要区分来自不同区域的探针,可以通过在配置 region 来插入 tag
#region: default
timeout: 5 # 探测的超时时间,单位是秒
limit: 10 # 并发限制
interval: 30 # 请求的间隔
headers: # 插入到 http 请求中的 headers,可以多条
user-agent: Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Mobile Safari/537.36 Edg/87.0.664.66

ping:
107: # n9e 节点上的 nid 号
- 114.114.114.114 # 要探测的 ip 地址列表
- 114.114.115.115

url:
107: # n9e 节点上的 nid 号
- https://www.baidu.com # 要探测的 ip 地址列表
- https://www.sjtu.edu.cn/
- https://bbs.ngacn.cc
- https://www.163.com
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/freedomkk-qfeng/n9e-probe

go 1.15

require (
github.com/gin-contrib/pprof v1.3.0
github.com/gin-gonic/gin v1.6.3
github.com/mattn/go-isatty v0.0.12
github.com/paulstuart/ping v0.0.0-20140925212352-0345a9703e43
github.com/spf13/viper v1.7.1
github.com/toolkits/pkg v1.1.3
github.com/ugorji/go/codec v1.2.2
go.uber.org/automaxprocs v1.3.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Loading

0 comments on commit 68af7e7

Please sign in to comment.