1
0

admin.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package admin
  2. import (
  3. "fmt"
  4. "runtime"
  5. "strings"
  6. "time"
  7. jsoniter "github.com/json-iterator/go"
  8. "gogs.io/gogs/internal/conf"
  9. "gogs.io/gogs/internal/context"
  10. "gogs.io/gogs/internal/cron"
  11. "gogs.io/gogs/internal/database"
  12. "gogs.io/gogs/internal/email"
  13. "gogs.io/gogs/internal/process"
  14. "gogs.io/gogs/internal/tool"
  15. )
  16. const (
  17. tmplDashboard = "admin/dashboard"
  18. tmplConfig = "admin/config"
  19. tmplMonitor = "admin/monitor"
  20. )
  21. // initTime is the time when the application was initialized.
  22. var initTime = time.Now()
  23. var sysStatus struct {
  24. Uptime string
  25. NumGoroutine int
  26. // General statistics.
  27. MemAllocated string // bytes allocated and still in use
  28. MemTotal string // bytes allocated (even if freed)
  29. MemSys string // bytes obtained from system (sum of XxxSys below)
  30. Lookups uint64 // number of pointer lookups
  31. MemMallocs uint64 // number of mallocs
  32. MemFrees uint64 // number of frees
  33. // Main allocation heap statistics.
  34. HeapAlloc string // bytes allocated and still in use
  35. HeapSys string // bytes obtained from system
  36. HeapIdle string // bytes in idle spans
  37. HeapInuse string // bytes in non-idle span
  38. HeapReleased string // bytes released to the OS
  39. HeapObjects uint64 // total number of allocated objects
  40. // Low-level fixed-size structure allocator statistics.
  41. // Inuse is bytes used now.
  42. // Sys is bytes obtained from system.
  43. StackInuse string // bootstrap stacks
  44. StackSys string
  45. MSpanInuse string // mspan structures
  46. MSpanSys string
  47. MCacheInuse string // mcache structures
  48. MCacheSys string
  49. BuckHashSys string // profiling bucket hash table
  50. GCSys string // GC metadata
  51. OtherSys string // other system allocations
  52. // Garbage collector statistics.
  53. NextGC string // next run in HeapAlloc time (bytes)
  54. LastGC string // last run in absolute time (ns)
  55. PauseTotalNs string
  56. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  57. NumGC uint32
  58. }
  59. func updateSystemStatus() {
  60. sysStatus.Uptime = tool.TimeSincePro(initTime)
  61. m := new(runtime.MemStats)
  62. runtime.ReadMemStats(m)
  63. sysStatus.NumGoroutine = runtime.NumGoroutine()
  64. sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
  65. sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
  66. sysStatus.MemSys = tool.FileSize(int64(m.Sys))
  67. sysStatus.Lookups = m.Lookups
  68. sysStatus.MemMallocs = m.Mallocs
  69. sysStatus.MemFrees = m.Frees
  70. sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
  71. sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
  72. sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
  73. sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
  74. sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
  75. sysStatus.HeapObjects = m.HeapObjects
  76. sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
  77. sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
  78. sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
  79. sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
  80. sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
  81. sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
  82. sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
  83. sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
  84. sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
  85. sysStatus.NextGC = tool.FileSize(int64(m.NextGC))
  86. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  87. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  88. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  89. sysStatus.NumGC = m.NumGC
  90. }
  91. func Dashboard(c *context.Context) {
  92. c.Title("admin.dashboard")
  93. c.PageIs("Admin")
  94. c.PageIs("AdminDashboard")
  95. c.Data["GitVersion"] = conf.Git.Version
  96. c.Data["GoVersion"] = runtime.Version()
  97. c.Data["BuildTime"] = conf.BuildTime
  98. c.Data["BuildCommit"] = conf.BuildCommit
  99. c.Data["Stats"] = database.GetStatistic(c.Req.Context())
  100. // FIXME: update periodically
  101. updateSystemStatus()
  102. c.Data["SysStatus"] = sysStatus
  103. c.Success(tmplDashboard)
  104. }
  105. // Operation types.
  106. type AdminOperation int
  107. const (
  108. CleanInactivateUser AdminOperation = iota + 1
  109. CleanRepoArchives
  110. CleanMissingRepos
  111. GitGCRepos
  112. SyncSSHAuthorizedKey
  113. SyncRepositoryHooks
  114. ReinitMissingRepository
  115. )
  116. func Operation(c *context.Context) {
  117. var err error
  118. var success string
  119. switch AdminOperation(c.QueryInt("op")) {
  120. case CleanInactivateUser:
  121. success = c.Tr("admin.dashboard.delete_inactivate_accounts_success")
  122. err = database.Handle.Users().DeleteInactivated()
  123. case CleanRepoArchives:
  124. success = c.Tr("admin.dashboard.delete_repo_archives_success")
  125. err = database.DeleteRepositoryArchives()
  126. case CleanMissingRepos:
  127. success = c.Tr("admin.dashboard.delete_missing_repos_success")
  128. err = database.DeleteMissingRepositories()
  129. case GitGCRepos:
  130. success = c.Tr("admin.dashboard.git_gc_repos_success")
  131. err = database.GitGcRepos()
  132. case SyncSSHAuthorizedKey:
  133. success = c.Tr("admin.dashboard.resync_all_sshkeys_success")
  134. err = database.RewriteAuthorizedKeys()
  135. case SyncRepositoryHooks:
  136. success = c.Tr("admin.dashboard.resync_all_hooks_success")
  137. err = database.SyncRepositoryHooks()
  138. case ReinitMissingRepository:
  139. success = c.Tr("admin.dashboard.reinit_missing_repos_success")
  140. err = database.ReinitMissingRepositories()
  141. }
  142. if err != nil {
  143. c.Flash.Error(err.Error())
  144. } else {
  145. c.Flash.Success(success)
  146. }
  147. c.RedirectSubpath("/admin")
  148. }
  149. func SendTestMail(c *context.Context) {
  150. emailAddr := c.Query("email")
  151. // Send a test email to the user's email address and redirect back to Config
  152. if err := email.SendTestMail(emailAddr); err != nil {
  153. c.Flash.Error(c.Tr("admin.config.email.test_mail_failed", emailAddr, err))
  154. } else {
  155. c.Flash.Info(c.Tr("admin.config.email.test_mail_sent", emailAddr))
  156. }
  157. c.Redirect(conf.Server.Subpath + "/admin/config")
  158. }
  159. func Config(c *context.Context) {
  160. c.Title("admin.config")
  161. c.PageIs("Admin")
  162. c.PageIs("AdminConfig")
  163. c.Data["App"] = conf.App
  164. c.Data["Server"] = conf.Server
  165. c.Data["SSH"] = conf.SSH
  166. c.Data["Repository"] = conf.Repository
  167. c.Data["Database"] = conf.Database
  168. c.Data["Security"] = conf.Security
  169. c.Data["Email"] = conf.Email
  170. c.Data["Auth"] = conf.Auth
  171. c.Data["User"] = conf.User
  172. c.Data["Session"] = conf.Session
  173. c.Data["Cache"] = conf.Cache
  174. c.Data["Attachment"] = conf.Attachment
  175. c.Data["Release"] = conf.Release
  176. c.Data["Picture"] = conf.Picture
  177. c.Data["HTTP"] = conf.HTTP
  178. c.Data["Mirror"] = conf.Mirror
  179. c.Data["Webhook"] = conf.Webhook
  180. c.Data["Git"] = conf.Git
  181. c.Data["LFS"] = conf.LFS
  182. c.Data["LogRootPath"] = conf.Log.RootPath
  183. type logger struct {
  184. Mode, Config string
  185. }
  186. loggers := make([]*logger, len(conf.Log.Modes))
  187. for i := range conf.Log.Modes {
  188. loggers[i] = &logger{
  189. Mode: strings.Title(conf.Log.Modes[i]),
  190. }
  191. result, _ := jsoniter.MarshalIndent(conf.Log.Configs[i], "", " ")
  192. loggers[i].Config = string(result)
  193. }
  194. c.Data["Loggers"] = loggers
  195. c.Success(tmplConfig)
  196. }
  197. func Monitor(c *context.Context) {
  198. c.Data["Title"] = c.Tr("admin.monitor")
  199. c.Data["PageIsAdmin"] = true
  200. c.Data["PageIsAdminMonitor"] = true
  201. c.Data["Processes"] = process.Processes
  202. c.Data["Entries"] = cron.ListTasks()
  203. c.Success(tmplMonitor)
  204. }