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
獲取一些系統當前時間,啓動信息等數據