示例#1
0
    def __init__(self, idx, start=None, stop=None):
        """Function for intializing the class.

        Args:
            idx: idx of datapoint
            start: Starting timestamp
            stop: Ending timestamp

        Returns:
            None

        """
        # Initialize important variables
        self.data = defaultdict(dict)

        # Get the datapoint's base_type
        datapointer = db_datapoint.GetIDX(idx)
        self.base_type = datapointer.base_type()
        self.agent_label = datapointer.agent_label()

        # Redefine start times
        if start is None:
            self.ts_start = jm_general.normalized_timestamp() - (3600 * 24)
        else:
            self.ts_start = jm_general.normalized_timestamp(start)

        # Redefine stop times
        if stop is None:
            self.ts_stop = jm_general.normalized_timestamp()
        else:
            self.ts_stop = jm_general.normalized_timestamp(stop)

        # Fix edge cases
        if self.ts_start > self.ts_stop:
            self.ts_start = self.ts_stop

        # Make sure datapoint exists
        if db_datapoint.idx_exists(idx) is False:
            log_message = ('idx %s not found.') % (idx)
            log.log2die(1049, log_message)

        # Establish a database session
        database = db.Database()
        session = database.session()
        result = session.query(Data.timestamp, Data.value).filter(and_(
            Data.timestamp >= self.ts_start,
            Data.timestamp <= self.ts_stop,
            Data.idx_datapoint == idx))

        # Massage data
        for instance in result:
            self.data[instance.timestamp] = instance.value

        # Return the session to the database pool after processing
        session.close()
示例#2
0
文件: agent.py 项目: Pr0x1m4/infoset
    def __init__(self, config, hostname):
        """Method initializing the class.

        Args:
            config: ConfigAgent configuration object
            agent_name: Name of agent
            hostname: Hostname that the agent applies to

        Returns:
            None

        """
        # Initialize key variables
        self.data = defaultdict(lambda: defaultdict(dict))
        agent_name = config.agent_name()
        uid = get_uid(agent_name)
        self.lang = language.Agent(agent_name)

        # Add timestamp
        self.data["timestamp"] = jm_general.normalized_timestamp()
        self.data["uid"] = uid
        self.data["agent"] = agent_name
        self.data["hostname"] = hostname

        # Construct URL for server
        if config.server_https() is True:
            prefix = "https://"
        else:
            prefix = "http://"
        self.url = ("%s%s:%s/receive/%s") % (prefix, config.server_name(), config.server_port(), uid)

        # Create the cache directory
        self.cache_dir = config.agent_cache_directory()
        if os.path.exists(self.cache_dir) is False:
            os.mkdir(self.cache_dir)
示例#3
0
    def __init__(self, idx, config, start=None, stop=None):
        """Function for intializing the class.

        Args:
            idx: idx of datapoint
            config: Config object
            start: Starting timestamp
            stop: Ending timestamp

        Returns:
            None

        """
        # Initialize important variables
        self.data = defaultdict(dict)

        # Get the datapoint's base_type
        datapointer = db_datapoint.GetIDX(idx, config)
        self.base_type = datapointer.base_type()

        # Redefine start / stop times
        if start is None:
            self.ts_start = jm_general.normalized_timestamp() - (3600 * 24)
        else:
            # Adjust for counters
            if self.base_type == 1:
                self.ts_start = start
            else:
                self.ts_start = start - 300
        if stop is None:
            self.ts_stop = jm_general.normalized_timestamp()
        else:
            self.ts_stop = stop
        if self.ts_start > self.ts_stop:
            self.ts_start = self.ts_stop

        # Prepare SQL query to read a record from the database.
        # Only active oids
        sql_query = (
            'SELECT value, timestamp '
            'FROM iset_data '
            'WHERE '
            '(timestamp >= %s AND timestamp <= %s) AND '
            'idx_datapoint=\'%s\'') % (
                self.ts_start, self.ts_stop, idx)

        # Do query and get results
        database = db.Database(config)
        query_results = database.query(sql_query, 1301)

        # Massage data
        for row in query_results:
            # uid found?
            if not row[0]:
                log_message = ('idx %s not found.') % (idx)
                log.log2die(1302, log_message)

            # Assign values
            timestamp = row[1]
            value = row[0]
            self.data[timestamp] = value