HFish/core/rpc/client/client.go

108 lines
2.6 KiB
Go
Raw Normal View History

2019-08-10 18:26:43 +08:00
package client
import (
"HFish/utils/log"
"HFish/utils/conf"
"HFish/core/rpc/core"
"strings"
2019-08-10 18:26:43 +08:00
)
// 上报状态结构
type Status struct {
AgentIp string
AgentName string
Web, Deep, Ssh, Redis, Mysql, Http, Telnet, Ftp, MemCahe, Plug string
2019-08-10 18:26:43 +08:00
}
// 上报结果结构
type Result struct {
AgentIp string
AgentName string
Type string
ProjectName string
SourceIp string
Info string
Id string // 数据库ID更新用 0 为新插入数据
}
func createClient() (*rpc.Client, string, bool) {
2019-08-10 18:26:43 +08:00
rpcAddr := conf.Get("rpc", "addr")
client, conn, err := rpc.Dial("tcp", rpcAddr)
2019-08-10 18:26:43 +08:00
if err != nil {
log.Pr("RPC", "127.0.0.1", "连接 RPC Server 失败")
return client, "", false
2019-08-10 18:26:43 +08:00
}
2019-09-02 12:56:08 +08:00
ipArr := strings.Split(conn.LocalAddr().String(), ":")
return client, ipArr[0], true
2019-08-10 18:26:43 +08:00
}
func reportStatus(rpcName string, ftpStatus string, telnetStatus string, httpStatus string, mysqlStatus string, redisStatus string, sshStatus string, webStatus string, darkStatus string, memCacheStatus string, plugStatus string) {
client, addr, boolStatus := createClient()
2019-08-10 18:26:43 +08:00
if boolStatus {
defer client.Close()
status := Status{
addr,
2019-08-10 18:26:43 +08:00
rpcName,
webStatus,
darkStatus,
sshStatus,
redisStatus,
mysqlStatus,
httpStatus,
telnetStatus,
2019-08-11 20:14:28 +08:00
ftpStatus,
memCacheStatus,
plugStatus,
2019-08-10 18:26:43 +08:00
}
var reply string
err := client.Call("HFishRPCService.ReportStatus", status, &reply)
if err != nil {
log.Pr("RPC", "127.0.0.1", "上报服务状态失败", err)
}
}
}
func ReportResult(typex string, projectName string, sourceIp string, info string, id string) string {
// projectName 只有 WEB 才需要传项目名 其他协议空即可
// id 0 为 新插入数据,非 0 都是更新数据
// id 非 0 的时候 sourceIp 为空
client, addr, boolStatus := createClient()
2019-08-10 18:26:43 +08:00
if boolStatus {
defer client.Close()
rpcName := conf.Get("rpc", "name")
result := Result{
addr,
2019-08-10 18:26:43 +08:00
rpcName,
typex,
projectName,
sourceIp,
info,
id,
}
var reply string
err := client.Call("HFishRPCService.ReportResult", result, &reply)
if err != nil {
log.Pr("RPC", "127.0.0.1", "上报上钩结果失败")
}
return reply
}
return ""
}
func Start(rpcName string, ftpStatus string, telnetStatus string, httpStatus string, mysqlStatus string, redisStatus string, sshStatus string, webStatus string, darkStatus string, memCacheStatus string, plugStatus string) {
reportStatus(rpcName, ftpStatus, telnetStatus, httpStatus, mysqlStatus, redisStatus, sshStatus, webStatus, darkStatus, memCacheStatus, plugStatus)
2019-08-10 18:26:43 +08:00
}