Пример #1
0
    def make_nagios_changes(self):
        path = os.path.join(self._tmp_conf['nagios_cfgs'], "host-services")
        pattern = re.compile("(?P<kk>^[\w\-]+)$")
        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "hostgroup-services")

        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "hostgroups")

        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))
        self.load_active_hosts()
        services_by_host_dic = {}
        for host_ip, hostname in self._active_hosts.iteritems():
            nh = nagios_host(host_ip, hostname, "", self._tmp_conf)
            nh.write()
            services_by_host = self.get_services_by_hosts(host_ip)
            for service in services_by_host:
                service_type =service['service_type']
                if not pattern.match(service_type):
                    logger.warning('Invalid service type: %s' % service_type)
                    continue
                protocol = service['protocol']
                hostname = service['hostname']
                port = service['port']
                if not services_by_host_dic.has_key(service_type):
                    services_by_host_dic[service_type] = []
                if hostname not in services_by_host_dic[service_type]:
                    services_by_host_dic[service_type].append(hostname)
                k = nagios_host_service(service_type,protocol,hostname,port,"120", self._tmp_conf)
                k.write()
        for key, value in services_by_host_dic.iteritems():
            if key=='unknown':
                continue
            members = ','.join(value)
            hg = nagios_host_group_service(key,key,members,self._tmp_conf)
            hg.write()
        data = self.get_host_groups()
        for hg in data:
            name = hg['name']
            data =self.get_hostlist_from_hg(name)
            host_list = []
            if len(data)>0:
                for host in data:
                    if self._active_hosts.has_key(host['host_ip']):
                        host_list.append(self._active_hosts[host['host_ip']])
            if len(host_list)> 0:
                hosts_list_str = ','.join(host_list)
                nhg = nagios_host_group(name, name, hosts_list_str, self._tmp_conf)
                nhg.write()
        logger.debug("Changes where applied! Reloading Nagios config.")
        
        self.reload_nagios()
Пример #2
0
    def make_nagios_changes(self):
        port = None
        db = OssimDB()
        db.connect (self._tmp_conf["ossim_host"],
                self._tmp_conf["ossim_base"],
                self._tmp_conf["ossim_user"],
                self._tmp_conf["ossim_pass"])
        query="select port from host_services where (protocol=6 or protocol=0) and nagios=1 group by port"
        services=db.exec_query(query)

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "host-services")

        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "hostgroup-services")

        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        i = 0

        for port in services:
            i+=1
            query = "select h.hostname, hs.service_type from host_services hs, host_scan h_sc, host h where (hs.protocol=6 or hs.protocol=0) and hs.port=%s and hs.ip=h_sc.host_ip and h_sc.plugin_id=2007 and hs.nagios=1 and h.ip=inet_ntoa(hs.ip) group by h.ip order by h.ip" % port['port']
            hosts = db.exec_query(query)
            list = ""
            for host in hosts:
                if list != "":
                    list += ","

                list += host['hostname']

            if list != "":
                k = nagios_host_service(list, port['port'], host['service_type'], "check_tcp!%d" % port['port'], "0", self._tmp_conf)
                k.select_command()
                k.write()

                hg = nagios_host_group_service(self.serv_port(port['port']),self.serv_name(port['port']),list,self._tmp_conf)
                hg.write()

        if port is not None and port in services:
            logger.debug("Changes where applied! Reloading Nagios config.")
            self.reload_nagios()

        db.close()
