models.go 14 KB

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