forked from grihey/atsd-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_delete.py
68 lines (50 loc) · 2.49 KB
/
docker_delete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python
import os
import pprint
import time
from atsd_client import connect, connect_url
from atsd_client.services import EntitiesService, MetricsService
'''
Locate a collection of entities (Docker hosts) that have not inserted data for more than 7 days.
Delete related entities (containers, images, network, volumes) based on entity-tag set to the given Docker host.
Delete Docker host entities.
'''
# Uncomment the next two lines to set custom local timezone
# os.environ['TZ'] = 'Europe/London'
# time.tzset()
tags_printer = pprint.PrettyPrinter(indent=4)
# Connect to ATSD server
#connection = connect('/path/to/connection.properties')
connection = connect_url('https://atsd_hostname:8443', 'username', 'password')
# Initialize services
entity_service = EntitiesService(connection)
metric_service = MetricsService(connection)
# select all entities that collect this metric
# this metric is collected by docker hosts
docker_hosts = metric_service.series('docker.cpu.sum.usage.total.percent')
print("Docker hosts found: " + str(len(docker_hosts)))
for docker_host_series in docker_hosts:
print("--------------")
# get minutes since last insert
elapsed_minutes = docker_host_series.get_elapsed_minutes()
entity_filter = "lower(tags.docker-host) = lower('" + docker_host_series.entity + "')"
# find related entities, which tag value equals docker host
entities = entity_service.list(expression=entity_filter, limit=0, tags="*")
print(" - FOUND " + str(len(entities)) + " objects for docker_host= " + docker_host_series.entity +
" : " + docker_host_series.last_insert_date.isoformat() + " : elapsed_minutes= " + str(elapsed_minutes))
# keep entities that have recent data (inserted within the last 7 days)
if elapsed_minutes < 7*24*60:
print(" - RETAIN (do not delete): " + docker_host_series.entity)
continue
print(" - DELETE objects for docker_host= " + docker_host_series.entity)
for entity in entities:
if entity.name == docker_host_series.entity:
# ignore the docker host itself, host is deleted later
continue
print("- Deleting " + entity.tags.get('docker-type', '') + " : " + entity.name + " : " + entity.tags.get('name', ''))
tags_printer.pprint(entity.tags)
# Uncomment next line to delete
# entity_service.delete(entity)
print("- DELETE Docker host: " + docker_host_series.entity)
# Uncomment next line to delete
# entity_service.delete(docker_host_series.entity)