From d8266c85d9b03a889290dfcee15beca571d9649d Mon Sep 17 00:00:00 2001 From: Adrian Goins Date: Mon, 12 Oct 2015 14:12:25 -0300 Subject: [PATCH 1/2] updates to change execute() to exec_create/exec_start 1. works under python2 2. network module uses /proc/net/dev for statistics, which should always work 3. #2 gets around images that don't have ifconfig installed --- containerHelper.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/containerHelper.py b/containerHelper.py index 261d0ad..b300a7e 100755 --- a/containerHelper.py +++ b/containerHelper.py @@ -1,15 +1,12 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python -__author__ = 'Christophe Labouisse' - -import argparse -import re -import os +__author__ = 'Christophe Labouisse and Adrian Goins' +import argparse, re, os +from sys import exit from docker import Client from docker.utils import kwargs_from_env - def display_cpu(args): detail = c.inspect_container(args.container) if bool(detail["State"]["Running"]): @@ -52,16 +49,26 @@ def display_memory(args): def display_network(args): detail = c.inspect_container(args.container) if bool(detail["State"]["Running"]): - ifconfig = c.execute(args.container, "ifconfig eth0") - m = re.search(("RX" if args.direction == "in" else "TX") + r" bytes:(\d+)", str(ifconfig)) - if m: - print(m.group(1)) + cid = c.exec_create(args.container, "cat /proc/net/dev") + r = c.exec_start(cid['Id']) + + for line in r.split('\n'): + i = line.split() + try: + if i[0] == 'eth0:': + break + except IndexError: + # blank line + continue + + if not line: + print 0 + exit(1) + + if args.direction == 'in': + print i[1] else: - b = c.execute(args.container, "cat /sys/devices/virtual/net/eth0/statistics/"+("rx" if args.direction == "in" else "tx")+"_bytes").decode() - if re.match(r"\s*\d+\s*", b): - print(b) - else: - print(0) + print i[9] else: print(0) From ca856cd5cc734135d8a8e8263073e3ffe545d998 Mon Sep 17 00:00:00 2001 From: Adrian Goins Date: Wed, 13 Apr 2016 10:02:48 -0300 Subject: [PATCH 2/2] added version (auto/listed) to Client creation When using docker-py 1.8.0 (released on Apr 6 2016), this script can generate the error message about client API version being newer than server (1.22 / 1.21). The fix for this, according to [here](https://github.com/docker/docker-py/issues/1032) is to specify the version or do specify 'auto' to use the server version. I've added `--version` as a keyword argument before the positional arguments for Client. --- containerHelper.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/containerHelper.py b/containerHelper.py index b300a7e..e432f81 100755 --- a/containerHelper.py +++ b/containerHelper.py @@ -88,6 +88,8 @@ def display_status(args): parser = argparse.ArgumentParser() +parser.add_argument("--version", help="API Version", default='auto') + parser.add_argument("container", help="Container name") subparsers = parser.add_subparsers(title="Counters", description="Available counters", dest="dataType") @@ -109,7 +111,8 @@ def display_status(args): status_parser = subparsers.add_parser("status", help="Display the container status") status_parser.set_defaults(func=display_status) -c = Client(**(kwargs_from_env())) - args = parser.parse_args() + +c = Client(version=args.version, **(kwargs_from_env())) + args.func(args)