admin.go 8.6 KB

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