当前位置: 首页>>代码示例>>Python>>正文


Python InfluxDBClusterClient.write_points方法代码示例

本文整理汇总了Python中influxdb.InfluxDBClusterClient.write_points方法的典型用法代码示例。如果您正苦于以下问题:Python InfluxDBClusterClient.write_points方法的具体用法?Python InfluxDBClusterClient.write_points怎么用?Python InfluxDBClusterClient.write_points使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在influxdb.InfluxDBClusterClient的用法示例。


在下文中一共展示了InfluxDBClusterClient.write_points方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: influxdb09

# 需要导入模块: from influxdb import InfluxDBClusterClient [as 别名]
# 或者: from influxdb.InfluxDBClusterClient import write_points [as 别名]
class influxdb09(influxdb):
    def __init__(self, cfg):
        influxdb.__init__(self, cfg)
        if 'influxdb_extra_tags' in cfg:
            self.influxdb_extra_tags = ast.literal_eval(
                cfg['influxdb_extra_tags'])
            print self.influxdb_extra_tags
        else:
            self.influxdb_extra_tags = {}

        hosts = map(lambda x: (x.split(':')[0],x.split(':')[1]), self.influxdb_servers)
        self.client = InfluxDBClusterClient(hosts, self.influxdb_user, self.influxdb_password, timeout = self.timeout)

    def build_url(self, server):
        """ Returns a url to specified InfluxDB-server """
        test_port = server.split(':')
        if len(test_port) < 2:
            server = "%s:%i" % (server, self.default_ports[self.scheme])

        return "%s://%s/write?u=%s&p=%s" % (self.scheme, server,
                                            self.influxdb_user,
                                            self.influxdb_password)

    def send(self, metrics):
        """ Connect to influxdb and send metrics """
        ret = 0
        perfdata = {}
        for m in metrics:
            ret += 1

            if (m.SERVICEDESC == ''):
                path = m.HOSTCHECKCOMMAND
                status = m.HOSTSTATE
            else:
                path = m.SERVICEDESC
                status = m.SERVICESTATE

            # Ensure a float gets passed
            # A measurement can not have integer and float values
            try:
                value = float(m.VALUE)
            except ValueError:
                value = 0

            # Add project as tag
            try:
                project = m.PROJECT
            except AttributeError:
                project = "NA"

            tags = {"check": m.LABEL, "host": m.HOSTNAME, "project": project, "status": status}

            # Add warning and critical threshold if exists
            try:
                tags.update({"warning": float(m.WARN)})
            except ValueError:
                pass

            try:
                tags.update({"critical": float(m.CRIT)})
            except ValueError:
                pass

            try:
                tags.update({"min": float(m.MIN)})
            except ValueError:
                pass

            try:
                tags.update({"max": float(m.MAX)})
            except ValueError:
                pass

            tags.update(self.influxdb_extra_tags)

            # perfdata has each project's metrics in a different array
            perfdata.setdefault(project, []).append({
                                                     "time": int(m.TIMET),
                                                     "measurement": path,
                                                     "tags": tags,
                                                     "fields": {"value": value}})

        for project in perfdata:
            try:
                self.client.write_points(perfdata[project], database=project,
                                         time_precision='s',
                                         batch_size = self.influxdb_max_metrics)

            except requests.exceptions.Timeout as error:
                self.log.critical("Timeout connecting to InfluxDB: %s", error)
                ret = 0
            except requests.exceptions.ConnectionError as error:
                self.log.critical("Error connecting to InfluxDB: %s", error)
                ret = 0
            except Exception as error:
                self.log.critical("Error writing points to InfluxDB: %s", error)
                # If send fails, set sended metrics as 0.
                # Will print a error message: "...insufficent metrics sent from..."
                ret = 0

#.........这里部分代码省略.........
开发者ID:adrianlzt,项目名称:graphios,代码行数:103,代码来源:graphios_backends.py


注:本文中的influxdb.InfluxDBClusterClient.write_points方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。