def handle(self, *args, **options): CollectorRegistry.load() if options.get('list_stats'): cols = CollectorRegistry.all() print 'Available Statistics:' for col in cols: print "{0:20} - {1}".format(col.stat.collector, col.desc) sys.exit(0) stats = options.get('stat') run_time = datetime.now() cur_col = None director = None if options.get('debug'): logger = logging.getLogger('django.db.backends') logger.setLevel(logging.DEBUG) try: with transaction.commit_on_success(): dbs = settings.TABLEIZER_DBS #Get all db settings except for the default txn_id = Snapshot.objects.get_next_txn() director = CollectionDirector(run_time) rds = {} for k,v in dbs.items(): host = v.get('HOST') if v.get('HOST') is not None and v.get('HOST') != '' else 'localhost' for coller in CollectorRegistry.all(): if stats is None or stats in coller.stat.collector: cur_col = coller rd = director.collect(host, coller) if rds.get(rd.stat) is None: rds[rd.stat] = [] print "NEW TXN: %d" % (txn_id) print "rd changed: %s" % (rd.changed) rds[rd.stat].append(rd) for k,v in rds.items(): changed = False for i in v: if i.changed: changed = True break if changed: for i in v: i.save(txn_id) except Exception, e: tb = traceback.format_exc() if settings.SEND_CRASHREPORTS: logger = logging.getLogger('tableizer') else: logger = logging.getLogger('management_command') logger.error(tb)
def execute(self): if len(self.args) < 2: print 'Need host to rename and the newname for it (try --help).' return 1 host = self.args.pop(0) newhost = self.args.pop(0) with transaction.commit_on_success(): s = Server.objects.find_by_name(host) s.name = newhost s.save() for col in CollectorRegistry.all(): n = col.stat.objects.filter(server=host).update(server=newhost) print 'Updated %d records in the %s table..' % (n, col.id) return 0
def handle(self, *args, **options): output_cfg = {} sql_conditions = {} find_type = 'normal' code = 0 CollectorRegistry.load() if options.get('list_stats'): cols = CollectorRegistry.all() print 'Available Statistics:' for col in cols: print "{0:20} - {1}".format(col.stat.collector, col.desc) sys.exit(0) since_regex = re.search('(\d+(?:\.?\d+)?)([hdwm])?', options.get('since', '')) if options.get('since') == 'last': find_type = 'last' elif since_regex is not None: num, unit = since_regex.groups() num = float(num) if unit == 'h': time = datetime.now() - timedelta(hours=num) elif unit == 'd': time = datetime.now() - timedelta(days=num) elif unit == 'w': time = datetime.now() - timedelta(weeks=num) elif unit == 'm': time = datetime.now() - timedelta(minutes=num) else: time = datetime.now() - timedelta(seconds=num) sql_conditions['since'] = time if options.get('where') is not None: sql_conditions['where'] = options.get('where') if options.get('group_by') is not None: sql_conditions['group'] = options.get('group_by') if options.get('select_columns') is not None: sql_conditions['select'] = options.get('select_columns') if options.get('order_by') is not None: sql_conditions['order'] = options.get('order_by') if options.get('limit') is not None: sql_conditions['limit'] = options.get('limit') for k,v in settings.REPORT_OPTIONS.items(): output_cfg[k] = v output_cfg['full'] = options.get('output_full') output_cfg['raw'] = options.get('output_raw') output_cfg['display_width'] = options.get('output_width') output = Formatter.get_runner_for(options.get('output'))(sys.stderr) try: Model = TrackingTable._TrackingTable__tables.get(options.get('stat', 'definition')) if find_type == 'normal': query = Model.objects.all() else: query = Model.objects.find_most_recent_versions(Model) if sql_conditions.get('since') is not None: query = query.filter(run_time__gte=sql_conditions.get('since')) if sql_conditions.get('where') is not None: query = query.extra(where=[sql_conditions.get('where'),]) if sql_conditions.get('order') is not None: cols = sql_conditions.get('order').split(',') query = query.order_by(*cols) # Do reject_ignores filter before limit if not output_cfg.get('raw', False): if settings.USE_INCLUDE_NOT_IGNORE: query = output.report_include(query) else: query = output.reject_ignores(query) if sql_conditions.get('limit') is not None: query = query[:sql_conditions.get('limit')] code = output.format(query, output_cfg) except Exception, e: tb = traceback.format_exc() if settings.SEND_CRASHREPORTS: logger = logging.getLogger('tableizer') else: logger = logging.getLogger('management_command') logger.error(tb)
def execute(self): args = self.args if len(args) < 2: print "Need a purge action(host/days). If purge action is host, pass hostnames to purge. If purge action is days, pass number of days to purge data older than the parameter." return 1 action = args.pop(0) if action not in ('host', 'days'): print "Invalid purge action. Choose between 'host' and 'days'" return 1 if action == 'host': if self.debug: print "Performing purge of %s" % (','.join(args)) with transaction.commit_on_success(): for host in args: if self.debug: print "Doing purge for %s" % (host) ### Clean out the references in the server tables s = Server.objects.find_by_name(host) schs = s.serverschema_set.all() tbls = DatabaseTable.objects.filter(schema__id__in=[x.id for x in schs]) n = tbls.count() tbls.delete() if self.debug: print "Deleted %d tables from the cache.." % (n) n = schs.count() schs.delete() if self.debug: print "Deleted %d schemas from the cache.." % (n) s.delete() for col in CollectorRegistry.all(): items = col.stat.objects.filter(server=host) n = items.count() items.delete() if self.debug: print "Deleted %d records from %s table.." % (n, col.id) cr_id = CollectorRun.objects.get_or_create(collector=col.id)[0].id stat_ids = col.stat.objects.all().values_list('id', flat=True) items = Snapshot.objects.filter(collector_run_id=cr_id).exclude(statistic_id__in=stat_ids) n = items.count() items.delete() if self.debug: print "Deleted %d %s records from the snapshots table.." % (n, col.id) elif action == 'days': days = int(args.pop()) if self.debug: print "Performing purge of data older than %d day/s." % days with transaction.commit_on_success(): dt = datetime.now() - timedelta(days=days) for col in CollectorRegistry.all(): items = col.stat.objects.filter(run_time__lte=dt) n = items.count() items.delete() if self.debug: print "Deleted %d records from %s table.." % (n, col.id) cr_id = CollectorRun.objects.get_or_create(collector=col.id)[0].id stat_ids = col.stat.objects.all().values_list('id', flat=True) items = Snapshot.objects.filter(collector_run_id=cr_id).exclude(statistic_id__in=stat_ids) n = items.count() items.delete() if self.debug: print "Deleted %d %s records from the snapshots table.." % (n, col.id) return 0