admin.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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/go-martini/martini"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. "github.com/gogits/gogs/modules/cron"
  14. "github.com/gogits/gogs/modules/middleware"
  15. "github.com/gogits/gogs/modules/process"
  16. "github.com/gogits/gogs/modules/setting"
  17. )
  18. const (
  19. DASHBOARD base.TplName = "admin/dashboard"
  20. USERS base.TplName = "admin/users"
  21. REPOS base.TplName = "admin/repos"
  22. AUTHS base.TplName = "admin/auths"
  23. CONFIG base.TplName = "admin/config"
  24. MONITOR_PROCESS base.TplName = "admin/monitor/process"
  25. MONITOR_CRON base.TplName = "admin/monitor/cron"
  26. )
  27. var startTime = time.Now()
  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_UNBIND_OAUTH AdminOperation = iota + 1
  100. CLEAN_INACTIVATE_USER
  101. )
  102. func Dashboard(ctx *middleware.Context) {
  103. ctx.Data["Title"] = "Admin Dashboard"
  104. ctx.Data["PageIsDashboard"] = true
  105. // Run operation.
  106. op, _ := base.StrTo(ctx.Query("op")).Int()
  107. if op > 0 {
  108. var err error
  109. var success string
  110. switch AdminOperation(op) {
  111. case CLEAN_UNBIND_OAUTH:
  112. success = "All unbind OAuthes have been deleted."
  113. err = models.CleanUnbindOauth()
  114. case CLEAN_INACTIVATE_USER:
  115. success = "All inactivate accounts have been deleted."
  116. err = models.DeleteInactivateUsers()
  117. }
  118. if err != nil {
  119. ctx.Flash.Error(err.Error())
  120. } else {
  121. ctx.Flash.Success(success)
  122. }
  123. ctx.Redirect("/admin")
  124. return
  125. }
  126. ctx.Data["Stats"] = models.GetStatistic()
  127. updateSystemStatus()
  128. ctx.Data["SysStatus"] = sysStatus
  129. ctx.HTML(200, DASHBOARD)
  130. }
  131. func Users(ctx *middleware.Context) {
  132. ctx.Data["Title"] = "User Management"
  133. ctx.Data["PageIsUsers"] = true
  134. var err error
  135. ctx.Data["Users"], err = models.GetUsers(200, 0)
  136. if err != nil {
  137. ctx.Handle(500, "admin.Users(GetUsers)", err)
  138. return
  139. }
  140. ctx.HTML(200, USERS)
  141. }
  142. func Repositories(ctx *middleware.Context) {
  143. ctx.Data["Title"] = "Repository Management"
  144. ctx.Data["PageIsRepos"] = true
  145. var err error
  146. ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(200, 0)
  147. if err != nil {
  148. ctx.Handle(500, "admin.Repositories", err)
  149. return
  150. }
  151. ctx.HTML(200, REPOS)
  152. }
  153. func Auths(ctx *middleware.Context) {
  154. ctx.Data["Title"] = "Auth Sources"
  155. ctx.Data["PageIsAuths"] = true
  156. var err error
  157. ctx.Data["Sources"], err = models.GetAuths()
  158. if err != nil {
  159. ctx.Handle(500, "admin.Auths", err)
  160. return
  161. }
  162. ctx.HTML(200, AUTHS)
  163. }
  164. func Config(ctx *middleware.Context) {
  165. ctx.Data["Title"] = "Server Configuration"
  166. ctx.Data["PageIsConfig"] = 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(martini.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["Service"] = setting.Service
  179. ctx.Data["DbCfg"] = models.DbCfg
  180. ctx.Data["WebhookTaskInterval"] = setting.WebhookTaskInterval
  181. ctx.Data["WebhookDeliverTimeout"] = setting.WebhookDeliverTimeout
  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["OauthEnabled"] = false
  188. if setting.OauthService != nil {
  189. ctx.Data["OauthEnabled"] = true
  190. ctx.Data["Oauther"] = setting.OauthService
  191. }
  192. ctx.Data["CacheAdapter"] = setting.CacheAdapter
  193. ctx.Data["CacheConfig"] = setting.CacheConfig
  194. ctx.Data["SessionProvider"] = setting.SessionProvider
  195. ctx.Data["SessionConfig"] = setting.SessionConfig
  196. ctx.Data["PictureService"] = setting.PictureService
  197. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  198. type logger struct {
  199. Mode, Config string
  200. }
  201. loggers := make([]*logger, len(setting.LogModes))
  202. for i := range setting.LogModes {
  203. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  204. }
  205. ctx.Data["Loggers"] = loggers
  206. ctx.HTML(200, CONFIG)
  207. }
  208. func Monitor(ctx *middleware.Context) {
  209. ctx.Data["Title"] = "Monitoring Center"
  210. ctx.Data["PageIsMonitor"] = true
  211. tab := ctx.Query("tab")
  212. switch tab {
  213. case "process":
  214. ctx.Data["PageIsMonitorProcess"] = true
  215. ctx.Data["Processes"] = process.Processes
  216. ctx.HTML(200, MONITOR_PROCESS)
  217. default:
  218. ctx.Data["PageIsMonitorCron"] = true
  219. ctx.Data["Entries"] = cron.ListEntries()
  220. ctx.HTML(200, MONITOR_CRON)
  221. }
  222. }