Search code examples
python-2.7munin

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


I am not getting the exact solution for adding node in munin.conf using python script. i tries using ConfigParser but since munin.conf is a sectionless file its not feasible to use that module. Can anyoune suggest me the possible solution?


Solution

  • Adding a host or node in munin with python script can be done via using python's config parser module. Below is the solution for it :-

    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
    

    here, for using config parser we need to do some changes in munin.conf. firstly you need to write a default section into file at top,second write the data which you wish to write, third deletethat section. Inthis way you can add a node to monitor in munin via python.