Exemplo n.º 1
0
    def __init__(self, agent_name):
        """Method initializing the class.

        Args:
            agent_name

        Returns:
            None

        """
        # Initialize key variables
        self.agent_name = agent_name
        self.agent_yaml = {}

        # Get the language used
        config = jm_configuration.Config()
        lang = config.language()

        # Determine the agent's language yaml file
        root_directory = jm_general.root_directory()
        yaml_file = (
            '%s/infoset/metadata/%s/agents/%s.yaml') % (
                root_directory, lang, self.agent_name)

        # Read the agent's language yaml file
        if os.path.exists(yaml_file) is True:
            with open(yaml_file, 'r') as file_handle:
                yaml_from_file = file_handle.read()
            self.agent_yaml = yaml.load(yaml_from_file)
        else:
            log_message = ('Agent language file %s does not exist.') % (
                yaml_file)
            log.log2warn(1034, log_message)
Exemplo n.º 2
0
def _check_when_disabled(agent_name):
    """Stop agent.

    Args:
        agent_filepath: Filepath of agent to be restarted.
        agent_name: Agent name

    Returns:
        None

    """
    # Initialize key variables
    agentconfig = jm_configuration.ConfigAgent(agent_name)
    agent_filename = agentconfig.agent_filename()

    # Get agent status variables
    root_dir = jm_general.root_directory()
    agent_filepath = ('%s/%s') % (root_dir, agent_filename)
    pid = hidden.File()
    pidfile = pid.pid(agent_name)

    # Shutdown agent if running
    if os.path.isfile(pidfile) is True:
        with open(pidfile, 'r') as f_handle:
            pidvalue = int(f_handle.readline().strip())
        if psutil.pid_exists(pidvalue) is True:
            log_message = (
                'Agent "%s" is alive, but should be disabled. '
                'Attempting to stop.'
                '') % (agent_name)
            log.log2quiet(1032, log_message)
            _stop(agent_filepath, agent_name)
Exemplo n.º 3
0
    def __init__(self):
        """Method for intializing the class.

        Args:
            None

        Returns:
            None

        """
        # Initialize key variables
        self.root = ('%s/.infoset') % (jm_general.root_directory())
Exemplo n.º 4
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)
Exemplo n.º 5
0
    def __init__(self):
        """Function for intializing the class.

        Args:
            None

        Returns:
            None

        """
        # Update the configuration directory
        # 'INFOSET_CONFIGDIR' is used for unittesting
        if 'INFOSET_CONFIGDIR' in os.environ:
            config_directory = os.environ['INFOSET_CONFIGDIR']
        else:
            config_directory = ('%s/etc') % (jm_general.root_directory())
        directories = [config_directory]

        # Return
        self.config_dict = jm_general.read_yaml_files(directories)
Exemplo n.º 6
0
    def __init__(self, agent_name):
        """Function for intializing the class.

        Args:
            agent_name: Name of agent used to get descriptions
                from configuration subdirectory

        Returns:
            None

        """
        # Update the configuration directory
        # 'INFOSET_CONFIGDIR' is used for unittesting
        if 'INFOSET_CONFIGDIR' in os.environ:
            config_directory = os.environ['INFOSET_CONFIGDIR']
        else:
            config_directory = ('%s/etc') % (jm_general.root_directory())
        directories = [config_directory]

        # Return
        self.config_dict = jm_general.read_yaml_files(directories)
        self.name = agent_name
Exemplo n.º 7
0
def _check_when_enabled(agent_name):
    """Stop agent.

    Args:
        agent_filepath: Filepath of agent to be restarted.
        agent_name: Agent name

    Returns:
        None

    """
    # Initialize key variables
    agentconfig = jm_configuration.ConfigAgent(agent_name)
    agent_filename = agentconfig.agent_filename()

    # Get agent status variables
    root_dir = jm_general.root_directory()
    agent_filepath = ('%s/%s') % (root_dir, agent_filename)
    pid = hidden.File()
    pidfile = pid.pid(agent_name)
    lockfile = pid.lock(agent_name)

    # Ignore agents that cannot be found
    if os.path.isfile(agent_filepath) is False:
        log_message = (
            'Agent executable file %s listed in the '
            'configuration file '
            'of agent "%s" does not exist. Please fix.'
            '') % (agent_filepath, agent_name)
        log.log2quiet(1075, log_message)
        return

    # Check for pid file
    if os.path.isfile(pidfile) is True:
        with open(pidfile, 'r') as f_handle:
            pidvalue = int(f_handle.readline().strip())

        # Check if service died catastrophically. No PID file
        if psutil.pid_exists(pidvalue) is False:
            log_message = (
                'Agent "%s" is dead. Attempting to restart.'
                '') % (agent_name)
            log.log2quiet(1041, log_message)

            # Remove PID file and restart
            os.remove(pidfile)
            _restart(agent_filepath, agent_name)

        else:
            # Check if agent hung without updating the PID
            if agentconfig.monitor_agent_pid() is True:
                try:
                    mtime = os.path.getmtime(pidfile)
                except OSError:
                    mtime = 0
                if mtime < int(time.time()) - (60 * 10):
                    log_message = (
                        'Agent "%s" is hung. Attempting to restart.'
                        '') % (agent_name)
                    log.log2quiet(1076, log_message)
                    _restart(agent_filepath, agent_name)
    else:
        if os.path.isfile(lockfile) is True:
            _restart(agent_filepath, agent_name)
        else:
            _start(agent_filepath, agent_name)