2017-07-27 49 views

回答

2

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

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中添加一个节点进行监控。

+0

你能否给我提供这个功能的输出...... –

相关问题