diff options
author | Leonardo Arena <rnalrd@alpinelinux.org> | 2018-07-25 09:49:12 +0000 |
---|---|---|
committer | Leonardo Arena <rnalrd@alpinelinux.org> | 2018-07-25 09:49:22 +0000 |
commit | bd3340f2e7fa5825da7f313622faef686cda17ab (patch) | |
tree | 9a9300e350e01064d90642145a032e34d2e6d75b /non-free/unifi/mongo_prune.js | |
parent | a31b8a8b2a3ab95945396c81089bed44fd89f6f6 (diff) | |
download | aports-bd3340f2e7fa5825da7f313622faef686cda17ab.tar.bz2 aports-bd3340f2e7fa5825da7f313622faef686cda17ab.tar.xz |
non-free/unifi: add DB cron job
(disabled by default)
Diffstat (limited to 'non-free/unifi/mongo_prune.js')
-rw-r--r-- | non-free/unifi/mongo_prune.js | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/non-free/unifi/mongo_prune.js b/non-free/unifi/mongo_prune.js new file mode 100644 index 0000000000..421c3f1438 --- /dev/null +++ b/non-free/unifi/mongo_prune.js @@ -0,0 +1,87 @@ +// keep N-day worth of data +var days=14; + +// change to false to have the script to really exclude old records +// from the database. While true, no change at all will be made to the DB +var dryrun=true; + +var now = new Date().getTime(), + time_criteria = now - days * 86400 * 1000, + time_criteria_in_seconds = time_criteria / 1000; + +print((dryrun ? "[dryrun] " : "") + "pruning data older than " + days + " days (" + time_criteria + ")... "); + +use ace; +var collectionNames = db.getCollectionNames(); +for (i=0; i<collectionNames.length; i++) { + var name = collectionNames[i]; + var query = null; + + if (name === 'event' || name === 'alarm') { + query = {time: {$lt:time_criteria}}; + } + + // rogue ap + if (name === 'rogue') { + query = {last_seen: {$lt:time_criteria_in_seconds}}; + } + + // removes vouchers expired more than '$days' ago + // active and unused vouchers are NOT touched + if (name === 'voucher') { + query = {end_time: {$lt:time_criteria_in_seconds}}; + } + + // guest authorization + if (name === 'guest') { + query = {end: {$lt:time_criteria_in_seconds}}; + } + + // if an user was only seen ONCE, $last_seen will not be defined + // so, if $last_seen not defined, lets use $first_seen instead + // also check if $blocked or $use_fixedip is set. If true, do NOT purge the + // entry no matter how old it is. We want blocked/fixed_ip users to continue + // blocked/fixed_ip + if (name === 'user') { + query = { blocked: { $ne: true}, use_fixedip: { $ne: true}, $or: [ + {last_seen: {$lt:time_criteria_in_seconds} }, + {last_seen: {$exists: false}, first_seen: {$lt:time_criteria_in_seconds} } + ] + }; + } + + if (query) { + count1 = db.getCollection(name).count(); + count2 = db.getCollection(name).find(query).count(); + print((dryrun ? "[dryrun] " : "") + "pruning " + count2 + " entries (total " + count1 + ") from " + name + "... "); + if (!dryrun) { + db.getCollection(name).remove(query); + db.runCommand({ compact: name }); + } + } +} + +use ace_stat; +var collectionNames = db.getCollectionNames(); +for (i=0; i<collectionNames.length; i++) { + var name = collectionNames[i]; + var query = null; + + // historical stats (stat.*) + if (name.indexOf('stat')==0) { + query = {time: {$lt:time_criteria}}; + } + + if (query) { + count1 = db.getCollection(name).count(); + count2 = db.getCollection(name).find(query).count(); + print((dryrun ? "[dryrun] " : "") + "pruning " + count2 + " entries (total " + count1 + ") from " + name + "... "); + if (!dryrun) { + db.getCollection(name).remove(query); + db.runCommand({ compact: name }); + } + } +} + +if (!dryrun) db.repairDatabase(); + |