Beispiel #1
0
def export_auditlog(format, query):
    "Export the audit log"
    logger = export_auditlog.get_logger()
    filename = 'auditlog-%s.%s' % (export_auditlog.request.id, format)
    content_type = 'text/csv' if format == 'csv' else 'application/pdf'
    results = dict(id=export_auditlog.request.id,
                    f=None,
                    content_type=content_type,
                    filename=filename,
                    errormsg='')
    try:
        dbquery = Session.query(AuditLog)
        if query:
            conn = SphinxClient()
            sphinxopts = extract_sphinx_opts(config['sphinx.url'])
            conn.SetServer(sphinxopts.get('host', '127.0.0.1'))
            conn.SetMatchMode(SPH_MATCH_EXTENDED2)
            conn.SetLimits(0, 500, 500)
            query = clean_sphinx_q(query)
            qresults = conn.Query(query, 'auditlog, auditlog_rt')
            if qresults and qresults['matches']:
                ids = [hit['id'] for hit in qresults['matches']]
                dbquery = dbquery.filter(AuditLog.id.in_(ids))

        dbquery = dbquery.order_by(desc('timestamp')).all()
        if format == 'pdf':
            PS = ParagraphStyle('auditlogp',
                                    fontName='Helvetica',
                                    fontSize=8,
                                    borderPadding=(2, 2, 2, 2))
            rows = [(Paragraph(item.timestamp.strftime('%Y-%m-%d %H:%M'), PS),
                    Paragraph(wrap_string(item.username, 27), PS),
                    Paragraph(wrap_string(item.info, 33), PS),
                    Paragraph(wrap_string(item.hostname, 27), PS),
                    Paragraph(wrap_string(item.remoteip, 15), PS),
                    Paragraph(CATEGORY_MAP[item.category], PS))
                    for item in dbquery]
            pdf = build_pdf(rows)
            results['f'] = pdf
        elif format == 'csv':
            rows = [item.tojson() for item in dbquery]
            keys = ('timestamp',
                    'username',
                    'info',
                    'hostname',
                    'remoteip',
                    'category')
            results['f'] = build_csv(rows, keys)
        logger.info("Audit Log export complete: %s" % results['filename'])
        return results
    except (DatabaseError), err:
        results['errormsg'] = str(err)
        logger.info("Audit Log export FAILURE: %s" % str(err))
        return results
Beispiel #2
0
def export_auditlog(format, query):
    "Export the audit log"
    logger = export_auditlog.get_logger()
    filename = 'auditlog-%s.%s' % (export_auditlog.request.id, format)
    content_type = 'text/csv' if format == 'csv' else 'application/pdf'
    results = dict(id=export_auditlog.request.id,
                   f=None,
                   content_type=content_type,
                   filename=filename,
                   errormsg='')
    try:
        dbquery = Session.query(AuditLog)
        if query:
            conn = SphinxClient()
            sphinxopts = extract_sphinx_opts(config['sphinx.url'])
            conn.SetServer(sphinxopts.get('host', '127.0.0.1'))
            conn.SetMatchMode(SPH_MATCH_EXTENDED2)
            conn.SetLimits(0, 500, 500)
            query = clean_sphinx_q(query)
            qresults = conn.Query(query, 'auditlog, auditlog_rt')
            if qresults and qresults['matches']:
                ids = [hit['id'] for hit in qresults['matches']]
                dbquery = dbquery.filter(AuditLog.id.in_(ids))

        dbquery = dbquery.order_by(desc('timestamp')).all()
        if format == 'pdf':
            PS = ParagraphStyle('auditlogp',
                                fontName='Helvetica',
                                fontSize=8,
                                borderPadding=(2, 2, 2, 2))
            rows = [(Paragraph(item.timestamp.strftime('%Y-%m-%d %H:%M'), PS),
                     Paragraph(wrap_string(item.username, 27),
                               PS), Paragraph(wrap_string(item.info, 33), PS),
                     Paragraph(wrap_string(item.hostname, 27), PS),
                     Paragraph(wrap_string(item.remoteip, 15),
                               PS), Paragraph(CATEGORY_MAP[item.category], PS))
                    for item in dbquery]
            pdf = build_pdf(rows)
            results['f'] = pdf
        elif format == 'csv':
            rows = [item.tojson() for item in dbquery]
            keys = ('timestamp', 'username', 'info', 'hostname', 'remoteip',
                    'category')
            results['f'] = build_csv(rows, keys)
        logger.info("Audit Log export complete: %s" % results['filename'])
        return results
    except (DatabaseError), err:
        results['errormsg'] = str(err)
        logger.info("Audit Log export FAILURE: %s" % str(err))
        return results
Beispiel #3
0
def wrap_headers(value, length=100):
    "wrap the headers"
    headers = value.split('\n')
    rstring = []
    for header in headers:
        if len(header) > length:
            header = wrap_string(header, length)
        rstring.append(header)
    return ('\n'.join(rstring))
Beispiel #4
0
def wrap_headers(value, length=100):
    "wrap the headers"
    headers = value.split('\n')
    rstring = []
    for header in headers:
        if len(header) > length:
            header = wrap_string(header, length)
        rstring.append(header)
    return ('\n'.join(rstring))