HFish/core/rpc/client/client.go

118 lines
3.0 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"
2020-02-24 19:50:43 +08:00
"fmt"
2019-08-10 18:26:43 +08:00
)
// 上报状态结构
type Status struct {
2019-10-28 22:25:23 +08:00
AgentIp string
AgentName string
Web, Deep, Ssh, Redis, Mysql, Http, Telnet, Ftp, MemCahe, Plug, ES, TFtp, Vnc 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 为新插入数据
}
2020-02-24 19:50:43 +08:00
var rpcClient *rpc.Client
var ipAddr string
func RpcInit() {
2019-08-10 18:26:43 +08:00
rpcAddr := conf.Get("rpc", "addr")
2020-02-24 19:50:43 +08:00
c, conn, err := rpc.Dial("tcp", rpcAddr)
2019-08-10 18:26:43 +08:00
if err != nil {
2020-02-24 19:50:43 +08:00
rpcClient = nil
2019-08-10 18:26:43 +08:00
log.Pr("RPC", "127.0.0.1", "连接 RPC Server 失败")
2020-02-24 19:50:43 +08:00
ipAddr = ""
} else {
rpcClient = c
ipAddr = strings.Split(conn.LocalAddr().String(), ":")[0]
fmt.Println("连接RPC Server 成功")
2019-08-10 18:26:43 +08:00
}
}
2019-10-28 22:25:23 +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, esStatus string, tftpStatus string, vncStatus string) {
2020-02-24 19:50:43 +08:00
if (rpcClient != nil) {
2019-08-10 18:26:43 +08:00
status := Status{
2020-02-24 19:50:43 +08:00
ipAddr,
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-10-28 22:25:23 +08:00
esStatus,
tftpStatus,
vncStatus,
2019-08-10 18:26:43 +08:00
}
var reply string
2020-02-24 19:50:43 +08:00
err := rpcClient.Call("HFishRPCService.ReportStatus", status, &reply)
2019-08-10 18:26:43 +08:00
if err != nil {
log.Pr("RPC", "127.0.0.1", "上报服务状态失败", err)
2020-02-24 19:50:43 +08:00
RpcInit()
} else {
fmt.Println("上报服务状态成功")
2019-08-10 18:26:43 +08:00
}
2020-02-24 19:50:43 +08:00
} else {
RpcInit()
2019-08-10 18:26:43 +08:00
}
}
func ReportResult(typex string, projectName string, sourceIp string, info string, id string) string {
2020-02-24 19:50:43 +08:00
var reply string
2019-08-10 18:26:43 +08:00
2020-02-24 19:50:43 +08:00
if (rpcClient != nil) {
// projectName 只有 WEB 才需要传项目名 其他协议空即可
// id 0 为 新插入数据,非 0 都是更新数据
// id 非 0 的时候 sourceIp 为空
2019-08-10 18:26:43 +08:00
rpcName := conf.Get("rpc", "name")
result := Result{
2020-02-24 19:50:43 +08:00
ipAddr,
2019-08-10 18:26:43 +08:00
rpcName,
typex,
projectName,
sourceIp,
info,
id,
}
2020-02-24 19:50:43 +08:00
err := rpcClient.Call("HFishRPCService.ReportResult", result, &reply)
2019-08-10 18:26:43 +08:00
if err != nil {
log.Pr("RPC", "127.0.0.1", "上报上钩结果失败")
2020-02-24 19:50:43 +08:00
RpcInit()
} else {
fmt.Println("上报上钩结果成功")
2019-08-10 18:26:43 +08:00
}
return reply
2020-02-24 19:50:43 +08:00
} else {
return "0"
2019-08-10 18:26:43 +08:00
}
}
2019-10-28 22:25:23 +08:00
func Start(rpcName string, ftpStatus string, telnetStatus string, httpStatus string, mysqlStatus string, redisStatus string, sshStatus string, webStatus string, darkStatus string, memCacheStatus string, plugStatus string, esStatus string, tftpStatus string, vncStatus string) {
reportStatus(rpcName, ftpStatus, telnetStatus, httpStatus, mysqlStatus, redisStatus, sshStatus, webStatus, darkStatus, memCacheStatus, plugStatus, esStatus, tftpStatus, vncStatus)
2019-08-10 18:26:43 +08:00
}