Пример #3
0
    def make_nagios_changes(self):
        """Loads all the host/host_groups/net/net_groups and creates 
        all the nagios configuration.
        """
        logger.info("Making nagios changes..")

        pattern = re.compile("(?P<kk>^[\w\-]+)$")

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "host-services")
        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "hostgroup-services")
        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "hostgroups")
        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "hosts")
        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        # 1 - Load the active hosts.
        self.load_active_hosts()
        services_by_host_dic = {}
        hostnames_dic = {}
        hostnames_dup_dic = {}

        # Looking for duplicate hostnames
        for host_id, host in self.__active_hosts.iteritems():
            hostname = host.getHostname()
            if (hostnames_dic.has_key(hostname)):
                hostnames_dup_dic[hostname] = True
            hostnames_dic[hostname] = True

        for host_id, host in self.__active_hosts.iteritems():
            hostname = host.getHostname()
            #host_ip = tupla[1]

            for ip in host.getIPList():
                realhostname = hostname

                if host.hasMoreThanOneIP() or hostnames_dup_dic.has_key(hostname):
                    realhostname = "%s_%s" % (hostname,ip)

                nh = nagios_host(ip,realhostname, "", self._tmp_conf)
                logger.info("nagios host: %s->%s" % (ip,realhostname))
                nh.write()
                for service in host.getServicesByIP(ip):
                    service_type = service.getServiceServiceType()
                    if not service_type:
                        continue
                    if not pattern.match(service_type):
                        logger.warning('Invalid service type: %s' % service_type)
                        continue
                    protocol = service.getServiceProtocol()
                    port = service.getServicePort()
                    if not services_by_host_dic.has_key(service_type):
                        services_by_host_dic[service_type] = []
                    if realhostname not in services_by_host_dic[service_type]:
                        services_by_host_dic[service_type].append(realhostname)
                    logger.info("nagios hostservice:  %s,%s,%s,%s" % (service_type,protocol,realhostname,port)) 
                    k = nagios_host_service(service_type,protocol,realhostname,port,"120", self._tmp_conf)
                    k.write()
        for key, value in services_by_host_dic.iteritems():
            if key=='unknown':
                continue
            members = ','.join(value)
            hg = nagios_host_group_service(key,key,members,self._tmp_conf)
            logger.info("nagios host group services %s,%s,%s"%(key,key,members))
            hg.write()
        data = self.get_host_groups()
        for hg in data:
            name = hg['name']   
            hostgroup_id = hg['id']
            data =self.get_hostlist_from_hg(hostgroup_id)
            host_list = []
            if len(data)>0:
                for host in data:
                    if self.__active_hosts.has_key(host['host_id']):
                        ac_host = self.__active_hosts[host['host_id']]
                        if ac_host.hasMoreThanOneIP():
                            for ip in ac_host.getIPList():
                                host_list.append(ac_host.getHostname()+"_%s" % ip)
                        else:
                            host_list.append(ac_host.getHostname())
                    else:
                        logger.info("host from hostgroup not in active hostsa :%s" %host['host_id'])
                logger.info("host from hg: %s" % host_list)
            if len(host_list)> 0:
                hosts_list_str = ','.join(host_list)
                logger.info("name:%s -  %s" % (name,hosts_list_str))
                nhg = nagios_host_group(name, name, hosts_list_str, self._tmp_conf)
                logger.info("nagios hostgroup :%s,%s,%s" % (name,name,hosts_list_str))
                nhg.write()
        logger.debug("Changes where applied! Reloading Nagios config.")
        # Change configuration file permissions.
        try:
            for root, dirs, files in os.walk(self._tmp_conf['nagios_cfgs']):
                for config in files:
                    config_file = root + "/" + config
                    os.chmod(config_file, 0644)
        except Exception as e:
            logger.error("An error occurred while changing the configuration files permissions: %s" % str(e))
        self.reload_nagios()
