admin.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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/pkg/context"
  15. "github.com/gogits/gogs/pkg/cron"
  16. "github.com/gogits/gogs/pkg/mailer"
  17. "github.com/gogits/gogs/pkg/process"
  18. "github.com/gogits/gogs/pkg/setting"
  19. "github.com/gogits/gogs/pkg/tool"
  20. )
  21. const (
  22. DASHBOARD = "admin/dashboard"
  23. CONFIG = "admin/config"
  24. MONITOR = "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 = tool.TimeSincePro(startTime)
  67. m := new(runtime.MemStats)
  68. runtime.ReadMemStats(m)
  69. sysStatus.NumGoroutine = runtime.NumGoroutine()
  70. sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
  71. sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
  72. sysStatus.MemSys = tool.FileSize(int64(m.Sys))
  73. sysStatus.Lookups = m.Lookups
  74. sysStatus.MemMallocs = m.Mallocs
  75. sysStatus.MemFrees = m.Frees
  76. sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
  77. sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
  78. sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
  79. sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
  80. sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
  81. sysStatus.HeapObjects = m.HeapObjects
  82. sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
  83. sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
  84. sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
  85. sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
  86. sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
  87. sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
  88. sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
  89. sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
  90. sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
  91. sysStatus.NextGC = tool.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(c *context.Context) {
  109. c.Data["Title"] = c.Tr("admin.dashboard")
  110. c.Data["PageIsAdmin"] = true
  111. c.Data["PageIsAdminDashboard"] = true
  112. // Run operation.
  113. op, _ := com.StrTo(c.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 = c.Tr("admin.dashboard.delete_inactivate_accounts_success")
  120. err = models.DeleteInactivateUsers()
  121. case CLEAN_REPO_ARCHIVES:
  122. success = c.Tr("admin.dashboard.delete_repo_archives_success")
  123. err = models.DeleteRepositoryArchives()
  124. case CLEAN_MISSING_REPOS:
  125. success = c.Tr("admin.dashboard.delete_missing_repos_success")
  126. err = models.DeleteMissingRepositories()
  127. case GIT_GC_REPOS:
  128. success = c.Tr("admin.dashboard.git_gc_repos_success")
  129. err = models.GitGcRepos()
  130. case SYNC_SSH_AUTHORIZED_KEY:
  131. success = c.Tr("admin.dashboard.resync_all_sshkeys_success")
  132. err = models.RewriteAllPublicKeys()
  133. case SYNC_REPOSITORY_HOOKS:
  134. success = c.Tr("admin.dashboard.resync_all_hooks_success")
  135. err = models.SyncRepositoryHooks()
  136. case REINIT_MISSING_REPOSITORY:
  137. success = c.Tr("admin.dashboard.reinit_missing_repos_success")
  138. err = models.ReinitMissingRepositories()
  139. }
  140. if err != nil {
  141. c.Flash.Error(err.Error())
  142. } else {
  143. c.Flash.Success(success)
  144. }
  145. c.Redirect(setting.AppSubURL + "/admin")
  146. return
  147. }
  148. c.Data["Stats"] = models.GetStatistic()
  149. // FIXME: update periodically
  150. updateSystemStatus()
  151. c.Data["SysStatus"] = sysStatus
  152. c.HTML(200, DASHBOARD)
  153. }
  154. func SendTestMail(c *context.Context) {
  155. email := c.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. c.Flash.Error(c.Tr("admin.config.test_mail_failed", email, err))
  159. } else {
  160. c.Flash.Info(c.Tr("admin.config.test_mail_sent", email))
  161. }
  162. c.Redirect(setting.AppSubURL + "/admin/config")
  163. }
  164. func Config(c *context.Context) {
  165. c.Data["Title"] = c.Tr("admin.config")
  166. c.Data["PageIsAdmin"] = true
  167. c.Data["PageIsAdminConfig"] = true
  168. c.Data["AppURL"] = setting.AppURL
  169. c.Data["Domain"] = setting.Domain
  170. c.Data["OfflineMode"] = setting.OfflineMode
  171. c.Data["DisableRouterLog"] = setting.DisableRouterLog
  172. c.Data["RunUser"] = setting.RunUser
  173. c.Data["RunMode"] = strings.Title(macaron.Env)
  174. c.Data["StaticRootPath"] = setting.StaticRootPath
  175. c.Data["LogRootPath"] = setting.LogRootPath
  176. c.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  177. c.Data["SSH"] = setting.SSH
  178. c.Data["RepoRootPath"] = setting.RepoRootPath
  179. c.Data["ScriptType"] = setting.ScriptType
  180. c.Data["Repository"] = setting.Repository
  181. c.Data["Service"] = setting.Service
  182. c.Data["DbCfg"] = models.DbCfg
  183. c.Data["Webhook"] = setting.Webhook
  184. c.Data["MailerEnabled"] = false
  185. if setting.MailService != nil {
  186. c.Data["MailerEnabled"] = true
  187. c.Data["Mailer"] = setting.MailService
  188. }
  189. c.Data["CacheAdapter"] = setting.CacheAdapter
  190. c.Data["CacheInterval"] = setting.CacheInterval
  191. c.Data["CacheConn"] = setting.CacheConn
  192. c.Data["SessionConfig"] = setting.SessionConfig
  193. c.Data["DisableGravatar"] = setting.DisableGravatar
  194. c.Data["EnableFederatedAvatar"] = setting.EnableFederatedAvatar
  195. c.Data["GitVersion"] = setting.Git.Version
  196. c.Data["Git"] = setting.Git
  197. type logger struct {
  198. Mode, Config string
  199. }
  200. loggers := make([]*logger, len(setting.LogModes))
  201. for i := range setting.LogModes {
  202. loggers[i] = &logger{
  203. Mode: strings.Title(setting.LogModes[i]),
  204. }
  205. result, _ := json.MarshalIndent(setting.LogConfigs[i], "", " ")
  206. loggers[i].Config = string(result)
  207. }
  208. c.Data["Loggers"] = loggers
  209. c.HTML(200, CONFIG)
  210. }
  211. func Monitor(c *context.Context) {
  212. c.Data["Title"] = c.Tr("admin.monitor")
  213. c.Data["PageIsAdmin"] = true
  214. c.Data["PageIsAdminMonitor"] = true
  215. c.Data["Processes"] = process.Processes
  216. c.Data["Entries"] = cron.ListTasks()
  217. c.HTML(200, MONITOR)
  218. }