admin.go 7.1 KB

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