ualex73 / monitor_docker

Monitor Docker containers from Home Assistant
Apache License 2.0
268 stars 34 forks source link

Feature Request: Monitored Conditions as attrubutes inside docker switch #16

Closed VirtualL closed 3 years ago

VirtualL commented 4 years ago

Hi, Currently, all the Monitored Conditions are made a new entity. E.G: for Docker "mosquito" this entity are made: switch.docker_mosquitto - switch docker ON/OFF sensor.docker_mosquitto_memory sensor.docker_mosquitto_state sensor.docker_mosquitto_status and so on... (sensor.docker_mosquitto_XXX)

There is an option to add all of these sensors on the main "switch" as attributes, for each docker? I don't like 20 sensors for each docker in my system. (8X20 its a lot of entities)

also, if someone wants to use these as sensors, he can do it like this:

# Example configuration.yaml entry
sensor:
  - platform: template
    sensors:
      docker_mosquitto_memory:
        friendly_name: "MQTT Memory Usage"
        value_template: '{{ states.switch.docker_mosquitto.attributes.memory }}'
      docker_mosquitto_memory:
        friendly_name: "MQTT State"
        value_template: '{{ states.switch.docker_mosquitto.attributes.state }}'

Also, it will work OK for automation.

Thank You

ualex73 commented 4 years ago

This looks like an interesting request, I will investigate if I can combine multiple sensors into 1 sensor per container. In such case you will get 2 per container, a sensor and switch (combining it into the switch is too difficult, because they are different components).

VirtualL commented 4 years ago

Sounds great for me! Thanks for the fast replay.

ualex73 commented 4 years ago

Just implemented it in the master branch, with a special condition "allinone". If you add that one, it will just create 1 sensor per container with the requested attributes. I will push it to HACS somewhere next week, but would be good if you could test it.

VirtualL commented 4 years ago

Hi, Thanks a lot! I replace the files( download Zip from the latest repo, and check if the file have "alleinone" option) and I try this 2 configurations:

monitor_docker:
  - name: Docker
    containers:
      - unifi-controller
      - letsencrypt
      - code-server #vscode
      - mosquitto #mqtt
      - openvpn-as #VPN
    rename:
      unifi-controller: UniFi
      letsencrypt: Let's Encrypt
      code-server: VS-Code
      mosquitto: MQTT Mosquitto
      openvpn-as: Open VPN
    monitored_conditions:
      - state #for each docker container
      - status #for each docker container
      - containers_running
      - containers_total
      - containers_memory_percentage
      - containers_cpu_percentage
      - version #for each docker container
      - memory #for each docker container
      - allinone
monitor_docker:
  - name: Docker
    containers:
      - unifi-controller
      - letsencrypt
      - code-server #vscode
      - mosquitto #mqtt
      - openvpn-as #VPN
    rename:
      unifi-controller: UniFi
      letsencrypt: Let's Encrypt
      code-server: VS-Code
      mosquitto: MQTT Mosquitto
      openvpn-as: Open VPN
    monitored_conditions:
      - allinone
      - state #for each docker container
      - status #for each docker container
      - containers_running
      - containers_total
      - containers_memory_percentage
      - containers_cpu_percentage
      - version #for each docker container
      - memory #for each docker container

