admin.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/setting"
  16. )
  17. var startTime = time.Now()
  18. var sysStatus struct {
  19. Uptime string
  20. NumGoroutine int
  21. // General statistics.
  22. MemAllocated string // bytes allocated and still in use
  23. MemTotal string // bytes allocated (even if freed)
  24. MemSys string // bytes obtained from system (sum of XxxSys below)
  25. Lookups uint64 // number of pointer lookups
  26. MemMallocs uint64 // number of mallocs
  27. MemFrees uint64 // number of frees
  28. // Main allocation heap statistics.
  29. HeapAlloc string // bytes allocated and still in use
  30. HeapSys string // bytes obtained from system
  31. HeapIdle string // bytes in idle spans
  32. HeapInuse string // bytes in non-idle span
  33. HeapReleased string // bytes released to the OS
  34. HeapObjects uint64 // total number of allocated objects
  35. // Low-level fixed-size structure allocator statistics.
  36. // Inuse is bytes used now.
  37. // Sys is bytes obtained from system.
  38. StackInuse string // bootstrap stacks
  39. StackSys string
  40. MSpanInuse string // mspan structures
  41. MSpanSys string
  42. MCacheInuse string // mcache structures
  43. MCacheSys string
  44. BuckHashSys string // profiling bucket hash table
  45. GCSys string // GC metadata
  46. OtherSys string // other system allocations
  47. // Garbage collector statistics.
  48. NextGC string // next run in HeapAlloc time (bytes)
  49. LastGC string // last run in absolute time (ns)
  50. PauseTotalNs string
  51. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  52. NumGC uint32
  53. }
  54. func updateSystemStatus() {
  55. sysStatus.Uptime = base.TimeSincePro(startTime)
  56. m := new(runtime.MemStats)
  57. runtime.ReadMemStats(m)
  58. sysStatus.NumGoroutine = runtime.NumGoroutine()
  59. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  60. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  61. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  62. sysStatus.Lookups = m.Lookups
  63. sysStatus.MemMallocs = m.Mallocs
  64. sysStatus.MemFrees = m.Frees
  65. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  66. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  67. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  68. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  69. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  70. sysStatus.HeapObjects = m.HeapObjects
  71. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  72. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  73. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  74. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  75. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  76. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  77. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  78. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  79. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  80. sysStatus.NextGC = base.FileSize(int64(m.NextGC))
  81. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  82. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  83. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  84. sysStatus.NumGC = m.NumGC
  85. }
  86. // Operation types.
  87. const (
  88. OT_CLEAN_OAUTH = iota + 1
  89. )
  90. func Dashboard(ctx *middleware.Context) {
  91. ctx.Data["Title"] = "Admin Dashboard"
  92. ctx.Data["PageIsDashboard"] = true
  93. // Run operation.
  94. op, _ := base.StrTo(ctx.Query("op")).Int()
  95. if op > 0 {
  96. var err error
  97. var success string
  98. switch op {
  99. case OT_CLEAN_OAUTH:
  100. success = "All unbind OAuthes have been deleted."
  101. err = models.CleanUnbindOauth()
  102. }
  103. if err != nil {
  104. ctx.Flash.Error(err.Error())
  105. } else {
  106. ctx.Flash.Success(success)
  107. }
  108. ctx.Redirect("/admin")
  109. return
  110. }
  111. ctx.Data["Stats"] = models.GetStatistic()
  112. updateSystemStatus()
  113. ctx.Data["SysStatus"] = sysStatus
  114. ctx.HTML(200, "admin/dashboard")
  115. }
  116. func Users(ctx *middleware.Context) {
  117. ctx.Data["Title"] = "User Management"
  118. ctx.Data["PageIsUsers"] = true
  119. var err error
  120. ctx.Data["Users"], err = models.GetUsers(200, 0)
  121. if err != nil {
  122. ctx.Handle(500, "admin.Users", err)
  123. return
  124. }
  125. ctx.HTML(200, "admin/users")
  126. }
  127. func Repositories(ctx *middleware.Context) {
  128. ctx.Data["Title"] = "Repository Management"
  129. ctx.Data["PageIsRepos"] = true
  130. var err error
  131. ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(200, 0)
  132. if err != nil {
  133. ctx.Handle(500, "admin.Repositories", err)
  134. return
  135. }
  136. ctx.HTML(200, "admin/repos")
  137. }
  138. func Auths(ctx *middleware.Context) {
  139. ctx.Data["Title"] = "Auth Sources"
  140. ctx.Data["PageIsAuths"] = true
  141. var err error
  142. ctx.Data["Sources"], err = models.GetAuths()
  143. if err != nil {
  144. ctx.Handle(500, "admin.Auths", err)
  145. return
  146. }
  147. ctx.HTML(200, "admin/auths")
  148. }
  149. func Config(ctx *middleware.Context) {
  150. ctx.Data["Title"] = "Server Configuration"
  151. ctx.Data["PageIsConfig"] = true
  152. ctx.Data["AppUrl"] = setting.AppUrl
  153. ctx.Data["Domain"] = setting.Domain
  154. ctx.Data["OfflineMode"] = setting.OfflineMode
  155. ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
  156. ctx.Data["RunUser"] = setting.RunUser
  157. ctx.Data["RunMode"] = strings.Title(martini.Env)
  158. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  159. ctx.Data["StaticRootPath"] = setting.StaticRootPath
  160. ctx.Data["LogRootPath"] = setting.LogRootPath
  161. ctx.Data["ScriptType"] = setting.ScriptType
  162. ctx.Data["Service"] = setting.Service
  163. ctx.Data["DbCfg"] = models.DbCfg
  164. ctx.Data["WebhookTaskInterval"] = setting.WebhookTaskInterval
  165. ctx.Data["WebhookDeliverTimeout"] = setting.WebhookDeliverTimeout
  166. ctx.Data["MailerEnabled"] = false
  167. if setting.MailService != nil {
  168. ctx.Data["MailerEnabled"] = true
  169. ctx.Data["Mailer"] = setting.MailService
  170. }
  171. ctx.Data["OauthEnabled"] = false
  172. if setting.OauthService != nil {
  173. ctx.Data["OauthEnabled"] = true
  174. ctx.Data["Oauther"] = setting.OauthService
  175. }
  176. ctx.Data["CacheAdapter"] = setting.CacheAdapter
  177. ctx.Data["CacheConfig"] = setting.CacheConfig
  178. ctx.Data["SessionProvider"] = setting.SessionProvider
  179. ctx.Data["SessionConfig"] = setting.SessionConfig
  180. ctx.Data["PictureService"] = setting.PictureService
  181. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  182. type logger struct {
  183. Mode, Config string
  184. }
  185. loggers := make([]*logger, len(setting.LogModes))
  186. for i := range setting.LogModes {
  187. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  188. }
  189. ctx.Data["Loggers"] = loggers
  190. ctx.HTML(200, "admin/config")
  191. }
  192. func Monitor(ctx *middleware.Context) {
  193. ctx.Data["Title"] = "Monitoring Center"
  194. ctx.Data["PageIsMonitor"] = true
  195. tab := ctx.Query("tab")
  196. switch tab {
  197. case "process":
  198. ctx.Data["PageIsMonitorProcess"] = true
  199. default:
  200. ctx.Data["PageIsMonitorCron"] = true
  201. ctx.Data["Entries"] = cron.ListEntries()
  202. }
  203. ctx.HTML(200, "admin/monitor/cron")
  204. }