models.go 14 KB

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