and nothing was working (Got only the "switch" for each container. When I Remove the new "allinone" from the monitored_conditions, everything working as before (without this new feature).

Thanks

ualex73 commented 3 years ago

Can you enable the debugging and copy-paste that? Currently I can't see what's going wrong on your end.

VirtualL commented 3 years ago

Hi, Sorry for the late answer... I am now running on HA V0.112.4 (8JUL2020)

If I have the new option "allinone" (like the write before) I got only these entities:

Screen Shot 2020-07-11 at 15 01 46

and this is the full log (I have more info in the log but just some updated of the docker, noting new of important)

2020-07-11 14:57:12 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for monitor_docker which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant.
2020-07-11 14:57:12 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for hacs which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant.
2020-07-11 14:57:13 WARNING (MainThread) [homeassistant.components.lovelace] Resources need to be specified in your configuration.yaml. Please see the docs.
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] Docker version: 19.03.5
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] home-assistant: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] Version: 19.03.5, Containers: 12, Running: 6, CPU: None%, Memory: NoneMB, None%
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] letsencrypt: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] unifi-controller: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] openvpn-as: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] ddclient: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] mosquitto: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] ddclient: Created 13 days ago
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] home-assistantOR: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] organizr: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] home-assistantOR: Created 19 days ago
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] Cloudflare-DDNS: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] organizr: Created 19 days ago
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] code-server: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] Cloudflare-DDNS: Exited (0) 19 days ago
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] TasmoAdmin: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] ha-dockermon: Container Monitored
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] TasmoAdmin: Created 7 months ago
2020-07-11 14:57:18 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] ha-dockermon: Exited (137) 7 days ago
2020-07-11 14:57:20 WARNING (MainThread) [homeassistant.loader] You are using a custom integration for authenticated which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant.
2020-07-11 14:57:20 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] mosquitto: CPU Usage=None%. Memory Usage=1.34MB, 0.02%
2020-07-11 14:57:20 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] code-server: CPU Usage=None%. Memory Usage=130.05MB, 1.65%
2020-07-11 14:57:20 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] home-assistant: CPU Usage=None%. Memory Usage=213.16MB, 2.71%
2020-07-11 14:57:20 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] letsencrypt: CPU Usage=None%. Memory Usage=35.93MB, 0.46%
2020-07-11 14:57:20 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] unifi-controller: CPU Usage=None%. Memory Usage=807.54MB, 10.25%
2020-07-11 14:57:20 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] openvpn-as: CPU Usage=None%. Memory Usage=243.06MB, 3.09%
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.switch] letsencrypt: Adding component Switch
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.switch] unifi-controller: Adding component Switch
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.switch] openvpn-as: Adding component Switch
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.switch] mosquitto: Adding component Switch
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.switch] code-server: Adding component Switch
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.helpers] letsencrypt: Added callback to container, entity: switch
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.helpers] unifi-controller: Added callback to container, entity: switch
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.helpers] openvpn-as: Added callback to container, entity: switch
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.helpers] mosquitto: Added callback to container, entity: switch
2020-07-11 14:57:20 DEBUG (MainThread) [custom_components.monitor_docker.helpers] code-server: Added callback to container, entity: switch
2020-07-11 14:57:21 INFO (MainThread) [custom_components.monitor_docker.sensor] Initializing Docker sensor 'containers_running'
2020-07-11 14:57:21 INFO (MainThread) [custom_components.monitor_docker.sensor] Initializing Docker sensor 'containers_total'
2020-07-11 14:57:21 INFO (MainThread) [custom_components.monitor_docker.sensor] Initializing Docker sensor 'containers_memory_percentage'
2020-07-11 14:57:21 INFO (MainThread) [custom_components.monitor_docker.sensor] Initializing Docker sensor 'containers_cpu_percentage'
2020-07-11 14:57:21 INFO (MainThread) [custom_components.monitor_docker.sensor] Initializing Docker sensor 'version'
2020-07-11 14:57:21 DEBUG (MainThread) [custom_components.monitor_docker.sensor] letsencrypt: Adding component Sensor(s)
**2020-07-11 14:57:24 ERROR (MainThread) [homeassistant.components.sensor] Error while setting up monitor_docker platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 178, in _async_setup_platform
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/local/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/config/custom_components/monitor_docker/sensor.py", line 105, in async_setup_platform
    condition_list=monitor_conditions,
  File "/config/custom_components/monitor_docker/sensor.py", line 247, in __init__
    self._name = sensor_name_format.format(name=alias)
KeyError: 'sensor'**
2020-07-11 14:57:28 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] ddclient: Created 13 days ago
2020-07-11 14:57:28 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] home-assistantOR: Created 19 days ago
2020-07-11 14:57:28 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] organizr: Created 19 days ago
2020-07-11 14:57:28 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] Version: 19.03.5, Containers: 12, Running: 6, CPU: None%, Memory: 1431.08MB, 18.18%
2020-07-11 14:57:28 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] Cloudflare-DDNS: Exited (0) 19 days ago
2020-07-11 14:57:28 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] TasmoAdmin: Created 7 months ago
2020-07-11 14:57:28 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] ha-dockermon: Exited (137) 7 days ago
2020-07-11 14:57:31 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] mosquitto: CPU Usage=0.04%. Memory Usage=1.34MB, 0.02%
2020-07-11 14:57:31 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] mosquitto: Send notify (1) to container
2020-07-11 14:57:31 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] code-server: CPU Usage=16.86%. Memory Usage=137.82MB, 1.75%
2020-07-11 14:57:31 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] code-server: Send notify (1) to container
2020-07-11 14:57:32 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] home-assistant: CPU Usage=66.85%. Memory Usage=307.49MB, 3.9%
2020-07-11 14:57:32 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] letsencrypt: CPU Usage=0.11%. Memory Usage=35.93MB, 0.46%
2020-07-11 14:57:32 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] letsencrypt: Send notify (1) to container
2020-07-11 14:57:32 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] unifi-controller: CPU Usage=2.03%. Memory Usage=807.62MB, 10.25%
2020-07-11 14:57:32 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] unifi-controller: Send notify (1) to container
2020-07-11 14:57:32 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] openvpn-as: CPU Usage=0.13%. Memory Usage=243.06MB, 3.09%
2020-07-11 14:57:32 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] openvpn-as: Send notify (1) to container
2020-07-11 14:57:38 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] ddclient: Created 13 days ago
2020-07-11 14:57:38 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] home-assistantOR: Created 19 days ago
2020-07-11 14:57:38 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] organizr: Created 19 days ago
2020-07-11 14:57:38 DEBUG (Thread-41) [custom_components.monitor_docker.helpers] Cloudflare-DDNS: Exited (0) 19 days ago
ualex73 commented 3 years ago

Thanks, this makes the error clear. I think I found the issue, I just committed a fix. Can you download it again try it?

VirtualL commented 3 years ago

Now it's working as should!

P.S I think if you can add to "uptime" a more human-readable timestamp it would be nice? Screen Shot 2020-07-11 at 15 29 15

you can see an example here: https://github.com/home-assistant/core/blob/dev/homeassistant/components/uptime/sensor.py

Thank you for the super-fast issues solver in GitHub :)

ualex73 commented 3 years ago

Good to hear it is working now.

Which format do you mean with more readable format? I use the 'uptime' sensor value as attribute with the 'allinone', only HA doesn't translate it anymore (which it does, if it is a separate sensor).

VirtualL commented 3 years ago

Got it, it's ok like this. Thank you.