Exemplo n.º 1
0
    def save_event(self, data):
        if self.event_dir is None:
            logger.warn(
                "Cannot save event -- Event reporter is not initialized.")
            return

        fileutil.mkdir(self.event_dir, mode=0o700)

        existing_events = os.listdir(self.event_dir)
        if len(existing_events) >= 1000:
            existing_events.sort()
            oldest_files = existing_events[:-999]
            logger.warn("Too many files under: {0}, removing oldest".format(
                self.event_dir))
            try:
                for f in oldest_files:
                    os.remove(os.path.join(self.event_dir, f))
            except IOError as e:
                raise EventError(e)

        filename = os.path.join(self.event_dir,
                                ustr(int(time.time() * 1000000)))
        try:
            with open(filename + ".tmp", 'wb+') as hfile:
                hfile.write(data.encode("utf-8"))
            os.rename(filename + ".tmp", filename + ".tld")
        except IOError as e:
            msg = "Failed to write events to file:{0}".format(e)
            raise EventError(msg)
Exemplo n.º 2
0
    def save_event(self, data):
        if self.event_dir is None:
            logger.warn("Cannot save event -- Event reporter is not initialized.")
            return

        try:
            fileutil.mkdir(self.event_dir, mode=0o700)
        except (IOError, OSError) as e:
            msg = "Failed to create events folder {0}. Error: {1}".format(self.event_dir, ustr(e))
            raise EventError(msg)

        try:
            existing_events = os.listdir(self.event_dir)
            if len(existing_events) >= MAX_NUMBER_OF_EVENTS:
                logger.periodic_warn(logger.EVERY_MINUTE, "[PERIODIC] Too many files under: {0}, current count:  {1}, "
                                                          "removing oldest event files".format(self.event_dir,
                                                                                               len(existing_events)))
                existing_events.sort()
                oldest_files = existing_events[:-999]
                for event_file in oldest_files:
                    os.remove(os.path.join(self.event_dir, event_file))
        except (IOError, OSError) as e:
            msg = "Failed to remove old events from events folder {0}. Error: {1}".format(self.event_dir, ustr(e))
            raise EventError(msg)

        filename = os.path.join(self.event_dir,
                                ustr(int(time.time() * 1000000)))
        try:
            with open(filename + ".tmp", 'wb+') as hfile:
                hfile.write(data.encode("utf-8"))
            os.rename(filename + ".tmp", filename + AGENT_EVENT_FILE_EXTENSION)
        except (IOError, OSError) as e:
            msg = "Failed to write events to file: {0}".format(e)
            raise EventError(msg)
Exemplo n.º 3
0
def parse_event(data_str):
    try:
        try:
            return parse_json_event(data_str)
        except ValueError:
            return parse_xml_event(data_str)
    except Exception as e:
        raise EventError("Error parsing event: {0}".format(ustr(e)))
Exemplo n.º 4
0
    def save_event(self, data):
        if self.event_dir is None:
            logger.warn("Event reporter is not initialized.")
            return

        if not os.path.exists(self.event_dir):
            os.mkdir(self.event_dir)
            os.chmod(self.event_dir, 0o700)
        if len(os.listdir(self.event_dir)) > 1000:
            raise EventError("Too many files under: {0}".format(
                self.event_dir))

        filename = os.path.join(self.event_dir,
                                ustr(int(time.time() * 1000000)))
        try:
            with open(filename + ".tmp", 'wb+') as hfile:
                hfile.write(data.encode("utf-8"))
            os.rename(filename + ".tmp", filename + ".tld")
        except IOError as e:
            raise EventError("Failed to write events to file:{0}", e)
Exemplo n.º 5
0
 def collect_event(self, evt_file_name):
     try:
         logger.verbose("Found event file: {0}", evt_file_name)
         with open(evt_file_name, "rb") as evt_file:
             # if fail to open or delete the file, throw exception
             data_str = evt_file.read().decode("utf-8", 'ignore')
         logger.verbose("Processed event file: {0}", evt_file_name)
         os.remove(evt_file_name)
         return data_str
     except IOError as e:
         msg = "Failed to process {0}, {1}".format(evt_file_name, e)
         raise EventError(msg)