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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
#! /usr/bin/env python
"""Query reporting system for client status."""
__revision__ = '$Revision$'
import os
import sys
try:
import Bcfg2.Server.Reports.settings
except ConfigParser.NoSectionError:
print("Your bcfg2.conf is currently missing the statistics section which "
"is necessary for the reporting interface. Please see bcfg2.conf(5) "
"for more details.")
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 fileinput
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, entrydict):
"""
Prints the fields specified in fields of cli, max_name
specifies the column width of the name column.
"""
fmt = ''
for field in fields:
if field == 'name':
fmt += ("%%-%ds " % (max_name))
else:
fmt += "%s "
fdata = []
for field in fields:
if field == 'time':
fdata.append(str(cli.current_interaction.timestamp))
elif field == 'state':
if cli.current_interaction.isclean():
fdata.append("clean")
else:
fdata.append("dirty")
else:
try:
fdata.append(getattr(cli, field))
except:
fdata.append("N/A")
display = fmt % tuple(fdata)
if len(entrydict) > 0:
display += " "
display += str(entrydict[cli])
print display
def print_entry(item, max_name):
fmt = ("%%-%ds " % (max_name))
fdata = item.entry.kind + ":" + item.entry.name
display = fmt % (fdata)
print display
fields = ""
sort = ""
badentry = ""
extraentry = ""
expire = ""
singlehost = ""
c_list = Client.objects.all()
result = list()
entrydict = dict()
args = sys.argv[1:]
opts, pargs = getopt(args, 'ab:cde:hs:x:',
['stale', 'sort=', 'fields=', 'badentry=', 'extraentry='])
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] == '--extraentry':
extraentry = option[1]
if option[0] == '-x':
expire = option[1]
if option[0] == '-s' or option[0] == '-b' or option[0] == '-e':
singlehost = 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()
print "Host expired."
else:
c_inst.expiration = None
print "Host un-expired."
c_inst.save()
elif '-h' in args:
print """Usage: bcfg2-reports [option] ...
Options and arguments (and corresponding environment variables):
-a : shows all hosts, including expired hosts
-b NAME : single-host mode - shows bad entries from the
current interaction of NAME
-c : shows only clean hosts
-d : shows only dirty hosts
-e NAME : single-host mode - shows extra entries from the
current interaction of NAME
-h : shows help and usage info about bcfg2-reports
-s NAME : single-host mode - shows bad and extra entries from
the current interaction of NAME
-x NAME : toggles expired/unexpired state of NAME
--badentry=KIND,NAME : shows only hosts whose current interaction has bad
entries in of KIND kind and NAME name; if a single
argument ARG1 is given, then KIND,NAME pairs will be
read from a file of name ARG1
--extraentry=KIND,NAME : shows only hosts whose current interaction has extra
entries in of KIND kind and NAME name; if a single
argument ARG1 is given, then KIND,NAME pairs will be
read from a file of name ARG1
--fields=ARG1,ARG2,... : only displays the fields ARG1,ARG2,...
(name,time,state)
--sort=ARG1,ARG2,... : sorts output on ARG1,ARG2,... (name,time,state)
--stale : shows hosts which haven't run in the last 24 hours
"""
elif singlehost != "":
for c_inst in c_list:
if singlehost == c_inst.name:
baditems = c_inst.current_interaction.bad()
if len(baditems) > 0 and ('-b' in args or '-s' in args):
print "Bad Entries:"
max_name = -1
for item in baditems:
if len(item.entry.name) > max_name:
max_name = len(item.entry.name)
for item in baditems:
print_entry(item, max_name)
extraitems = c_inst.current_interaction.extra()
if len(extraitems) > 0 and ('-e' in args or '-s' in args):
print "Extra Entries:"
max_name = -1
for item in extraitems:
if len(item.entry.name) > max_name:
max_name = len(item.entry.name)
for item in extraitems:
print_entry(item, max_name)
else:
if fields == "":
fields = ['name', 'time', 'state']
else:
fields = fields.split(',')
if sort != "":
sort = sort.split(',')
if badentry != "":
badentry = badentry.split(',')
if extraentry != "":
extraentry = extraentry.split(',')
# stale hosts
if '--stale' in args:
for c_inst in c_list:
if c_inst.current_interaction.isstale():
result.append(c_inst)
# clean hosts
elif '-c' in args:
for c_inst in c_list:
if c_inst.current_interaction.isclean():
result.append(c_inst)
# dirty hosts
elif '-d' in args:
for c_inst in c_list:
if not c_inst.current_interaction.isclean():
result.append(c_inst)
elif badentry != "":
if len(badentry) == 1:
fileread = fileinput.input(badentry[0])
for line in fileread:
badentry = line.strip().split(',')
for c_inst in c_list:
baditems = c_inst.current_interaction.bad()
for item in baditems:
if item.name == badentry[1] and item.kind == badentry[0]:
result.append(c_inst)
if c_inst in entrydict:
entrydict.get(c_inst).append(badentry[1])
else:
entrydict[c_inst] = [badentry[1]]
break
else:
for c_inst in c_list:
baditems = c_inst.current_interaction.bad()
for item in baditems:
if item.name == badentry[1] and item.kind == badentry[0]:
result.append(c_inst)
break
elif extraentry != "":
if len(extraentry) == 1:
fileread = fileinput.input(extraentry[0])
for line in fileread:
extraentry = line.strip().split(',')
for c_inst in c_list:
extraitems = c_inst.current_interaction.extra()
for item in extraitems:
if item.name == extraentry[1] and item.kind == extraentry[0]:
result.append(c_inst)
if c_inst in entrydict:
entrydict.get(c_inst).append(extraentry[1])
else:
entrydict[c_inst] = [extraentry[1]]
break
else:
for c_inst in c_list:
extraitems = c_inst.current_interaction.extra()
for item in extraitems:
if item.name == extraentry[1] and item.kind == extraentry[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:
if '-a' in args or c_inst.expiration == None:
print_fields(fields, c_inst, max_name, entrydict)
|