admin.go 7.8 KB

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