123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package main
- import (
- "encoding/json"
- "fmt"
- "html/template"
- "io"
- "net/http"
- "path/filepath"
- "strings"
- "time"
- "cnphper.com/model"
- "github.com/gomodule/redigo/redis"
- )
- type RedisInfo struct {
- Id int64
- Address string
- Remark string
- Error string
- Data map[string]string
- }
- type StatRet struct {
- Errno int `json:"errno"`
- Error string `json:"error"`
- Data []*RedisInfo `json:"data"`
- }
- type InfoRet struct {
- Errno int `json:"errno"`
- Error string `json:"error"`
- Data *RedisInfo `json:"data"`
- }
- func parseRedisInfo(info string) map[string]string {
- stats := make(map[string]string, 1024)
- lines := strings.Split(info, "\r\n")
- num := len(lines)
- for i := 0; i < num; i++ {
- line := strings.TrimSpace(lines[i])
- if line == "" || line[0] == '#' {
- continue
- }
- parts := strings.SplitN(line, ":", 2)
- if len(parts) != 2 {
- continue
- }
- stats[parts[0]] = parts[1]
- }
- return stats
- }
- func queryRedisInfo(item *model.RedisCfgRow) *RedisInfo {
- info := RedisInfo{
- Id: item.Id,
- Address: item.Address,
- Remark: item.Remark,
- }
- timeout := time.Second * time.Duration(item.MaxConnectWait)
- options := []redis.DialOption{
- redis.DialConnectTimeout(timeout),
- redis.DialReadTimeout(timeout),
- redis.DialWriteTimeout(timeout),
- }
- if item.Password != "" {
- options = append(options, redis.DialPassword(item.Password))
- }
- conn, err := redis.Dial("tcp", item.Address, options...)
- defer conn.Close()
- if err == nil {
- str, err := redis.String(conn.Do("info"))
- if err == nil {
- info.Data = parseRedisInfo(str)
- } else {
- info.Error = err.Error()
- }
- } else {
- info.Error = err.Error()
- }
- return &info
- }
- func index_default(resp http.ResponseWriter, req *http.Request) {
- sess, ok := checkLogin(resp, req)
- if !ok {
- return
- }
- //视图输出
- files := []string{
- filepath.Join(Cfg.TmplDir, "index.tmpl"),
- filepath.Join(Cfg.TmplDir, "header.tmpl"),
- filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
- }
- tmpl, err := template.New("index.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
- if err != nil {
- resp.WriteHeader(500)
- io.WriteString(resp, err.Error())
- return
- }
- tmpl.Execute(resp, struct {
- Sess *Session
- Req *http.Request
- Title string
- }{
- sess,
- req,
- "首页",
- })
- }
- func index_stats(resp http.ResponseWriter, req *http.Request) {
- _, ok := checkLogin(resp, req)
- if !ok {
- return
- }
- mdlRedisCfg := model.NewRedisCfg(Db)
- rows, err := mdlRedisCfg.GetAll(0)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 0, Error: err.Error()})
- resp.Write(json)
- return
- }
- stats := make([]*RedisInfo, 0)
- for _, row := range rows {
- stats = append(stats, queryRedisInfo(row))
- }
- json, _ := json.Marshal(StatRet{Errno: 0, Error: "", Data: stats})
- resp.Write(json)
- }
- func index_info(resp http.ResponseWriter, req *http.Request) {
- _, ok := checkLogin(resp, req)
- if !ok {
- return
- }
- req.ParseForm()
- idstr := req.Form.Get("id")
- if idstr == "" {
- json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "缺少ID字段!"})
- resp.Write(json)
- return
- }
- id := int(0)
- _, err := fmt.Sscanf(idstr, "%d", &id)
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 2, Error: "ID的值不是整数!"})
- resp.Write(json)
- return
- }
- if id <= 0 {
- json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "ID的值必须大于0!"})
- resp.Write(json)
- return
- }
- mdlRedisCfg := model.NewRedisCfg(Db)
- row, err := mdlRedisCfg.Get(int64(id))
- if err != nil {
- json, _ := json.Marshal(ErrorRet{Errno: 4, Error: err.Error()})
- resp.Write(json)
- } else {
- info := queryRedisInfo(row)
- if info.Error == "" {
- json, _ := json.Marshal(InfoRet{Errno: 0, Error: "", Data: info})
- resp.Write(json)
- } else {
- json, _ := json.Marshal(ErrorRet{Errno: 5, Error: info.Error})
- resp.Write(json)
- }
- }
- }
|