Пример #1
0
    def listevents(self, outputfile):
        """
        Get common DB entries for table ipmievents, stored by captureevents.

        :param: outputfile - JSON output file, if None - special format to stdout.
        :returns: status, reply
        :raises: None
        """

        table_name = 'ipmievents'
        if outputfile is not None:
            reply = "{'" + table_name + "':["
        else:
            reply = ''

        sqlcommand = "SELECT id, datetime, event, details, status from {0}".format(table_name)
        try:
            ipmidata = DataModel().ExecuteRawQueryStatement(sqlcommand)
        except sqlalchemy.exc.OperationalError as ex:
            self.logger.error(traceback.format_exc())
            if outputfile is not None:
                reply = reply + '{"exception":' + str(ex) + '}'
        except Exception as ex:
            self.logger.error(traceback.format_exc())
            if outputfile is not None:
                reply = reply + '{"exception":' + str(ex) + '}'

        # Print out the ipmievents data in the common database.
        first = True
        ipmidata_keys = ipmidata.keys()
        for entry in ipmidata:
            if outputfile is not None:
                reply = reply + ("\n  {" if first else "},\n  {")
                first = second = False
                for v in ipmidata_keys:
                    reply = ((reply + ",") if second else reply) + "'" + str(v) + "':'" + str(entry[v]) + "'"
                    second = True
                self.logger.info(entry)
            else:
                print(entry[0], '|', entry[1], '|', entry[2], '|', entry[3], '|', entry[4])
        if outputfile is not None:
            if ipmidata is not None:
                reply = reply + '}'

        if outputfile is not None:
            reply = reply + "\n]}"
        self.logger.info("status=0 json={0}".format(reply))
        return 0, reply