summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/sbin/bcfg2-reports158
1 files changed, 158 insertions, 0 deletions
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)