File: docker_ps.py

package info (click to toggle)
bumblebee-status 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,844 kB
  • sloc: python: 13,430; sh: 68; makefile: 29
file content (48 lines) | stat: -rw-r--r-- 1,263 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-

"""Displays the number of docker containers running

Requires the following python packages:
    * docker

contributed by `jlopezzarza <https://github.com/jlopezzarza>`_ - many thanks!
"""

import docker

from requests.exceptions import ConnectionError

import core.module
import core.widget
import core.decorators


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 = ""

    def state(self, widget):
        state = []
        if self.__info == "OK - 0":
            state.append("warning")
        elif self.__info in ["n/a", "off"]:
            state.append("critical")
        return state

    def docker_info(self, widget):
        try:
            cli = docker.DockerClient(base_url="unix://var/run/docker.sock")
            cli.ping()
            self.__info = "OK - {}".format(
                len(cli.containers.list(filters={"status": "running"}))
            )
        except ConnectionError:
            self.__info = "off"
        except Exception:
            self.__info = "n/a"
        return self.__info


# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4