admin.go 8.3 KB

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