diff --git a/containerHelper.py b/containerHelper.py index 261d0ad..e432f81 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) @@ -81,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") @@ -102,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)