Exemplo n.º 1
0
 def openArchive(self, tz=None):
     if self.args.verbose:
         print("create new archive %s" % self.args.archive)
     if self.args.archive is None:
         raise pmapi.pmUsageErr()
     # TODO create path to archive if necessary
     self.pmi = pmi.pmiLogImport(self.args.archive)
     if self.args.host is not None:
         self.pmi.pmiSetHostname(self.args.host)
     if self.args.zone is not None:
         self.pmi.pmiSetTimezone(self.args.zone)
Exemplo n.º 2
0
    def write_archive(self, timestamp):
        """ Write an archive record """
        if timestamp is None:
            # Complete and close
            self.pmi.pmiEnd()
            self.pmi = None
            return

        if self.pmi is None:
            # Create a new archive
            self.pmi = pmi.pmiLogImport(self.outfile)
            self.recorded = {}  # pylint: disable=attribute-defined-outside-init
            if self.context.type == PM_CONTEXT_ARCHIVE:
                self.pmi.pmiSetHostname(
                    self.context.pmGetArchiveLabel().hostname)
            self.pmi.pmiSetTimezone(self.context.get_current_tz(self.opts))
            for i, metric in enumerate(self.metrics):
                self.recorded[metric] = []
                self.pmi.pmiAddMetric(metric, self.pmconfig.pmids[i],
                                      self.pmconfig.descs[i].contents.type,
                                      self.pmconfig.descs[i].contents.indom,
                                      self.pmconfig.descs[i].contents.sem,
                                      self.pmconfig.descs[i].contents.units)

        # Add current values
        data = 0
        results = self.pmconfig.get_sorted_results()
        for i, metric in enumerate(results):
            for inst, name, value in results[metric]:
                if inst != PM_IN_NULL and inst not in self.recorded[metric]:
                    try:
                        self.recorded[metric].append(inst)
                        self.pmi.pmiAddInstance(
                            self.pmconfig.descs[i].contents.indom, name, inst)
                    except pmi.pmiErr as error:
                        if error.args[0] == PMI_ERR_DUPINSTNAME:
                            pass
                if self.pmconfig.descs[i].contents.type == PM_TYPE_STRING:
                    self.pmi.pmiPutValue(metric, name, value)
                elif self.pmconfig.descs[i].contents.type == PM_TYPE_FLOAT or \
                     self.pmconfig.descs[i].contents.type == PM_TYPE_DOUBLE:
                    self.pmi.pmiPutValue(metric, name, "%f" % value)
                else:
                    self.pmi.pmiPutValue(metric, name, "%d" % value)
                data = 1

        # Flush
        if data:
            self.pmi.pmiWrite(int(self.pmfg_ts().strftime('%s')),
                              self.pmfg_ts().microsecond)