监控数据
-
机器性能指标:cpu,mem,网卡,磁盘……
-
业务监控
-
开源软件状态:Nginx,Redis,MySQL
-
snmp采集网络设备指标
设计原理
-
自发现采集值
-
不同类型数据采集分不同goroutine
-
进程和端口通过用户配置进行监控
配置文件
-
hostname和ip默认留空,agent自动探测
-
hbs和transfer都是配置其rpc地址
-
collector网卡采集前缀
-
ignore为true时取消上报
组织结构
-
cron:间隔执行的代码,即定时任务
-
funcs:信息采集
-
g:全局数据结构
-
http:简单的dashboard的server,获取单机监控指标数据
-
plugins:插件处理机制
-
public:静态资源文件
心跳机制
-
了解agent、plugin版本信息,方便升级
-
获取监听的进程和端口
-
获取本机执行的插件列表
与HBS、Transfer交互
调用关系
代码解读
-
main入口
go cron.InitDataHistory()
// 上报本机状态
cron.ReportAgentStatus()
// 同步插件
cron.SyncMinePlugins()
// 同步监控端口、路径、进程和URL
cron.SyncBuiltinMetrics()
// 后门调试agent,允许执行shell指令的ip列表
cron.SyncTrustableIps()
// 开始数据次采集
cron.Collect()
// 启动dashboard server
go http.Start()
-
ReportAgentStatus:汇报agent本身状态
// 判断hbs配置是否正常,正常则上报agent状态
if g.Config().Heartbeat.Enabled && g.Config().Heartbeat.Addr != "" {
// 根据配置的interval间隔上报信息
go reportAgentStatus(time.Duration(g.Config().Heartbeat.Interval) * time.Second)
}
func reportAgentStatus(interval time.Duration) {
for {
// 获取hostname, 出错则错误赋值给hostname
hostname, err := g.Hostname()
if err != nil {
hostname = fmt.Sprintf("error:%s", err.Error())
}
// 请求发送信息
req := model.AgentReportRequest{
Hostname: hostname,
IP: g.IP(),
AgentVersion: g.VERSION,
// 通过shell指令获取plugin版本,能否go实现
PluginVersion: g.GetCurrPluginVersion(),
}
var resp model.SimpleRpcResponse
// 调用rpc接口
err = g.HbsClient.Call("Agent.ReportStatus", req, &resp)
if err != nil || resp.Code != 0 {
log.Println("call Agent.ReportStatus fail:", err, "Request:", req, "Response:", resp)
}
time.Sleep(interval)
}
}