Пример #4
0
    def make_nagios_changes(self):
        """Loads all the host/host_groups/net/net_groups and creates 
        all the nagios configuration.
        """
        logger.info("Making nagios changes..")

        pattern = re.compile("(?P<kk>^\w[\w\-\s]+)$")

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "host-services")
        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "hostgroup-services")
        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "hostgroups")
        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        path = os.path.join(self._tmp_conf['nagios_cfgs'], "hosts")
        for fi in os.listdir(path):
            os.remove(os.path.join(path, fi))

        # 1 - Load the active hosts.
        self.load_active_hosts()
        services_by_host_dic = {}
        hostnames_dic = {}
        hostnames_dup_dic = {}

        # Looking for duplicate hostnames
        for host_id, host in self.__active_hosts.iteritems():
            hostname = host.getHostname()
            if (hostnames_dic.has_key(hostname)):
                hostnames_dup_dic[hostname] = True
            hostnames_dic[hostname] = True

        for host_id, host in self.__active_hosts.iteritems():
            hostname = host.getHostname()
            #host_ip = tupla[1]

            for ip in host.getIPList():
                realhostname = hostname

                if host.hasMoreThanOneIP() or hostnames_dup_dic.has_key(hostname):
                    realhostname = "%s_%s" % (hostname,ip)

                nh = nagios_host(ip,realhostname, "", self._tmp_conf)
                logger.info("nagios host: %s->%s" % (ip,realhostname))
                nh.write()
                for service in host.getServicesByIP(ip):
                    service_type = service.getServiceServiceType()
                    if not service_type:
                        continue
                    if not pattern.match(service_type):
                        logger.warning('Invalid service type: %s' % service_type)
                        continue
                    protocol = service.getServiceProtocol()
                    port = service.getServicePort()
                    if not services_by_host_dic.has_key(service_type):
                        services_by_host_dic[service_type] = []
                    if realhostname not in services_by_host_dic[service_type]:
                        services_by_host_dic[service_type].append(realhostname)
                    logger.info("nagios hostservice:  %s,%s,%s,%s" % (service_type,protocol,realhostname,port)) 
                    k = nagios_host_service(service_type,protocol,realhostname,port,"120", self._tmp_conf)
                    k.write()
        for key, value in services_by_host_dic.iteritems():
            if key=='unknown':
                continue
            members = ','.join(value)
            hg = nagios_host_group_service(key,key,members,self._tmp_conf)
            logger.info("nagios host group services %s,%s,%s"%(key,key,members))
            hg.write()
        data = self.get_host_groups()
        for hg in data:
            name = hg['name']   
            hostgroup_id = hg['id']
            data =self.get_hostlist_from_hg(hostgroup_id)
            host_list = []
            if len(data)>0:
                for host in data:
                    if self.__active_hosts.has_key(host['host_id']):
                        ac_host = self.__active_hosts[host['host_id']]
                        if ac_host.hasMoreThanOneIP():
                            for ip in ac_host.getIPList():
                                host_list.append(ac_host.getHostname()+"_%s" % ip)
                        else:
                            host_list.append(ac_host.getHostname())
                    else:
                        logger.info("host from hostgroup not in active hostsa :%s" %host['host_id'])
                logger.info("host from hg: %s" % host_list)
            if len(host_list)> 0:
                hosts_list_str = ','.join(host_list)
                logger.info("name:%s -  %s" % (name,hosts_list_str))
                nhg = nagios_host_group(name, name, hosts_list_str, self._tmp_conf)
                logger.info("nagios hostgroup :%s,%s,%s" % (name,name,hosts_list_str))
                nhg.write()
        logger.debug("Changes where applied! Reloading Nagios config.")
        # Change configuration file permissions.
        try:
            for root, dirs, files in os.walk(self._tmp_conf['nagios_cfgs']):
                for config in files:
                    config_file = root + "/" + config
                    os.chmod(config_file, 0644)
                for cdir in dirs:
                    config_dir = root + "/" + cdir
                    os.chmod(config_dir, 0755)
            os.chmod(self._tmp_conf['nagios_cfgs'], 0755)

        except Exception as e:
            logger.error("An error occurred while changing the configuration files permissions: %s" % str(e))
        self.reload_nagios()
