Skip to content

Commit

Permalink
updates to change execute() to exec_create/exec_start
Browse files Browse the repository at this point in the history
1. works under python2
2. network module uses /proc/net/dev for statistics, which should always work
3. ggtools#2 gets around images that don't have ifconfig installed
  • Loading branch information
oskapt committed Oct 12, 2015
1 parent 12371ea commit d8266c8
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 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 Down

0 comments on commit d8266c8

Please sign in to comment.