You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin.go 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package admin
  5. import (
  6. "fmt"
  7. "net/http"
  8. "runtime"
  9. "sort"
  10. "time"
  11. activities_model "code.gitea.io/gitea/models/activities"
  12. "code.gitea.io/gitea/models/db"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/graceful"
  15. "code.gitea.io/gitea/modules/json"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/updatechecker"
  19. "code.gitea.io/gitea/modules/web"
  20. "code.gitea.io/gitea/services/context"
  21. "code.gitea.io/gitea/services/cron"
  22. "code.gitea.io/gitea/services/forms"
  23. release_service "code.gitea.io/gitea/services/release"
  24. repo_service "code.gitea.io/gitea/services/repository"
  25. )
  26. const (
  27. tplDashboard base.TplName = "admin/dashboard"
  28. tplSystemStatus base.TplName = "admin/system_status"
  29. tplSelfCheck base.TplName = "admin/self_check"
  30. tplCron base.TplName = "admin/cron"
  31. tplQueue base.TplName = "admin/queue"
  32. tplStacktrace base.TplName = "admin/stacktrace"
  33. tplQueueManage base.TplName = "admin/queue_manage"
  34. tplStats base.TplName = "admin/stats"
  35. )
  36. var sysStatus struct {
  37. StartTime string
  38. NumGoroutine int
  39. // General statistics.
  40. MemAllocated string // bytes allocated and still in use
  41. MemTotal string // bytes allocated (even if freed)
  42. MemSys string // bytes obtained from system (sum of XxxSys below)
  43. Lookups uint64 // number of pointer lookups
  44. MemMallocs uint64 // number of mallocs
  45. MemFrees uint64 // number of frees
  46. // Main allocation heap statistics.
  47. HeapAlloc string // bytes allocated and still in use
  48. HeapSys string // bytes obtained from system
  49. HeapIdle string // bytes in idle spans
  50. HeapInuse string // bytes in non-idle span
  51. HeapReleased string // bytes released to the OS
  52. HeapObjects uint64 // total number of allocated objects
  53. // Low-level fixed-size structure allocator statistics.
  54. // Inuse is bytes used now.
  55. // Sys is bytes obtained from system.
  56. StackInuse string // bootstrap stacks
  57. StackSys string
  58. MSpanInuse string // mspan structures
  59. MSpanSys string
  60. MCacheInuse string // mcache structures
  61. MCacheSys string
  62. BuckHashSys string // profiling bucket hash table
  63. GCSys string // GC metadata
  64. OtherSys string // other system allocations
  65. // Garbage collector statistics.
  66. NextGC string // next run in HeapAlloc time (bytes)
  67. LastGCTime string // last run time
  68. PauseTotalNs string
  69. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  70. NumGC uint32
  71. }
  72. func updateSystemStatus() {
  73. sysStatus.StartTime = setting.AppStartTime.Format(time.RFC3339)
  74. m := new(runtime.MemStats)
  75. runtime.ReadMemStats(m)
  76. sysStatus.NumGoroutine = runtime.NumGoroutine()
  77. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  78. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  79. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  80. sysStatus.Lookups = m.Lookups
  81. sysStatus.MemMallocs = m.Mallocs
  82. sysStatus.MemFrees = m.Frees
  83. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  84. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  85. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  86. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  87. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  88. sysStatus.HeapObjects = m.HeapObjects
  89. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  90. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  91. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  92. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  93. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  94. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  95. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  96. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  97. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  98. sysStatus.NextGC = base.FileSize(int64(m.NextGC))
  99. sysStatus.LastGCTime = time.Unix(0, int64(m.LastGC)).Format(time.RFC3339)
  100. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  101. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  102. sysStatus.NumGC = m.NumGC
  103. }
  104. func prepareStartupProblemsAlert(ctx *context.Context) {
  105. if len(setting.StartupProblems) > 0 {
  106. content := setting.StartupProblems[0]
  107. if len(setting.StartupProblems) > 1 {
  108. content += fmt.Sprintf(" (and %d more)", len(setting.StartupProblems)-1)
  109. }
  110. ctx.Flash.Error(content, true)
  111. }
  112. }
  113. // Dashboard show admin panel dashboard
  114. func Dashboard(ctx *context.Context) {
  115. ctx.Data["Title"] = ctx.Tr("admin.dashboard")
  116. ctx.Data["PageIsAdminDashboard"] = true
  117. ctx.Data["NeedUpdate"] = updatechecker.GetNeedUpdate(ctx)
  118. ctx.Data["RemoteVersion"] = updatechecker.GetRemoteVersion(ctx)
  119. updateSystemStatus()
  120. ctx.Data["SysStatus"] = sysStatus
  121. ctx.Data["SSH"] = setting.SSH
  122. prepareStartupProblemsAlert(ctx)
  123. ctx.HTML(http.StatusOK, tplDashboard)
  124. }
  125. func SystemStatus(ctx *context.Context) {
  126. updateSystemStatus()
  127. ctx.Data["SysStatus"] = sysStatus
  128. ctx.HTML(http.StatusOK, tplSystemStatus)
  129. }
  130. // DashboardPost run an admin operation
  131. func DashboardPost(ctx *context.Context) {
  132. form := web.GetForm(ctx).(*forms.AdminDashboardForm)
  133. ctx.Data["Title"] = ctx.Tr("admin.dashboard")
  134. ctx.Data["PageIsAdminDashboard"] = true
  135. updateSystemStatus()
  136. ctx.Data["SysStatus"] = sysStatus
  137. // Run operation.
  138. if form.Op != "" {
  139. switch form.Op {
  140. case "sync_repo_branches":
  141. go func() {
  142. if err := repo_service.AddAllRepoBranchesToSyncQueue(graceful.GetManager().ShutdownContext()); err != nil {
  143. log.Error("AddAllRepoBranchesToSyncQueue: %v: %v", ctx.Doer.ID, err)
  144. }
  145. }()
  146. ctx.Flash.Success(ctx.Tr("admin.dashboard.sync_branch.started"))
  147. case "sync_repo_tags":
  148. go func() {
  149. if err := release_service.AddAllRepoTagsToSyncQueue(graceful.GetManager().ShutdownContext()); err != nil {
  150. log.Error("AddAllRepoTagsToSyncQueue: %v: %v", ctx.Doer.ID, err)
  151. }
  152. }()
  153. ctx.Flash.Success(ctx.Tr("admin.dashboard.sync_tag.started"))
  154. default:
  155. task := cron.GetTask(form.Op)
  156. if task != nil {
  157. go task.RunWithUser(ctx.Doer, nil)
  158. ctx.Flash.Success(ctx.Tr("admin.dashboard.task.started", ctx.Tr("admin.dashboard."+form.Op)))
  159. } else {
  160. ctx.Flash.Error(ctx.Tr("admin.dashboard.task.unknown", form.Op))
  161. }
  162. }
  163. }
  164. if form.From == "monitor" {
  165. ctx.Redirect(setting.AppSubURL + "/admin/monitor/cron")
  166. } else {
  167. ctx.Redirect(setting.AppSubURL + "/admin")
  168. }
  169. }
  170. func SelfCheck(ctx *context.Context) {
  171. ctx.Data["PageIsAdminSelfCheck"] = true
  172. ctx.Data["StartupProblems"] = setting.StartupProblems
  173. if len(setting.StartupProblems) == 0 && !setting.IsProd {
  174. if time.Now().Unix()%2 == 0 {
  175. ctx.Data["StartupProblems"] = []string{"This is a test warning message in dev mode"}
  176. }
  177. }
  178. r, err := db.CheckCollationsDefaultEngine()
  179. if err != nil {
  180. ctx.Flash.Error(fmt.Sprintf("CheckCollationsDefaultEngine: %v", err), true)
  181. }
  182. if r != nil {
  183. ctx.Data["DatabaseType"] = setting.Database.Type
  184. ctx.Data["DatabaseCheckResult"] = r
  185. hasProblem := false
  186. if !r.CollationEquals(r.DatabaseCollation, r.ExpectedCollation) {
  187. ctx.Data["DatabaseCheckCollationMismatch"] = true
  188. hasProblem = true
  189. }
  190. if !r.IsCollationCaseSensitive(r.DatabaseCollation) {
  191. ctx.Data["DatabaseCheckCollationCaseInsensitive"] = true
  192. hasProblem = true
  193. }
  194. ctx.Data["DatabaseCheckInconsistentCollationColumns"] = r.InconsistentCollationColumns
  195. hasProblem = hasProblem || len(r.InconsistentCollationColumns) > 0
  196. ctx.Data["DatabaseCheckHasProblems"] = hasProblem
  197. }
  198. ctx.HTML(http.StatusOK, tplSelfCheck)
  199. }
  200. func CronTasks(ctx *context.Context) {
  201. ctx.Data["Title"] = ctx.Tr("admin.monitor.cron")
  202. ctx.Data["PageIsAdminMonitorCron"] = true
  203. ctx.Data["Entries"] = cron.ListTasks()
  204. ctx.HTML(http.StatusOK, tplCron)
  205. }
  206. func MonitorStats(ctx *context.Context) {
  207. ctx.Data["Title"] = ctx.Tr("admin.monitor.stats")
  208. ctx.Data["PageIsAdminMonitorStats"] = true
  209. bs, err := json.Marshal(activities_model.GetStatistic(ctx).Counter)
  210. if err != nil {
  211. ctx.ServerError("MonitorStats", err)
  212. return
  213. }
  214. statsCounter := map[string]any{}
  215. err = json.Unmarshal(bs, &statsCounter)
  216. if err != nil {
  217. ctx.ServerError("MonitorStats", err)
  218. return
  219. }
  220. statsKeys := make([]string, 0, len(statsCounter))
  221. for k := range statsCounter {
  222. if statsCounter[k] == nil {
  223. continue
  224. }
  225. statsKeys = append(statsKeys, k)
  226. }
  227. sort.Strings(statsKeys)
  228. ctx.Data["StatsKeys"] = statsKeys
  229. ctx.Data["StatsCounter"] = statsCounter
  230. ctx.HTML(http.StatusOK, tplStats)
  231. }