Пример #1
0
def insert_oids(directory=None):
    """Update the database with certain key data.

    Args:
        directory: Directory to add to list

    Returns:
        None

    """
    # Initialize key variables
    root_dir = jm_general.root_directory()
    oids_directories = [('%s/infoset/metadata/oids') % (root_dir)]

    # Create a list of existing agent labels, that are unique by definition
    agent_labels = []
    all_oids = db_oid.all_oids()
    for item in all_oids:
        agent_labels.append(item['agent_label'])

    # Add directory to the search path if required
    if directory is not None:
        if os.path.isdir(directory) is True:
            oids_directories.extend(directory)

    # Read in the oid data
    oids_yaml = jm_general.read_yaml_files(oids_directories)

    # Get a list of all labels
    for item in oids_yaml:
        oid_values = item['oid_values']
        oid_labels = item['oid_labels']
        agent_label = item['agent_label']
        base_type = item['base_type']
        multiplier = item['multiplier']

        if db_oid.oid_values_exists(oid_values) is False:
            if agent_label not in agent_labels:
                # Prepare SQL query to read a record from the database.
                record = OID(
                    oid_values=jm_general.encode(oid_values),
                    oid_labels=jm_general.encode(oid_labels),
                    agent_label=jm_general.encode(agent_label),
                    base_type=base_type,
                    multiplier=multiplier)
                database = db.Database()
                database.add(record, 1091)
Пример #2
0
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)