admin.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. USERS base.TplName = "admin/users"
  22. REPOS base.TplName = "admin/repos"
  23. AUTHS base.TplName = "admin/auths"
  24. CONFIG base.TplName = "admin/config"
  25. MONITOR_PROCESS base.TplName = "admin/monitor/process"
  26. MONITOR_CRON base.TplName = "admin/monitor/cron"
  27. )
  28. var (
  29. startTime = time.Now()
  30. )
  31. var sysStatus struct {
  32. Uptime string
  33. NumGoroutine int
  34. // General statistics.
  35. MemAllocated string // bytes allocated and still in use
  36. MemTotal string // bytes allocated (even if freed)
  37. MemSys string // bytes obtained from system (sum of XxxSys below)
  38. Lookups uint64 // number of pointer lookups
  39. MemMallocs uint64 // number of mallocs
  40. MemFrees uint64 // number of frees
  41. // Main allocation heap statistics.
  42. HeapAlloc string // bytes allocated and still in use
  43. HeapSys string // bytes obtained from system
  44. HeapIdle string // bytes in idle spans
  45. HeapInuse string // bytes in non-idle span
  46. HeapReleased string // bytes released to the OS
  47. HeapObjects uint64 // total number of allocated objects
  48. // Low-level fixed-size structure allocator statistics.
  49. // Inuse is bytes used now.
  50. // Sys is bytes obtained from system.
  51. StackInuse string // bootstrap stacks
  52. StackSys string
  53. MSpanInuse string // mspan structures
  54. MSpanSys string
  55. MCacheInuse string // mcache structures
  56. MCacheSys string
  57. BuckHashSys string // profiling bucket hash table
  58. GCSys string // GC metadata
  59. OtherSys string // other system allocations
  60. // Garbage collector statistics.
  61. NextGC string // next run in HeapAlloc time (bytes)
  62. LastGC string // last run in absolute time (ns)
  63. PauseTotalNs string
  64. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  65. NumGC uint32
  66. }
  67. func updateSystemStatus() {
  68. sysStatus.Uptime = base.TimeSincePro(startTime)
  69. m := new(runtime.MemStats)
  70. runtime.ReadMemStats(m)
  71. sysStatus.NumGoroutine = runtime.NumGoroutine()
  72. sysStatus.MemAllocated = base.FileSize(int64(m.Alloc))
  73. sysStatus.MemTotal = base.FileSize(int64(m.TotalAlloc))
  74. sysStatus.MemSys = base.FileSize(int64(m.Sys))
  75. sysStatus.Lookups = m.Lookups
  76. sysStatus.MemMallocs = m.Mallocs
  77. sysStatus.MemFrees = m.Frees
  78. sysStatus.HeapAlloc = base.FileSize(int64(m.HeapAlloc))
  79. sysStatus.HeapSys = base.FileSize(int64(m.HeapSys))
  80. sysStatus.HeapIdle = base.FileSize(int64(m.HeapIdle))
  81. sysStatus.HeapInuse = base.FileSize(int64(m.HeapInuse))
  82. sysStatus.HeapReleased = base.FileSize(int64(m.HeapReleased))
  83. sysStatus.HeapObjects = m.HeapObjects
  84. sysStatus.StackInuse = base.FileSize(int64(m.StackInuse))
  85. sysStatus.StackSys = base.FileSize(int64(m.StackSys))
  86. sysStatus.MSpanInuse = base.FileSize(int64(m.MSpanInuse))
  87. sysStatus.MSpanSys = base.FileSize(int64(m.MSpanSys))
  88. sysStatus.MCacheInuse = base.FileSize(int64(m.MCacheInuse))
  89. sysStatus.MCacheSys = base.FileSize(int64(m.MCacheSys))
  90. sysStatus.BuckHashSys = base.FileSize(int64(m.BuckHashSys))
  91. sysStatus.GCSys = base.FileSize(int64(m.GCSys))
  92. sysStatus.OtherSys = base.FileSize(int64(m.OtherSys))
  93. sysStatus.NextGC = base.FileSize(int64(m.NextGC))
  94. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  95. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  96. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  97. sysStatus.NumGC = m.NumGC
  98. }
  99. // Operation types.
  100. type AdminOperation int
  101. const (
  102. CLEAN_UNBIND_OAUTH AdminOperation = iota + 1
  103. CLEAN_INACTIVATE_USER
  104. )
  105. func Dashboard(ctx *middleware.Context) {
  106. ctx.Data["Title"] = "Admin Dashboard"
  107. ctx.Data["PageIsDashboard"] = 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 Users(ctx *middleware.Context) {
  135. ctx.Data["Title"] = "User Management"
  136. ctx.Data["PageIsUsers"] = true
  137. p := com.StrTo(ctx.Query("p")).MustInt()
  138. if p < 1 {
  139. p = 1
  140. }
  141. pageNum := 50
  142. count := models.CountUsers()
  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["Users"], err = models.GetUsers(pageNum, (p-1)*pageNum)
  154. if err != nil {
  155. ctx.Handle(500, "admin.Users(GetUsers)", err)
  156. return
  157. }
  158. ctx.HTML(200, USERS)
  159. }
  160. func Repositories(ctx *middleware.Context) {
  161. ctx.Data["Title"] = "Repository Management"
  162. ctx.Data["PageIsRepos"] = true
  163. p := com.StrTo(ctx.Query("p")).MustInt()
  164. if p < 1 {
  165. p = 1
  166. }
  167. pageNum := 50
  168. count := models.CountRepositories()
  169. curCount := int64((p-1)*pageNum + pageNum)
  170. if curCount > count {
  171. p = int(count) / pageNum
  172. } else if count > curCount {
  173. ctx.Data["NextPageNum"] = p + 1
  174. }
  175. if p > 1 {
  176. ctx.Data["LastPageNum"] = p - 1
  177. }
  178. var err error
  179. ctx.Data["Repos"], err = models.GetRepositoriesWithUsers(pageNum, (p-1)*pageNum)
  180. if err != nil {
  181. ctx.Handle(500, "admin.Repositories", err)
  182. return
  183. }
  184. ctx.HTML(200, REPOS)
  185. }
  186. func Auths(ctx *middleware.Context) {
  187. ctx.Data["Title"] = "Auth Sources"
  188. ctx.Data["PageIsAuths"] = true
  189. var err error
  190. ctx.Data["Sources"], err = models.GetAuths()
  191. if err != nil {
  192. ctx.Handle(500, "admin.Auths", err)
  193. return
  194. }
  195. ctx.HTML(200, AUTHS)
  196. }
  197. func Config(ctx *middleware.Context) {
  198. ctx.Data["Title"] = "Server Configuration"
  199. ctx.Data["PageIsConfig"] = true
  200. ctx.Data["AppUrl"] = setting.AppUrl
  201. ctx.Data["Domain"] = setting.Domain
  202. ctx.Data["OfflineMode"] = setting.OfflineMode
  203. ctx.Data["DisableRouterLog"] = setting.DisableRouterLog
  204. ctx.Data["RunUser"] = setting.RunUser
  205. ctx.Data["RunMode"] = strings.Title(macaron.Env)
  206. ctx.Data["RepoRootPath"] = setting.RepoRootPath
  207. ctx.Data["StaticRootPath"] = setting.StaticRootPath
  208. ctx.Data["LogRootPath"] = setting.LogRootPath
  209. ctx.Data["ScriptType"] = setting.ScriptType
  210. ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  211. ctx.Data["Service"] = setting.Service
  212. ctx.Data["DbCfg"] = models.DbCfg
  213. ctx.Data["WebhookTaskInterval"] = setting.WebhookTaskInterval
  214. ctx.Data["WebhookDeliverTimeout"] = setting.WebhookDeliverTimeout
  215. ctx.Data["MailerEnabled"] = false
  216. if setting.MailService != nil {
  217. ctx.Data["MailerEnabled"] = true
  218. ctx.Data["Mailer"] = setting.MailService
  219. }
  220. ctx.Data["OauthEnabled"] = false
  221. if setting.OauthService != nil {
  222. ctx.Data["OauthEnabled"] = true
  223. ctx.Data["Oauther"] = setting.OauthService
  224. }
  225. ctx.Data["CacheAdapter"] = setting.CacheAdapter
  226. ctx.Data["CacheInternal"] = setting.CacheInternal
  227. ctx.Data["CacheConn"] = setting.CacheConn
  228. ctx.Data["SessionProvider"] = setting.SessionProvider
  229. ctx.Data["SessionConfig"] = setting.SessionConfig
  230. ctx.Data["PictureService"] = setting.PictureService
  231. ctx.Data["DisableGravatar"] = setting.DisableGravatar
  232. type logger struct {
  233. Mode, Config string
  234. }
  235. loggers := make([]*logger, len(setting.LogModes))
  236. for i := range setting.LogModes {
  237. loggers[i] = &logger{setting.LogModes[i], setting.LogConfigs[i]}
  238. }
  239. ctx.Data["Loggers"] = loggers
  240. ctx.HTML(200, CONFIG)
  241. }
  242. func Monitor(ctx *middleware.Context) {
  243. ctx.Data["Title"] = "Monitoring Center"
  244. ctx.Data["PageIsMonitor"] = true
  245. tab := ctx.Query("tab")
  246. switch tab {
  247. case "process":
  248. ctx.Data["PageIsMonitorProcess"] = true
  249. ctx.Data["Processes"] = process.Processes
  250. ctx.HTML(200, MONITOR_PROCESS)
  251. default:
  252. ctx.Data["PageIsMonitorCron"] = true
  253. ctx.Data["Entries"] = cron.ListEntries()
  254. ctx.HTML(200, MONITOR_CRON)
  255. }
  256. }