functions.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "time"
  8. "redisdog/model"
  9. "github.com/gomodule/redigo/redis"
  10. )
  11. type ErrorRet struct {
  12. Errno int `json:"errno"`
  13. Error string `json:"error"`
  14. }
  15. type RedisInfo struct {
  16. Id int64
  17. Address string
  18. Remark string
  19. Error string
  20. Data map[string]string
  21. }
  22. func getSession(req *http.Request) (*Session, bool) {
  23. var sessid string
  24. cookies := req.Cookies()
  25. if len(cookies) > 0 {
  26. for _, item := range cookies {
  27. if item.Name == "sessid" && item.Value != "" {
  28. sessid = item.Value
  29. break
  30. }
  31. }
  32. }
  33. if sessid == "" {
  34. return nil, false
  35. } else {
  36. return SessPoll.Get(sessid)
  37. }
  38. }
  39. func checkLogin(resp http.ResponseWriter, req *http.Request) (*Session, bool) {
  40. sess, ok := getSession(req)
  41. if !ok {
  42. resp.Header().Set("Location", "/login")
  43. resp.WriteHeader(302)
  44. }
  45. return sess, ok
  46. }
  47. func parseRedisInfo(info string) map[string]string {
  48. stats := make(map[string]string, 1024)
  49. lines := strings.Split(info, "\r\n")
  50. num := len(lines)
  51. for i := 0; i < num; i++ {
  52. line := strings.TrimSpace(lines[i])
  53. if line == "" || line[0] == '#' {
  54. continue
  55. }
  56. parts := strings.SplitN(line, ":", 2)
  57. if len(parts) != 2 {
  58. continue
  59. }
  60. stats[parts[0]] = parts[1]
  61. }
  62. return stats
  63. }
  64. func queryRedisInfo(item *model.RedisCfgRow) *RedisInfo {
  65. info := RedisInfo{
  66. Id: item.Id,
  67. Address: item.Address,
  68. Remark: item.Remark,
  69. }
  70. timeout := time.Second * time.Duration(item.MaxConnectWait)
  71. options := []redis.DialOption{
  72. redis.DialConnectTimeout(timeout),
  73. redis.DialReadTimeout(timeout),
  74. redis.DialWriteTimeout(timeout),
  75. redis.DialKeepAlive(time.Duration(0)),
  76. }
  77. if item.Password != "" {
  78. options = append(options, redis.DialPassword(item.Password))
  79. }
  80. conn, err := redis.Dial("tcp", item.Address, options...)
  81. if err != nil {
  82. info.Error = err.Error()
  83. return &info
  84. }
  85. defer conn.Close()
  86. str, err := redis.String(conn.Do("info"))
  87. if err == nil {
  88. info.Data = parseRedisInfo(str)
  89. config, err := redis.StringMap(conn.Do("config", "get", "save"))
  90. if err == nil {
  91. info.Data["save"] = config["save"]
  92. } else {
  93. info.Data["save"] = ""
  94. info.Error = err.Error()
  95. }
  96. } else {
  97. info.Error = err.Error()
  98. }
  99. return &info
  100. }
  101. func resetRedisConfig(item *model.RedisCfgRow, maxMemory int64) (bool, error) {
  102. timeout := time.Second * time.Duration(item.MaxConnectWait)
  103. options := []redis.DialOption{
  104. redis.DialConnectTimeout(timeout),
  105. redis.DialReadTimeout(timeout),
  106. redis.DialWriteTimeout(timeout),
  107. redis.DialKeepAlive(time.Duration(0)),
  108. }
  109. if item.Password != "" {
  110. options = append(options, redis.DialPassword(item.Password))
  111. }
  112. conn, err := redis.Dial("tcp", item.Address, options...)
  113. if err != nil {
  114. return false, err
  115. }
  116. defer conn.Close()
  117. ret, err := redis.String(conn.Do("config", "set", "maxmemory", fmt.Sprintf("%d", maxMemory)))
  118. return ret == "OK", err
  119. }
  120. func ERROR_RET(errno int, errmsg string) []byte {
  121. ret, _ := json.Marshal(ErrorRet{Errno: errno, Error: errmsg})
  122. return ret
  123. }
  124. func number2size(n float64) string {
  125. if n < 1024 {
  126. return fmt.Sprintf("%d", int(n))
  127. } else if n < 1024*1024 {
  128. return fmt.Sprintf("%.2fKB", n/1024)
  129. } else if n < 1024*1024*1024 {
  130. return fmt.Sprintf("%.2fMB", n/1024/1024)
  131. } else if n < 1024*1024*1024*1024 {
  132. return fmt.Sprintf("%.2fGB", n/1024/1024/1024)
  133. } else {
  134. return fmt.Sprintf("%.2fTB", n/1024/1024/1024/1024)
  135. }
  136. }
  137. func SYSLOG(level, msg string) {
  138. if SysLogger == nil {
  139. return
  140. }
  141. SysLogger.Add(&model.SysLogRow{
  142. LogLevel: level,
  143. LogMsg: msg,
  144. })
  145. }