Skip to content

Commit f402af5

Browse files
committed
added initial support for stats retrieval
1 parent d051202 commit f402af5

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

docker/client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,11 +966,35 @@ def start(self, container, binds=None, port_bindings=None, lxc_conf=None,
966966
res = self._post_json(url, data=start_config)
967967
self._raise_for_status(res)
968968

969+
def stats(self, container, aggregate=0):
970+
if isinstance(container, dict):
971+
container = container.get('Id')
972+
url = self._url("/containers/{0}/stats".format(container))
973+
res = self._get(url, stream=True)
974+
self._raise_for_status(res)
975+
976+
stats_stream = res.iter_lines()
977+
stats_array = []
978+
stats_aggregated = 0
979+
980+
if aggregate:
981+
for stat_obj in stats_stream:
982+
if stats_aggregated >= aggregate:
983+
stats_aggregated = 0
984+
yield stats_array
985+
stats_array = []
986+
stats_array.append(stat_obj)
987+
stats_aggregated += 1
988+
else:
989+
for stat_obj in stats_stream:
990+
yield stat_obj
991+
969992
def stop(self, container, timeout=10):
970993
if isinstance(container, dict):
971994
container = container.get('Id')
972995
params = {'t': timeout}
973996
url = self._url("/containers/{0}/stop".format(container))
997+
974998
res = self._post(url, params=params,
975999
timeout=(timeout + self.timeout))
9761000
self._raise_for_status(res)

docs/api.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,32 @@ from. Optionally a single string joining container id's with commas
664664
None
665665
```
666666

667+
## stats
668+
669+
The Docker API parallel to the `docker stats` command.
670+
This will stream statistics for a specific container.
671+
672+
`aggregate`, if 0 will return an iterable stream object of messages.
673+
If aggregate is above 0, an iterable stream object of a list of messages in the size of `aggregate` will be returned.
674+
675+
**Params**:
676+
677+
* container (str): The container to start
678+
* aggregate (int): The number of messages to aggregate before returning a stream object.
679+
680+
```python
681+
>>> from docker import Client
682+
>>> aggregate = 10
683+
>>> cli = Client(base_url='tcp://127.0.0.1:2375')
684+
>>> stats_obj = cli.stats('elasticsearch', aggregate=aggregate)
685+
>>> for stat in stats:
686+
>>> print(stat)
687+
['{"read":"2015-02-11T21:47:30.49388286+02:00","network":{"rx_bytes":666052,"rx_packets":4409 ...
688+
...
689+
...
690+
...
691+
```
692+
667693
## stop
668694

669695
Stops a container. Similar to the `docker stop` command.

docs/change_log.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Change Log
22
==========
33

4+
0.7.3
5+
-----
6+
7+
### Features
8+
9+
* Added support for `stats` API.
10+
11+
412
0.7.2
513
-----
614

tests/integration_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from test import Cleanup
3030

3131
# FIXME: missing tests for
32-
# export; history; import_image; insert; port; push; tag; get; load
32+
# export; history; import_image; insert; port; push; tag; get; load; stats;
3333

3434
DEFAULT_BASE_URL = os.environ.get('DOCKER_HOST')
3535

0 commit comments

Comments
 (0)