admin.go 7.9 KB

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