2017-07-27 54 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

你能否給我提供這個功能的輸出...... –

相關問題