From d38bb5eb6be3a858abc82107b59b3cedc3da45c8 Mon Sep 17 00:00:00 2001 From: Narayan Desai Date: Sat, 12 Jul 2008 00:14:29 +0000 Subject: Add report client script in a directly executable location (works around weird django bug) git-svn-id: https://svn.mcs.anl.gov/repos/bcfg/trunk/bcfg2@4777 ce84e21b-d406-0410-9b95-82705330c041 --- src/sbin/bcfg2-reports | 158 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100755 src/sbin/bcfg2-reports (limited to 'src/sbin/bcfg2-reports') diff --git a/src/sbin/bcfg2-reports b/src/sbin/bcfg2-reports new file mode 100755 index 000000000..a141dcb1d --- /dev/null +++ b/src/sbin/bcfg2-reports @@ -0,0 +1,158 @@ +#!/usr/bin/env python +'''query reports system''' +__revision__ = '$Revision: $' + +import os, sys +try: + import Bcfg2.Server.Reports.settings +except: + sys.stderr.write("Failed to load configuration settings. is /etc/bcfg2.conf readable?") + sys.exit(1) + +project_directory = os.path.dirname(Bcfg2.Server.Reports.settings.__file__) +project_name = os.path.basename(project_directory) +sys.path.append(os.path.join(project_directory, '..')) +project_module = __import__(project_name, '', '', ['']) +sys.path.pop() +# Set DJANGO_SETTINGS_MODULE appropriately. +os.environ['DJANGO_SETTINGS_MODULE'] = 'Bcfg2.Server.Reports.settings' + +from Bcfg2.Server.Reports.reports.models import Client +from getopt import getopt +import datetime + +def timecompare(client1, client2): + '''compares two clients by their timestamps''' + return cmp(client1.current_interaction.timestamp, \ + client2.current_interaction.timestamp) + +def namecompare(client1, client2): + '''compares two clients by their names''' + return cmp(client1.name, client2.name) + +def statecompare(client1, client2): + '''compares two clients by their states''' + clean1 = client1.current_interaction.isclean() + clean2 = client2.current_interaction.isclean() + + if clean1 and not clean2: + return -1 + elif clean2 and not clean1: + return 1 + else: + return 0 + +def crit_compare(criterion, client1, client2): + '''compares two clients by the criteria provided in criterion''' + for crit in criterion: + comp = 0 + if crit == 'name': + comp = namecompare(client1, client2) + elif crit == 'state': + comp = statecompare(client1, client2) + elif crit == 'time': + comp = timecompare(client1, client2) + + if comp != 0: + return comp + + return 0 + +def print_fields(fields, cli, max_name): + '''prints the fields specified in fields of cli, max_name specifies the column width of the name column''' + display = "" + if 'name' in fields: + display += cli.name + for i in range(len(cli.name), max_name): + display += " " + if 'time' in fields: + display += " " + display += str(cli.current_interaction.timestamp) + if 'state' in fields: + display += " " + if cli.current_interaction.isclean(): + display += "clean" + else: + display += "dirty" + print display + + +if __name__ == '__main__': + fields = "" + sort = "" + badentry = "" + expire = "" + + c_list = Client.objects.all() + + result = list() + + args = sys.argv[1:] + opts, pargs = getopt(args, 'cdx:', ['sort=', 'fields=', 'badentry=']) + + for option in opts: + if len(option) > 0: + if option[0] == '--fields': + fields = option[1] + if option[0] == '--sort': + sort = option[1] + if option[0] == '--badentry': + badentry = option[1] + if option[0] == '-x': + expire = option[1] + + if expire != "": + for c_inst in c_list: + if expire == c_inst.name: + if c_inst.expiration == None: + c_inst.expiration = datetime.datetime.now() + else: + c_inst.expiration = None + c_inst.save() + + else: + if fields == "": + fields = ['name', 'time', 'state'] + else: + fields = fields.split(',') + + if sort != "": + sort = sort.split(',') + + if badentry != "": + badentry = badentry.split(',') + + if '-c' in args: + for c_inst in c_list: + if c_inst.current_interaction.isclean(): + result.append(c_inst) + + elif '-d' in args: + for c_inst in c_list: + if not c_inst.current_interaction.isclean(): + result.append(c_inst) + + elif badentry != "": + for c_inst in c_list: + baditems = c_inst.current_interaction.bad_items.all() + for item in baditems: + if item.name == badentry[1] and item.kind == badentry[0]: + result.append(c_inst) + break + + else: + for c_inst in c_list: + result.append(c_inst) + + max_name = -1 + if 'name' in fields: + for c_inst in result: + if len(c_inst.name) > max_name: + max_name = len(c_inst.name) + + if sort != "": + result.sort(lambda x, y: crit_compare(sort, x, y)) + + if fields != "": + for c_inst in result: + print_fields(fields, c_inst, max_name) -- cgit v1.2.3-1-g7c22