snap.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package flags
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path"
  8. "time"
  9. "github.com/coroot/coroot-node-agent/utils"
  10. "github.com/coroot/coroot-node-agent/utils/modelse"
  11. "github.com/jedib0t/go-pretty/v6/table"
  12. )
  13. func DumpTableFeatures(outputFormat string) {
  14. dumpPath := path.Join(utils.GetDefaultRuntimePath(), "memdump")
  15. fileName := path.Join(dumpPath, "app.snap")
  16. // 打开文件并获取文件信息
  17. file, err := os.Open(fileName)
  18. if err != nil {
  19. fmt.Println("no app.snap")
  20. os.Exit(0)
  21. }
  22. defer file.Close()
  23. // 获取文件修改时间
  24. fileInfo, err := file.Stat()
  25. if err != nil {
  26. fmt.Println("Failed to get file information")
  27. os.Exit(0)
  28. }
  29. fileModTime := fileInfo.ModTime().Format("2006/01/02 15:04:05")
  30. // 读取文件内容
  31. content, err := io.ReadAll(file)
  32. if err != nil {
  33. fmt.Println("Failed to read file content")
  34. os.Exit(0)
  35. }
  36. s := make(map[uint32]modelse.AppStatusInfo)
  37. err = json.Unmarshal(content, &s)
  38. if err != nil {
  39. fmt.Println(err.Error())
  40. os.Exit(0)
  41. }
  42. // 根据输出格式选择显示方式
  43. if outputFormat == "json" {
  44. // JSON格式输出
  45. output := map[string]interface{}{
  46. "snap_updated": fileModTime,
  47. "data": s,
  48. }
  49. jsonData, err := json.MarshalIndent(output, "", " ")
  50. if err != nil {
  51. fmt.Println("Failed to marshal JSON:", err.Error())
  52. os.Exit(1)
  53. }
  54. fmt.Println(string(jsonData))
  55. } else {
  56. // 默认表格格式输出
  57. t := table.NewWriter()
  58. t.SetTitle(fmt.Sprintf("Application Status (Snap Updated: %s)", fileModTime))
  59. for pid, info := range s {
  60. service := fmt.Sprintf("%s", info.Sn)
  61. t.AppendRow(table.Row{
  62. //info.AgentID,
  63. //info.UpdateAt,
  64. pid,
  65. info.ProcName,
  66. info.AppName,
  67. info.Container,
  68. info.Rule,
  69. info.Language,
  70. service,
  71. info.AppID,
  72. info.RegisterAt,
  73. info.PreStatus.String(),
  74. info.Status.String(),
  75. info.StackStatus,
  76. })
  77. }
  78. t.SetAutoIndex(true)
  79. t.AppendHeader(table.Row{
  80. //"agent id",
  81. //"update at",
  82. "pid",
  83. "process",
  84. "app name",
  85. "container",
  86. "app rule",
  87. "code",
  88. "service",
  89. "app id",
  90. "reg at",
  91. "app pre status",
  92. "app status",
  93. "stack status",
  94. })
  95. fmt.Println(t.Render())
  96. }
  97. os.Exit(0)
  98. }
  99. func DumpRuleTableFeatures(outputFormat string) {
  100. dumpPath := path.Join(utils.GetDefaultRuntimePath(), "memdump")
  101. fileName := path.Join(dumpPath, "rule.snap")
  102. // 打开文件并获取文件信息
  103. file, err := os.Open(fileName)
  104. if err != nil {
  105. fmt.Println("no rule.snap")
  106. os.Exit(0)
  107. }
  108. defer file.Close()
  109. // 获取文件修改时间
  110. fileInfo, err := file.Stat()
  111. if err != nil {
  112. fmt.Println("Failed to get file information")
  113. os.Exit(0)
  114. }
  115. fileModTime := fileInfo.ModTime().Format("2006/01/02 15:04:05")
  116. // 读取文件内容
  117. content, err := io.ReadAll(file)
  118. if err != nil {
  119. fmt.Println("Failed to read file content")
  120. os.Exit(0)
  121. }
  122. var whiteData modelse.WhiteDataV2
  123. err = json.Unmarshal(content, &whiteData)
  124. if err != nil {
  125. fmt.Println(err.Error())
  126. os.Exit(0)
  127. }
  128. // 根据输出格式选择显示方式
  129. if outputFormat == "json" {
  130. // JSON格式输出
  131. lastUpdatedTime := time.Unix(int64(whiteData.LastUpdatedTime), 0).Format("2006/01/02 15:04:05")
  132. output := map[string]interface{}{
  133. "rule_updated": lastUpdatedTime,
  134. "snap_updated": fileModTime,
  135. "data": whiteData.SettingList,
  136. }
  137. jsonData, err := json.MarshalIndent(output, "", " ")
  138. if err != nil {
  139. fmt.Println("Failed to marshal JSON:", err.Error())
  140. os.Exit(1)
  141. }
  142. fmt.Println(string(jsonData))
  143. } else {
  144. // 默认表格格式输出
  145. t := table.NewWriter()
  146. // 设置表格标题,包含规则更新时间和文件更新时间
  147. lastUpdatedTime := time.Unix(int64(whiteData.LastUpdatedTime), 0).Format("2006/01/02 15:04:05")
  148. t.SetTitle(fmt.Sprintf("Rule Configuration (Rule Updated: %s, Snap Updated: %s)", lastUpdatedTime, fileModTime))
  149. for _, setting := range whiteData.SettingList {
  150. t.AppendRow(table.Row{
  151. setting.AppName,
  152. setting.ProcessKey,
  153. setting.Filters,
  154. setting.WhiteStackSettingInfo.OpenStack,
  155. setting.WhiteStackSettingInfo.WhiteList,
  156. setting.WhiteStackSettingInfo.BlackList,
  157. })
  158. }
  159. t.SetAutoIndex(true)
  160. t.AppendHeader(table.Row{
  161. "App Name",
  162. "Setting rule",
  163. "Filters",
  164. "Stack Switch",
  165. "White List",
  166. "Black List",
  167. })
  168. fmt.Println(t.Render())
  169. }
  170. os.Exit(0)
  171. }