admin.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. "github.com/Unknwon/macaron"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/models/cron"
  14. "github.com/gogits/gogs/modules/base"
  15. "github.com/gogits/gogs/modules/middleware"
  16. "github.com/gogits/gogs/modules/process"
  17. "github.com/gogits/gogs/modules/setting"
  18. )
  19. const (
  20. DASHBOARD base.TplName = "admin/dashboard"
  21. CONFIG base.TplName = "admin/config"
  22. MONITOR base.TplName = "admin/monitor"
  23. )
  24. var (
  25. startTime = time.Now()
  26. )
  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 = base.TimeSincePro(startTime)
  65. m := new(runtime.MemStats)
  66. runtime.ReadMemStats(m)
  67. sysStatus.NumGoroutine = runtime.NumGoroutine()
  68. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  69. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  70. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  71. sysStatus.Lookups = m.Lookups
  72. sysStatus.MemMallocs = m.Mallocs
  73. sysStatus.MemFrees = m.Frees
  74. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  75. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  76. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  77. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  78. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  79. sysStatus.HeapObjects = m.HeapObjects
  80. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  81. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  82. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  83. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  84. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  85. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  86. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  87. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  88. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  89. sysStatus.NextGC = base.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. GIT_GC_REPOS
  101. SYNC_SSH_AUTHORIZED_KEY
  102. SYNC_REPOSITORY_UPDATE_HOOK
  103. )
  104. func Dashboard(ctx *middleware.Context) {
  105. ctx.Data["Title"] = ctx.Tr("admin.dashboard")
  106. ctx.Data["PageIsAdmin"] = true
  107. ctx.Data["PageIsAdminDashboard"] = true
  108. // Run operation.
  109. op, _ := com.StrTo(ctx.Query("op")).Int()
  110. if op > 0 {
  111. var err error
  112. var success string
  113. switch AdminOperation(op) {
  114. case CLEAN_INACTIVATE_USER:
  115. success = ctx.Tr("admin.dashboard.delete_inactivate_accounts_success")
  116. err = models.DeleteInactivateUsers()
  117. case CLEAN_REPO_ARCHIVES:
  118. success = ctx.Tr("admin.dashboard.delete_repo_archives_success")
  119. err = models.DeleteRepositoryArchives()
  120. case GIT_GC_REPOS:
  121. success = ctx.Tr("admin.dashboard.git_gc_repos_success")
  122. err = models.GitGcRepos()
  123. case SYNC_SSH_AUTHORIZED_KEY:
  124. success = ctx.Tr("admin.dashboard.resync_all_sshkeys_success")
  125. err = models.RewriteAllPublicKeys()
  126. case SYNC_REPOSITORY_UPDATE_HOOK:
  127. success = ctx.Tr("admin.dashboard.resync_all_update_hooks_success")
  128. err = models.RewriteRepositoryUpdateHook()
  129. }
  130. if err != nil {
  131. ctx.Flash.Error(err.Error())
  132. } else {
  133. ctx.Flash.Success(success)
  134. }
  135. ctx.Redirect(setting.AppSubUrl + "/admin")
  136. return
  137. }
  138. ctx.Data["Stats"] = models.GetStatistic()
  139. // FIXME: update periodically
  140. updateSystemStatus()
  141. ctx.Data["SysStatus"] = sysStatus
  142. ctx.HTML(200, DASHBOARD)
  143. }
  144. func Config(ctx *middleware.Context) {
  145. ctx.Data["Title"] = ctx.Tr("admin.config")
  146. ctx.Data["PageIsAdmin"] = true
  147. ctx.Data["PageIsAdminConfig"] = true
  148. ctx.Data["AppUrl"] = setting.AppUrl
  149. ctx.Data["Domain"] = setting.Domain
  150. ctx.Data["OfflineMode"] = setting.OfflineMode
  151. ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
  152. ctx.Data["RunUser"] = setting.RunUser
  153. ctx.Data["RunMode"] = strings.Title(macaron.Env)
  154. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  155. ctx.Data["StaticRootPath"] = setting.StaticRootPath
  156. ctx.Data["LogRootPath"] = setting.LogRootPath
  157. ctx.Data["ScriptType"] = setting.ScriptType
  158. ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  159. ctx.Data["Service"] = setting.Service
  160. ctx.Data["DbCfg"] = models.DbCfg
  161. ctx.Data["Webhook"] = setting.Webhook
  162. ctx.Data["MailerEnabled"] = false
  163. if setting.MailService != nil {
  164. ctx.Data["MailerEnabled"] = true
  165. ctx.Data["Mailer"] = setting.MailService
  166. }
  167. ctx.Data["CacheAdapter"] = setting.CacheAdapter
  168. ctx.Data["CacheInternal"] = setting.CacheInternal
  169. ctx.Data["CacheConn"] = setting.CacheConn
  170. ctx.Data["SessionConfig"] = setting.SessionConfig
  171. ctx.Data["PictureService"] = setting.PictureService
  172. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  173. type logger struct {
  174. Mode, Config string
  175. }
  176. loggers := make([]*logger, len(setting.LogModes))
  177. for i := range setting.LogModes {
  178. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  179. }
  180. ctx.Data["Loggers"] = loggers
  181. ctx.HTML(200, CONFIG)
  182. }
  183. func Monitor(ctx *middleware.Context) {
  184. ctx.Data["Title"] = ctx.Tr("admin.monitor")
  185. ctx.Data["PageIsAdmin"] = true
  186. ctx.Data["PageIsAdminMonitor"] = true
  187. ctx.Data["Processes"] = process.Processes
  188. ctx.Data["Entries"] = cron.ListTasks()
  189. ctx.HTML(200, MONITOR)
  190. }