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-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-04-13 13:38:05 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(seconds=5)
|
|
|
|
def __init__(self, config):
|
|
|
|
super().__init__(config, core.widget.Widget(self.docker_info))
|
|
|
|
self.__info = ''
|
2020-04-13 13:31:30 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
state = []
|
2020-04-13 13:38:05 +02:00
|
|
|
if self.__info == 'OK - 0':
|
2020-04-13 13:31:48 +02:00
|
|
|
state.append('warning')
|
2020-04-13 13:38:05 +02:00
|
|
|
elif self.__info in ['n/a', 'daemon off']:
|
2020-04-13 13:31:48 +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:
|
|
|
|
cli = docker.DockerClient(base_url='unix://var/run/docker.sock')
|
|
|
|
cli.ping()
|
|
|
|
except ConnectionError:
|
2020-04-13 13:38:05 +02:00
|
|
|
self.__info = 'daemon off'
|
2020-04-13 13:31:30 +02:00
|
|
|
except Exception:
|
2020-04-13 13:38:05 +02:00
|
|
|
self.__info = 'n/a'
|
|
|
|
self.__info = 'OK - {}'.format(len(cli.containers.list(filters={'status': 'running'})))
|
|
|
|
return self.__info
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|