|
@@ -1,10 +1,9 @@
|
1
|
1
|
#!/usr/bin/env python3
|
2
|
2
|
|
3
|
3
|
from collections import defaultdict
|
|
4
|
+from monitor import Monitor
|
4
|
5
|
import argparse
|
5
|
|
-import docker
|
6
|
6
|
import etcd
|
7
|
|
-import sys
|
8
|
7
|
|
9
|
8
|
|
10
|
9
|
def etcd_put(client, prefix, obj):
|
|
@@ -17,31 +16,10 @@ def etcd_put(client, prefix, obj):
|
17
|
16
|
client.write(new_prefix, str(value))
|
18
|
17
|
|
19
|
18
|
|
20
|
|
-def get_addresses(container):
|
21
|
|
- return {k: v['IPAddress'] for k, v in container['NetworkSettings']['Networks'].items()}
|
22
|
|
-
|
23
|
|
-
|
24
|
|
-def get_ports(container):
|
25
|
|
- ports = defaultdict(dict)
|
26
|
|
- for port in container['Ports']:
|
27
|
|
- ports[port['Type']][port['PrivatePort']] = port['PublicPort'] if 'PublicPort' in port else 0
|
28
|
|
- return ports
|
29
|
|
-
|
30
|
|
-
|
31
|
|
-def add_containers(infos):
|
|
19
|
+def add_containers(new_containers):
|
32
|
20
|
global containers, host_index, label_index, network_index
|
33
|
|
- for info in infos:
|
34
|
|
- container = {
|
35
|
|
- 'host': host,
|
36
|
|
- 'image': info['Image'],
|
37
|
|
- 'labels': info['Labels'],
|
38
|
|
- 'net': {
|
39
|
|
- 'addr': get_addresses(info),
|
40
|
|
- 'ports': get_ports(info)
|
41
|
|
- }
|
42
|
|
- }
|
43
|
|
-
|
44
|
|
- name = info['Names'][0][1:]
|
|
21
|
+ for container in new_containers:
|
|
22
|
+ name = container['name']
|
45
|
23
|
containers[name] = container
|
46
|
24
|
|
47
|
25
|
for k, v in container['labels'].items():
|
|
@@ -51,8 +29,6 @@ def add_containers(infos):
|
51
|
29
|
|
52
|
30
|
host_index[host][name] = name
|
53
|
31
|
|
54
|
|
-
|
55
|
|
-def write_all():
|
56
|
32
|
etcd_put(etcd_client, prefix + '/containers', containers)
|
57
|
33
|
etcd_put(etcd_client, prefix + '/labels', label_index)
|
58
|
34
|
etcd_put(etcd_client, prefix + '/networks', network_index)
|
|
@@ -66,34 +42,19 @@ parser.add_argument('--etcd-host', help='Host to connect to etcd on', default='e
|
66
|
42
|
parser.add_argument('--etcd-prefix', help='Prefix to use when adding keys to etcd', default='/docker')
|
67
|
43
|
args = parser.parse_args()
|
68
|
44
|
|
69
|
|
-docker_client = docker.Client(base_url='unix://var/run/docker.sock')
|
|
45
|
+monitor = Monitor(args.name, add_containers, lambda x: None)
|
70
|
46
|
etcd_client = etcd.Client(host=args.etcd_host, port=args.etcd_port)
|
71
|
47
|
prefix = args.etcd_prefix
|
72
|
48
|
host = args.name
|
73
|
49
|
|
74
|
|
-event_gen = docker_client.events(decode=True, filters={'type': 'container', 'event': ['die', 'start']})
|
75
|
|
-
|
76
|
50
|
containers = {}
|
77
|
51
|
label_index = defaultdict(dict)
|
78
|
52
|
network_index = defaultdict(dict)
|
79
|
53
|
host_index = defaultdict(dict)
|
80
|
54
|
|
81
|
|
-add_containers(docker_client.containers())
|
82
|
|
-
|
83
|
55
|
try:
|
84
|
56
|
etcd_client.delete(prefix, recursive=True)
|
85
|
57
|
except etcd.EtcdKeyNotFound:
|
86
|
58
|
pass
|
87
|
59
|
|
88
|
|
-write_all()
|
89
|
|
-
|
90
|
|
-for event in event_gen:
|
91
|
|
- if event['Action'] == 'start':
|
92
|
|
- print('New container %s' % event['id'])
|
93
|
|
- add_containers(docker_client.containers(filters={'id': event['id']}))
|
94
|
|
- write_all()
|
95
|
|
- elif event['Action'] == 'die':
|
96
|
|
- print('Dead container %s' % event['id'])
|
97
|
|
- else:
|
98
|
|
- print('Unexpected event %s' % event['Action'])
|
99
|
|
-
|
|
60
|
+monitor.monitor()
|