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 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