Exemplo n.º 1
0
    def submit_data(self):
        try:
#            logger.info("begin submmit data,%s", self.send_log[0])
            self.submit()
        except Exception, e:
            logger.error("Exception accured during parser %s submit! (%s)" %
                         (self.__class__.__name__, e))
Exemplo n.º 2
0
def gen_picture(data1, data2, xlabel, xlen=28, pic_name="test"):
    try:
        s = [i for i in range(xlen)]
        fig, ax1 = plt.subplots()
        fig.set_dpi(40)
        fig.set_size_inches(8, 4)
        ax1.plot(s, data1, "r^--", linewidth=1.5, label="Health degree")
        ax1.set_xlabel("Date", fontsize=15)
        ax1.set_ylim(98, 100)
        ax1.set_ylabel("Health degree", color="r", fontsize=10, labelpad=0)
        for t1 in ax1.get_yticklabels():
            t1.set_color("r")

        ax2 = ax1.twinx()
        ax2.plot(s, data2, "bo-", linewidth=1.5, label="Total")
        ax2.set_ylabel("Total count", color="b", fontsize=10, labelpad=0)
        for t2 in ax2.get_yticklabels():
            t2.set_color("b")

        plt.xticks([3 + 6 * i for i in range(5)], xlabel)
        plt.grid(True)
        plt.title(pic_name)
        plt.savefig("/tmp/%s.png" % pic_name, bbox_inches="tight")
        print "done with save"
    except Exception, e:
        logger.error("some error in generrate picture")
        logger.error(e)
Exemplo n.º 3
0
def cal_avr_health_degree(datas):
    # the health_degree is the last item of each data accroding the poll_data
    # func
    try:
        if datas[0]:
            total_count = sum(data[1] for data in datas)
            avr_health_degree = sum(float(data[-1] * data[1]) / total_count for data in datas if data[-1])
            return round(avr_health_degree, 3)
        else:
            return 0.000
    except Exception, e:
        logger.error("wrong when cal avg health degree %s" % e)
        print e
Exemplo n.º 4
0
    def check(self):
        if not self._file_path or not self._log_name:
            logger.error("File path %s of %s was not initialed correctlly!"
                         % (self._file_path, self._log_name))
            return -1
        logger.debug("start parse %s in %s" %
                     (self._file_path, self._log_name))
        if not os.path.exists(self._file_path):
            logger.error("no exists file %s" % self._file_path)
            return -2

        try:
            file_stat = os.stat(self._file_path)
            if not stat.S_ISREG(file_stat.st_mode):
                logger.error("%s is not regular file" % self._file_path)
                return -2

            logger.debug("last inode:%d, last pos:%d" %
                         (self._inode, self._last_pos))
            if self._inode <= 0:
                self._inode = file_stat.st_ino
                self._last_pos = 0
                if self._fp:
                    self._fp.close()
                    self._fp = None
            elif self._inode != file_stat.st_ino:
                logger.info("File(%s)'s inode has been changed from %d to %d!"
                            % (self._file_path, self._inode, file_stat.st_ino))
                self._inode = file_stat.st_ino
                # here we can consider whether system archiving happened or someone remove the log, then do something appropriately
                # and now we just consider it system archiving
                self._last_pos = 0
                if self._fp:
                    self._fp.close()
                    self._fp = None
            if self._last_pos > file_stat.st_size:
                logger.info("File(%s)'s size has been changed from %d to %d!"
                            % (self._file_path, self._last_pos, file_stat.st_size))
                # here, may be system archiving happened or someone cut the
                # log, so the same as upstair.
                self._last_pos = 0
            elif self._last_pos < file_stat.st_size:
                # normal condition we come to here
                logger.debug("File(%s) size increase from %d to %d"
                             % (self._file_path, self._last_pos, file_stat.st_size))
                self.__read_content()

            return 0

        except Exception, e:
            logger.error("Exception accured during Check File %s! (%s)" %
                         (self._file_path, e))
            return -3
Exemplo n.º 5
0
def init():
    init_time()
    init_log(setting.LOG_PATH,
             maxB=setting.MAX_BYTES,
             bc=setting.BACK_COUNT,
             level=setting.LOG_LEVEL)
    signal.signal(signal.SIGCHLD, signal.SIG_IGN)
    signal.signal(signal.SIGTERM, sig_term_handler)
    today = get_today()
    for event_id in EventID.EVENT_IDS:
        try:
            print "event_id:%d" % event_id
            parser_obj = ParserFactory.new(event_id)
            if parser_obj:
                parser_list[event_id] = parser_obj
                parser_obj.update_log_date(today)
            else:
                logger.error("wrong event id:%s" % event_id)
        except Exception, e:
            logger.error("wrong in new ParserFactory:%s" % e)
Exemplo n.º 6
0
            if ret == 0:
                if update_record_flag:
                    logger.info("time to update XML record, %s, inode:%s, offset:%s"
                                % (file_path, inode, offset))
                    # show current state of parse log
                    parse_log.show_state()
                    # update the record in xml by update interval
                    record_xml.set_log_record(file_path, inode, offset)
            else:
                logger.info("%s check error!" % log_type)

        time.sleep(0.3)

if __name__ == "__main__":
    try:
        init()
        run()
    except Exception, e:
        for (event_id, parser_obj) in parser_list.items():
            print "desctruct event %s" % event_id
            parser_obj.destruct()

        for parse_log in parse_log_list:
            (file_path, log_date, log_type, inode,
             offset) = parse_log.get_file_record()
            logger.info("lastly update XML record, %s, inode:%s, offset:%s"
                        % (file_path, inode, offset))
            # update the record in xml by update interval
            record_xml.set_log_record(file_path, inode, offset)
            logger.error("Exception happen,%s" % e)