summaryrefslogtreecommitdiffstats
path: root/src/lib/Server/Admin/Reports.py
blob: 1a233570319a92a0b675bd82b46c9556aa2428fb (plain)
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
import os, sys, binascii
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'] = '%s.settings' % project_name

from Bcfg2.Server.Reports.reports.models import Client
import Bcfg2.Server.Admin

def timecompare(client1, client2):
    return cmp(client1.current_interaction.timestamp, \
                   client2.current_interaction.timestamp)

class Reports(Bcfg2.Server.Admin.Mode):
    __shorthelp__ = 'bcfg2-admin reports'
    __longhelp__ = __shorthelp__ + '\n\t Command line interface for the reporting system'

    def __call__(self, args):
        Bcfg2.Server.Admin.Mode.__call__(self, args)
        if "-h" in args:
            print "Usage: "
            print self.__shorthelp__
            raise SystemExit(1)

        c_list = Client.objects.all()
        
        result = list()
        
        if '-c' in args or '-d' in args:    
            for c_inst in c_list:
                if '-c' in args and c_inst.current_interaction.isclean() or \
                        '-d' in args and not \
                        c_inst.current_interaction.isclean():
                    result.append(c_inst)
        else:
            result = c_list
                    
        if '-s' in args:
            result.sort(timecompare)
                        
        for c_inst in result:
            print c_inst, c_inst.current_interaction.timestamp