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.

admin.go 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/base"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/cron"
  16. "code.gitea.io/gitea/modules/process"
  17. "code.gitea.io/gitea/modules/setting"
  18. )
  19. const (
  20. tplDashboard base.TplName = "admin/dashboard"
  21. tplConfig base.TplName = "admin/config"
  22. tplMonitor 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, "en")
  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 Operation types.
  96. type Operation int
  97. const (
  98. cleanInactivateUser Operation = iota + 1
  99. cleanRepoArchives
  100. cleanMissingRepos
  101. gitGCRepos
  102. syncSSHAuthorizedKey
  103. syncRepositoryUpdateHook
  104. reinitMissingRepository
  105. syncExternalUsers
  106. gitFsck
  107. )
  108. // Dashboard show admin panel dashboard
  109. func Dashboard(ctx *context.Context) {
  110. ctx.Data["Title"] = ctx.Tr("admin.dashboard")
  111. ctx.Data["PageIsAdmin"] = true
  112. ctx.Data["PageIsAdminDashboard"] = true
  113. // Run operation.
  114. op, _ := com.StrTo(ctx.Query("op")).Int()
  115. if op > 0 {
  116. var err error
  117. var success string
  118. switch Operation(op) {
  119. case cleanInactivateUser:
  120. success = ctx.Tr("admin.dashboard.delete_inactivate_accounts_success")
  121. err = models.DeleteInactivateUsers()
  122. case cleanRepoArchives:
  123. success = ctx.Tr("admin.dashboard.delete_repo_archives_success")
  124. err = models.DeleteRepositoryArchives()
  125. case cleanMissingRepos:
  126. success = ctx.Tr("admin.dashboard.delete_missing_repos_success")
  127. err = models.DeleteMissingRepositories(ctx.User)
  128. case gitGCRepos:
  129. success = ctx.Tr("admin.dashboard.git_gc_repos_success")
  130. err = models.GitGcRepos()
  131. case syncSSHAuthorizedKey:
  132. success = ctx.Tr("admin.dashboard.resync_all_sshkeys_success")
  133. err = models.RewriteAllPublicKeys()
  134. case syncRepositoryUpdateHook:
  135. success = ctx.Tr("admin.dashboard.resync_all_hooks_success")
  136. err = models.SyncRepositoryHooks()
  137. case reinitMissingRepository:
  138. success = ctx.Tr("admin.dashboard.reinit_missing_repos_success")
  139. err = models.ReinitMissingRepositories()
  140. case syncExternalUsers:
  141. success = ctx.Tr("admin.dashboard.sync_external_users_started")
  142. go models.SyncExternalUsers()
  143. case gitFsck:
  144. success = ctx.Tr("admin.dashboard.git_fsck_started")
  145. go models.GitFsck()
  146. }
  147. if err != nil {
  148. ctx.Flash.Error(err.Error())
  149. } else {
  150. ctx.Flash.Success(success)
  151. }
  152. ctx.Redirect(setting.AppSubURL + "/admin")
  153. return
  154. }
  155. ctx.Data["Stats"] = models.GetStatistic()
  156. // FIXME: update periodically
  157. updateSystemStatus()
  158. ctx.Data["SysStatus"] = sysStatus
  159. ctx.HTML(200, tplDashboard)
  160. }
  161. // SendTestMail send test mail to confirm mail service is OK
  162. func SendTestMail(ctx *context.Context) {
  163. email := ctx.Query("email")
  164. // Send a test email to the user's email address and redirect back to Config
  165. if err := models.SendTestMail(email); err != nil {
  166. ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err))
  167. } else {
  168. ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
  169. }
  170. ctx.Redirect(setting.AppSubURL + "/admin/config")
  171. }
  172. // Config show admin config page
  173. func Config(ctx *context.Context) {
  174. ctx.Data["Title"] = ctx.Tr("admin.config")
  175. ctx.Data["PageIsAdmin"] = true
  176. ctx.Data["PageIsAdminConfig"] = true
  177. ctx.Data["CustomConf"] = setting.CustomConf
  178. ctx.Data["AppUrl"] = setting.AppURL
  179. ctx.Data["Domain"] = setting.Domain
  180. ctx.Data["OfflineMode"] = setting.OfflineMode
  181. ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
  182. ctx.Data["RunUser"] = setting.RunUser
  183. ctx.Data["RunMode"] = strings.Title(macaron.Env)
  184. ctx.Data["GitVersion"] = setting.Git.Version
  185. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  186. ctx.Data["StaticRootPath"] = setting.StaticRootPath
  187. ctx.Data["LogRootPath"] = setting.LogRootPath
  188. ctx.Data["ScriptType"] = setting.ScriptType
  189. ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  190. ctx.Data["SSH"] = setting.SSH
  191. ctx.Data["Service"] = setting.Service
  192. ctx.Data["DbCfg"] = models.DbCfg
  193. ctx.Data["Webhook"] = setting.Webhook
  194. ctx.Data["MailerEnabled"] = false
  195. if setting.MailService != nil {
  196. ctx.Data["MailerEnabled"] = true
  197. ctx.Data["Mailer"] = setting.MailService
  198. }
  199. ctx.Data["CacheAdapter"] = setting.CacheService.Adapter
  200. ctx.Data["CacheInterval"] = setting.CacheService.Interval
  201. ctx.Data["CacheConn"] = setting.CacheService.Conn
  202. ctx.Data["SessionConfig"] = setting.SessionConfig
  203. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  204. ctx.Data["EnableFederatedAvatar"] = setting.EnableFederatedAvatar
  205. ctx.Data["Git"] = setting.Git
  206. type logger struct {
  207. Mode, Config string
  208. }
  209. loggers := make([]*logger, len(setting.LogModes))
  210. for i := range setting.LogModes {
  211. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  212. }
  213. ctx.Data["Loggers"] = loggers
  214. ctx.HTML(200, tplConfig)
  215. }
  216. // Monitor show admin monitor page
  217. func Monitor(ctx *context.Context) {
  218. ctx.Data["Title"] = ctx.Tr("admin.monitor")
  219. ctx.Data["PageIsAdmin"] = true
  220. ctx.Data["PageIsAdminMonitor"] = true
  221. ctx.Data["Processes"] = process.GetManager().Processes
  222. ctx.Data["Entries"] = cron.ListTasks()
  223. ctx.HTML(200, tplMonitor)
  224. }