models.go 15 KB

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