示例#1
0
文件: test_init.py 项目: rikroe/core
async def setup_prometheus_client(hass, hass_client, namespace):
    """Initialize an hass_client with Prometheus component."""
    # Reset registry
    prometheus_client.REGISTRY = prometheus_client.CollectorRegistry(auto_describe=True)
    prometheus_client.ProcessCollector(registry=prometheus_client.REGISTRY)
    prometheus_client.PlatformCollector(registry=prometheus_client.REGISTRY)
    prometheus_client.GCCollector(registry=prometheus_client.REGISTRY)

    config = {}
    if namespace is not None:
        config[prometheus.CONF_PROM_NAMESPACE] = namespace
    assert await async_setup_component(
        hass, prometheus.DOMAIN, {prometheus.DOMAIN: config}
    )
    await hass.async_block_till_done()

    return await hass_client()
示例#2
0
 def __init__(self):
     self.registry = prometheus_client.CollectorRegistry(auto_describe=True)
     prometheus_client.GCCollector(registry=self.registry)
     prometheus_client.PlatformCollector(registry=self.registry)
     prometheus_client.ProcessCollector(registry=self.registry)
     self.registry.register(self)
示例#3
0
    def __init__(self, listen_host: str) -> None:
        if not prometheus_available:
            raise RuntimeError("`prometheus_client` is not installed. "
                               "Run `pip install 'afancontrol[metrics]'`.")

        self._listen_addr, port_str = listen_host.rsplit(":", 1)
        self._listen_port = int(port_str)

        self._http_server = None  # type: Optional[HTTPServer]

        self._last_metrics_collect_clock = float("nan")

        # Create a separate registry for this instance instead of using
        # the default one (which is global and doesn't allow to instantiate
        # this class more than once due to having metrics below being
        # registered for a second time):
        self.registry = prom.CollectorRegistry(auto_describe=True)

        # Register some default prometheus_client metrics:
        prom.ProcessCollector(registry=self.registry)
        if hasattr(prom, "PlatformCollector"):
            prom.PlatformCollector(registry=self.registry)
        if hasattr(prom, "GCCollector"):
            prom.GCCollector(registry=self.registry)

        # Temps:
        self.temperature_is_failing = prom.Gauge(
            "temperature_is_failing",
            "The temperature sensor is failing (it isn't returning any data)",
            ["temp_name"],
            registry=self.registry,
        )
        self.temperature_current = prom.Gauge(
            "temperature_current",
            "The current temperature value (in Celsius) from a temperature sensor",
            ["temp_name"],
            registry=self.registry,
        )
        self.temperature_min = prom.Gauge(
            "temperature_min",
            "The min temperature value (in Celsius) for a temperature sensor",
            ["temp_name"],
            registry=self.registry,
        )
        self.temperature_max = prom.Gauge(
            "temperature_max",
            "The max temperature value (in Celsius) for a temperature sensor",
            ["temp_name"],
            registry=self.registry,
        )
        self.temperature_panic = prom.Gauge(
            "temperature_panic",
            "The panic temperature value (in Celsius) for a temperature sensor",
            ["temp_name"],
            registry=self.registry,
        )
        self.temperature_threshold = prom.Gauge(
            "temperature_threshold",
            "The threshold temperature value (in Celsius) for a temperature sensor",
            ["temp_name"],
            registry=self.registry,
        )
        self.temperature_is_panic = prom.Gauge(
            "temperature_is_panic",
            "Is panic temperature reached for a temperature sensor",
            ["temp_name"],
            registry=self.registry,
        )
        self.temperature_is_threshold = prom.Gauge(
            "temperature_is_threshold",
            "Is threshold temperature reached for a temperature sensor",
            ["temp_name"],
            registry=self.registry,
        )

        # Fans:
        self.fan_rpm = prom.Gauge(
            "fan_rpm",
            "Fan speed (in RPM) as reported by the fan",
            ["fan_name"],
            registry=self.registry,
        )
        self.fan_pwm = prom.Gauge(
            "fan_pwm",
            "Current fan's PWM value (from 0 to 255)",
            ["fan_name"],
            registry=self.registry,
        )
        self.fan_pwm_normalized = prom.Gauge(
            "fan_pwm_normalized",
            "Current fan's normalized PWM value (from 0.0 to 1.0, within "
            "the `fan_pwm_line_start` and `fan_pwm_line_end` interval)",
            ["fan_name"],
            registry=self.registry,
        )
        self.fan_pwm_line_start = prom.Gauge(
            "fan_pwm_line_start",
            "PWM value where a linear correlation with RPM starts for the fan",
            ["fan_name"],
            registry=self.registry,
        )
        self.fan_pwm_line_end = prom.Gauge(
            "fan_pwm_line_end",
            "PWM value where a linear correlation with RPM ends for the fan",
            ["fan_name"],
            registry=self.registry,
        )
        self.fan_is_stopped = prom.Gauge(
            "fan_is_stopped",
            "Is PWM fan stopped because the corresponding temperatures "
            "are already low",
            ["fan_name"],
            registry=self.registry,
        )
        self.fan_is_failing = prom.Gauge(
            "fan_is_failing",
            "Is PWM fan marked as failing (e.g. because it has jammed)",
            ["fan_name"],
            registry=self.registry,
        )

        # Arduino boards:
        self.arduino_is_connected = prom.Gauge(
            "arduino_is_connected",
            "Is Arduino board connected via Serial",
            ["arduino_name"],
            registry=self.registry,
        )
        self.arduino_status_age_seconds = prom.Gauge(
            "arduino_status_age_seconds",
            "Seconds since the last `status` message from "
            "the Arduino board (measured at the latest tick)",
            ["arduino_name"],
            registry=self.registry,
        )

        # Others:
        self.is_panic = prom.Gauge("is_panic",
                                   "Is in panic mode",
                                   registry=self.registry)
        self.is_threshold = prom.Gauge("is_threshold",
                                       "Is in threshold mode",
                                       registry=self.registry)

        self.tick_duration = prom.Histogram(
            # Summary would have been better there, but prometheus_client
            # doesn't yet support quantiles in Summaries.
            # See: https://github.com/prometheus/client_python/issues/92
            "tick_duration",
            "Duration of a single tick",
            buckets=(0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 5.0, 10.0, float("inf")),
            registry=self.registry,
        )
        last_metrics_tick_seconds_ago = prom.Gauge(
            "last_metrics_tick_seconds_ago",
            "The time in seconds since the last tick (which also updates these metrics)",
            registry=self.registry,
        )
        last_metrics_tick_seconds_ago.set_function(
            lambda: self.last_metrics_tick_seconds_ago)