drgawfsf1069 2019-07-26 16:04
浏览 40
已采纳

解析文件中的数据时大小不一的地图的地图

I am building an API for Nagios that is inspired by this project. I've started recreating the code that reads the status.dat file and stores the data in a number of objects which are then used to create hosts, services, info dictionaries which is found in the core.py file.

Below is my Go version of the python code which seems to work as expected. It is still in its early stages so I apologise for any coding bad practice.

var mu = &sync.RWMutex{}

func openStatusFile() *os.File {
    file, err := os.Open("/usr/local/nagios/var/status.dat")
    if err != nil {
    }
    return file
}

func nextStanza() <-chan map[string]string {

    myChannel := make(chan map[string]string)

    scanner := bufio.NewScanner(openStatusFile())

    current := make(map[string]string)

    go func() {
        for scanner.Scan() {
            mainline := scanner.Text()
            line := strings.TrimSpace(mainline)
            if strings.HasSuffix(line, "{") {
                if len(current) != 0 {
                    myChannel <- current
                }
                result := strings.SplitN(line, " ", 2)
                mu.Lock()
                current["type"] = result[0]
                mu.Unlock()
            } else if strings.Contains(line, "=") {
                result := strings.SplitN(line, "=", 2)
                key := result[0]
                val := result[1]
                mu.Lock()
                current[key] = val
                mu.Unlock()
            }
        }
        close(myChannel)
    }()
    return myChannel
}

In the main function, I create my nested map to hold just the host data for now and this completes without any complaints. The problem I am getting, is that when I check the length of this map, I am expecting to see 104 hosts, but I get different results every time I run this test file.

func main() {

    hoststatus := nextStanza()

    hosts := make(map[string]map[string]string)
    // services := make(map[string]map[string]map[string]string)
    var host string
    // var service string

    for obj := range hoststatus {
        var hostPlaceHolder string
        var typePlaceHolder string

        mu.Lock()
        hostPlaceHolder = obj["host_name"]
        mu.Unlock()

        if hostPlaceHolder != "" {
            host = hostPlaceHolder
        }

        mu.Lock()
        typePlaceHolder = obj["type"]
        mu.Unlock()

        if typePlaceHolder == "hoststatus" {
            mu.Lock()
            hosts[host] = obj
            mu.Unlock()
        }
    }
    fmt.Println(len(hosts))
}

First run:

$ go run -race mytest.go
93

Second run:

$ go run -race mytest.go
95

Third run:

$ go run -race mytest.go
63

You get the idea.

I feel the issue is to do with the map, because if I just print the hosts without putting them into a map, I see all the hosts I am expecting. What would be the reason for the map be a different size on each run?

  • 写回答

2条回答 默认 最新

  • dqg63264 2019-07-31 13:04
    关注

    I was able to resolve my issue by emptying the current map after it is sent to the channel.

    myChannel <- current
    current = make(map[string]string)
    

    Then in the main() function after the for obj := range hoststatus loop, I placed this data into a separate map to then work from.

    hostStatusMap := make(map[string]string)
        for k, v := range obj {
            hostStatusMap[k] = v
        }
    

    I was also able to remove the locks scattered throughout the code and it now returns the correct length of hosts on each run.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