admin.go 7.7 KB

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