-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_maintenance.py
166 lines (136 loc) · 7.14 KB
/
table_maintenance.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python
#----------------------------------------------------------------------
# Copyright (c) 2013-2015 Raytheon BBN Technologies
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
#----------------------------------------------------------------------
# Script to maintain GEC22 data tables, archiving data older than 24 hours
import optparse
import os
import sys
import time
# The tables maintained by this script
maintained_tables = [
{'tablename' : 'bvermeulen_sdx', 'id_column' : 'id'},
{'tablename' : 'msb_metrics', 'id_column' : 'id'},
{'tablename' : 'nriganikytopo2', 'id_column' : 'id'},
{'tablename' : 'starlight_metrics', 'id_column' : 'id'},
{'tablename' : 'nmetrics_cpu', 'id_column' : 'oml_tuple_id'},
{'tablename' : 'nmetrics_memory', 'id_column' : 'oml_tuple_id'},
{'tablename' : 'nmetrics_network', 'id_column' : 'oml_tuple_id'}
]
class TableMaintainer():
def __init__(self):
self._opts = self.parse_options()
def add_parser_options(self, parser):
parser.add_option("--dbname", help="Name of database", default="gec22")
parser.add_option("--dbhost", help="host of database", default='localhost')
parser.add_option("--dbuser", help="Database user", default="oml2")
parser.add_option("--dbpass", help="Database password", default="0mlisg00d4u")
parser.add_option("--dbport", help="Database port", default="5432")
parser.add_option("--outfile", help="Database script filename")
parser.add_option("--dumpdir", help="Database dump directory")
parser.add_option('--frequency', help="How often to gather metrics in seconds",
default="3600")
parser.add_option("--window", help="Length of window of data maintained in tables",
default="86400")
# Check that all required parser arguments are provided
def check_options(self, opts):
required_missing_fields = []
if opts.outfile == None: required_missing_fields.append('outfile')
if opts.dumpdir == None: required_missing_fields.append('dumpdir')
if len(required_missing_fields) > 0:
raise Exception("Missing required arguments: " +
" ".join(required_missing_fields))
# Parse command line options
def parse_options(self):
argv = sys.argv[1:]
parser = optparse.OptionParser()
self.add_parser_options(parser)
[opts, args] = parser.parse_args(argv)
self.check_options(opts)
return opts
def dump_database(self, tablename, timestamp):
dumpfile = "%s-%s.sql" % (tablename, timestamp)
dumpfile = os.path.join(self._opts.dumpdir, dumpfile)
cmd = "PGPASSWORD=%s /usr/bin/pg_dump -U %s -h %s -t %s -f %s %s"
cmd = cmd % (self._opts.dbpass, self._opts.dbuser, self._opts.dbhost,
tablename, dumpfile, self._opts.dbname)
print "Dump command: %s" % (cmd)
os.system(cmd)
def run(self):
timestamp = int(time.time())
print "Running TableMaintainer at %d" % (timestamp)
sql_file = open(self._opts.outfile, 'wb')
# Start a transaction
sql_file.write('begin transaction;\n')
# Loop over all maintained tables
for table in maintained_tables:
tablename = table['tablename']
id_column = table['id_column']
# archive_tablename = "%s_ARCHIVE" % tablename
window = int(self._opts.window)
print "Backing up table %s" % (tablename)
self.dump_database(tablename, timestamp)
print "Maintaining table %s" % tablename
# Make sure there is an empty archive table (copy of table)
# create_stmt = "create table if not exists %s (like %s)" % (archive_tablename, tablename)
# sql_file.write(create_stmt + ";\n");
# Insert a record into table history
insert_stmt = "insert into table_history (timestamp, max_id, tablename) " +\
"values (%d, (select max(%s) from %s), '%s')" % \
(timestamp, id_column, tablename, tablename);
insert_stmt = "insert into table_history (timestamp, max_id, tablename) " +\
"values (%d, (select max(%s) from %s), '%s')" % \
(timestamp, id_column, tablename, tablename);
print "INSERT = %s" % insert_stmt
sql_file.write(insert_stmt + ";\n");
# Copy all rows with ID more than 24 hours old from data table into archive table
# copy_template = "insert into %s select * from %s where %s < " +\
# "(select max(max_id) from table_history where tablename = '%s' " + \
# "and timestamp = (select max(timestamp) from table_history " + \
# "where tablename = '%s' and timestamp < %d))"
# copy_stmt = copy_template % \
# (archive_tablename, tablename, id_column, tablename, tablename, (timestamp-window))
# sql_file.write(copy_stmt + ";\n");
# Delete all rows with ID more than 24 hours old from data table
delete_template = "delete from %s where %s < (select max(max_id) from table_history " + \
"where tablename = '%s' and timestamp = (select max(timestamp) " + \
"from table_history where tablename = '%s' and timestamp < %d))"
delete_stmt = delete_template % \
(tablename, id_column, tablename, tablename, (timestamp-window))
sql_file.write(delete_stmt + ";\n");
# Commit transaction
sql_file.write("commit;\n");
sql_file.close()
# Invoke SQL file
sql_cmd = "PGPASSWORD=%s psql -U %s -h %s %s -f %s" % \
(self._opts.dbpass, self._opts.dbuser, self._opts.dbhost,
self._opts.dbname, self._opts.outfile)
print "INVOKING CMD = %s" % sql_cmd
os.system(sql_cmd)
def main():
tbl_maint = TableMaintainer();
while True:
tbl_maint.run()
frequency = int(tbl_maint._opts.frequency)
time.sleep(frequency)
if __name__ == "__main__":
sys.exit(main())