admin.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package admin
  7. import (
  8. "fmt"
  9. "gitote/gitote/models"
  10. "gitote/gitote/pkg/context"
  11. "gitote/gitote/pkg/cron"
  12. "gitote/gitote/pkg/mailer"
  13. "gitote/gitote/pkg/process"
  14. "gitote/gitote/pkg/setting"
  15. "gitote/gitote/pkg/tool"
  16. "runtime"
  17. "strings"
  18. "time"
  19. "github.com/Unknwon/com"
  20. "github.com/json-iterator/go"
  21. "gopkg.in/macaron.v1"
  22. )
  23. const (
  24. // DASHBOARD page template
  25. DASHBOARD = "admin/dashboard"
  26. // ANALYTICS page template
  27. ANALYTICS = "admin/analytics"
  28. // API page template
  29. API = "admin/api"
  30. // CONFIG page template
  31. CONFIG = "admin/config"
  32. // MONITOR page template
  33. MONITOR = "admin/monitor"
  34. // STATUS page template
  35. STATUS = "admin/status"
  36. )
  37. var (
  38. startTime = time.Now()
  39. )
  40. var sysStatus struct {
  41. Uptime string
  42. NumGoroutine int
  43. // General statistics.
  44. MemAllocated string // bytes allocated and still in use
  45. MemTotal string // bytes allocated (even if freed)
  46. MemSys string // bytes obtained from system (sum of XxxSys below)
  47. Lookups uint64 // number of pointer lookups
  48. MemMallocs uint64 // number of mallocs
  49. MemFrees uint64 // number of frees
  50. // Main allocation heap statistics.
  51. HeapAlloc string // bytes allocated and still in use
  52. HeapSys string // bytes obtained from system
  53. HeapIdle string // bytes in idle spans
  54. HeapInuse string // bytes in non-idle span
  55. HeapReleased string // bytes released to the OS
  56. HeapObjects uint64 // total number of allocated objects
  57. // Low-level fixed-size structure allocator statistics.
  58. // Inuse is bytes used now.
  59. // Sys is bytes obtained from system.
  60. StackInuse string // bootstrap stacks
  61. StackSys string
  62. MSpanInuse string // mspan structures
  63. MSpanSys string
  64. MCacheInuse string // mcache structures
  65. MCacheSys string
  66. BuckHashSys string // profiling bucket hash table
  67. GCSys string // GC metadata
  68. OtherSys string // other system allocations
  69. // Garbage collector statistics.
  70. NextGC string // next run in HeapAlloc time (bytes)
  71. LastGC string // last run in absolute time (ns)
  72. PauseTotalNs string
  73. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  74. NumGC uint32
  75. }
  76. func updateSystemStatus() {
  77. sysStatus.Uptime = tool.TimeSincePro(startTime)
  78. m := new(runtime.MemStats)
  79. runtime.ReadMemStats(m)
  80. sysStatus.NumGoroutine = runtime.NumGoroutine()
  81. sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
  82. sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
  83. sysStatus.MemSys = tool.FileSize(int64(m.Sys))
  84. sysStatus.Lookups = m.Lookups
  85. sysStatus.MemMallocs = m.Mallocs
  86. sysStatus.MemFrees = m.Frees
  87. sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
  88. sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
  89. sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
  90. sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
  91. sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
  92. sysStatus.HeapObjects = m.HeapObjects
  93. sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
  94. sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
  95. sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
  96. sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
  97. sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
  98. sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
  99. sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
  100. sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
  101. sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
  102. sysStatus.NextGC = tool.FileSize(int64(m.NextGC))
  103. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  104. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  105. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  106. sysStatus.NumGC = m.NumGC
  107. }
  108. // AdminOperation types.
  109. type AdminOperation int
  110. const (
  111. // CLEAN_INACTIVATE_USER operation value
  112. CLEAN_INACTIVATE_USER AdminOperation = iota + 1
  113. // CLEAN_REPO_ARCHIVES operation value
  114. CLEAN_REPO_ARCHIVES
  115. // CLEAN_MISSING_REPOS operation value
  116. CLEAN_MISSING_REPOS
  117. // GIT_GC_REPOS operation value
  118. GIT_GC_REPOS
  119. // SYNC_SSH_AUTHORIZED_KEY operation value
  120. SYNC_SSH_AUTHORIZED_KEY
  121. // SYNC_REPOSITORY_HOOKS operation value
  122. SYNC_REPOSITORY_HOOKS
  123. // REINIT_MISSING_REPOSITORY operation value
  124. REINIT_MISSING_REPOSITORY
  125. )
  126. // Dashboard shows dashboard page
  127. func Dashboard(c *context.Context) {
  128. c.Data["Title"] = "Dashboard"
  129. c.Data["PageIsAdmin"] = true
  130. c.Data["PageIsAdminDashboard"] = true
  131. // Run operation.
  132. op, _ := com.StrTo(c.Query("op")).Int()
  133. if op > 0 {
  134. var err error
  135. var success string
  136. switch AdminOperation(op) {
  137. case CLEAN_REPO_ARCHIVES:
  138. success = "All repositories archives have been deleted successfully."
  139. err = models.DeleteRepositoryArchives()
  140. case CLEAN_MISSING_REPOS:
  141. success = "All repository records that lost Git files have been deleted successfully."
  142. err = models.DeleteMissingRepositories()
  143. case GIT_GC_REPOS:
  144. success = "All repositories have done garbage collection successfully."
  145. err = models.GitGcRepos()
  146. case SYNC_SSH_AUTHORIZED_KEY:
  147. success = "All public keys have been rewritten successfully."
  148. err = models.RewriteAuthorizedKeys()
  149. case SYNC_REPOSITORY_HOOKS:
  150. success = "All repositories' pre-receive, update and post-receive hooks have been resynced successfully."
  151. err = models.SyncRepositoryHooks()
  152. case REINIT_MISSING_REPOSITORY:
  153. success = "All repository records that lost Git files have been reinitialized successfully."
  154. err = models.ReinitMissingRepositories()
  155. }
  156. if err != nil {
  157. c.Flash.Error(err.Error())
  158. } else {
  159. c.Flash.Success(success)
  160. }
  161. c.Redirect(setting.AppSubURL + "/admin")
  162. return
  163. }
  164. c.Data["Stats"] = models.GetStatistic()
  165. // FIXME: update periodically
  166. updateSystemStatus()
  167. c.Data["SysStatus"] = sysStatus
  168. c.Data["GitVersion"] = setting.Git.Version
  169. c.HTML(200, DASHBOARD)
  170. }
  171. // SendTestMail send mail to verify the email
  172. func SendTestMail(c *context.Context) {
  173. email := c.Query("email")
  174. // Send a test email to the user's email address and redirect back to Config
  175. if err := mailer.SendTestMail(email); err != nil {
  176. c.Flash.Error(c.Tr("admin.config.test_mail_failed", email, err))
  177. } else {
  178. c.Flash.Info(c.Tr("admin.config.test_mail_sent", email))
  179. }
  180. c.Redirect(setting.AppSubURL + "/admin/config")
  181. }
  182. // Config shows configuration page
  183. func Config(c *context.Context) {
  184. c.Data["Title"] = "Configuration"
  185. c.Data["PageIsAdmin"] = true
  186. c.Data["PageIsAdminConfig"] = true
  187. c.Data["AppURL"] = setting.AppURL
  188. c.Data["Domain"] = setting.Domain
  189. c.Data["OfflineMode"] = setting.OfflineMode
  190. c.Data["DisableRouterLog"] = setting.DisableRouterLog
  191. c.Data["RunUser"] = setting.RunUser
  192. c.Data["RunMode"] = strings.Title(macaron.Env)
  193. c.Data["StaticRootPath"] = setting.StaticRootPath
  194. c.Data["LogRootPath"] = setting.LogRootPath
  195. c.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
  196. c.Data["SSH"] = setting.SSH
  197. c.Data["RepoRootPath"] = setting.RepoRootPath
  198. c.Data["ScriptType"] = setting.ScriptType
  199. c.Data["Repository"] = setting.Repository
  200. c.Data["HTTP"] = setting.HTTP
  201. c.Data["DbCfg"] = models.DbCfg
  202. c.Data["Service"] = setting.Service
  203. c.Data["Webhook"] = setting.Webhook
  204. c.Data["MailerEnabled"] = false
  205. if setting.MailService != nil {
  206. c.Data["MailerEnabled"] = true
  207. c.Data["Mailer"] = setting.MailService
  208. }
  209. c.Data["CacheAdapter"] = setting.CacheAdapter
  210. c.Data["CacheInterval"] = setting.CacheInterval
  211. c.Data["CacheConn"] = setting.CacheConn
  212. c.Data["SessionConfig"] = setting.SessionConfig
  213. c.Data["DisableGravatar"] = setting.DisableGravatar
  214. c.Data["EnableFederatedAvatar"] = setting.EnableFederatedAvatar
  215. c.Data["GitVersion"] = setting.Git.Version
  216. c.Data["Git"] = setting.Git
  217. type logger struct {
  218. Mode, Config string
  219. }
  220. loggers := make([]*logger, len(setting.LogModes))
  221. for i := range setting.LogModes {
  222. loggers[i] = &logger{
  223. Mode: strings.Title(setting.LogModes[i]),
  224. }
  225. result, _ := jsoniter.MarshalIndent(setting.LogConfigs[i], "", " ")
  226. loggers[i].Config = string(result)
  227. }
  228. c.Data["Loggers"] = loggers
  229. c.HTML(200, CONFIG)
  230. }
  231. // Monitor shows monitor page
  232. func Monitor(c *context.Context) {
  233. c.Data["Title"] = "Monitoring"
  234. c.Data["PageIsAdmin"] = true
  235. c.Data["PageIsAdminMonitor"] = true
  236. c.Data["Processes"] = process.Processes
  237. c.Data["Entries"] = cron.ListTasks()
  238. c.HTML(200, MONITOR)
  239. }
  240. // Status shows status page
  241. func Status(c *context.Context) {
  242. c.Data["Title"] = "Status"
  243. c.Data["PageIsAdminStatus"] = true
  244. c.HTML(200, STATUS)
  245. }
  246. // Analytics shows analytics page
  247. func Analytics(c *context.Context) {
  248. c.Data["Title"] = "Analytics"
  249. c.Data["PageIsAdminAnalytics"] = true
  250. c.HTML(200, ANALYTICS)
  251. }
  252. // AdminAPI shows api configuration
  253. func AdminAPI(c *context.Context) {
  254. c.Data["Title"] = "API"
  255. c.Data["PageIsAdminAPI"] = true
  256. c.HTML(200, API)
  257. }