class Generate_VMs_Nagios_Conf:
    hostnames = []
    nodes_group = []
    
    def __init__(self):
        self.db = Db_Connector()
        self.create_nagios_conf_running_vms();
        self.reload_nagios_if_necessary()
    
    def reload_nagios_if_necessary(self):
        # getting date for logging purposes
        now = datetime.datetime.now()
        print now.strftime("%d-%m-%Y %H:%M:%S")
        if os.path.isfile(running_vms.cluster.cluster_config.VMS_NAGIOS_FILE):
            md5a = commands.getoutput("md5sum "+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF+" | awk '{ print $1 }'")
            md5b = commands.getoutput("md5sum %s | awk ' { print $1 }'"%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
            if md5a != md5b:
                print 'Moving File'
                os.system('mv '+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s'%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
                self.reload_nagios()
            else:
                print 'no changes'
        else:
            os.system('mv '+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s'%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
            self.reload_nagios()

    def reload_nagios(self):
        os.system(running_vms.cluster.cluster_config.COMMAND_TO_RELOAD_NAGIOS)


    def create_nagios_conf_running_vms(self):
        vms = self.db.get_info_for_monitoring_running_Vms()
        if len(vms) > 0:
            try:
                fh = open(running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF,'w')

                try:
                    tree = ET.parse('basic_monitoring.xml')
                except TypeError, e:
                    print 'error :',e

                for vm in vms:
                    #Check if the machine has a valide ip address, if not , it's not possible to monitorate
                    if vm['public_dns_name'] != '0.0.0.0':
                        hostname = "%s_%s_%s"%(vm['user'],vm['instance_id'],vm['node_hostname'])
                        self.hostnames.append(hostname)
                        alias = "%s/%s(%s-%s)"%(vm['user'],vm['instance_id'],vm['node_hostname'],vm['node_ip'])
                        if vm['node_ip'] != '':
                            vm_host = [vm['node_ip'],hostname]
                            self.nodes_group.append(vm_host)
                        host_def = self.define_host(hostname, alias, vm['public_dns_name'])
                        fh.write(host_def)
                        service = tree.findall('services/service')
                        for s in service:
                            child = s.getchildren()
                            services = self.define_passive_service(hostname, child)
                            fh.write(services)
        
                hostgroup = self.define_hostgroup()
                fh.write(hostgroup)
        
                fh.close()
            except IOError, (errno, strerror):
                print "I/O error ({0}): {1}".format(errno,strerror)
class Generate_VMs_Nagios_Conf:
    hostnames = []
    nodes_group = []

    def __init__(self):
        self.db = Db_Connector()
        self.create_nagios_conf_running_vms()
        self.reload_nagios_if_necessary()

    def reload_nagios_if_necessary(self):
        # getting date for logging purposes
        now = datetime.datetime.now()
        print now.strftime("%d-%m-%Y %H:%M:%S")
        if os.path.isfile(running_vms.cluster.cluster_config.VMS_NAGIOS_FILE):
            md5a = commands.getoutput(
                "md5sum " +
                running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF +
                " | awk '{ print $1 }'")
            md5b = commands.getoutput(
                "md5sum %s | awk ' { print $1 }'" %
                running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
            if md5a != md5b:
                print 'Moving File'
                os.system('mv ' +
                          running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF +
                          ' %s' %
                          running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
                self.reload_nagios()
            else:
                print 'no changes'
        else:
            os.system('mv ' +
                      running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF +
                      ' %s' %
                      running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
            self.reload_nagios()

    def reload_nagios(self):
        os.system(running_vms.cluster.cluster_config.COMMAND_TO_RELOAD_NAGIOS)

    def create_nagios_conf_running_vms(self):
        vms = self.db.get_info_for_monitoring_running_Vms()
        if len(vms) > 0:
            try:
                fh = open(running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF,
                          'w')

                try:
                    tree = ET.parse('basic_monitoring.xml')
                except TypeError, e:
                    print 'error :', e

                for vm in vms:
                    #Check if the machine has a valide ip address, if not , it's not possible to monitorate
                    if vm['public_dns_name'] != '0.0.0.0':
                        hostname = "%s_%s_%s" % (vm['user'], vm['instance_id'],
                                                 vm['node_hostname'])
                        self.hostnames.append(hostname)
                        alias = "%s/%s(%s-%s)" % (
                            vm['user'], vm['instance_id'], vm['node_hostname'],
                            vm['node_ip'])
                        if vm['node_ip'] != '':
                            vm_host = [vm['node_ip'], hostname]
                            self.nodes_group.append(vm_host)
                        host_def = self.define_host(hostname, alias,
                                                    vm['public_dns_name'])
                        fh.write(host_def)
                        service = tree.findall('services/service')
                        for s in service:
                            child = s.getchildren()
                            services = self.define_passive_service(
                                hostname, child)
                            fh.write(services)

                hostgroup = self.define_hostgroup()
                fh.write(hostgroup)

                fh.close()
            except IOError, (errno, strerror):
                print "I/O error ({0}): {1}".format(errno, strerror)
Example #3
0
class Generate_VMs_Nagios_Conf:
    def __init__(self):
        self.db = Db_Connector()
        now = datetime.datetime.now()
        self.create_nagios_conf_running_vms()
        self.reload_nagios_if_necessary()

    def reload_nagios_if_necessary(self):
        # getting date for logging purposes
        now = datetime.datetime.now()
        print now.strftime("%d-%m-%Y %H:%M:%S")
        if os.path.isfile(running_vms.cluster.cluster_config.VMS_NAGIOS_FILE):
            md5a = commands.getoutput(
                "md5sum " +
                running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF +
                " | awk '{ print $1 }'")
            md5b = commands.getoutput(
                "md5sum %s | awk ' { print $1 }'" %
                running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
            if md5a != md5b:
                print 'Moving File'
                os.system('mv ' +
                          running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF +
                          ' %s' %
                          running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
                self.reload_nagios()
            else:
                print 'no changes'
        else:
            os.system('mv ' +
                      running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF +
                      ' %s' %
                      running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
            self.reload_nagios()

    def reload_nagios(self):
        os.system(running_vms.cluster.cluster_config.COMMAND_TO_RELOAD_NAGIOS)

    def create_nagios_conf_running_vms(self):
        vms = self.db.get_info_for_monitoring_running_Vms()
        vms_str = ''
        try:
            fh = open(running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF, 'w')
            nodes_groups = []
            hostnames = []
            for vm in vms:
                #Check if the machine has a valide ip address, if not , it's not possible to monitorate
                if vm['public_dns_name'] != '0.0.0.0':
                    hostname = "%s_%s_%s" % (vm['user'], vm['instance_id'],
                                             vm['node_hostname'])
                    hostnames.append(hostname)
                    alias = "%s/%s(%s-%s)" % (vm['user'], vm['instance_id'],
                                              vm['node_hostname'],
                                              vm['node_ip'])
                    #Verify if there's a node where the vm runs associate, if yes add dependence
                    if vm['node_ip'] != '':
                        conf = "define host {\n \
                                    use linux-server     \n \
                                    address                %s\n \
                                    host_name               %s\n \
                                    alias                   %s\n \
                                }\n" % (vm['public_dns_name'], hostname, alias)
                    else:
                        conf = "define host {\n \
                                    use linux-server     \n \
                                    address                %s\n \
                                    host_name               %s\n \
                                    alias                   %s\n \
                                }\n" % (vm['public_dns_name'], hostname, alias)

                    service_conf = self.define_service(
                        hostname, 'PING', 'check_ping!100.0,20%!500.0,60%')
                    service_conf_passive = self.define_service_passive(
                        hostname)
                    fh.write(conf)
                    fh.write(service_conf)
                    fh.write(service_conf_passive)
                    vms_str = vms_str + "%s, " % hostname
                    vm_group = [hostname, vm['node_ip']]
                    nodes_groups.append(vm_group)

            service_group = self.define_service_group(vms_str)
            hostgroup = self.define_hostgroup(vms_str, nodes_groups)
            self.remove_old_conf()
            #        fh.write(service_group)
            fh.write(hostgroup)
            fh.close()
        except IOError as (errno, strerror):
            print "I/O error ({0}): {1}".format(errno, strerror)
class Generate_VMs_Nagios_Conf:

    def __init__(self):
        self.db = Db_Connector()
        now = datetime.datetime.now()
        self.create_nagios_conf_running_vms();
        self.reload_nagios_if_necessary()

    def reload_nagios_if_necessary(self):
        # getting date for logging purposes
        now = datetime.datetime.now()
        print now.strftime("%d-%m-%Y %H:%M:%S")
        if os.path.isfile(running_vms.cluster.cluster_config.VMS_NAGIOS_FILE):
            md5a = commands.getoutput("md5sum "+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF+" | awk '{ print $1 }'")
            md5b = commands.getoutput("md5sum %s | awk ' { print $1 }'"%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
            if md5a != md5b:
                print 'Moving File'
                os.system('mv '+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s'%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
                self.reload_nagios()
            else:
                print 'no changes'
        else:
            os.system('mv '+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s'%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE)
            self.reload_nagios()

    def reload_nagios(self):
        os.system(running_vms.cluster.cluster_config.COMMAND_TO_RELOAD_NAGIOS)

    def create_nagios_conf_running_vms(self):
        vms = self.db.get_info_for_monitoring_running_Vms()
        vms_str = ''
        try:
            fh = open(running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF,'w')
            nodes_groups = []
            hostnames = []
            for vm in vms:
                #Check if the machine has a valide ip address, if not , it's not possible to monitorate
                if vm['public_dns_name'] != '0.0.0.0':
                    hostname = "%s_%s_%s"%(vm['user'],vm['instance_id'],vm['node_hostname'])
                    hostnames.append(hostname)
                    alias = "%s/%s(%s-%s)"%(vm['user'],vm['instance_id'],vm['node_hostname'],vm['node_ip'])
                    #Verify if there's a node where the vm runs associate, if yes add dependence
                    if vm['node_ip'] != '':
                        conf = "define host {\n \
                                    use linux-server     \n \
                                    address                %s\n \
                                    host_name               %s\n \
                                    alias                   %s\n \
                                }\n" % (vm['public_dns_name'],hostname,alias)
                    else:
                        conf = "define host {\n \
                                    use linux-server     \n \
                                    address                %s\n \
                                    host_name               %s\n \
                                    alias                   %s\n \
                                }\n" % (vm['public_dns_name'],hostname,alias)

                    service_conf = self.define_service(hostname,'PING','check_ping!100.0,20%!500.0,60%')
                    service_conf_passive = self.define_service_passive(hostname)
                    fh.write(conf)
                    fh.write(service_conf)
                    fh.write(service_conf_passive)
                    vms_str = vms_str + "%s, "%hostname
                    vm_group = [ hostname,vm['node_ip'] ]
                    nodes_groups.append(vm_group)

            service_group = self.define_service_group(vms_str)
            hostgroup = self.define_hostgroup(vms_str,nodes_groups)
            self.remove_old_conf()
    #        fh.write(service_group)
            fh.write(hostgroup)
            fh.close()
        except IOError as (errno, strerror):
            print "I/O error ({0}): {1}".format(errno,strerror)