admin.go 7.1 KB

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