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

How to add node to monitor in munin using Python script?

我没有得到使用 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 中进行监控