index.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "net/http"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "cnphper.com/model"
  12. "github.com/gomodule/redigo/redis"
  13. )
  14. type RedisInfo struct {
  15. Id int64
  16. Address string
  17. Remark string
  18. Error string
  19. Data map[string]string
  20. }
  21. type StatRet struct {
  22. Errno int `json:"errno"`
  23. Error string `json:"error"`
  24. Data []*RedisInfo `json:"data"`
  25. }
  26. type InfoRet struct {
  27. Errno int `json:"errno"`
  28. Error string `json:"error"`
  29. Data *RedisInfo `json:"data"`
  30. }
  31. func parseRedisInfo(info string) map[string]string {
  32. stats := make(map[string]string, 1024)
  33. lines := strings.Split(info, "\r\n")
  34. num := len(lines)
  35. for i := 0; i < num; i++ {
  36. line := strings.TrimSpace(lines[i])
  37. if line == "" || line[0] == '#' {
  38. continue
  39. }
  40. parts := strings.SplitN(line, ":", 2)
  41. if len(parts) != 2 {
  42. continue
  43. }
  44. stats[parts[0]] = parts[1]
  45. }
  46. return stats
  47. }
  48. func queryRedisInfo(item *model.RedisCfgRow) *RedisInfo {
  49. info := RedisInfo{
  50. Id: item.Id,
  51. Address: item.Address,
  52. Remark: item.Remark,
  53. }
  54. timeout := time.Second * time.Duration(item.MaxConnectWait)
  55. options := []redis.DialOption{
  56. redis.DialConnectTimeout(timeout),
  57. redis.DialReadTimeout(timeout),
  58. redis.DialWriteTimeout(timeout),
  59. }
  60. if item.Password != "" {
  61. options = append(options, redis.DialPassword(item.Password))
  62. }
  63. conn, err := redis.Dial("tcp", item.Address, options...)
  64. defer conn.Close()
  65. if err == nil {
  66. str, err := redis.String(conn.Do("info"))
  67. if err == nil {
  68. info.Data = parseRedisInfo(str)
  69. } else {
  70. info.Error = err.Error()
  71. }
  72. } else {
  73. info.Error = err.Error()
  74. }
  75. return &info
  76. }
  77. func index_default(resp http.ResponseWriter, req *http.Request) {
  78. sess, ok := checkLogin(resp, req)
  79. if !ok {
  80. return
  81. }
  82. //视图输出
  83. files := []string{
  84. filepath.Join(Cfg.TmplDir, "index.tmpl"),
  85. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  86. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  87. }
  88. tmpl, err := template.New("index.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  89. if err != nil {
  90. resp.WriteHeader(500)
  91. io.WriteString(resp, err.Error())
  92. return
  93. }
  94. tmpl.Execute(resp, struct {
  95. Sess *Session
  96. Req *http.Request
  97. Title string
  98. }{
  99. sess,
  100. req,
  101. "首页",
  102. })
  103. }
  104. func index_stats(resp http.ResponseWriter, req *http.Request) {
  105. _, ok := checkLogin(resp, req)
  106. if !ok {
  107. return
  108. }
  109. mdlRedisCfg := model.NewRedisCfg(Db)
  110. rows, err := mdlRedisCfg.GetAll(0)
  111. if err != nil {
  112. json, _ := json.Marshal(ErrorRet{Errno: 0, Error: err.Error()})
  113. resp.Write(json)
  114. return
  115. }
  116. stats := make([]*RedisInfo, 0)
  117. for _, row := range rows {
  118. stats = append(stats, queryRedisInfo(row))
  119. }
  120. json, _ := json.Marshal(StatRet{Errno: 0, Error: "", Data: stats})
  121. resp.Write(json)
  122. }
  123. func index_info(resp http.ResponseWriter, req *http.Request) {
  124. _, ok := checkLogin(resp, req)
  125. if !ok {
  126. return
  127. }
  128. req.ParseForm()
  129. idstr := req.Form.Get("id")
  130. if idstr == "" {
  131. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "缺少ID字段!"})
  132. resp.Write(json)
  133. return
  134. }
  135. id := int(0)
  136. _, err := fmt.Sscanf(idstr, "%d", &id)
  137. if err != nil {
  138. json, _ := json.Marshal(ErrorRet{Errno: 2, Error: "ID的值不是整数!"})
  139. resp.Write(json)
  140. return
  141. }
  142. if id <= 0 {
  143. json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "ID的值必须大于0!"})
  144. resp.Write(json)
  145. return
  146. }
  147. mdlRedisCfg := model.NewRedisCfg(Db)
  148. row, err := mdlRedisCfg.Get(int64(id))
  149. if err != nil {
  150. json, _ := json.Marshal(ErrorRet{Errno: 4, Error: err.Error()})
  151. resp.Write(json)
  152. } else {
  153. info := queryRedisInfo(row)
  154. if info.Error == "" {
  155. json, _ := json.Marshal(InfoRet{Errno: 0, Error: "", Data: info})
  156. resp.Write(json)
  157. } else {
  158. json, _ := json.Marshal(ErrorRet{Errno: 5, Error: info.Error})
  159. resp.Write(json)
  160. }
  161. }
  162. }