Python 2.7 如何使用Python脚本在munin中添加要监视的节点?

Python 2.7 如何使用Python脚本在munin中添加要监视的节点?,python-2.7,munin,Python 2.7,Munin,我没有得到使用python脚本在munin.conf中添加节点的确切解决方案。我尝试使用ConfigParser,但由于munin.conf是一个无节文件,因此无法使用该模块。任何人都可以向我推荐可能的解决方案吗?使用python脚本在munin中添加主机或节点可以通过使用python的配置解析器模块来完成。下面是它的解决方案:- def addhost(self, host): cfile = '/etc/munin/munin.conf' hostname = host['h

我没有得到使用python脚本在munin.conf中添加节点的确切解决方案。我尝试使用ConfigParser,但由于munin.conf是一个无节文件,因此无法使用该模块。任何人都可以向我推荐可能的解决方案吗?

使用python脚本在munin中添加主机或节点可以通过使用python的配置解析器模块来完成。下面是它的解决方案:-

def addhost(self, host):
    cfile = '/etc/munin/munin.conf'
    hostname = host['host_name']
    address = host['address']
    # use_node_name = host['use_node_name']
    with open(cfile, "r+") as f:
        s = f.read()
        f.seek(0)
        f.write("[default]\n" + s)
        config.readfp(f)
        # config.add_section(hostname)
        # config.set(hostname, 'address '+ address )
        # config.set(hostname, 'use_node_name yes')

        #config.write(fout)
    with open(cfile,"w") as fout:
        print config.sections()
        #config.readfp(fout)
        config.add_section(hostname)
        config.set(hostname, 'address ' + address)
        config.set(hostname, 'use_node_name yes')
        config.write(fout)
    for line in fileinput.input(cfile, inplace=1):
        line = line.strip()
        if not '[default]' in line:
            print line
在这里,为了使用配置解析器,我们需要在munin.conf中做一些更改。首先,您需要在顶部的文件中写入一个默认部分,然后写入您希望写入的数据,最后删除该部分。通过这种方式,您可以通过python在munin中添加一个要监视的节点