您的位置 首页 golang

Golang解析redis info

简介

redis info命令包含了 redis 服务的大部分信息,我们可以通过获取info信息对redis服务进行监控。

将文本处理成指定的格式,处理起来就很方便了。

Go 获取redis信息,并解析

 package main

import (
    "encoding/ json "
    "fmt"
    "strconv"
    "strings"

    goredis "github.com/go-redis/redis"
)

// GetInfo 获取redis info all信息
func GetInfo(host, port, password string) (result string, err error) {
    client := goredis.NewClient(&goredis.Options{
        Addr:     host + ":" + port,
        Password: password, // no password set
    })
    defer client. Close ()
    cmdString := client.Info("all")
    result = cmdString.Val()
    return
}

// ParseInfo 解析redis info信息
func ParseInfo(infoString string) (result map[string]map[string]interface{}) {
    result = map[string]map[string]interface{}{}
    lines := strings.Split(infoString, "\n")
    section := "default"
    for i := range lines {
        // 跳过空行
        if strings.TrimSpace(lines[i]) == "" {
            continue
        }
        // 信息节标记
        if strings.HasPrefix(lines[i], "#") {
            sections := strings.Fields(lines[i])
            if len(sections) > 1 {
                section = sections[1]
                // result[section] = map[string]interface{}{}
            }
            continue
        }
        // 分离key value
        items := strings.Split(lines[i], ":")
        // 如果小于2,就说明没有值
        if len(items) < 1 {
            continue
        }
        if strings.Contains(items[1], ",") {
            valueItems := strings.Split(items[1], ",")
            subMap := map[string]interface{}{}
            for i := range valueItems {
                subValueitems := strings.Split(valueItems[i], "=")
                if len(subValueitems) < 1 {
                    continue
                }
                floatValue, err := strconv.ParseFloat(strings.TrimSpace(subValueitems[1]), 10)
                if err == nil {
                    subMap[subValueitems[0]] = floatValue
                } else {
                    subMap[subValueitems[0]] = strings.TrimSpace(subValueitems[1])
                }
            }
            if _, ok := result[section]; ok {
                result[section][items[0]] = subMap
            } else {
                result[section] = map[string]interface{}{
                    items[0]: subMap,
                }
            }

        } else {
            floatValue, err := strconv.ParseFloat(strings.TrimSpace(items[1]), 10)
            var vlaue interface{} = strings.TrimSpace(items[1])
            if err == nil {
                vlaue = floatValue
            }
            if _, ok := result[section]; ok {
                result[section][items[0]] = vlaue
            } else {
                result[section] = map[string]interface{}{
                    items[0]: vlaue,
                }
            }
        }
    }
    return
}

func main() {
    info, err := GetInfo("127.0.0.1", "6379", "")
    if err != nil {
        panic(err)
    }
    // fmt.Println(info)
    result := ParseInfo(info)
    resultJSON, _ := json.Marshal(&result)
    fmt.Println(string(resultJSON))
}  

将结果转换成 map[string]interface{} 格式,然后转换成 json

文章来源:智云一二三科技

文章标题:Golang解析redis info

文章地址:https://www.zhihuclub.com/86809.shtml

关于作者: 智云科技

热门文章

网站地图