models.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. Sys string `json:"sys"`
  110. //AppNameAnalysis []map[string]string `json:"app_name_analysis"`
  111. //CwAppTransform string `json:"cw_app_transform"`
  112. //OrgCode string `json:"org_code"`
  113. //AppZone *string `json:"app_zone"`
  114. }
  115. func (r *RegisterAppReq) Print() {
  116. println(r.String())
  117. }
  118. func (r *RegisterAppReq) String() string {
  119. a, err := json.Marshal(r)
  120. if err != nil {
  121. return err.Error()
  122. }
  123. return string(a)
  124. }
  125. func (w *WhiteListReq) String() string {
  126. a, err := json.Marshal(w)
  127. if err != nil {
  128. return err.Error()
  129. }
  130. return string(a)
  131. }
  132. type WhiteListReq struct {
  133. HostId int64 `json:"host_id"`
  134. AccountId int `json:"account_id"`
  135. WhiteType int `json:"white_type"`
  136. }
  137. type WhiteListResp struct {
  138. Code int `json:"code"`
  139. Msg string `json:"msg"`
  140. Data WhiteData `json:"data"`
  141. }
  142. type WhiteSettingInfo struct {
  143. AppName string `json:"app_name"`
  144. //K8SNamespace string `json:"k8s_namespace"`
  145. //K8SWorkLoadName string `json:"k8s_workLoadName"`
  146. Filters string `json:"filters"`
  147. //Type string `json:"type"`
  148. //PodName string `json:"pod_name"`
  149. ProcessKey string `json:"process_key"`
  150. //OpenStack int `json:"collect_stack"`
  151. //UpdateTime int64 `json:"update_time"`
  152. WhiteStackSettingInfo WhiteStackSettingT
  153. }
  154. type WhiteStackSettingT struct {
  155. // from getCodeSetting
  156. OpenStack int
  157. WhiteList string
  158. BlackList string
  159. }
  160. type Appscope struct {
  161. Settings []WhiteSettingInfo `json:"settings"`
  162. Code string `json:"code"`
  163. }
  164. type WhiteData struct {
  165. Executable []string `json:"executable"`
  166. OneagentEnable int `json:"oneagent_enable"`
  167. LastUpdatedTime int `json:"last_updated_time"`
  168. Appscope []Appscope `json:"appscope"`
  169. }
  170. type WhiteDataV2 struct {
  171. SettingList []WhiteSettingInfo `json:"appscope"`
  172. LastUpdatedTime int `json:"last_updated_time"`
  173. }
  174. type ReportRequest struct {
  175. RegistRequest
  176. AgentID string `json:"agent_id"`
  177. StateDetail struct {
  178. Msg string `json:"msg"`
  179. } `json:"statedetail"`
  180. State string `json:"state"`
  181. ConnectionTest bool `json:"_connection_test"`
  182. }
  183. // 注册请求数据结构
  184. type RegistRequest struct {
  185. Pid int `json:"agent_pid"`
  186. User string `json:"agent_run_user"`
  187. IP string `json:"ip"`
  188. HostName string `json:"host_name"`
  189. DaemonInstanceID string `json:"daemon_instance_id"`
  190. ContentIdentity string `json:"contentIdentity"`
  191. DaemonVersion string `json:"daemon_version"`
  192. Os string `json:"os"`
  193. OsVersion string `json:"os_version"`
  194. CPUCount int `json:"cpu_count"`
  195. Mem int64 `json:"mem"`
  196. Timestamp int64 `json:"timestamp"`
  197. Agents []AgentInfo `json:"agents"`
  198. Status string `json:"status"`
  199. Tags string `json:"tags"`
  200. TagNames string `json:"tagNames"`
  201. AgentID string `json:"agent_ids"`
  202. ConfigIds string `json:"config_ids"`
  203. IsTestingConnection bool `json:"is_connection_test"`
  204. Parent string `json:"parent"`
  205. Connect string `json:"connect"`
  206. OperatingEnv string `json:"operatingEnv"`
  207. *K8SInfo // k8s,容器化
  208. SystemVersion string `json:"system_version"`
  209. // 新增安装配置项
  210. Identification string `json:"identification"`
  211. ClusterId string `json:"cluster_id"`
  212. ProxyLocalIp string `json:"proxy_local_ip"`
  213. PluginInstallParams string `json:"plugin_install_params"`
  214. // 新增系统 uuid
  215. SystemUUID string `json:"system_uuid"`
  216. HostID string `json:"host_id"`
  217. }
  218. // k8s,容器化
  219. type K8SInfo struct {
  220. Host_type string `json:"host_type,omitempty"`
  221. Namespace string `json:"namespace"` // 命名空间
  222. // Cluster_name string `json:"cluster_name"`
  223. Pod_name string `json:"pod_name"` // 容器组名称
  224. Pod_ip string `json:"pod_ip"` // 容器组ip
  225. Node_name string `json:"node_name"` // 节点名称
  226. Node_ip string `json:"node_ip"` // 节点ip
  227. Cluster_name string `json:"k8s_cluster_name"` // 集群名称
  228. ImageName string `json:"image_name"` // 镜像名称
  229. ImageVersion string `json:"image_version"` // 镜像版本
  230. K8sModuleName string `json:"k8s_module_name"` // k8s模块名称
  231. }
  232. type AgentInfo struct {
  233. IsNamePipe bool `json:"pipe"`
  234. EnableDeepDiscover bool `json:"enable_deep_discover"`
  235. Pid int `json:"pid"`
  236. ConfigPath string `json:"config"`
  237. User string `json:"username"`
  238. Agent_instance_id string `json:"agent_instance_id"`
  239. Agent_id string `json:"agent_id"`
  240. Version string `json:"version"`
  241. Url string `json:"url"`
  242. Collectors []Collectors `json:"collectors"`
  243. P_resource_occupancy []Resource_occupancy `json:"p_resource_occupancy"`
  244. Fusing string `json:"fusing"`
  245. FusingVersion float64 `json:"fusing_rules_version"`
  246. Fusing_condition Fusing_condition `json:"fusing_condition"`
  247. // 注册时安装配置文件的ID
  248. ConfigId string `json:"configId"`
  249. FusingId string `json:"fusingId"`
  250. DepthDiscoverCycle string `json:"cdc_cycle"`
  251. Prefix string `json:"prefix"` // cdc接口路由前缀
  252. // 日志路径字段
  253. LogPath string `json:"log_path"`
  254. // proxy 内网ip
  255. ProxyLocalIp string `json:"proxy_local_ip"`
  256. }
  257. type CodeSettingReq struct {
  258. AccountId int `json:"accountId"`
  259. AgentId int64 `json:"agentId"`
  260. Appid int64 `json:"appId"`
  261. HostId int64 `json:"hostId"`
  262. }
  263. type CodeSettingResp struct {
  264. BlackWhiteSettings struct {
  265. WhiteList string `json:"white_list"`
  266. BlackList string `json:"black_list"`
  267. BlackWhiteList int `json:"black_white_list"`
  268. CollectStack int `json:"collect_stack"`
  269. } `json:"black_white_settings"`
  270. }
  271. type EbpfAppReq struct {
  272. AccountId int `json:"account_id"`
  273. }
  274. type EbpfAppResp map[string]string
  275. type TaskStatus struct {
  276. TaskId string `json:"taskid"`
  277. TaskStatus string `json:"taskStatus"`
  278. }
  279. type Collectors struct {
  280. Name interface{} `json:"name"`
  281. Status string `json:"status"`
  282. CollectorConfig map[string]interface{} `json:"collectorConfig"`
  283. Id string `json:"id"`
  284. Type string `json:"type"`
  285. }
  286. // 资源使用
  287. type Resource_occupancy struct {
  288. Pid int `json:"pid"`
  289. Cpu float64 `json:"cpu"`
  290. Mem float32 `json:"mem"`
  291. }
  292. // agent熔断规则
  293. type Fusing_condition struct {
  294. Cpu int `json:"cpu"`
  295. Memory int `json:"memory"`
  296. Disk int `json:"disk"`
  297. }
  298. // 结果解析
  299. type RespJson struct {
  300. Code int `json:"code"`
  301. Msg string `json:"msg"`
  302. Status string `json:"status"`
  303. Encryption bool `json:"encryption"`
  304. Data json.RawMessage `json:"data"`
  305. }
  306. type RegistorRespData struct {
  307. Agents []AgentList `json:"agents"`
  308. Ip string `json:"realIp"`
  309. // server 当前时间戳
  310. ServerNow int64 `json:"serverNow"`
  311. AgentInstallInfos []AgentInstallInfo `json:"agentInstallInfos"`
  312. }
  313. type AgentList struct {
  314. Agent_instance_id string `json:"path"`
  315. Status string `json:"status"`
  316. Collectors []Collectors `json:"collectors"`
  317. FusingRulesVersion float64 `json:"fusing_rules_version"`
  318. Default_FusingContent map[string]interface{} `json:"default_FusingContent"`
  319. }
  320. type AgentInstallInfo struct {
  321. AgentId string `json:"agentId"`
  322. InstallPath string `json:"installPath"`
  323. Config map[string]interface{} `json:"config"`
  324. ConfigId string `json:"configId"`
  325. FusingId string `json:"fusingId"`
  326. }
  327. type HeartBeatInfo struct {
  328. Fusing bool `json:"fusing"`
  329. Pid int `json:"agent_pid"`
  330. Status int `json:"fuse_status"`
  331. FusingVersion float64 `json:"fusing_rules_version"`
  332. AgentInstanceID string `json:"agent_instance_id"`
  333. ConfVersion string `json:"contentIdentity"`
  334. User string `json:"agent_run_user"`
  335. P_resource_occupancy []map[string]float64 `json:"p_resource_occupancy"`
  336. Fusing_condition map[string]float64 `json:"fusing_condition"`
  337. }
  338. type DaemonHeartBeatInfo struct {
  339. Status int `json:"status"`
  340. Daemon_instance_id string `json:"daemon_instance_id"`
  341. ContentIdentity string `json:"contentIdentity"`
  342. P_resource_occupancy []map[string]float64 `json:"p_resource_occupancy"`
  343. HeartBeatInfo []HeartBeatInfo `json:"agents"`
  344. Timestamp int64 `json:"timestamp"`
  345. Alive_agent []string `json:"agent_instance_ids"` // 离线agentid上报
  346. }
  347. type HeartConfContent struct {
  348. Content string `json:"content"`
  349. ContentID string `json:"contentIdentity"`
  350. }
  351. type HeartJsonConf struct {
  352. Content json.RawMessage `json:"content"`
  353. ContentID string `json:"contentIdentity"`
  354. }
  355. type FuseContent struct {
  356. Content json.RawMessage `json:"fusing"`
  357. }
  358. type HB_Config struct {
  359. HB_Config_Detail map[string]HeartConfContent `json:"config"`
  360. }
  361. type Task struct {
  362. TaskId string `json:"taskId"`
  363. Detail Detail `json:"detail"`
  364. Status string `json:"status"`
  365. TimeStamp int64 `json:"timestamp"`
  366. TaskConfig map[string]interface{} `json:"taskConfig"`
  367. AgentInstanceId string `json:"agentPath"`
  368. Operate string `json:"operate"`
  369. }
  370. func (t *Task) GetTaskConfigString(key string) (string, string) {
  371. err := ""
  372. v, ok := t.TaskConfig[key]
  373. if !ok {
  374. log.Errorf("task is %v Get value err, Can not get %s", t, key)
  375. err = fmt.Sprintf("task id is %s, Get value err, Can not get %s", t.TaskId, key)
  376. return "", err
  377. }
  378. result, ok := v.(string)
  379. if !ok {
  380. log.Errorf("task is %v Get value err, Value is not a string %v", t, v)
  381. err = fmt.Sprintf("task id is %s, Get value err, Value is not a string %v", t.TaskId, v)
  382. return "", err
  383. }
  384. return result, ""
  385. }
  386. func (t *Task) GetTaskConfigAny(key string) (interface{}, string) {
  387. err := ""
  388. v, ok := t.TaskConfig[key]
  389. if !ok {
  390. log.Warnf("task is %v Get value err, Can not get %s", t, key)
  391. err = fmt.Sprintf("task id is %s, Get value err, Can not get %s", t.TaskId, key)
  392. return nil, err
  393. }
  394. return v, ""
  395. }
  396. type Detail struct {
  397. Msg string `json:"msg"`
  398. AgentInfo
  399. }
  400. type TaskResult struct {
  401. Code int `json:"code"`
  402. Task_Id string `json:"taskId"`
  403. Detail Detail `json:"detail"`
  404. Status string `json:"status"`
  405. TimeStamp int64 `json:"timestamp"`
  406. }
  407. // Agent心跳结果解析
  408. type HeartBeatRespJson struct {
  409. Code string `json:"code"`
  410. Data AgentHeartbeat `json:"data"`
  411. }
  412. type AgentHeartbeat struct {
  413. P_resource_occupancy []map[string]float64 `json:"p_resource_occupancy"`
  414. Fusing bool `json:"fusing"`
  415. FusingVersion float64 `json:"fusing_rules_version"`
  416. Fusing_condition map[string]int `json:"fusing_condition"`
  417. ConfVersion string `json:"contentIdentity"`
  418. }
  419. // 熔断功能对象创建
  420. var (
  421. Pid int
  422. CPUNUM float64
  423. )
  424. // 资源对象,包括资源限制名称、资源的阈值、阈值触发次数、阈值触发次数状态为熔断
  425. type Rule struct {
  426. ThresholdTimes int // 触发阈值的次数统计
  427. RecoverTimes int // 恢复次数统计
  428. Threshold float64 // 阈值
  429. Enable bool // 是否启用
  430. }
  431. // 解析agent的数据结构
  432. type Result struct {
  433. Timestamp int64 `json:"timestamp"`
  434. Pid int `json:"pid"`
  435. Command string `json:"command"`
  436. Class string `json:"type"`
  437. Metrics map[string]float64 `json:"metric"`
  438. }
  439. // 熔断规则配置数据结构
  440. type FuseConfig struct {
  441. TholdCount int `json:"tholdCount"` // 多少次阈值触发熔断
  442. RecoveryCount int `json:"recoveryCount"` // 多少次正常可以恢复agent的状态
  443. Window int `json:"monFrequency"` // 多长时间采集一次agent的指标数据
  444. Version int `json:"version"` // 熔断规则版本
  445. ShakeCountThreshold int `json:"shakeCountThreshold"` // 抖动次数阈值
  446. Cycle int `json:"cycle"` // 抖动触发周期
  447. Threshold map[string]float64 `json:"threshold"` // 熔断规则阈值
  448. }