admin.go 7.3 KB

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