Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates to change execute() to exec_create/exec_start #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions containerHelper.py
Original file line number Diff line number Diff line change
@@ -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"]):
Expand Down Expand Up @@ -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)

Expand All @@ -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")
Expand All @@ -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)