models.go 16 KB

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