admin.go 7.8 KB

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