-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-sensor.py
executable file
·384 lines (334 loc) · 12.7 KB
/
delete-sensor.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env python
# # -*- coding: utf-8 -*-
""" Smogdance
An open-source collection of scripts to collect, store and graph air quality data
from publicly available sources.
This removes a sensor from disk and database:
- moves the sensor record from sensors to sensors_deleted in the db
- moves all historical data associated with the sensor from sensors_data to sensors_data_deleted
- deletes sensor spiderfile from the disk
- recreates the city's sensor local id's (or deletes the city spider dir if only sensor)
- recreates the city's mrtg directory (or deletes it if only sensor)
gnd, 2017 - 2020
"""
import re
import os
import sys
import time
import shlex
import MySQLdb
import datetime
import subprocess
import ConfigParser
### load config
settings_file = os.path.join(sys.path[0], 'settings_python')
config = ConfigParser.ConfigParser()
config.readfp(open(settings_file))
DATA_DIR = config.get('globals', 'DATA_DIR')
TEMP_DIR = config.get('globals', 'TEMP_DIR')
STATS_DIR = config.get('globals', 'STATS_DIR')
STATS_DIR_DEL = config.get('globals', 'STATS_DIR_DEL')
SPIDER_DIR = config.get('globals', 'SPIDER_DIR')
MRTG_TEMPLATE = config.get('globals', 'MRTG_TEMPLATE')
SPIDER_TEMPLATE = config.get('globals', 'SPIDER_TEMPLATE')
SPIDER_COMMAND = config.get('globals', 'SPIDER_COMMAND')
### connect to the db
DB_HOST = config.get('database', 'DB_HOST')
DB_USER = config.get('database', 'DB_USER')
DB_PASS = config.get('database', 'DB_PASS')
DB_NAME = config.get('database', 'DB_NAME')
DB_TABLE = config.get('database', 'DB_TABLE')
DB_TABLE_DEL = config.get('database', 'DB_TABLE_DEL')
DATA_TABLE = config.get('database', 'DATA_TABLE')
DATA_TABLE_DEL = config.get('database', 'DATA_TABLE_DEL')
db = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASS, db=DB_NAME, use_unicode=True, charset="utf8")
cur = db.cursor()
### Import Smog functions
from smog_functions import *
def usage():
print "Usage: delete-sensor.py <sensor_id>"
### check input
sensor_id = ""
if (len(sys.argv) > 1):
if (sys.argv[1].isdigit()):
sensor_id = int(sys.argv[1])
else:
sys.exit("Sensor id must be an integer")
usage()
sys.exit()
else:
print("Not enough parameters")
usage()
sys.exit()
#####
##### Get sensor params
#####
query = "SELECT local_id, name, city, country, substances FROM %s where id = '%d'" % (DB_TABLE, sensor_id)
cur.execute(query)
for line in cur.fetchall():
local_id = line[0]
name = line[1]
city = line[2]
country = line[3]
substances = line[4]
city_dir = city.replace(" ", "_")
#####
##### Move sensor definition into sensors_deleted
#####
query = "INSERT INTO %s SELECT * FROM %s WHERE id = '%d'" % (DB_TABLE_DEL, DB_TABLE, sensor_id)
try:
print "Moving db sensor definitions"
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
db.commit()
#####
##### Delete sensor definition from sensors
#####
query = "DELETE FROM %s WHERE id = '%d'" % (DB_TABLE, sensor_id)
try:
print "Deleting db sensor definitions"
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
db.commit()
#####
##### Move the sensor data into sensor_data_deleted
#####
query = "INSERT INTO %s SELECT * FROM %s WHERE sensor_id = '%d'" % (DATA_TABLE_DEL, DATA_TABLE, sensor_id)
try:
print "Moving db sensor data"
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
db.commit()
#####
##### Delete the sensor data
#####
query = "DELETE FROM %s WHERE sensor_id = '%d'" % (DATA_TABLE, sensor_id)
try:
print "Deleting db sensor data"
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
db.commit()
#####
##### Delete spider file from disk
#####
spider_file = "%s/%s/%s/%s.py" % (SPIDER_DIR, country, city_dir, local_id)
try:
print "Removing spider file %s" % (spider_file)
os.remove(spider_file)
except:
sys.exit("Something went wrong when removing: " + spider_file)
#####
##### Deterine city_substances
#####
city_substances = []
query = "SELECT substances FROM %s WHERE city = '%s' ORDER BY local_id" % (DB_TABLE, city)
try:
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
for line in cur.fetchall():
temp_substances = line[0].split()
for temp_substance in temp_substances:
if temp_substance not in city_substances:
city_substances.append(temp_substance)
print "Substances for %s: %s" % (city, city_substances)
#####
##### Recreate or delete local ids, spider files
#####
# Determine how many sensors from the city are left
query = "SELECT count(id) FROM %s WHERE city = '%s'" % (DB_TABLE, city)
try:
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
res = cur.fetchone()
sensor_count = res[0]
print "%d sensors remaining for %s" % (sensor_count, city)
# If this wasnt the last sensor in the city, renumber the leftover sensors
if ((sensor_count > 0) and (local_id < sensor_count)):
id_mapping = {}
new_local_id = 0
query = "SELECT id, local_id FROM %s WHERE city = '%s' ORDER BY local_id" % (DB_TABLE, city)
try:
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
for line in cur.fetchall():
temp_sensor_id = line[0]
old_local_id = line[1]
query = "UPDATE %s SET local_id = %d WHERE id = '%d'" % (DB_TABLE, new_local_id, temp_sensor_id)
try:
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
db.commit()
# Delete old spiderfiles
sensor_city_dir = "%s/%s/%s" % (SPIDER_DIR, country, city_dir)
files = [f for f in os.listdir(sensor_city_dir) if os.path.isfile(os.path.join(sensor_city_dir, f))]
for file in files:
if ((".py" in file) or (".pyc" in file)):
os.remove("%s/%s" % (sensor_city_dir, file))
# store mapping
id_mapping[old_local_id] = new_local_id
new_local_id += 1
# If this was the last sensor, delete the directory and all its contents
if (sensor_count == 0):
sensor_city_dir = "%s/%s/%s" % (SPIDER_DIR, country, city_dir)
files = [f for f in os.listdir(sensor_city_dir) if os.path.isfile(os.path.join(sensor_city_dir, f))]
for file in files:
os.remove("%s/%s" % (sensor_city_dir, file))
os.rmdir(sensor_city_dir)
#####
##### Regenerate spiderfiles
#####
if ((sensor_count > 0) and (local_id < sensor_count)):
query = "SELECT local_id, name, link_src, link_xpaths FROM %s WHERE city = '%s' ORDER BY id" % (DB_TABLE, city)
try:
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
### create new sensor spider on disk
for line in cur.fetchall():
temp_local_id = line[0]
temp_name = line[1]
temp_link_src = line[2]
temp_link_xpaths = line[3]
temp_spider_file = "%s/%s/%s/%s.py" % (SPIDER_DIR, country, city_dir, temp_local_id)
template = fill_spider_template(TEMP_DIR, SPIDER_TEMPLATE, temp_name, temp_link_src, temp_link_xpaths)
write_template(temp_spider_file, template)
#####
##### Regenerate mrtg config files with new local ids
#####
if (sensor_count > 0):
# Delete old mrtg config files
for substance in city_substances:
mrtg_name = "%s.cfg" % (substance)
mrtg_file = "%s/%s/%s/%s" % (SPIDER_DIR, country, city_dir, mrtg_name)
print "Deleting old mrtg config: %s" % (mrtg_file)
if os.path.isfile(mrtg_file):
os.remove(mrtg_file)
# Now create new mrtg config files
query = "SELECT id, local_id, name, substances FROM %s WHERE city = '%s' ORDER BY id" % (DB_TABLE, city)
try:
cur.execute(query)
except:
sys.exit("Something went wrong: " + query)
### create new sensor spider on disk
for line in cur.fetchall():
temp_sensor_id = line[0]
temp_local_id = line[1]
temp_name = line[2]
temp_substances = line[3]
for temp_substance in temp_substances.split():
mrtg_name = "%s.cfg" % (temp_substance)
print "Recreating %s" % (mrtg_name)
mrtg_workdir = "%s/%s/%s/" %(STATS_DIR, country, city_dir)
mrtg_file = "%s/%s/%s/%s" % (SPIDER_DIR, country, city_dir, mrtg_name)
template = fill_mrtg_template(DATA_DIR, SPIDER_COMMAND, MRTG_TEMPLATE, temp_sensor_id, temp_local_id, temp_name, city, country, temp_substance)
write_mrtg_template(mrtg_file, mrtg_workdir, template)
#####
##### Remove city from mrtg runlist if last sensor
#####
if (sensor_count == 0):
runlist = "%s/%s" % (DATA_DIR, 'mrtg.runlist')
if (os.path.isfile(runlist)):
# Read runlist
f = open(runlist, 'r')
lines = f.readlines()
f.close()
# Write new runlist
f = open(runlist, 'w')
for line in lines:
runlist_name = "%s/%s\n" % (country, city_dir)
if (runlist_name not in line):
f.write(line)
f.close()
#####
##### Move mrtg data for deleted sensor
#####
# prepare directories
if (not os.path.isdir("%s" % (STATS_DIR_DEL))):
os.makedirs("%s" % (STATS_DIR_DEL))
if (not os.path.isdir("%s/%s" % (STATS_DIR_DEL, country))):
os.makedirs("%s/%s" % (STATS_DIR_DEL, country))
if (not os.path.isdir("%s/%s/%s" % (STATS_DIR_DEL, country, city_dir))):
os.makedirs("%s/%s/%s" % (STATS_DIR_DEL, country, city_dir))
# Now move all files called like city-local_id*
for sub in substances:
old_dir = "%s/%s/%s" % (STATS_DIR, country, city_dir)
new_dir = "%s/%s/%s" % (STATS_DIR_DEL, country, city_dir)
pattern = "%s-%s_%s" % (city, local_id, sub)
files = [f for f in os.listdir(old_dir) if os.path.isfile(os.path.join(old_dir, f))]
for file in files:
if (pattern in file):
print "Moving %s" % (file)
os.rename("%s/%s" % (old_dir, file), "%s/%s" % (new_dir, file))
#####
##### Rename mrtg data for leftover sensors
#####
if ((sensor_count > 0) and (local_id < sensor_count)):
old_dir = "%s/%s/%s" % (STATS_DIR, country, city_dir)
for old_id, new_id in id_mapping.iteritems():
pattern = "%s-%s.*\.(png|log|old)" % (city, old_id)
files = [f for f in os.listdir(old_dir) if os.path.isfile(os.path.join(old_dir, f))]
for file in files:
if re.match(pattern, file):
name_root = "%s-%s" % (city, new_id)
name_tail = file.split('_')[1]
new_name = "%s_%s" % (name_root, name_tail)
print "Renaming %s to %s" % (file, new_name)
os.rename("%s/%s" % (old_dir, file), "%s/%s" % (old_dir, new_name))
#####
##### Recreate mrtg stats for city
#####
### create index with indexmaker
if (sensor_count > 0):
mrtg_statdir = "%s/%s/%s/" %(STATS_DIR, country, city_dir)
# remove old index files
for file in os.listdir(mrtg_statdir):
if file.endswith(".html"):
os.remove("%s/%s" % (mrtg_statdir, file))
# Create new index files
for substance in city_substances:
links = ""
index_sub = 'pm10'
if 'pm10' in city_substances:
index_sub = 'pm10'
elif 'pm25' in city_substances:
index_sub = 'pm25'
elif 'co' in city_substances:
index_sub = 'co'
elif 'o3' in city_substances:
index_sub = 'o3'
for sub in city_substances:
if (sub != substance):
if (sub == index_sub):
links += "<a href=index.html>%s</a> " % (sub.upper())
else:
links += "<a href=%s.html>%s</a> " % (sub, sub.upper())
if (substance == index_sub):
command = "indexmaker --output=%s/%s/%s/%s.html --title=\"%s %s Levels (%s)\" --nolegend %s/%s/%s/%s.cfg" % (STATS_DIR, country, city_dir, "index", city.capitalize(), substance.upper(), links, SPIDER_DIR, country, city_dir, substance)
else:
command = "indexmaker --output=%s/%s/%s/%s.html --title=\"%s %s Levels (%s)\" --nolegend %s/%s/%s/%s.cfg" % (STATS_DIR, country, city_dir, substance, city.capitalize(), substance.upper(), links, SPIDER_DIR, country, city_dir, substance)
command_args = shlex.split(command)
### run indexmaker
try:
process = subprocess.Popen(command_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out = process.communicate()
except subprocess.CalledProcessError as e:
print e
out = "UGH"
pass
### if non-zero return, we have a problem
if (out[1] != ""):
print "Something went wrong: %s" % (out[1])
else:
print "mrtg index for %s created" % (substance)
#####
##### Done
#####
print "Sensor %s from %s with local_id %d and id %d deleted." % (name, city, local_id, sensor_id)