summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Reports/importscript.py
blob: 61f0c103c73284929f2d5a6f3621d8b6ceb225e6 (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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#! /usr/bin/env python
"""
Imports statistics.xml and clients.xml files in to database backend for
new statistics engine
"""

import binascii
import os
import sys
import traceback
try:
    import Bcfg2.settings
except Exception:
    e = sys.exc_info()[1]
    sys.stderr.write("Failed to load configuration settings. %s\n" % e)
    sys.exit(1)

project_directory = os.path.dirname(Bcfg2.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 *
from lxml.etree import XML, XMLSyntaxError
from getopt import getopt, GetoptError
from datetime import datetime
from time import strptime
from django.db import connection, transaction
from Bcfg2.Server.Plugins.Metadata import ClientMetadata
import logging
import Bcfg2.Logger
import platform

# Compatibility import
from Bcfg2.Bcfg2Py3k import ConfigParser


def build_reason_kwargs(r_ent, encoding, logger):
    binary_file = False
    sensitive_file = False
    unpruned_entries = ''
    if r_ent.get('sensitive') in ['true', 'True']:
        sensitive_file = True
        rc_diff = ''
    elif r_ent.get('current_bfile', False):
        binary_file = True
        rc_diff = r_ent.get('current_bfile')
        if len(rc_diff) > 1024 * 1024:
            rc_diff = ''
        elif len(rc_diff) == 0:
            # No point in flagging binary if we have no data
            binary_file = False
    elif r_ent.get('current_bdiff', False):
        rc_diff = binascii.a2b_base64(r_ent.get('current_bdiff'))
    elif r_ent.get('current_diff', False):
        rc_diff = r_ent.get('current_diff')
    else:
        rc_diff = ''
    # detect unmanaged entries in pruned directories
    if r_ent.get('prune', 'false') == 'true' and r_ent.get('qtest'):
        unpruned_elist = [e.get('path') for e in r_ent.findall('Prune')]
        unpruned_entries = "\n".join(unpruned_elist)
    if not binary_file:
        try:
            rc_diff = rc_diff.decode(encoding)
        except:
            logger.error("Reason isn't %s encoded, cannot decode it" % encoding)
            rc_diff = ''
    return dict(owner=r_ent.get('owner', default=""),
                current_owner=r_ent.get('current_owner', default=""),
                group=r_ent.get('group', default=""),
                current_group=r_ent.get('current_group', default=""),
                perms=r_ent.get('perms', default=""),
                current_perms=r_ent.get('current_perms', default=""),
                status=r_ent.get('status', default=""),
                current_status=r_ent.get('current_status', default=""),
                to=r_ent.get('to', default=""),
                current_to=r_ent.get('current_to', default=""),
                version=r_ent.get('version', default=""),
                current_version=r_ent.get('current_version', default=""),
                current_exists=r_ent.get('current_exists', default="True").capitalize() == "True",
                current_diff=rc_diff,
                is_binary=binary_file,
                is_sensitive=sensitive_file,
                unpruned=unpruned_entries)

def _fetch_reason(elem, kargs, logger):
    try:
        rr = None
        try:
            rr = Reason.objects.filter(**kargs)[0]
        except IndexError:
            rr = Reason(**kargs)
            rr.save()
            logger.debug("Created reason: %s" % rr.id)
    except Exception:
        ex = sys.exc_info()[1]
        logger.error("Failed to create reason for %s: %s" % (elem.get('name'), ex))
        rr = Reason(current_exists=elem.get('current_exists',
            default="True").capitalize() == "True")
        rr.save()
    return rr


def load_stats(sdata, encoding, vlevel, logger, quick=False, location=''):
    for node in sdata.findall('Node'):
        name = node.get('name')
        for statistics in node.findall('Statistics'):
            try:
                load_stat(name, statistics, encoding, vlevel, logger, quick, location)
            except:
                logger.error("Failed to create interaction for %s: %s" %
                    (name, traceback.format_exc().splitlines()[-1]))

@transaction.commit_on_success
def load_stat(cobj, statistics, encoding, vlevel, logger, quick, location):
    if isinstance(cobj, ClientMetadata):
        client_name = cobj.hostname
    else:
        client_name = cobj
    client, created = Client.objects.get_or_create(name=client_name)
    if created and vlevel > 0:
        logger.info("Client %s added to db" % client_name)

    timestamp = datetime(*strptime(statistics.get('time'))[0:6])
    ilist = Interaction.objects.filter(client=client,
                                       timestamp=timestamp)
    if ilist:
        current_interaction = ilist[0]
        if vlevel > 0:
            logger.info("Interaction for %s at %s with id %s already exists" % \
                (client.id, timestamp, current_interaction.id))
        return
    else:
        newint = Interaction(client=client,
                             timestamp=timestamp,
                             state=statistics.get('state',
                                                  default="unknown"),
                             repo_rev_code=statistics.get('revision',
                                                          default="unknown"),
                             goodcount=statistics.get('good',
                                                      default="0"),
                             totalcount=statistics.get('total',
                                                       default="0"),
                             server=location)
        newint.save()
        current_interaction = newint
        if vlevel > 0:
            logger.info("Interaction for %s at %s with id %s INSERTED in to db" % (client.id,
                timestamp, current_interaction.id))

    if isinstance(cobj, ClientMetadata):
        try:
            imeta = InteractionMetadata(interaction=current_interaction)
            profile, created = Group.objects.get_or_create(name=cobj.profile)
            imeta.profile = profile
            imeta.save() # save here for m2m

            #FIXME - this should be more efficient
            group_set = []
            for group_name in cobj.groups:
                group, created = Group.objects.get_or_create(name=group_name)
                if created:
                    logger.debug("Added group %s" % group)
                imeta.groups.add(group)
            for bundle_name in cobj.bundles:
                bundle, created = Bundle.objects.get_or_create(name=bundle_name)
                if created:
                    logger.debug("Added bundle %s" % bundle)
                imeta.bundles.add(bundle)
            imeta.save()
        except:
            logger.error("Failed to save interaction metadata for %s: %s" %
                (client_name, traceback.format_exc().splitlines()[-1]))


    entries_cache = {}
    [entries_cache.__setitem__((e.kind, e.name), e) \
        for e in Entries.objects.all()]
    counter_fields = {TYPE_BAD: 0,
                      TYPE_MODIFIED: 0,
                      TYPE_EXTRA: 0}
    pattern = [('Bad/*', TYPE_BAD),
               ('Extra/*', TYPE_EXTRA),
               ('Modified/*', TYPE_MODIFIED)]
    for (xpath, type) in pattern:
        for x in statistics.findall(xpath):
            counter_fields[type] = counter_fields[type] + 1
            rr = _fetch_reason(x, build_reason_kwargs(x, encoding, logger), logger)

            try:
                entry = entries_cache[(x.tag, x.get('name'))]
            except KeyError:
                entry, created = Entries.objects.get_or_create(\
                    name=x.get('name'), kind=x.tag)

            Entries_interactions(entry=entry, reason=rr,
                                 interaction=current_interaction,
                                 type=type).save()
            if vlevel > 0:
                logger.info("%s interaction created with reason id %s and entry %s" % (xpath, rr.id, entry.id))

    # add good entries
    good_reason = None
    for x in statistics.findall('Good/*'):
        if good_reason == None:
            # Do this once.  Really need to fix Reasons...
            good_reason = _fetch_reason(x, build_reason_kwargs(x, encoding, logger), logger)
        try:
            entry = entries_cache[(x.tag, x.get('name'))]
        except KeyError:
            entry, created = Entries.objects.get_or_create(\
                name=x.get('name'), kind=x.tag)
        Entries_interactions(entry=entry, reason=good_reason,
                             interaction=current_interaction,
                             type=TYPE_GOOD).save()
        if vlevel > 0:
            logger.info("%s interaction created with reason id %s and entry %s" % (xpath, good_reason.id, entry.id))

    # Update interaction counters
    current_interaction.bad_entries = counter_fields[TYPE_BAD]
    current_interaction.modified_entries = counter_fields[TYPE_MODIFIED]
    current_interaction.extra_entries = counter_fields[TYPE_EXTRA]
    current_interaction.save()

    mperfs = []
    for times in statistics.findall('OpStamps'):
        for metric, value in list(times.items()):
            mmatch = []
            if not quick:
                mmatch = Performance.objects.filter(metric=metric, value=value)

            if mmatch:
                mperf = mmatch[0]
            else:
                mperf = Performance(metric=metric, value=value)
                mperf.save()
            mperfs.append(mperf)
    current_interaction.performance_items.add(*mperfs)


if __name__ == '__main__':
    from sys import argv
    verb = 0
    cpath = "/etc/bcfg2.conf"
    clientpath = False
    statpath = False
    syslog = False

    try:
        opts, args = getopt(argv[1:], "hvudc:s:CS", ["help",
                                                     "verbose",
                                                     "updates",
                                                     "debug",
                                                     "clients=",
                                                     "stats=",
                                                     "config=",
                                                     "syslog"])
    except GetoptError:
        mesg = sys.exc_info()[1]
        # print help information and exit:
        print("%s\nUsage:\nimportscript.py [-h] [-v] [-u] [-d] [-S] [-C bcfg2 config file] [-s statistics-file]" % (mesg))
        raise SystemExit(2)

    for o, a in opts:
        if o in ("-h", "--help"):
            print("Usage:\nimportscript.py [-h] [-v] -s <statistics-file> \n")
            print("h : help; this message")
            print("v : verbose; print messages on record insertion/skip")
            print("u : updates; print status messages as items inserted semi-verbose")
            print("d : debug; print most SQL used to manipulate database")
            print("C : path to bcfg2.conf config file.")
            print("s : statistics.xml file")
            print("S : syslog; output to syslog")
            raise SystemExit
        if o in ["-C", "--config"]:
            cpath = a

        if o in ("-v", "--verbose"):
            verb = 1
        if o in ("-u", "--updates"):
            verb = 2
        if o in ("-d", "--debug"):
            verb = 3
        if o in ("-c", "--clients"):
            print("DeprecationWarning: %s is no longer used" % o)

        if o in ("-s", "--stats"):
            statpath = a
        if o in ("-S", "--syslog"):
            syslog = True

    logger = logging.getLogger('importscript.py')
    logging.getLogger().setLevel(logging.INFO)
    Bcfg2.Logger.setup_logging('importscript.py',
                               True,
                               syslog, level=logging.INFO)

    cf = ConfigParser.ConfigParser()
    cf.read([cpath])

    if not statpath:
        try:
            statpath = "%s/etc/statistics.xml" % cf.get('server', 'repository')
        except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
            print("Could not read bcfg2.conf; exiting")
            raise SystemExit(1)
    try:
        statsdata = XML(open(statpath).read())
    except (IOError, XMLSyntaxError):
        print("StatReports: Failed to parse %s" % (statpath))
        raise SystemExit(1)

    try:
        encoding = cf.get('components', 'encoding')
    except:
        encoding = 'UTF-8'

    q = '-O3' in sys.argv

    # don't load this at the top.  causes a circular import error
    from Bcfg2.Server.SchemaUpdater import update_database, UpdaterError
    # Be sure the database is ready for new schema
    try:
        update_database()
    except UpdaterError:
        raise SystemExit(1)
    load_stats(statsdata,
               encoding,
               verb,
               logger,
               quick=q,
               location=platform.node())