-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwatchdog.py
executable file
·84 lines (71 loc) · 2.45 KB
/
watchdog.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
import os
import sys
import json
import time
import psycopg2
import logging
import logging.handlers
import smtplib
import requests
from docker import Client
import mydb.admin_db as admin_db
import mydb.postgres_util as postgres
import mydb.mariadb_util as mariadb
import mydb.mongodb_util as mongodb
import mydb.container_util as container_util
import mydb.config as config
"""Verify that containers are in running state.
get list of containers from docker
get list of containers from admin_db
--> both lists should match <--
export DOCKER_CLIENT_TIMEOUT=120
"""
def check_directories(dirs, path, admin_list):
for dir in dirs:
if dir not in admin_list:
print('container volume with no container: %s/%s' % (path,dir))
def check_storage(admin_list):
db_dirs = os.listdir(config.var.dbvol)
check_directories(db_dirs, config.var.dbvol, admin_list)
backup_dirs = os.listdir(config.var.backupvol)
check_directories(backup_dirs, config.var.backupvol, admin_list)
def watch_dog():
dbtype = 'Postgres'
logging.info("Watchdog begining")
t = time.localtime()
ts = "%s-%02d-%02d_%02d-%02d-%02d" % (t[0], t[1], t[2], t[3], t[4], t[5])
cli = Client(base_url=config.var.base_url)
docker_containers = cli.containers()
admin_list = admin_db.list_containers()
inspect = {}
docker_list = []
for con in docker_containers:
name = con['Names'][0][1:]
docker_list.append(name)
inspect[name] = 'running'
containers = []
for (c_id, con_name) in admin_list:
containers.append(con_name)
data = admin_db.get_container_data('', c_id)
info= data['Info']
dbname = info['Name']
info['username'] = 'cron'
if con_name in docker_list:
result = container_util.exec_command(dbname, 'uptime')
else:
print('%s is listed as running but is not!' % con_name)
continue
if 'load average' not in result:
print('(%d) %s is not running' % (c_id, dbname))
continue
if con_name in inspect:
inspect[con_name] = 'found'
print("%-30s %-10s %-35s %s" % (info['Name'], info['dbengine'], info['Image'], info['BACKUP_FREQ']))
for con in inspect:
if inspect[con] == 'running':
print('%s is running but not in admin' % con)
return containers
if __name__ == "__main__":
container_list = watch_dog()
check_storage(container_list)