bumblebee-status/modules/contrib/docker_ps.py

44 lines
1.1 KiB
Python
Raw Normal View History

2020-04-13 13:31:30 +02:00
# -*- coding: utf-8 -*-
"""Displays the number of docker containers running
Requires the following python packages:
* docker
"""
import docker
2020-04-13 13:31:30 +02:00
from requests.exceptions import ConnectionError
import core.module
import core.widget
import core.decorators
2020-04-13 13:31:30 +02:00
class Module(core.module.Module):
@core.decorators.every(seconds=5)
def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.docker_info))
self.__info = ''
2020-04-13 13:31:30 +02:00
def state(self, widget):
state = []
if self.__info == 'OK - 0':
2020-04-13 13:31:48 +02:00
state.append('warning')
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
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:
self.__info = 'daemon off'
2020-04-13 13:31:30 +02:00
except Exception:
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