2020-04-13 13:31:30 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""Displays the number of docker containers running
|
|
|
|
|
|
|
|
Requires the following python packages:
|
|
|
|
* docker
|
|
|
|
|
2020-05-08 20:58:35 +02:00
|
|
|
contributed by `jlopezzarza <https://github.com/jlopezzarza>`_ - many thanks!
|
2020-04-13 13:31:30 +02:00
|
|
|
"""
|
|
|
|
|
2020-04-13 13:38:05 +02:00
|
|
|
import docker
|
2020-04-13 13:31:30 +02:00
|
|
|
|
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
|
2020-04-13 13:38:05 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.decorators
|
2020-04-13 13:31:30 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-13 13:38:05 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(seconds=5)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.docker_info))
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__info = ""
|
2020-04-13 13:31:30 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
state = []
|
2020-05-03 11:15:52 +02:00
|
|
|
if self.__info == "OK - 0":
|
|
|
|
state.append("warning")
|
2020-05-08 16:10:27 +02:00
|
|
|
elif self.__info in ["n/a", "off"]:
|
2020-05-03 11:15:52 +02:00
|
|
|
state.append("critical")
|
2020-04-13 13:31:30 +02:00
|
|
|
return state
|
|
|
|
|
2020-04-13 13:38:05 +02:00
|
|
|
def docker_info(self, widget):
|
2020-04-13 13:31:30 +02:00
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
cli = docker.DockerClient(base_url="unix://var/run/docker.sock")
|
2020-04-13 13:31:30 +02:00
|
|
|
cli.ping()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__info = "OK - {}".format(
|
|
|
|
len(cli.containers.list(filters={"status": "running"}))
|
|
|
|
)
|
2020-04-13 13:31:30 +02:00
|
|
|
except ConnectionError:
|
2020-05-08 16:10:27 +02:00
|
|
|
self.__info = "off"
|
2020-04-13 13:31:30 +02:00
|
|
|
except Exception:
|
2020-05-03 11:15:52 +02:00
|
|
|
self.__info = "n/a"
|
2020-04-13 13:38:05 +02:00
|
|
|
return self.__info
|
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-13 13:38:05 +02:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|