Пример #5
0
    def make_nagios_changes(self):
        """Loads all the host/host_groups/net/net_groups and creates 
        all the nagios configuration.
        """
        logger.info("Making nagios changes..")

        # 1 - Load the active hosts.
        self.load_active_hosts()

        # Check if the return is the same as last time.
        # This will prevent unnecessary reloads and disk writes of configs
        # This *could* still run after the first change, but not subsequent ones without change
        current_hash = hash(str(sorted(self.__active_hosts)))

        if current_hash == self.__prevhash:
            logger.info("No Nagios Changes")
        else:
            # Store new activity diff.
            self.__save_nagios_activity_diff(current_hash)

            services_by_host_dic = {}
            hostnames_dic = {}
            hostnames_dup_dic = {}
            pattern = re.compile("(?P<kk>^\w[\w\-\s]+)$")

            path = os.path.join(self._tmp_conf['nagios_cfgs'], "host-services")
            for fi in os.listdir(path):
                os.remove(os.path.join(path, fi))

            path = os.path.join(self._tmp_conf['nagios_cfgs'], "hostgroup-services")
            for fi in os.listdir(path):
                os.remove(os.path.join(path, fi))

            path = os.path.join(self._tmp_conf['nagios_cfgs'], "hostgroups")
            for fi in os.listdir(path):
                os.remove(os.path.join(path, fi))

            path = os.path.join(self._tmp_conf['nagios_cfgs'], "hosts")
            for fi in os.listdir(path):
                os.remove(os.path.join(path, fi))

            # Looking for duplicate hostname
            for host_id, host in self.__active_hosts.iteritems():
                hostname = host.getHostname()
                if hostname in hostnames_dic:
                    hostnames_dup_dic[hostname] = True
                hostnames_dic[hostname] = True

            for host_id, host in self.__active_hosts.iteritems():
                hostname = host.getHostname()

                for ip in host.getIPList():
                    realhostname = hostname

                    if host.hasMoreThanOneIP() or hostname in hostnames_dup_dic:
                        realhostname = "%s_%s" % (hostname, ip)

                    nh = nagios_host(ip, realhostname, "", self._tmp_conf)
                    logger.info("nagios host: %s->%s" % (ip, realhostname))
                    nh.write()

                    for service in host.getServicesByIP(ip):
                        service_type = service.getServiceServiceType()
                        if not service_type:
                            continue
                        if not pattern.match(service_type):
                            logger.warning('Invalid service type: %s' % service_type)
                            continue
                        protocol = service.getServiceProtocol()
                        port = service.getServicePort()

                        if service_type not in services_by_host_dic:
                            services_by_host_dic[service_type] = []
                        if realhostname not in services_by_host_dic[service_type]:
                            services_by_host_dic[service_type].append(realhostname)
                        logger.info("nagios hostservice:  %s,%s,%s,%s" % (service_type, protocol, realhostname, port))
                        k = nagios_host_service(service_type, protocol, realhostname, port, "120", self._tmp_conf)
                        k.write()

            for key, value in services_by_host_dic.iteritems():
                if key == 'unknown':
                    continue
                members = ','.join(value)
                hg = nagios_host_group_service(key, key, members, self._tmp_conf)
                logger.info("nagios host group services %s,%s,%s" % (key, key, members))
                hg.write()

            data = self.get_host_groups()
            for hg in data:
                name = hg['name']
                hostgroup_id = hg['id']
                data = self.get_hostlist_from_hg(hostgroup_id)
                host_list = []

                if len(data) > 0:
                    for host in data:
                        host_id = host['host_id']
                        if host_id in self.__active_hosts:
                            ac_host = self.__active_hosts[host_id]
                            if ac_host.hasMoreThanOneIP():
                                for ip in ac_host.getIPList():
                                    host_list.append(ac_host.getHostname()+"_%s" % ip)
                            else:
                                host_list.append(ac_host.getHostname())
                        else:
                            logger.info("host from hostgroup not in active hosts :%s" % host_id)
                    logger.info("host from hg: %s" % host_list)

                if len(host_list) > 0:
                    hosts_list_str = ','.join(host_list)
                    logger.info("name:%s -  %s" % (name, hosts_list_str))
                    nhg = nagios_host_group(name, name, hosts_list_str, self._tmp_conf)
                    logger.info("nagios hostgroup :%s,%s,%s" % (name, name, hosts_list_str))
                    nhg.write()

            logger.debug("Changes were applied! Reloading Nagios config.")
            # Change configuration file permissions.
            try:
                for root, dirs, files in os.walk(self._tmp_conf['nagios_cfgs']):
                    for config in files:
                        config_file = root + "/" + config
                        os.chmod(config_file, 0644)
                    for cdir in dirs:
                        config_dir = root + "/" + cdir
                        os.chmod(config_dir, 0755)
                os.chmod(self._tmp_conf['nagios_cfgs'], 0755)
            except Exception as e:
                logger.error("An error occurred while changing the configuration files permissions: %s" % str(e))

            self.reload_nagios()