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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
#! /usr/bin/env python
'''Imports statistics.xml and clients.xml files in to database backend for new statistics engine'''
__revision__ = '$Revision: 4639 $'
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'] = '%s.settings' % project_name
from Bcfg2.Server.Reports.reports.models import Client
from getopt import getopt
import datetime
import Bcfg2.Server.Admin
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
class Reports(Bcfg2.Server.Admin.Mode):
def __call__(self, args):
Bcfg2.Server.Admin.Mode.__call__(self, args)
if "-h" in args:
print "Usage: "
print self.__shorthelp__
raise SystemExit(1)
fields = ""
sort = ""
badentry = ""
expire = ""
c_list = Client.objects.all()
result = list()
try:
opts, pargs = getopt(args, 'cdx:', ['sort=', 'fields='])
except:
print "error"
print self.__shorthelp__
raise SystemExit(1)
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)
|