agen
t的目的是收集目标机器上的全部数据,并动态同步hbs
中的配置信息,将数据上报到transfer
中,是全部监控数据的产生源头。node
agent
上报的数据主要分为三类:mysql
agent
起了一个定时任务,定时与HBS
同步一些信息,获取一些动态数据以及上报本身的状态信息。默认上报时间间隔是一分钟。
上报给HBS
的数据:linux
agent
版本plugin
版本 脚本所在git目录的版本,git rev-parse HEAD)从HBS
获取的数据:git
agent提供了HTTP服务,用于管理者检查agent服务是否正常,远程操做agent等。
主要提供的Http服务以下:github
agent源码得分块分析,下面是各个块的源码分析:golang
下面是agent的默认配置,已在下面源码中作了注释redis
{ "debug": true, // 开启debug模式 "hostname": "", // 定义主机名,若是没定义,则会去获取本机主机名 "ip": "", // 定义上报时的IP地址,若未配置则自动获取本机IP "plugin": { // 关于插件的配置,配置插件目录,git地址等 "enabled": false, "dir": "./plugin", "git": "https://github.com/open-falcon/plugin.git", "logs": "./logs" }, "heartbeat": { // 配置HBS服务地址和同步数据周期,默认是60秒 "enabled": true, "addr": "127.0.0.1:6030", "interval": 60, "timeout": 1000 }, "transfer": { // 配置transfer集群地址,以及同步数据周期,默认是60秒同步一次 "enabled": true, "addrs": [ "127.0.0.1:8433", "127.0.0.1:8433" ], "interval": 60, "timeout": 1000 }, "http": { // 配置http服务启动端口 "enabled": true, "listen": ":1988", "backdoor": false }, "collector": { // 配置网卡名称开头,由于在linux物理机上,网卡名并不必定都是以eth开头 "ifacePrefix": ["eth", "em"], "mountPoint": [] }, "default_tags": { // 设置默认tags }, "ignore": { // 忽略的上报数据,有些内置的上报数据是用户不关心的,能够在这里配置不上报这些数据 "cpu.busy": true, "df.bytes.free": true, "df.bytes.total": true, "df.bytes.used": true, "df.bytes.used.percent": true, "df.inodes.total": true, "df.inodes.free": true, "df.inodes.used": true, "df.inodes.used.percent": true, "mem.memtotal": true, "mem.memused": true, "mem.memused.percent": true, "mem.memfree": true, "mem.swaptotal": true, "mem.swapused": true, "mem.swapfree": true } }
IP
和rpc
链接池 big mapper
HBS
获取本地链接HBS
的网口的IP地址。HBS
通讯。下面是建立的big mapper
的源码,把各类须要获取的内置监控项的数据都存储在这个大列表中,等待发送到transfer中。sql
type FuncsAndInterval struct { Fs []func() []*model.MetricValue Interval int } var Mappers []FuncsAndInterval func BuildMappers() { interval := g.Config().Transfer.Interval Mappers = []FuncsAndInterval{ { Fs: []func() []*model.MetricValue{ AgentMetrics, CpuMetrics, NetMetrics, KernelMetrics, LoadAvgMetrics, MemMetrics, DiskIOMetrics, IOStatsMetrics, NetstatMetrics, ProcMetrics, UdpMetrics, }, Interval: interval, }, { Fs: []func() []*model.MetricValue{ DeviceMetrics, }, Interval: interval, }, { Fs: []func() []*model.MetricValue{ PortMetrics, SocketStatSummaryMetrics, }, Interval: interval, }, { Fs: []func() []*model.MetricValue{ DuMetrics, }, Interval: interval, }, { Fs: []func() []*model.MetricValue{ UrlMetrics, }, Interval: interval, }, } }
cron.InitDataHistory
方法每隔一秒钟同步一次CPU
状态和disk list
状态,只保留最近两次的数据。cron.ReportAgentStatus
方法每隔60秒向HBS
上报本身的信息,数据包括:
cron.SyncMinePlugins
方法同步HBS
中的MinePlugins
,获取最新的plugins
路径,再删除旧的路径添加新的路径cron.SyncBuiltinMetrics
方法同步HBS
中的BuiltinMetrics
,获取须要监控的ports,paths,procs,urls,并把这些组合成相应的metrics,插入到须要监控的metrics中cron.SyncTrustableIps
方法同步HBS
中的TrustableIps
,获取信任IP列表,在这个列表中的IP能够经过agent执行shell命令cron.Collect
方法默认每隔60将big mapper
中的数据向transfer汇报一次,Endpoint
是获取的hostname
,若cfg
中配置了则用cfg
的配置,这时候会检查cfg
中若是配置了default_tags
,则会在上传每条数据以前,在其Tags
中增长default_tags
中的tags。注意,在cfg
中若是配置了多个transfer
地址,则每份数据都会发给全部的transfer
节点。Http服务提供的路由接口代码以下:shell
func init() { configAdminRoutes() configCpuRoutes() configDfRoutes() configHealthRoutes() configIoStatRoutes() configKernelRoutes() configMemoryRoutes() configPageRoutes() configPluginRoutes() configPushRoutes() configRunRoutes() configSystemRoutes() }
configAdminRoutes
提供以下api:
"/exit"
退出agentJ进程"/config/reload"
重载agent的配置"/workdir"
获取agent的工做根目录"/ips"
获取受信任IP列表configCpuRoutes
获取CPU相关的状态信息configDfRoutes
获取挂载磁盘容量使用信息configHealthRoutes
检查agent是否存活以及版本信息configIoStatRoutes
获取磁盘IO状态信息configKernelRoutes
获取内核信息configMemoryRoutes
获取内存信息configPageRoutes
获取页面展现当前主机的监控信息configPluginRoutes
可查看、更新plugins列表,从git同步plugins到指定目录configPushRoutes
经过"/v1/push"
接口能够将其余服务收集的数据push到agent中转发给transferconfigRunRoutes
执行shell命令并返回给客户,须要验证客户IP地址是不是受信任IPconfigSystemRoutes
获取一些系统当前时间,启动信息等数据