示例#1
0
    def _poll(self):
        """Query all remote hosts for data.

        Args:
            None

        Returns:
            None

        """
        # Initialize key variables
        pollers = []

        # Create a list of polling objects
        hostnames = self.config.agent_hostnames()

        for hostname in hostnames:
            # Only poll hosts that exist in the database
            if db_host.hostname_exists(hostname) is False:
                log_message = (
                    'Agent "%s": Hostname %s in the configuration file '
                    'does not exist in the database. '
                    'Run the snmp_evaluate_hosts.py script.'
                    '') % (self.agent_name, hostname)
                log.log2warn(1095, log_message)
                continue

            # Add poller
            poller = Poller(hostname, self.agent_name)
            pollers.append(poller)

        # Start threaded polling
        if bool(pollers) is True:
            Agent.threads(self.agent_name, pollers)
示例#2
0
文件: cache.py 项目: Pr0x1m4/infoset
    def _insert_host(self):
        """Insert new agent into database.

        Args:
            None

        Returns:
            None

        """
        # Initialize key variables
        uid = self.ingest.uid()
        hostname = self.ingest.hostname()

        # Update Host table
        if dhost.hostname_exists(hostname) is False:
            # Add to Host table
            record = Host(hostname=jm_general.encode(hostname))
            database = db.Database()
            database.add(record, 1080)

        # Get idx of host
        host_info = dhost.GetHost(hostname)
        idx_host = host_info.idx()

        # Get idx of agent
        uid_info = agent.GetUID(uid)
        idx_agent = uid_info.idx()

        # Update HostAgent table
        if hagent.host_agent_exists(idx_host, idx_agent) is False:
            # Add to HostAgent table
            record = HostAgent(idx_host=idx_host, idx_agent=idx_agent)
            database = db.Database()
            database.add(record, 1038)
示例#3
0
def _check_duplicates(information):
    """Check whether reported data reported is already in the database.

    Args:
        None

    Returns:
        valid: True if valid

    """
    # Initialize key variables
    valid = True

    # Check that we are evaluating a dict
    if isinstance(information, dict) is False:
        log_message = ('Ingest data is not a dictionary')
        log.log2warn(1116, log_message)
        valid = False
        return valid

    # Check that we have the correct keys in the dict
    if _check_primary_keys_exist(information) is False:
        valid = False
        return valid

    # Get values
    timestamp = int(information['timestamp'])
    uid = information['uid']
    hostname = information['hostname']

    # Check if there is a duplicate entry for this UID
    if db_agent.uid_exists(uid) is not False:
        idx_agent = db_agent.GetUID(uid).idx()

        # Check if host exists
        if db_host.hostname_exists(hostname) is True:
            idx_host = db_host.GetHost(hostname).idx()

            # Check for host / agent entry existence
            if db_hostagent.host_agent_exists(
                    idx_host, idx_agent) is True:
                # Check if this host / agent has been updated before
                last_timesamp = db_hostagent.GetHostAgent(
                    idx_host, idx_agent).last_timestamp()

                # Validate
                if timestamp <= last_timesamp:
                    log_message = (
                        'Data for UID %s, hostname %s at timestamp %s '
                        'is already found in database.'
                        '') % (uid, hostname, timestamp)
                    log.log2warn(1113, log_message)
                    valid = False

    # Return
    return valid
示例#4
0
    def _check_duplicates(self):
        """Method initializing the class.

        Args:
            None

        Returns:
            valid: True if valid

        """
        # Initialize key variables
        valid = True

        # Get values
        timestamp = int(self.information['timestamp'])
        uid = self.information['uid']
        hostname = self.information['hostname']

        # Check if there is a duplicate entry for this UID
        if db_agent.uid_exists(uid) is not False:
            idx_agent = db_agent.GetUID(uid).idx()

            # Check if host exists
            if db_host.hostname_exists(hostname) is True:
                idx_host = db_host.GetHost(hostname).idx()

                # Check for host / agent entry existence
                if db_hostagent.host_agent_exists(
                        idx_host, idx_agent) is True:
                    # Check if this host / agent has been updated before
                    last_timesamp = db_hostagent.GetIDX(
                        idx_host, idx_agent).last_timestamp()

                    # Validate
                    if timestamp <= last_timesamp:
                        valid = False

        # Return
        return valid
def main():
    """Process agent data.

    Args:
        None

    Returns:
        None

    """
    # Process Cache
    agent_name = 'snmp'

    # Process CLI
    cli_args = cli()

    # Get configuration
    config = jm_configuration.ConfigAgent(agent_name)

    # Get hosts
    if cli_args.hostname.lower() == 'all':
        hostnames = config.agent_hostnames()
    else:
        # Check to make sure hostname exists in the configuration
        all_hosts = config.agent_hostnames()
        cli_hostname = cli_args.hostname
        if cli_hostname not in all_hosts:
            log_message = (
                'Agent "%s": Hostname %s is not present '
                'in the the configuration file.'
                '') % (agent_name, cli_hostname)
            log.log2die(1103, log_message)
        else:
            hostnames = [cli_hostname]

    # Get OIDs
    oids = db_oid.all_oids()

    # Process each hostname
    for hostname in hostnames:
        # Get SNMP information
        snmp_config = jm_configuration.ConfigSNMP()
        validate = snmp_manager.Validate(hostname, snmp_config.snmp_auth())
        snmp_params = validate.credentials()

        # Check SNMP supported
        if bool(snmp_params) is True:
            snmp_object = snmp_manager.Interact(snmp_params)

            # Check support for each OID
            for item in oids:
                # Get oid
                oid = item['oid_values']

                # Actions must be taken if a valid OID is found
                if snmp_object.oid_exists(oid) is True:
                    # Insert into iset_hostoid table if necessary
                    if db_host.hostname_exists(hostname) is False:
                        record = Host(
                            hostname=jm_general.encode(hostname),
                            snmp_enabled=1)
                        database = db.Database()
                        database.add(record, 1089)

                    # Get idx for host and oid
                    idx_host = db_host.GetHost(hostname).idx()
                    idx_oid = item['idx']

                    # Insert an entry in the HostOID table if required
                    if db_hostoid.host_oid_exists(idx_host, idx_oid) is False:
                        # Prepare SQL query to read a record from the database.
                        record = HostOID(idx_host=idx_host, idx_oid=idx_oid)
                        database = db.Database()
                        database.add(record, 1090)