admin.go 8.7 KB

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