admin.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. ANALYTICS = "admin/analytics"
  21. API = "admin/api"
  22. CONFIG = "admin/config"
  23. MONITOR = "admin/monitor"
  24. STATUS = "admin/status"
  25. )
  26. var (
  27. startTime = time.Now()
  28. )
  29. var sysStatus struct {
  30. Uptime string
  31. NumGoroutine int
  32. // General statistics.
  33. MemAllocated string // bytes allocated and still in use
  34. MemTotal string // bytes allocated (even if freed)
  35. MemSys string // bytes obtained from system (sum of XxxSys below)
  36. Lookups uint64 // number of pointer lookups
  37. MemMallocs uint64 // number of mallocs
  38. MemFrees uint64 // number of frees
  39. // Main allocation heap statistics.
  40. HeapAlloc string // bytes allocated and still in use
  41. HeapSys string // bytes obtained from system
  42. HeapIdle string // bytes in idle spans
  43. HeapInuse string // bytes in non-idle span
  44. HeapReleased string // bytes released to the OS
  45. HeapObjects uint64 // total number of allocated objects
  46. // Low-level fixed-size structure allocator statistics.
  47. // Inuse is bytes used now.
  48. // Sys is bytes obtained from system.
  49. StackInuse string // bootstrap stacks
  50. StackSys string
  51. MSpanInuse string // mspan structures
  52. MSpanSys string
  53. MCacheInuse string // mcache structures
  54. MCacheSys string
  55. BuckHashSys string // profiling bucket hash table
  56. GCSys string // GC metadata
  57. OtherSys string // other system allocations
  58. // Garbage collector statistics.
  59. NextGC string // next run in HeapAlloc time (bytes)
  60. LastGC string // last run in absolute time (ns)
  61. PauseTotalNs string
  62. PauseNs string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
  63. NumGC uint32
  64. }
  65. func updateSystemStatus() {
  66. sysStatus.Uptime = tool.TimeSincePro(startTime)
  67. m := new(runtime.MemStats)
  68. runtime.ReadMemStats(m)
  69. sysStatus.NumGoroutine = runtime.NumGoroutine()
  70. sysStatus.MemAllocated = tool.FileSize(int64(m.Alloc))
  71. sysStatus.MemTotal = tool.FileSize(int64(m.TotalAlloc))
  72. sysStatus.MemSys = tool.FileSize(int64(m.Sys))
  73. sysStatus.Lookups = m.Lookups
  74. sysStatus.MemMallocs = m.Mallocs
  75. sysStatus.MemFrees = m.Frees
  76. sysStatus.HeapAlloc = tool.FileSize(int64(m.HeapAlloc))
  77. sysStatus.HeapSys = tool.FileSize(int64(m.HeapSys))
  78. sysStatus.HeapIdle = tool.FileSize(int64(m.HeapIdle))
  79. sysStatus.HeapInuse = tool.FileSize(int64(m.HeapInuse))
  80. sysStatus.HeapReleased = tool.FileSize(int64(m.HeapReleased))
  81. sysStatus.HeapObjects = m.HeapObjects
  82. sysStatus.StackInuse = tool.FileSize(int64(m.StackInuse))
  83. sysStatus.StackSys = tool.FileSize(int64(m.StackSys))
  84. sysStatus.MSpanInuse = tool.FileSize(int64(m.MSpanInuse))
  85. sysStatus.MSpanSys = tool.FileSize(int64(m.MSpanSys))
  86. sysStatus.MCacheInuse = tool.FileSize(int64(m.MCacheInuse))
  87. sysStatus.MCacheSys = tool.FileSize(int64(m.MCacheSys))
  88. sysStatus.BuckHashSys = tool.FileSize(int64(m.BuckHashSys))
  89. sysStatus.GCSys = tool.FileSize(int64(m.GCSys))
  90. sysStatus.OtherSys = tool.FileSize(int64(m.OtherSys))
  91. sysStatus.NextGC = tool.FileSize(int64(m.NextGC))
  92. sysStatus.LastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(m.LastGC))/1000/1000/1000)
  93. sysStatus.PauseTotalNs = fmt.Sprintf("%.1fs", float64(m.PauseTotalNs)/1000/1000/1000)
  94. sysStatus.PauseNs = fmt.Sprintf("%.3fs", float64(m.PauseNs[(m.NumGC+255)%256])/1000/1000/1000)
  95. sysStatus.NumGC = m.NumGC
  96. }
  97. // Operation types.
  98. type AdminOperation int
  99. const (
  100. CLEAN_INACTIVATE_USER AdminOperation = iota + 1
  101. CLEAN_REPO_ARCHIVES
  102. CLEAN_MISSING_REPOS
  103. GIT_GC_REPOS
  104. SYNC_SSH_AUTHORIZED_KEY
  105. SYNC_REPOSITORY_HOOKS
  106. REINIT_MISSING_REPOSITORY
  107. )
  108. func Dashboard(c *context.Context) {
  109. c.Data["Title"] = "Dashboard"
  110. c.Data["PageIsAdmin"] = true
  111. c.Data["PageIsAdminDashboard"] = true
  112. // Run operation.
  113. op, _ := com.StrTo(c.Query("op")).Int()
  114. if op > 0 {
  115. var err error
  116. var success string
  117. switch AdminOperation(op) {
  118. case CLEAN_REPO_ARCHIVES:
  119. success = "All repositories archives have been deleted successfully."
  120. err = models.DeleteRepositoryArchives()
  121. case CLEAN_MISSING_REPOS:
  122. success = "All repository records that lost Git files have been deleted successfully."
  123. err = models.DeleteMissingRepositories()
  124. case GIT_GC_REPOS:
  125. success = "All repositories have done garbage collection successfully."
  126. err = models.GitGcRepos()
  127. case SYNC_SSH_AUTHORIZED_KEY:
  128. success = "All public keys have been rewritten successfully."
  129. err = models.RewriteAuthorizedKeys()
  130. case SYNC_REPOSITORY_HOOKS:
  131. success = "All repositories' pre-receive, update and post-receive hooks have been resynced successfully."
  132. err = models.SyncRepositoryHooks()
  133. case REINIT_MISSING_REPOSITORY:
  134. success = "All repository records that lost Git files have been reinitialized successfully."
  135. err = models.ReinitMissingRepositories()
  136. }
  137. if err != nil {
  138. c.Flash.Error(err.Error())
  139. } else {
  140. c.Flash.Success(success)
  141. }
  142. c.Redirect(setting.AppSubURL + "/admin")
  143. return
  144. }
  145. c.Data["Stats"] = models.GetStatistic()
  146. // FIXME: update periodically
  147. updateSystemStatus()
  148. c.Data["SysStatus"] = sysStatus
  149. c.Data["GitVersion"] = setting.Git.Version
  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. }
  223. func Analytics(c *context.Context) {
  224. c.Data["Title"] = "Analytics"
  225. c.Data["PageIsAdminAnalytics"] = true
  226. c.HTML(200, ANALYTICS)
  227. }
  228. func AdminAPI(c *context.Context) {
  229. c.Data["Title"] = "API"
  230. c.Data["PageIsAdminAPI"] = true
  231. c.HTML(200, API)
  232. }