You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
7.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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/Unknwon/com"
  11. "gopkg.in/macaron.v1"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/context"
  15. "github.com/gogits/gogs/modules/cron"
  16. "github.com/gogits/gogs/modules/mailer"
  17. "github.com/gogits/gogs/modules/process"
  18. "github.com/gogits/gogs/modules/setting"
  19. )
  20. const (
  21. DASHBOARD base.TplName = "admin/dashboard"
  22. CONFIG base.TplName = "admin/config"
  23. MONITOR base.TplName = "admin/monitor"
  24. )
  25. var (
  26. startTime = time.Now()
  27. )
  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 = base.TimeSincePro(startTime)
  66. m := new(runtime.MemStats)
  67. runtime.ReadMemStats(m)
  68. sysStatus.NumGoroutine = runtime.NumGoroutine()
  69. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  70. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  71. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  72. sysStatus.Lookups = m.Lookups
  73. sysStatus.MemMallocs = m.Mallocs
  74. sysStatus.MemFrees = m.Frees
  75. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  76. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  77. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  78. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  79. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  80. sysStatus.HeapObjects = m.HeapObjects
  81. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  82. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  83. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  84. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  85. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  86. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  87. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  88. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  89. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  90. sysStatus.NextGC = base.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_UPDATE_HOOK
  105. REINIT_MISSING_REPOSITORY
  106. )
  107. func Dashboard(ctx *context.Context) {
  108. ctx.Data["Title"] = ctx.Tr("admin.dashboard")
  109. ctx.Data["PageIsAdmin"] = true
  110. ctx.Data["PageIsAdminDashboard"] = true
  111. // Run operation.
  112. op, _ := com.StrTo(ctx.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 = ctx.Tr("admin.dashboard.delete_inactivate_accounts_success")
  119. err = models.DeleteInactivateUsers()
  120. case CLEAN_REPO_ARCHIVES:
  121. success = ctx.Tr("admin.dashboard.delete_repo_archives_success")
  122. err = models.DeleteRepositoryArchives()
  123. case CLEAN_MISSING_REPOS:
  124. success = ctx.Tr("admin.dashboard.delete_missing_repos_success")
  125. err = models.DeleteMissingRepositories()
  126. case GIT_GC_REPOS:
  127. success = ctx.Tr("admin.dashboard.git_gc_repos_success")
  128. err = models.GitGcRepos()
  129. case SYNC_SSH_AUTHORIZED_KEY:
  130. success = ctx.Tr("admin.dashboard.resync_all_sshkeys_success")
  131. err = models.RewriteAllPublicKeys()
  132. case SYNC_REPOSITORY_UPDATE_HOOK:
  133. success = ctx.Tr("admin.dashboard.resync_all_update_hooks_success")
  134. err = models.RewriteRepositoryUpdateHook()
  135. case REINIT_MISSING_REPOSITORY:
  136. success = ctx.Tr("admin.dashboard.reinit_missing_repos_success")
  137. err = models.ReinitMissingRepositories()
  138. }
  139. if err != nil {
  140. ctx.Flash.Error(err.Error())
  141. } else {
  142. ctx.Flash.Success(success)
  143. }
  144. ctx.Redirect(setting.AppSubUrl + "/admin")
  145. return
  146. }
  147. ctx.Data["Stats"] = models.GetStatistic()
  148. // FIXME: update periodically
  149. updateSystemStatus()
  150. ctx.Data["SysStatus"] = sysStatus
  151. ctx.HTML(200, DASHBOARD)
  152. }
  153. func SendTestMail(ctx *context.Context) {
  154. email := ctx.Query("email")
  155. // Send a test email to the user's email address and redirect back to Config
  156. if err := mailer.SendTestMail(email); err != nil {
  157. ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err))
  158. } else {
  159. ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
  160. }
  161. ctx.Redirect(setting.AppSubUrl + "/admin/config")
  162. }
  163. func Config(ctx *context.Context) {
  164. ctx.Data["Title"] = ctx.Tr("admin.config")
  165. ctx.Data["PageIsAdmin"] = true
  166. ctx.Data["PageIsAdminConfig"] = true
  167. ctx.Data["AppUrl"] = setting.AppUrl
  168. ctx.Data["Domain"] = setting.Domain
  169. ctx.Data["OfflineMode"] = setting.OfflineMode
  170. ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
  171. ctx.Data["RunUser"] = setting.RunUser
  172. ctx.Data["RunMode"] = strings.Title(macaron.Env)
  173. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  174. ctx.Data["StaticRootPath"] = setting.StaticRootPath
  175. ctx.Data["LogRootPath"] = setting.LogRootPath
  176. ctx.Data["ScriptType"] = setting.ScriptType
  177. ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  178. ctx.Data["SSH"] = setting.SSH
  179. ctx.Data["Service"] = setting.Service
  180. ctx.Data["DbCfg"] = models.DbCfg
  181. ctx.Data["Webhook"] = setting.Webhook
  182. ctx.Data["MailerEnabled"] = false
  183. if setting.MailService != nil {
  184. ctx.Data["MailerEnabled"] = true
  185. ctx.Data["Mailer"] = setting.MailService
  186. }
  187. ctx.Data["CacheAdapter"] = setting.CacheAdapter
  188. ctx.Data["CacheInternal"] = setting.CacheInternal
  189. ctx.Data["CacheConn"] = setting.CacheConn
  190. ctx.Data["SessionConfig"] = setting.SessionConfig
  191. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  192. type logger struct {
  193. Mode, Config string
  194. }
  195. loggers := make([]*logger, len(setting.LogModes))
  196. for i := range setting.LogModes {
  197. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  198. }
  199. ctx.Data["Loggers"] = loggers
  200. ctx.HTML(200, CONFIG)
  201. }
  202. func Monitor(ctx *context.Context) {
  203. ctx.Data["Title"] = ctx.Tr("admin.monitor")
  204. ctx.Data["PageIsAdmin"] = true
  205. ctx.Data["PageIsAdminMonitor"] = true
  206. ctx.Data["Processes"] = process.Processes
  207. ctx.Data["Entries"] = cron.ListTasks()
  208. ctx.HTML(200, MONITOR)
  209. }