models.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. package modelse
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. log "github.com/sirupsen/logrus"
  6. )
  7. var (
  8. Ip = ""
  9. StopAgent = "agent_stop"
  10. StartAgent = "agent_start"
  11. ReStartAgent = "agent_restart"
  12. UpgradeAgent = "agent_upgrade"
  13. RemoveAgent = "agent_uninstall"
  14. InstallAgent = "agent_install"
  15. FuseConf = "daemon_fuse"
  16. RestartDaemon = "daemon_restart"
  17. FuseUpdate = "agent_fusingrule_update"
  18. ConfUpdate = "agent_config_update"
  19. PipePath = ""
  20. ProxyNginxName = "proxy"
  21. LocalNginxName = "gateway"
  22. IsUseProxy = false
  23. DataPort = ""
  24. ConfigPort = ""
  25. FilePort = ""
  26. WorkPath = ""
  27. // 配置下发
  28. ConfigurateDistribute = "configuration_distribute"
  29. // 熔断下发
  30. FuseRuleDistribute = "fusingrule_distribute"
  31. // 任务操作
  32. CreateRunner = "collector_start"
  33. StopRunner = "collector_stop"
  34. ReSetRunner = "collector_reset"
  35. DeleteRunner = "collector_delete"
  36. // Daemon操作
  37. UpgradeDaemon = "daemon_upgrade"
  38. UninstallDaemon = "daemon_uninstall"
  39. // 任务状态
  40. Running = "running"
  41. Success = "success"
  42. Fail = "fail"
  43. InValid = "invalid"
  44. )
  45. // 状态编码
  46. var (
  47. SuccessCode = 100000
  48. // agent 获取失败
  49. AgentGetErrorCode = 200000
  50. // agent 启动错误码
  51. AgentStartupErrorCode = 200001
  52. // agent 停止错误码
  53. AgentStopErrorCode = 200002
  54. // agent 重启错误码
  55. AgentRestartErrorCode = 200003
  56. // agent 卸载错误码
  57. AgentUninstallErrorCode = 200004
  58. // agent 安装失败
  59. AgentInstallErrorCode = 200005
  60. // agent 更新熔断规则失败
  61. AgentFuseUpdateErrorCode = 200006
  62. // agent 升级失败
  63. AgentUpgradeErrorCode = 200007
  64. // distribute configuration failed
  65. DistributeConfigErrorCode = 200008
  66. DistributeFuseErrorCode = 200009
  67. // agent任务失败
  68. RunnerErrorCode = 300001
  69. // daemon 升级失败
  70. DaemonUpgradeErrorCode = 400001
  71. // daemon 卸载失败
  72. DaemonDeleteErrorCode = 400002
  73. // daemon 卸载失败
  74. DaemonRestartErrorCode = 400003
  75. // 任务被丢弃
  76. FailExecByHighLevelTask = 500001
  77. // 任务无效
  78. InvaildTask = 500002
  79. )
  80. type RegisterHostReq struct {
  81. LicenseKey string `json:"license_key"`
  82. HostId int64 `json:"host_id"`
  83. HostIp string `json:"host_ip"`
  84. HostName string `json:"host_name"`
  85. HostNameAlias string `json:"host_name_alias"`
  86. HostType int `json:"host_type"`
  87. Version string `json:"version"`
  88. SysVersion string `json:"sys_version"`
  89. SystemUuid string `json:"system_uuid"`
  90. Ebpf bool `json:"ebpf"`
  91. AgentType int `json:"agent_type"`
  92. }
  93. // RegisterHostResponse 注册主机响应结构
  94. type RegisterHostResponse struct {
  95. AccountID int `json:"account_id"`
  96. }
  97. type RegisterAppReq struct {
  98. AppId int64 `json:"appId"`
  99. AppName string `json:"appName"`
  100. AccountId int `json:"accountId"`
  101. AgentId int64 `json:"agentId"`
  102. Sn string `json:"sn"`
  103. Sport int `json:"sport"`
  104. ServiceType string `json:"serviceType"`
  105. CodeType int `json:"codeType"`
  106. App_type int `json:"app_type"`
  107. HostId int64 `json:"hostId"`
  108. AppLang string `json:"app_lang"`
  109. //AppNameAnalysis []map[string]string `json:"app_name_analysis"`
  110. //CwAppTransform string `json:"cw_app_transform"`
  111. //OrgCode string `json:"org_code"`
  112. //AppZone *string `json:"app_zone"`
  113. }
  114. func (r *RegisterAppReq) Print() {
  115. println(r.String())
  116. }
  117. func (r *RegisterAppReq) String() string {
  118. a, err := json.Marshal(r)
  119. if err != nil {
  120. return err.Error()
  121. }
  122. return string(a)
  123. }
  124. func (w *WhiteListReq) String() string {
  125. a, err := json.Marshal(w)
  126. if err != nil {
  127. return err.Error()
  128. }
  129. return string(a)
  130. }
  131. type WhiteListReq struct {
  132. HostId int64 `json:"host_id"`
  133. AccountId int `json:"account_id"`
  134. WhiteType int `json:"white_type"`
  135. }
  136. type WhiteListResp struct {
  137. Code int `json:"code"`
  138. Msg string `json:"msg"`
  139. Data WhiteData `json:"data"`
  140. }
  141. type WhiteSettingInfo struct {
  142. AppName string `json:"app_name"`
  143. //K8SNamespace string `json:"k8s_namespace"`
  144. //K8SWorkLoadName string `json:"k8s_workLoadName"`
  145. Filters string `json:"filters"`
  146. //Type string `json:"type"`
  147. //PodName string `json:"pod_name"`
  148. ProcessKey string `json:"process_key"`
  149. //OpenStack int `json:"collect_stack"`
  150. //UpdateTime int64 `json:"update_time"`
  151. WhiteStackSettingInfo WhiteStackSettingT
  152. }
  153. type WhiteStackSettingT struct {
  154. // from getCodeSetting
  155. OpenStack int
  156. WhiteList string
  157. BlackList string
  158. }
  159. type Appscope struct {
  160. Settings []WhiteSettingInfo `json:"settings"`
  161. Code string `json:"code"`
  162. }
  163. type WhiteData struct {
  164. Executable []string `json:"executable"`
  165. OneagentEnable int `json:"oneagent_enable"`
  166. LastUpdatedTime int `json:"last_updated_time"`
  167. Appscope []Appscope `json:"appscope"`
  168. }
  169. type WhiteDataV2 struct {
  170. SettingList []WhiteSettingInfo `json:"appscope"`
  171. LastUpdatedTime int `json:"last_updated_time"`
  172. }
  173. type ReportRequest struct {
  174. RegistRequest
  175. AgentID string `json:"agent_id"`
  176. StateDetail struct {
  177. Msg string `json:"msg"`
  178. } `json:"statedetail"`
  179. State string `json:"state"`
  180. ConnectionTest bool `json:"_connection_test"`
  181. }
  182. // 注册请求数据结构
  183. type RegistRequest struct {
  184. Pid int `json:"agent_pid"`
  185. User string `json:"agent_run_user"`
  186. IP string `json:"ip"`
  187. HostName string `json:"host_name"`
  188. DaemonInstanceID string `json:"daemon_instance_id"`
  189. ContentIdentity string `json:"contentIdentity"`
  190. DaemonVersion string `json:"daemon_version"`
  191. Os string `json:"os"`
  192. OsVersion string `json:"os_version"`
  193. CPUCount int `json:"cpu_count"`
  194. Mem int64 `json:"mem"`
  195. Timestamp int64 `json:"timestamp"`
  196. Agents []AgentInfo `json:"agents"`
  197. Status string `json:"status"`
  198. Tags string `json:"tags"`
  199. TagNames string `json:"tagNames"`
  200. AgentID string `json:"agent_ids"`
  201. ConfigIds string `json:"config_ids"`
  202. IsTestingConnection bool `json:"is_connection_test"`
  203. Parent string `json:"parent"`
  204. Connect string `json:"connect"`
  205. OperatingEnv string `json:"operatingEnv"`
  206. *K8SInfo // k8s,容器化
  207. SystemVersion string `json:"system_version"`
  208. // 新增安装配置项
  209. Identification string `json:"identification"`
  210. ClusterId string `json:"cluster_id"`
  211. ProxyLocalIp string `json:"proxy_local_ip"`
  212. PluginInstallParams string `json:"plugin_install_params"`
  213. // 新增系统 uuid
  214. SystemUUID string `json:"system_uuid"`
  215. HostID string `json:"host_id"`
  216. }
  217. // k8s,容器化
  218. type K8SInfo struct {
  219. Host_type string `json:"host_type,omitempty"`
  220. Namespace string `json:"namespace"` // 命名空间
  221. // Cluster_name string `json:"cluster_name"`
  222. Pod_name string `json:"pod_name"` // 容器组名称
  223. Pod_ip string `json:"pod_ip"` // 容器组ip
  224. Node_name string `json:"node_name"` // 节点名称
  225. Node_ip string `json:"node_ip"` // 节点ip
  226. Cluster_name string `json:"k8s_cluster_name"` // 集群名称
  227. ImageName string `json:"image_name"` // 镜像名称
  228. ImageVersion string `json:"image_version"` // 镜像版本
  229. K8sModuleName string `json:"k8s_module_name"` // k8s模块名称
  230. }
  231. type AgentInfo struct {
  232. IsNamePipe bool `json:"pipe"`
  233. EnableDeepDiscover bool `json:"enable_deep_discover"`
  234. Pid int `json:"pid"`
  235. ConfigPath string `json:"config"`
  236. User string `json:"username"`
  237. Agent_instance_id string `json:"agent_instance_id"`
  238. Agent_id string `json:"agent_id"`
  239. Version string `json:"version"`
  240. Url string `json:"url"`
  241. Collectors []Collectors `json:"collectors"`
  242. P_resource_occupancy []Resource_occupancy `json:"p_resource_occupancy"`
  243. Fusing string `json:"fusing"`
  244. FusingVersion float64 `json:"fusing_rules_version"`
  245. Fusing_condition Fusing_condition `json:"fusing_condition"`
  246. // 注册时安装配置文件的ID
  247. ConfigId string `json:"configId"`
  248. FusingId string `json:"fusingId"`
  249. DepthDiscoverCycle string `json:"cdc_cycle"`
  250. Prefix string `json:"prefix"` // cdc接口路由前缀
  251. // 日志路径字段
  252. LogPath string `json:"log_path"`
  253. // proxy 内网ip
  254. ProxyLocalIp string `json:"proxy_local_ip"`
  255. }
  256. type CodeSettingReq struct {
  257. AccountId int `json:"accountId"`
  258. AgentId int64 `json:"agentId"`
  259. Appid int64 `json:"appId"`
  260. HostId int64 `json:"hostId"`
  261. }
  262. type CodeSettingResp struct {
  263. BlackWhiteSettings struct {
  264. WhiteList string `json:"white_list"`
  265. BlackList string `json:"black_list"`
  266. BlackWhiteList int `json:"black_white_list"`
  267. CollectStack int `json:"collect_stack"`
  268. } `json:"black_white_settings"`
  269. }
  270. type EbpfAppReq struct {
  271. AccountId int `json:"account_id"`
  272. }
  273. type EbpfAppResp map[string]string
  274. type TaskStatus struct {
  275. TaskId string `json:"taskid"`
  276. TaskStatus string `json:"taskStatus"`
  277. }
  278. type Collectors struct {
  279. Name interface{} `json:"name"`
  280. Status string `json:"status"`
  281. CollectorConfig map[string]interface{} `json:"collectorConfig"`
  282. Id string `json:"id"`
  283. Type string `json:"type"`
  284. }
  285. // 资源使用
  286. type Resource_occupancy struct {
  287. Pid int `json:"pid"`
  288. Cpu float64 `json:"cpu"`
  289. Mem float32 `json:"mem"`
  290. }
  291. // agent熔断规则
  292. type Fusing_condition struct {
  293. Cpu int `json:"cpu"`
  294. Memory int `json:"memory"`
  295. Disk int `json:"disk"`
  296. }
  297. // 结果解析
  298. type RespJson struct {
  299. Code int `json:"code"`
  300. Msg string `json:"msg"`
  301. Status string `json:"status"`
  302. Encryption bool `json:"encryption"`
  303. Data json.RawMessage `json:"data"`
  304. }
  305. type RegistorRespData struct {
  306. Agents []AgentList `json:"agents"`
  307. Ip string `json:"realIp"`
  308. // server 当前时间戳
  309. ServerNow int64 `json:"serverNow"`
  310. AgentInstallInfos []AgentInstallInfo `json:"agentInstallInfos"`
  311. }
  312. type AgentList struct {
  313. Agent_instance_id string `json:"path"`
  314. Status string `json:"status"`
  315. Collectors []Collectors `json:"collectors"`
  316. FusingRulesVersion float64 `json:"fusing_rules_version"`
  317. Default_FusingContent map[string]interface{} `json:"default_FusingContent"`
  318. }
  319. type AgentInstallInfo struct {
  320. AgentId string `json:"agentId"`
  321. InstallPath string `json:"installPath"`
  322. Config map[string]interface{} `json:"config"`
  323. ConfigId string `json:"configId"`
  324. FusingId string `json:"fusingId"`
  325. }
  326. type HeartBeatInfo struct {
  327. Fusing bool `json:"fusing"`
  328. Pid int `json:"agent_pid"`
  329. Status int `json:"fuse_status"`
  330. FusingVersion float64 `json:"fusing_rules_version"`
  331. AgentInstanceID string `json:"agent_instance_id"`
  332. ConfVersion string `json:"contentIdentity"`
  333. User string `json:"agent_run_user"`
  334. P_resource_occupancy []map[string]float64 `json:"p_resource_occupancy"`
  335. Fusing_condition map[string]float64 `json:"fusing_condition"`
  336. }
  337. type DaemonHeartBeatInfo struct {
  338. Status int `json:"status"`
  339. Daemon_instance_id string `json:"daemon_instance_id"`
  340. ContentIdentity string `json:"contentIdentity"`
  341. P_resource_occupancy []map[string]float64 `json:"p_resource_occupancy"`
  342. HeartBeatInfo []HeartBeatInfo `json:"agents"`
  343. Timestamp int64 `json:"timestamp"`
  344. Alive_agent []string `json:"agent_instance_ids"` // 离线agentid上报
  345. }
  346. type HeartConfContent struct {
  347. Content string `json:"content"`
  348. ContentID string `json:"contentIdentity"`
  349. }
  350. type HeartJsonConf struct {
  351. Content json.RawMessage `json:"content"`
  352. ContentID string `json:"contentIdentity"`
  353. }
  354. type FuseContent struct {
  355. Content json.RawMessage `json:"fusing"`
  356. }
  357. type HB_Config struct {
  358. HB_Config_Detail map[string]HeartConfContent `json:"config"`
  359. }
  360. type Task struct {
  361. TaskId string `json:"taskId"`
  362. Detail Detail `json:"detail"`
  363. Status string `json:"status"`
  364. TimeStamp int64 `json:"timestamp"`
  365. TaskConfig map[string]interface{} `json:"taskConfig"`
  366. AgentInstanceId string `json:"agentPath"`
  367. Operate string `json:"operate"`
  368. }
  369. func (t *Task) GetTaskConfigString(key string) (string, string) {
  370. err := ""
  371. v, ok := t.TaskConfig[key]
  372. if !ok {
  373. log.Errorf("task is %v Get value err, Can not get %s", t, key)
  374. err = fmt.Sprintf("task id is %s, Get value err, Can not get %s", t.TaskId, key)
  375. return "", err
  376. }
  377. result, ok := v.(string)
  378. if !ok {
  379. log.Errorf("task is %v Get value err, Value is not a string %v", t, v)
  380. err = fmt.Sprintf("task id is %s, Get value err, Value is not a string %v", t.TaskId, v)
  381. return "", err
  382. }
  383. return result, ""
  384. }
  385. func (t *Task) GetTaskConfigAny(key string) (interface{}, string) {
  386. err := ""
  387. v, ok := t.TaskConfig[key]
  388. if !ok {
  389. log.Warnf("task is %v Get value err, Can not get %s", t, key)
  390. err = fmt.Sprintf("task id is %s, Get value err, Can not get %s", t.TaskId, key)
  391. return nil, err
  392. }
  393. return v, ""
  394. }
  395. type Detail struct {
  396. Msg string `json:"msg"`
  397. AgentInfo
  398. }
  399. type TaskResult struct {
  400. Code int `json:"code"`
  401. Task_Id string `json:"taskId"`
  402. Detail Detail `json:"detail"`
  403. Status string `json:"status"`
  404. TimeStamp int64 `json:"timestamp"`
  405. }
  406. // Agent心跳结果解析
  407. type HeartBeatRespJson struct {
  408. Code string `json:"code"`
  409. Data AgentHeartbeat `json:"data"`
  410. }
  411. type AgentHeartbeat struct {
  412. P_resource_occupancy []map[string]float64 `json:"p_resource_occupancy"`
  413. Fusing bool `json:"fusing"`
  414. FusingVersion float64 `json:"fusing_rules_version"`
  415. Fusing_condition map[string]int `json:"fusing_condition"`
  416. ConfVersion string `json:"contentIdentity"`
  417. }
  418. // 熔断功能对象创建
  419. var (
  420. Pid int
  421. CPUNUM float64
  422. )
  423. // 资源对象,包括资源限制名称、资源的阈值、阈值触发次数、阈值触发次数状态为熔断
  424. type Rule struct {
  425. ThresholdTimes int // 触发阈值的次数统计
  426. RecoverTimes int // 恢复次数统计
  427. Threshold float64 // 阈值
  428. Enable bool // 是否启用
  429. }
  430. // 解析agent的数据结构
  431. type Result struct {
  432. Timestamp int64 `json:"timestamp"`
  433. Pid int `json:"pid"`
  434. Command string `json:"command"`
  435. Class string `json:"type"`
  436. Metrics map[string]float64 `json:"metric"`
  437. }
  438. // 熔断规则配置数据结构
  439. type FuseConfig struct {
  440. TholdCount int `json:"tholdCount"` // 多少次阈值触发熔断
  441. RecoveryCount int `json:"recoveryCount"` // 多少次正常可以恢复agent的状态
  442. Window int `json:"monFrequency"` // 多长时间采集一次agent的指标数据
  443. Version int `json:"version"` // 熔断规则版本
  444. ShakeCountThreshold int `json:"shakeCountThreshold"` // 抖动次数阈值
  445. Cycle int `json:"cycle"` // 抖动触发周期
  446. Threshold map[string]float64 `json:"threshold"` // 熔断规则阈值
  447. }