Example #1
0
def dashboard_json(request):
    context = {}
    if request.method == 'GET':
        if 'source' in request.GET:
            source = request.GET.get('source')
            set_server(request, source)
    if request.method == 'POST':
        request.session['django_timezone'] = request.POST['timezone']
        return redirect(request.POST['return_url'])

    source_url, source_certs, source_verify = get_server(request)

    puppet_run_time = get_server(request, type='run_time')
    dashboard_show = request.GET.get('show', 'recent')
    events_params = {
        'query': {
            1:
            '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
        },
        'summarize_by': 'certname',
    }
    reports_params = {
        'query': {
            1:
            '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
        }
    }
    nodes_params = {
        'limit': 25,
        'order_by': {
            'order_field': {
                'field': 'report_timestamp',
                'order': 'desc',
            },
            'query_field': {
                'field': 'certname'
            },
        },
    }

    jobs = {
        'tot_resource': {
            'url':
            source_url,
            'certs':
            source_certs,
            'verify':
            source_verify,
            'id':
            'tot_resource',
            'path':
            'mbeans/puppetlabs.puppetdb.query.population:type=default,name=num-resources',
        },
        'avg_resource': {
            'url':
            source_url,
            'certs':
            source_certs,
            'verify':
            source_verify,
            'id':
            'avg_resource',
            'path':
            'mbeans/puppetlabs.puppetdb.query.population:type=default,name=avg-resources-per-node',
        },
        'all_nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'all_nodes',
            'path': '/nodes',
            'request': request
        },
        'events': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'event_counts',
            'path': '/event-counts',
            'api_version': 'v4',
            'params': events_params,
            'request': request
        },
        'reports': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'reports',
            'path': '/reports',
            'params': reports_params,
            'request': request
        },
        'nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'nodes',
            'path': '/nodes',
            'params': nodes_params,
            'request': request
        },
    }
    puppetdb_results = run_puppetdb_jobs(jobs)

    # Assign vars from the completed jobs
    # Number of results from all_nodes is our population.
    puppet_population = len(puppetdb_results['all_nodes'])
    # Total resources managed by puppet metric
    total_resources = puppetdb_results['tot_resource']
    # Average resource per node metric
    avg_resource_node = puppetdb_results['avg_resource']
    # Information about all active nodes in puppet
    all_nodes_list = puppetdb_results['all_nodes']
    all_nodes_dict = {item['certname']: item for item in all_nodes_list}
    # All available events for the latest puppet reports
    event_list = puppetdb_results['event_counts']
    event_dict = {item['subject']['title']: item for item in event_list}
    # All of the latest reports
    reports_list = puppetdb_results['reports']
    reports_dict = {item['certname']: item for item in reports_list}
    # 25 Nodes
    node_list = puppetdb_results['nodes']
    node_dict = {item['certname']: item for item in node_list}

    failed_list, changed_list, unreported_list, mismatch_list, pending_list = dictstatus(
        all_nodes_dict,
        reports_dict,
        event_dict,
        sort=True,
        sortby='latestReport',
        get_status='notall',
        puppet_run_time=puppet_run_time)

    pending_list = [x for x in pending_list if x not in unreported_list]
    changed_list = [
        x for x in changed_list if x not in unreported_list
        and x not in failed_list and x not in pending_list
    ]
    failed_list = [x for x in failed_list if x not in unreported_list]
    unreported_list = [x for x in unreported_list if x not in failed_list]

    node_unreported_count = len(unreported_list)
    node_fail_count = len(failed_list)
    node_change_count = len(changed_list)
    node_off_timestamps_count = len(mismatch_list)
    node_pending_count = len(pending_list)

    if dashboard_show == 'recent':
        merged_nodes_list = dictstatus(node_dict,
                                       reports_dict,
                                       event_dict,
                                       sort=False,
                                       get_status="all",
                                       puppet_run_time=puppet_run_time)
    elif dashboard_show == 'failed':
        merged_nodes_list = failed_list
    elif dashboard_show == 'unreported':
        merged_nodes_list = unreported_list
    elif dashboard_show == 'changed':
        merged_nodes_list = changed_list
    elif dashboard_show == 'mismatch':
        merged_nodes_list = mismatch_list
    elif dashboard_show == 'pending':
        merged_nodes_list = pending_list
    else:
        merged_nodes_list = dictstatus(node_dict,
                                       reports_dict,
                                       event_dict,
                                       sort=False,
                                       get_status="all",
                                       puppet_run_time=puppet_run_time)

    context['node_list'] = merged_nodes_list
    context['selected_view'] = dashboard_show
    context['population'] = puppet_population
    context['total_resource'] = total_resources['Value']
    context['avg_resource'] = "{:.2f}".format(avg_resource_node['Value'])
    context['failed_nodes'] = node_fail_count
    context['changed_nodes'] = node_change_count
    context['unreported_nodes'] = node_unreported_count
    context['mismatching_timestamps'] = node_off_timestamps_count
    context['pending_nodes'] = node_pending_count

    return HttpResponse(json.dumps(context), content_type="application/json")
Example #2
0
def nodes_json(request):
    if request.method == 'GET':
        if 'source' in request.GET:
            source = request.GET.get('source')
            set_server(request, source)
    if request.method == 'POST':
        request.session['django_timezone'] = request.POST['timezone']
        return redirect(request.POST['return_url'])

    source_url, source_certs, source_verify = get_server(request)
    valid_sort_fields = (
        'certname',
        'catalog_timestamp',
        'report_timestamp',
        'facts_timestamp',
        'successes',
        'noops',
        'failures',
        'skips')
    try:
        # If user requested to download csv formatted file. Default value is False
        dl_csv = request.GET.get('dl_csv', False)
        if dl_csv == 'true':
            dl_csv = True
        else:
            dl_csv = False
        # Add limits to session
        if request.GET.get('limits', False):
            if request.session['limits'] != int(request.GET.get('limits', 50)):
                request.session['limits'] = int(request.GET.get('limits', 50))
            if request.session['limits'] <= 0:
                request.session['limits'] = 50
        else:
            if 'limits' not in request.session:
                request.session['limits'] = 50

        # Cur Page Number
        if request.GET.get('page', False):
            if request.session['page'] != int(request.GET.get('page', 1)):
                request.session['page'] = int(request.GET.get('page', 1))
            if request.session['page'] <= 0:
                request.session['page'] = 1
        else:
            if 'page' not in request.session:
                request.session['page'] = 1

        # Cur sort field
        if request.GET.get('sortfield', False):
            if request.session['sortfield'] != request.GET.get('sortfield'):
                request.session['sortfield'] = request.GET.get('sortfield')
            if request.session['sortfield'] not in valid_sort_fields:
                request.session['sortfield'] = 'report_timestamp'
        else:
            if 'sortfield' not in request.session:
                request.session['sortfield'] = 'report_timestamp'

        # Cur sort order
        if request.GET.get('sortfieldby', False):
            avail_sortorder = ['asc', 'desc']
            if request.session['sortfieldby'] != request.GET.get('sortfieldby'):
                request.session['sortfieldby'] = request.GET.get('sortfieldby')
            if request.session['sortfieldby'] not in avail_sortorder:
                request.session['sortfieldby'] = 'desc'
        else:
            if 'sortfieldby' not in request.session:
                request.session['sortfieldby'] = 'desc'
        # Search parameters takes a valid puppetdb query string
        if request.GET.get('search', False):
            if 'search' in request.session and (request.session['search'] == request.GET.get('search')):
                pass
            else:
                if request.GET.get('search') == 'clear_rules':
                    request.session['sortfield'] = 'report_timestamp'
                    request.session['sortfieldby'] = 'desc'
                    request.session['page'] = 1
                    request.session['search'] = None
                else:
                    request.session['page'] = 1
                    request.session['search'] = request.GET.get('search')
        else:
            if 'search' not in request.session:
                request.session['sortfield'] = 'report_timestamp'
                request.session['sortfieldby'] = 'desc'
                request.session['page'] = 1
                request.session['search'] = None

        # Set offset
        request.session['offset'] = (request.session['limits'] * request.session['page']) - request.session[
            'limits']
    except:
        return HttpResponseBadRequest('Oh no! Your filters were invalid.')

    # Valid sort field that the user can search agnaist.
    sort_field = request.session['sortfield']
    sort_field_order = request.session['sortfieldby']
    page_num = request.session['page']

    if request.session['search'] is not None:
        node_params = {
            'query':
                {
                    1: request.session['search']
                },
        }
    else:
        node_params = {
            'query': {},
        }

    nodes_sort_fields = ['certname', 'catalog_timestamp', 'report_timestamp', 'facts_timestamp']
    if sort_field in nodes_sort_fields:
        node_params['order_by'] = {
            'order_field':
                {
                    'field': sort_field,
                    'order': sort_field_order,
                },
        }
        if dl_csv is False:
            node_params['limit'] = request.session['limits']
            node_params['offset'] = request.session['offset']
        node_params['include_total'] = 'true'
    else:
        node_params['order_by'] = {
            'order_field':
                {
                    'field': 'report_timestamp',
                    'order': 'desc',
                },
        }
    node_sort_fields = ['certname', 'catalog_timestamp', 'report_timestamp', 'facts_timestamp']
    if sort_field in node_sort_fields:
        try:
            node_list, node_headers = puppetdb.api_get(
                api_url=source_url,
                cert=source_certs,
                verify=source_verify,
                path='/nodes',
                api_version='v4',
                params=puppetdb.mk_puppetdb_query(
                    node_params, request),
            )
        except:
            node_list = []
            node_headers = dict()
            node_headers['X-Records'] = 0
    else:
        node_list = puppetdb.api_get(
            api_url=source_url,
            cert=source_certs,
            verify=source_verify,
            path='/nodes',
            api_version='v4',
            params=puppetdb.mk_puppetdb_query(
                node_params, request),
        )

    # Work out the number of pages from the xrecords response
    # return fields that you can sort by
    # for each node in the node_list, find out if the latest run has any failures
    # v3/event-counts --data-urlencode query='["=","latest-report?",true]'
    # --data-urlencode summarize-by='certname'
    report_params = {
        'query':
            {
                1: '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
            },
        'summarize_by': 'certname',
    }
    status_sort_fields = ['successes', 'failures', 'skips', 'noops']

    report_status_params = {
        'query':
            {
                1: '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
            }
    }
    report_status_list = puppetdb.api_get(
        api_url=source_url,
        cert=source_certs,
        verify=source_verify,
        path='/reports',
        params=puppetdb.mk_puppetdb_query(report_status_params, request),
        api_version='v4',
    )
    if sort_field in status_sort_fields:
        if request.session['search'] is not None:
            report_params['query'] = {'operator': 'and',
                                      1: request.session['search'],
                                      2: '["=","latest_report?",true]',
                                      3: '["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]',
                                      }
        report_params['order_by'] = {
            'order_field':
                {
                    'field': sort_field,
                    'order': sort_field_order,
                }
        }
        report_params['include_total'] = 'true'
        # Don't limit results if its CSV
        if dl_csv is False:
            report_params['limit'] = request.session['limits']
            report_params['offset'] = request.session['offset']

        report_list, report_headers = puppetdb.api_get(
            api_url=source_url,
            cert=source_certs,
            verify=source_verify,
            path='/event-counts',
            params=puppetdb.mk_puppetdb_query(report_params, request),
            api_version='v4',
        )
    else:
        report_list = puppetdb.api_get(
            api_url=source_url,
            cert=source_certs,
            verify=source_verify,
            path='event-counts',
            params=puppetdb.mk_puppetdb_query(report_params, request),
            api_version='v4',
        )
    # number of results depending on sort field.
    if sort_field in status_sort_fields:
        xrecords = report_headers['X-Records']
        total_results = xrecords
    elif sort_field in nodes_sort_fields:
        xrecords = node_headers['X-Records']
        total_results = xrecords

    num_pages_wdec = float(xrecords) / request.session['limits']
    num_pages_wodec = float("{:.0f}".format(num_pages_wdec))
    if num_pages_wdec > num_pages_wodec:
        num_pages = num_pages_wodec + 1
    else:
        num_pages = num_pages_wodec

    # Converts lists of dicts to dicts.
    node_dict = {item['certname']: item for item in node_list}
    status_dict = {item['certname']: item for item in report_status_list}
    report_dict = {item['subject']['title']: item for item in report_list}
    if sort_field_order == 'desc':
        rows = dictstatus(
            node_dict, status_dict, report_dict, sortby=sort_field, asc=True, sort=False)
        sort_field_order_opposite = 'asc'
    elif sort_field_order == 'asc':
        rows = dictstatus(
            node_dict, status_dict, report_dict, sortby=sort_field, asc=False, sort=False)
        sort_field_order_opposite = 'desc'

    if dl_csv is True:
        if rows is []:
            pass
        else:
            # Generate a sequence of rows. The range is based on the maximum number of
            # rows that can be handled by a single sheet in most spreadsheet
            # applications.
            include_facts = request.GET.get('include_facts', False)
            csv_headers = ['Certname',
                           'Latest Catalog',
                           'Latest Report',
                           'Latest Facts',
                           'Success',
                           'Noop',
                           'Failure',
                           'Skipped']
            if include_facts is not False:
                merged_list_facts = []
                facts = {}
                for fact in include_facts.split(','):
                    # Sanitize the fact input from the user
                    fact = fact.strip()
                    # Add the fact name to the headers list
                    csv_headers.append(fact)

                    # build the params for each fact.
                    facts_params = {
                        'query':
                            {
                                1: '["=","name","' + fact + '"]'
                            },
                    }
                    fact_list = puppetdb.api_get(
                        api_url=source_url,
                        cert=source_certs,
                        verify=source_verify,
                        path='facts',
                        params=puppetdb.mk_puppetdb_query(facts_params),
                        api_version='v4',
                    )
                    # Populate the facts dict with the facts we have retrieved
                    # Convert the fact list into a fact dict!
                    facts[fact] = {item['certname']: item for item in fact_list}

                i = 1
                jobs = {}
                # Add ID to each job so that it can be assembled in
                # the same order after we recieve the job results
                # We do this via jobs so that we can get faster results.
                for node in rows:
                    jobs[i] = {
                        'id': i,
                        'include_facts': include_facts.split(','),
                        'node': node,
                        'facts': facts,
                    }
                    i += 1

                csv_results = generate_csv(jobs)
                rows = []
                i = 1
                # with the job results we can now recreate merged_list
                # in the order we sent them.
                while i <= len(csv_results):
                    rows.append(csv_results[i])
                    i += 1
            # Insert the csv header to the top of the list.
            rows.insert(0, csv_headers)
            pseudo_buffer = Echo()
            writer = csv.writer(pseudo_buffer)
            response = StreamingHttpResponse((writer.writerow(row) for row in rows),
                                             content_type="text/csv")
            response['Content-Disposition'] = 'attachment; filename="puppetdata-%s.csv"' % (datetime.datetime.now())
            return response

    """
    c_r_s* = current request sort
    c_r_* = current req
    r_s* = requests available
    """
    context = {
        'nodeList': rows,
        'total_nodes': total_results,
        'c_r_page': page_num,
        'c_r_limit': request.session['limits'],
        'r_sfield': valid_sort_fields,
        'c_r_sfield': sort_field,
        'r_sfieldby': ['asc', 'desc'],
        'c_r_sfieldby': sort_field_order,
        'c_r_sfieldby_o': sort_field_order_opposite,
        'tot_pages': '{0:g}'.format(num_pages),
    }
    return HttpResponse(json.dumps(context), content_type="application/json")
Example #3
0
def nodes_json(request):
    if request.method == 'GET':
        if 'source' in request.GET:
            source = request.GET.get('source')
            set_server(request, source)
    if request.method == 'POST':
        request.session['django_timezone'] = request.POST['timezone']
        return redirect(request.POST['return_url'])

    source_url, source_certs, source_verify = get_server(request)
    valid_sort_fields = ('certname', 'catalog_timestamp', 'report_timestamp',
                         'facts_timestamp', 'successes', 'noops', 'failures',
                         'skips')
    try:
        # If user requested to download csv formatted file. Default value is False
        dl_csv = request.GET.get('dl_csv', False)
        if dl_csv == 'true':
            dl_csv = True
        else:
            dl_csv = False
        # Add limits to session
        if request.GET.get('limits', False):
            if request.session['limits'] != int(request.GET.get('limits', 50)):
                request.session['limits'] = int(request.GET.get('limits', 50))
            if request.session['limits'] <= 0:
                request.session['limits'] = 50
        else:
            if 'limits' not in request.session:
                request.session['limits'] = 50

        # Cur Page Number
        if request.GET.get('page', False):
            if request.session['page'] != int(request.GET.get('page', 1)):
                request.session['page'] = int(request.GET.get('page', 1))
            if request.session['page'] <= 0:
                request.session['page'] = 1
        else:
            if 'page' not in request.session:
                request.session['page'] = 1

        # Cur sort field
        if request.GET.get('sortfield', False):
            if request.session['sortfield'] != request.GET.get('sortfield'):
                request.session['sortfield'] = request.GET.get('sortfield')
            if request.session['sortfield'] not in valid_sort_fields:
                request.session['sortfield'] = 'report_timestamp'
        else:
            if 'sortfield' not in request.session:
                request.session['sortfield'] = 'report_timestamp'

        # Cur sort order
        if request.GET.get('sortfieldby', False):
            avail_sortorder = ['asc', 'desc']
            if request.session['sortfieldby'] != request.GET.get(
                    'sortfieldby'):
                request.session['sortfieldby'] = request.GET.get('sortfieldby')
            if request.session['sortfieldby'] not in avail_sortorder:
                request.session['sortfieldby'] = 'desc'
        else:
            if 'sortfieldby' not in request.session:
                request.session['sortfieldby'] = 'desc'
        # Search parameters takes a valid puppetdb query string
        if request.GET.get('search', False):
            if 'search' in request.session and (request.session['search']
                                                == request.GET.get('search')):
                pass
            else:
                if request.GET.get('search') == 'clear_rules':
                    request.session['sortfield'] = 'report_timestamp'
                    request.session['sortfieldby'] = 'desc'
                    request.session['page'] = 1
                    request.session['search'] = None
                else:
                    request.session['page'] = 1
                    request.session['search'] = request.GET.get('search')
        else:
            if 'search' not in request.session:
                request.session['sortfield'] = 'report_timestamp'
                request.session['sortfieldby'] = 'desc'
                request.session['page'] = 1
                request.session['search'] = None

        # Set offset
        request.session['offset'] = (
            request.session['limits'] *
            request.session['page']) - request.session['limits']
    except:
        return HttpResponseBadRequest('Oh no! Your filters were invalid.')

    # Valid sort field that the user can search agnaist.
    sort_field = request.session['sortfield']
    sort_field_order = request.session['sortfieldby']
    page_num = request.session['page']

    if request.session['search'] is not None:
        node_params = {
            'query': {
                1: request.session['search']
            },
        }
    else:
        node_params = {
            'query': {},
        }

    nodes_sort_fields = [
        'certname', 'catalog_timestamp', 'report_timestamp', 'facts_timestamp'
    ]
    if sort_field in nodes_sort_fields:
        node_params['order_by'] = {
            'order_field': {
                'field': sort_field,
                'order': sort_field_order,
            },
        }
        if dl_csv is False:
            node_params['limit'] = request.session['limits']
            node_params['offset'] = request.session['offset']
        node_params['include_total'] = 'true'
    else:
        node_params['order_by'] = {
            'order_field': {
                'field': 'report_timestamp',
                'order': 'desc',
            },
        }
    node_sort_fields = [
        'certname', 'catalog_timestamp', 'report_timestamp', 'facts_timestamp'
    ]
    if sort_field in node_sort_fields:
        try:
            node_list, node_headers = puppetdb.api_get(
                api_url=source_url,
                cert=source_certs,
                verify=source_verify,
                path='/nodes',
                api_version='v4',
                params=puppetdb.mk_puppetdb_query(node_params, request),
            )
        except:
            node_list = []
            node_headers = dict()
            node_headers['X-Records'] = 0
    else:
        node_list = puppetdb.api_get(
            api_url=source_url,
            cert=source_certs,
            verify=source_verify,
            path='/nodes',
            api_version='v4',
            params=puppetdb.mk_puppetdb_query(node_params, request),
        )

    # Work out the number of pages from the xrecords response
    # return fields that you can sort by
    # for each node in the node_list, find out if the latest run has any failures
    # v3/event-counts --data-urlencode query='["=","latest-report?",true]'
    # --data-urlencode summarize-by='certname'
    report_params = {
        'query': {
            1:
            '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
        },
        'summarize_by': 'certname',
    }
    status_sort_fields = ['successes', 'failures', 'skips', 'noops']

    report_status_params = {
        'query': {
            1:
            '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
        }
    }
    report_status_list = puppetdb.api_get(
        api_url=source_url,
        cert=source_certs,
        verify=source_verify,
        path='/reports',
        params=puppetdb.mk_puppetdb_query(report_status_params, request),
        api_version='v4',
    )
    if sort_field in status_sort_fields:
        if request.session['search'] is not None:
            report_params['query'] = {
                'operator':
                'and',
                1:
                request.session['search'],
                2:
                '["=","latest_report?",true]',
                3:
                '["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]',
            }
        report_params['order_by'] = {
            'order_field': {
                'field': sort_field,
                'order': sort_field_order,
            }
        }
        report_params['include_total'] = 'true'
        # Don't limit results if its CSV
        if dl_csv is False:
            report_params['limit'] = request.session['limits']
            report_params['offset'] = request.session['offset']

        report_list, report_headers = puppetdb.api_get(
            api_url=source_url,
            cert=source_certs,
            verify=source_verify,
            path='/event-counts',
            params=puppetdb.mk_puppetdb_query(report_params, request),
            api_version='v4',
        )
    else:
        report_list = puppetdb.api_get(
            api_url=source_url,
            cert=source_certs,
            verify=source_verify,
            path='event-counts',
            params=puppetdb.mk_puppetdb_query(report_params, request),
            api_version='v4',
        )
    # number of results depending on sort field.
    if sort_field in status_sort_fields:
        xrecords = report_headers['X-Records']
        total_results = xrecords
    elif sort_field in nodes_sort_fields:
        xrecords = node_headers['X-Records']
        total_results = xrecords

    num_pages_wdec = float(xrecords) / request.session['limits']
    num_pages_wodec = float("{:.0f}".format(num_pages_wdec))
    if num_pages_wdec > num_pages_wodec:
        num_pages = num_pages_wodec + 1
    else:
        num_pages = num_pages_wodec

    # Converts lists of dicts to dicts.
    node_dict = {item['certname']: item for item in node_list}
    status_dict = {item['certname']: item for item in report_status_list}
    report_dict = {item['subject']['title']: item for item in report_list}
    if sort_field_order == 'desc':
        rows = dictstatus(node_dict,
                          status_dict,
                          report_dict,
                          sortby=sort_field,
                          asc=True,
                          sort=False)
        sort_field_order_opposite = 'asc'
    elif sort_field_order == 'asc':
        rows = dictstatus(node_dict,
                          status_dict,
                          report_dict,
                          sortby=sort_field,
                          asc=False,
                          sort=False)
        sort_field_order_opposite = 'desc'

    if dl_csv is True:
        if rows is []:
            pass
        else:
            # Generate a sequence of rows. The range is based on the maximum number of
            # rows that can be handled by a single sheet in most spreadsheet
            # applications.
            include_facts = request.GET.get('include_facts', False)
            csv_headers = [
                'Certname', 'Latest Catalog', 'Latest Report', 'Latest Facts',
                'Success', 'Noop', 'Failure', 'Skipped'
            ]
            if include_facts is not False:
                merged_list_facts = []
                facts = {}
                for fact in include_facts.split(','):
                    # Sanitize the fact input from the user
                    fact = fact.strip()
                    # Add the fact name to the headers list
                    csv_headers.append(fact)

                    # build the params for each fact.
                    facts_params = {
                        'query': {
                            1: '["=","name","' + fact + '"]'
                        },
                    }
                    fact_list = puppetdb.api_get(
                        api_url=source_url,
                        cert=source_certs,
                        verify=source_verify,
                        path='facts',
                        params=puppetdb.mk_puppetdb_query(facts_params),
                        api_version='v4',
                    )
                    # Populate the facts dict with the facts we have retrieved
                    # Convert the fact list into a fact dict!
                    facts[fact] = {
                        item['certname']: item
                        for item in fact_list
                    }

                i = 1
                jobs = {}
                # Add ID to each job so that it can be assembled in
                # the same order after we recieve the job results
                # We do this via jobs so that we can get faster results.
                for node in rows:
                    jobs[i] = {
                        'id': i,
                        'include_facts': include_facts.split(','),
                        'node': node,
                        'facts': facts,
                    }
                    i += 1

                csv_results = generate_csv(jobs)
                rows = []
                i = 1
                # with the job results we can now recreate merged_list
                # in the order we sent them.
                while i <= len(csv_results):
                    rows.append(csv_results[i])
                    i += 1
            # Insert the csv header to the top of the list.
            rows.insert(0, csv_headers)
            pseudo_buffer = Echo()
            writer = csv.writer(pseudo_buffer)
            response = StreamingHttpResponse(
                (writer.writerow(row) for row in rows),
                content_type="text/csv")
            response[
                'Content-Disposition'] = 'attachment; filename="puppetdata-%s.csv"' % (
                    datetime.datetime.now())
            return response
    """
    c_r_s* = current request sort
    c_r_* = current req
    r_s* = requests available
    """
    context = {
        'nodeList': rows,
        'total_nodes': total_results,
        'c_r_page': page_num,
        'c_r_limit': request.session['limits'],
        'r_sfield': valid_sort_fields,
        'c_r_sfield': sort_field,
        'r_sfieldby': ['asc', 'desc'],
        'c_r_sfieldby': sort_field_order,
        'c_r_sfieldby_o': sort_field_order_opposite,
        'tot_pages': '{0:g}'.format(num_pages),
    }
    return HttpResponse(json.dumps(context), content_type="application/json")
Example #4
0
def dashboard_nodes_json(request):
    context = {}
    if request.method == 'GET':
        if 'source' in request.GET:
            source = request.GET.get('source')
            set_server(request, source)
    if request.method == 'POST':
        request.session['django_timezone'] = request.POST['timezone']
        return redirect(request.POST['return_url'])

    source_url, source_certs, source_verify = get_server(request)

    puppet_run_time = get_server(request, type='run_time')

    # Dashboard to show nodes of "recent, failed, unreported or changed"
    dashboard_show = request.GET.get('show', 'recent')
    events_params = {
        'query': {
            1:
            '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
        },
        'summarize_by': 'certname',
    }
    all_nodes_params = {
        'query': {
            1:
            '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
        },
    }
    reports_params = {
        'query': {
            1:
            '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
        }
    }
    nodes_params = {
        'limit': 25,
        'order_by': {
            'order_field': {
                'field': 'report_timestamp',
                'order': 'desc',
            },
            'query_field': {
                'field': 'certname'
            },
        },
    }

    jobs = {
        'all_nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'all_nodes',
            'path': '/nodes',
            'request': request
        },
        'events': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'event_counts',
            'path': 'event-counts',
            'api_version': 'v4',
            'params': events_params,
            'request': request
        },
        'nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'nodes',
            'path': '/nodes',
            'params': nodes_params,
            'request': request
        },
        'reports': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'reports',
            'path': '/reports',
            'params': reports_params,
            'request': request
        },
    }

    puppetdb_results = run_puppetdb_jobs(jobs)
    # Information about all active nodes in puppet
    all_nodes_list = puppetdb_results['all_nodes']
    all_nodes_dict = {item['certname']: item for item in all_nodes_list}
    # All available events for the latest puppet reports
    event_list = puppetdb_results['event_counts']
    event_dict = {item['subject']['title']: item for item in event_list}
    # All of the latest reports
    reports_list = puppetdb_results['reports']
    reports_dict = {item['certname']: item for item in reports_list}
    # 25 Nodes
    node_list = puppetdb_results['nodes']
    node_dict = {item['certname']: item for item in node_list}

    failed_list, changed_list, unreported_list, mismatch_list, pending_list = dictstatus(
        all_nodes_dict,
        reports_dict,
        event_dict,
        sort=True,
        sortby='latestReport',
        get_status='notall',
        puppet_run_time=puppet_run_time)
    pending_list = [x for x in pending_list if x not in unreported_list]
    changed_list = [
        x for x in changed_list if x not in unreported_list
        and x not in failed_list and x not in pending_list
    ]
    failed_list = [x for x in failed_list if x not in unreported_list]
    unreported_list = [x for x in unreported_list if x not in failed_list]

    if dashboard_show == 'recent':
        merged_nodes_list = dictstatus(node_dict,
                                       reports_dict,
                                       event_dict,
                                       sort=False,
                                       get_status="all",
                                       puppet_run_time=puppet_run_time)
    elif dashboard_show == 'failed':
        merged_nodes_list = failed_list
    elif dashboard_show == 'unreported':
        merged_nodes_list = unreported_list
    elif dashboard_show == 'changed':
        merged_nodes_list = changed_list
    elif dashboard_show == 'mismatch':
        merged_nodes_list = mismatch_list
    elif dashboard_show == 'pending':
        merged_nodes_list = pending_list
    else:
        merged_nodes_list = dictstatus(node_dict,
                                       reports_dict,
                                       event_dict,
                                       sort=False,
                                       get_status="all",
                                       puppet_run_time=puppet_run_time)

    context['node_list'] = merged_nodes_list
    context['selected_view'] = dashboard_show

    return HttpResponse(json.dumps(context), content_type="application/json")
Example #5
0
def dashboard_nodes_json(request):
    context = {}
    if request.method == 'GET':
        if 'source' in request.GET:
            source = request.GET.get('source')
            set_server(request, source)
    if request.method == 'POST':
        request.session['django_timezone'] = request.POST['timezone']
        return redirect(request.POST['return_url'])

    source_url, source_certs, source_verify = get_server(request)

    puppet_run_time = get_server(request, type='run_time')

    # Dashboard to show nodes of "recent, failed, unreported or changed"
    dashboard_show = request.GET.get('show', 'recent')
    events_params = {
        'query':
            {
                1: '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
            },
        'summarize_by': 'certname',
    }
    all_nodes_params = {
        'query':
            {
                1: '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
            },
    }
    reports_params = {
        'query':
            {
                1: '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
            }
    }
    nodes_params = {
        'limit': 25,
        'order_by': {
            'order_field': {
                'field': 'report_timestamp',
                'order': 'desc',
            },
            'query_field': {'field': 'certname'},
        },
    }

    jobs = {
        'all_nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'all_nodes',
            'path': '/nodes',
            'request': request
        },
        'events': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'event_counts',
            'path': 'event-counts',
            'api_version': 'v4',
            'params': events_params,
            'request': request
        },
        'nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'nodes',
            'path': '/nodes',
            'params': nodes_params,
            'request': request
        },
        'reports': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'reports',
            'path': '/reports',
            'params': reports_params,
            'request': request
        },
    }

    puppetdb_results = run_puppetdb_jobs(jobs)
    # Information about all active nodes in puppet
    all_nodes_list = puppetdb_results['all_nodes']
    all_nodes_dict = {item['certname']: item for item in all_nodes_list}
    # All available events for the latest puppet reports
    event_list = puppetdb_results['event_counts']
    event_dict = {item['subject']['title']: item for item in event_list}
    # All of the latest reports
    reports_list = puppetdb_results['reports']
    reports_dict = {item['certname']: item for item in reports_list}
    # 25 Nodes
    node_list = puppetdb_results['nodes']
    node_dict = {item['certname']: item for item in node_list}

    failed_list, changed_list, unreported_list, mismatch_list, pending_list = dictstatus(all_nodes_dict,
                                                                                         reports_dict,
                                                                                         event_dict,
                                                                                         sort=True,
                                                                                         sortby='latestReport',
                                                                                         get_status='notall',
                                                                                         puppet_run_time=puppet_run_time)
    pending_list = [x for x in pending_list if x not in unreported_list]
    changed_list = [x for x in changed_list if
                    x not in unreported_list and x not in failed_list and x not in pending_list]
    failed_list = [x for x in failed_list if x not in unreported_list]
    unreported_list = [x for x in unreported_list if x not in failed_list]

    if dashboard_show == 'recent':
        merged_nodes_list = dictstatus(
            node_dict, reports_dict, event_dict, sort=False, get_status="all", puppet_run_time=puppet_run_time)
    elif dashboard_show == 'failed':
        merged_nodes_list = failed_list
    elif dashboard_show == 'unreported':
        merged_nodes_list = unreported_list
    elif dashboard_show == 'changed':
        merged_nodes_list = changed_list
    elif dashboard_show == 'mismatch':
        merged_nodes_list = mismatch_list
    elif dashboard_show == 'pending':
        merged_nodes_list = pending_list
    else:
        merged_nodes_list = dictstatus(
            node_dict, reports_dict, event_dict, sort=False, get_status="all", puppet_run_time=puppet_run_time)

    context['node_list'] = merged_nodes_list
    context['selected_view'] = dashboard_show

    return HttpResponse(json.dumps(context), content_type="application/json")
Example #6
0
def dashboard_json(request):
    context = {}
    if request.method == 'GET':
        if 'source' in request.GET:
            source = request.GET.get('source')
            set_server(request, source)
    if request.method == 'POST':
        request.session['django_timezone'] = request.POST['timezone']
        return redirect(request.POST['return_url'])

    source_url, source_certs, source_verify = get_server(request)

    puppet_run_time = get_server(request, type='run_time')
    dashboard_show = request.GET.get('show', 'recent')
    events_params = {
        'query':
            {
                1: '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
            },
        'summarize_by': 'certname',
    }
    reports_params = {
        'query':
            {
                1: '["and",["=","latest_report?",true],["in", "certname",["extract", "certname",["select_nodes",["null?","deactivated",true]]]]]'
            }
    }
    nodes_params = {
        'limit': 25,
        'order_by': {
            'order_field': {
                'field': 'report_timestamp',
                'order': 'desc',
            },
            'query_field': {'field': 'certname'},
        },
    }

    jobs = {
        'tot_resource': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'tot_resource',
            'path': 'mbeans/puppetlabs.puppetdb.query.population:type=default,name=num-resources',
        },
        'avg_resource': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'avg_resource',
            'path': 'mbeans/puppetlabs.puppetdb.query.population:type=default,name=avg-resources-per-node',
        },
        'all_nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'all_nodes',
            'path': '/nodes',
            'request': request
        },
        'events': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'event_counts',
            'path': '/event-counts',
            'api_version': 'v4',
            'params': events_params,
            'request': request
        },
        'reports': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'reports',
            'path': '/reports',
            'params': reports_params,
            'request': request
        },
        'nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'nodes',
            'path': '/nodes',
            'params': nodes_params,
            'request': request
        },
    }
    puppetdb_results = run_puppetdb_jobs(jobs)

    # Assign vars from the completed jobs
    # Number of results from all_nodes is our population.
    puppet_population = len(puppetdb_results['all_nodes'])
    # Total resources managed by puppet metric
    total_resources = puppetdb_results['tot_resource']
    # Average resource per node metric
    avg_resource_node = puppetdb_results['avg_resource']
    # Information about all active nodes in puppet
    all_nodes_list = puppetdb_results['all_nodes']
    all_nodes_dict = {item['certname']: item for item in all_nodes_list}
    # All available events for the latest puppet reports
    event_list = puppetdb_results['event_counts']
    event_dict = {item['subject']['title']: item for item in event_list}
    # All of the latest reports
    reports_list = puppetdb_results['reports']
    reports_dict = {item['certname']: item for item in reports_list}
    # 25 Nodes
    node_list = puppetdb_results['nodes']
    node_dict = {item['certname']: item for item in node_list}

    failed_list, changed_list, unreported_list, mismatch_list, pending_list = dictstatus(all_nodes_dict,
                                                                                         reports_dict,
                                                                                         event_dict,
                                                                                         sort=True,
                                                                                         sortby='latestReport',
                                                                                         get_status='notall',
                                                                                         puppet_run_time=puppet_run_time)

    pending_list = [x for x in pending_list if x not in unreported_list]
    changed_list = [x for x in changed_list if
                    x not in unreported_list and x not in failed_list and x not in pending_list]
    failed_list = [x for x in failed_list if x not in unreported_list]
    unreported_list = [x for x in unreported_list if x not in failed_list]

    node_unreported_count = len(unreported_list)
    node_fail_count = len(failed_list)
    node_change_count = len(changed_list)
    node_off_timestamps_count = len(mismatch_list)
    node_pending_count = len(pending_list)

    if dashboard_show == 'recent':
        merged_nodes_list = dictstatus(
            node_dict, reports_dict, event_dict, sort=False, get_status="all", puppet_run_time=puppet_run_time)
    elif dashboard_show == 'failed':
        merged_nodes_list = failed_list
    elif dashboard_show == 'unreported':
        merged_nodes_list = unreported_list
    elif dashboard_show == 'changed':
        merged_nodes_list = changed_list
    elif dashboard_show == 'mismatch':
        merged_nodes_list = mismatch_list
    elif dashboard_show == 'pending':
        merged_nodes_list = pending_list
    else:
        merged_nodes_list = dictstatus(
            node_dict, reports_dict, event_dict, sort=False, get_status="all", puppet_run_time=puppet_run_time)

    context['node_list'] = merged_nodes_list
    context['selected_view'] = dashboard_show
    context['population'] = puppet_population
    context['total_resource'] = total_resources['Value']
    context['avg_resource'] = "{:.2f}".format(avg_resource_node['Value'])
    context['failed_nodes'] = node_fail_count
    context['changed_nodes'] = node_change_count
    context['unreported_nodes'] = node_unreported_count
    context['mismatching_timestamps'] = node_off_timestamps_count
    context['pending_nodes'] = node_pending_count

    return HttpResponse(json.dumps(context), content_type="application/json")
Example #7
0
    def test_nodes_merged_data(self):
        nodes_timestamps = {
            'failed-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node1': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=55)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node2': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=55)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node3': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=50)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'unreported-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=127)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=126)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=125)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'changed-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'unchanged-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=25)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=24)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=23)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'pending-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=16)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=13)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
        }

        nodes_data = [{
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['failed-node']['catalog'],
            'certname':
            'failed-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['failed-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['failed-node']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['missmatch-node1']['catalog'],
            'certname':
            'missmatch-node1.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['missmatch-node1']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['missmatch-node1']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['missmatch-node2']['catalog'],
            'certname':
            'missmatch-node2.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['missmatch-node2']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['missmatch-node2']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['missmatch-node3']['catalog'],
            'certname':
            'missmatch-node3.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['missmatch-node3']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['missmatch-node3']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['unreported-node']['catalog'],
            'certname':
            'unreported-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['unreported-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['unreported-node']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['changed-node']['catalog'],
            'certname':
            'changed-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['changed-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['changed-node']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['unchanged-node']['catalog'],
            'certname':
            'unchanged-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['unchanged-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['unchanged-node']['report'],
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['pending-node']['catalog'],
            'certname':
            'pending-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['pending-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['pending-node']['report'],
        }]

        events_data = {
            'changed-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {
                    'title': 'changed-node.example.com'
                },
                'subject-type': 'certname',
                'successes': 78
            },
            'pending-node.example.com': {
                'failures': 0,
                'noops': 100,
                'skips': 0,
                'subject': {
                    'title': 'pending-node.example.com'
                },
                'subject-type': 'certname',
                'successes': 0
            },
            'failed-node.example.com': {
                'failures': 20,
                'noops': 0,
                'skips': 10,
                'subject': {
                    'title': 'failed-node.example.com'
                },
                'subject-type': 'certname',
                'successes': 5
            },
            'unreported-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {
                    'title': 'unreported-node.example.com'
                },
                'subject-type': 'certname',
                'successes': 0
            },
            'unchanged-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {
                    'title': 'unchanged-node.example.com'
                },
                'subject-type': 'certname',
                'successes': 0
            },
            'missmatch-node1.example.com': {
                'failures': 20,
                'noops': 0,
                'skips': 10,
                'subject': {
                    'title': 'missmatch-node1.example.com'
                },
                'subject-type': 'certname',
                'successes': 5
            },
            'missmatch-node2.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {
                    'title': 'missmatch-node2.example.com'
                },
                'subject-type': 'certname',
                'successes': 25
            },
            'missmatch-node3.example.com': {
                'failures': 0,
                'noops': 50,
                'skips': 0,
                'subject': {
                    'title': 'missmatch-node3.example.com'
                },
                'subject-type': 'certname',
                'successes': 0
            }
        }

        reports_data = {
            'changed-node.example.com': {
                'status': 'changed',
            },
            'pending-node.example.com': {
                'status': 'pending',
            },
            'failed-node.example.com': {
                'status': 'failed',
            },
            'missmatch-node1.example.com': {
                'status': 'failed',
            },
            'missmatch-node2.example.com': {
                'status': 'changed',
            },
            'missmatch-node3.example.com': {
                'status': 'pending',
            },
            'unreported-node.example.com': {
                'status': 'unchanged',
            },
            'unchanged-node.example.com': {
                'status': 'unchanged',
            }
        }

        merged_list = dictstatus(nodes_data,
                                 reports_data,
                                 events_data,
                                 sort=False,
                                 get_status='all')
        # ('certname', 'latestCatalog', 'latestReport', 'latestFacts', 'success', 'noop', 'failure', 'skipped')
        merged_expected = [
            ('failed-node.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['failed-node']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['failed-node']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['failed-node']['facts'])),
                 'Y-m-d H:i:s'), 5, 0, 20, 10, 'failed'),
            ('missmatch-node1.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node1']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node1']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node1']['facts'])),
                 'Y-m-d H:i:s'), 5, 0, 20, 10, 'failed'),
            ('missmatch-node2.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node2']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node2']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node2']['facts'])),
                 'Y-m-d H:i:s'), 25, 0, 0, 0, 'changed'),
            ('missmatch-node3.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node3']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node3']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node3']['facts'])),
                 'Y-m-d H:i:s'), 0, 50, 0, 0, 'pending'),
            ('unreported-node.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['unreported-node']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['unreported-node']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['unreported-node']['facts'])),
                 'Y-m-d H:i:s'), 0, 0, 0, 0, 'unchanged'),
            ('changed-node.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['changed-node']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['changed-node']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['changed-node']['facts'])),
                 'Y-m-d H:i:s'), 78, 0, 0, 0, 'changed'),
            ('unchanged-node.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['unchanged-node']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['unchanged-node']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['unchanged-node']['facts'])),
                 'Y-m-d H:i:s'), 0, 0, 0, 0, 'unchanged'),
            ('pending-node.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['pending-node']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['pending-node']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['pending-node']['facts'])),
                 'Y-m-d H:i:s'), 0, 100, 0, 0, 'pending')
        ]
        # failed_list, changed_list, unreported_list, mismatch_list, pending_list
        merged_list.sort(key=lambda tup: tup[0])
        merged_expected.sort(key=lambda tup: tup[0])
        self.assertEqual(merged_list, merged_expected)
Example #8
0
    def test_nodes_seperated_data(self):
        nodes_timestamps = {
            'failed-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node1': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=55)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node2': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=55)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node3': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=50)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'unreported-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=127)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=126)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=125)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'changed-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'unchanged-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=25)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=24)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=23)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'pending-node': {
                'catalog':
                ((datetime.utcnow() -
                  timedelta(minutes=16)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts':
                ((datetime.utcnow() -
                  timedelta(minutes=13)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report':
                ((datetime.utcnow() -
                  timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
        }

        nodes_data = [{
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['failed-node']['catalog'],
            'certname':
            'failed-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['failed-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['failed-node']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['missmatch-node1']['catalog'],
            'certname':
            'missmatch-node1.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['missmatch-node1']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['missmatch-node1']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['missmatch-node2']['catalog'],
            'certname':
            'missmatch-node2.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['missmatch-node2']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['missmatch-node2']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['missmatch-node3']['catalog'],
            'certname':
            'missmatch-node3.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['missmatch-node3']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['missmatch-node3']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['unreported-node']['catalog'],
            'certname':
            'unreported-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['unreported-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['unreported-node']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['changed-node']['catalog'],
            'certname':
            'changed-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['changed-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['changed-node']['report']
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['unchanged-node']['catalog'],
            'certname':
            'unchanged-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['unchanged-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['unchanged-node']['report'],
        }, {
            'catalog_environment':
            'production',
            'catalog_timestamp':
            nodes_timestamps['pending-node']['catalog'],
            'certname':
            'pending-node.example.com',
            'deactivated':
            None,
            'facts_environment':
            'production',
            'facts_timestamp':
            nodes_timestamps['pending-node']['facts'],
            'report_environment':
            'production',
            'report_timestamp':
            nodes_timestamps['pending-node']['report'],
        }]

        events_data = {
            'changed-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {
                    'title': 'changed-node.example.com'
                },
                'subject-type': 'certname',
                'successes': 78
            },
            'pending-node.example.com': {
                'failures': 0,
                'noops': 100,
                'skips': 0,
                'subject': {
                    'title': 'pending-node.example.com'
                },
                'subject-type': 'certname',
                'successes': 0
            },
            'unreported-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {
                    'title': 'unreported-node.example.com'
                },
                'subject-type': 'certname',
                'successes': 0
            },
            'failed-node.example.com': {
                'failures': 20,
                'noops': 0,
                'skips': 10,
                'subject': {
                    'title': 'failed-node.example.com'
                },
                'subject-type': 'certname',
                'successes': 5
            },
            'missmatch-node1.example.com': {
                'failures': 20,
                'noops': 0,
                'skips': 10,
                'subject': {
                    'title': 'missmatch-node1.example.com'
                },
                'subject-type': 'certname',
                'successes': 5
            },
            'missmatch-node2.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {
                    'title': 'missmatch-node2.example.com'
                },
                'subject-type': 'certname',
                'successes': 25
            },
            'missmatch-node3.example.com': {
                'failures': 0,
                'noops': 50,
                'skips': 0,
                'subject': {
                    'title': 'missmatch-node3.example.com'
                },
                'subject-type': 'certname',
                'successes': 0
            }
        }
        reports_data = {
            'changed-node.example.com': {
                'status': 'changed',
            },
            'pending-node.example.com': {
                'status': 'unchanged',
            },
            'failed-node.example.com': {
                'status': 'failed',
            },
            'unreported-node.example.com': {
                'status': 'unchanged',
            },
            'missmatch-node1.example.com': {
                'status': 'failed',
            },
            'missmatch-node2.example.com': {
                'status': 'changed',
            },
            'missmatch-node3.example.com': {
                'status': 'unchanged',
            }
        }
        failed_list, changed_list, unreported_list, missmatch_list, pending_list = dictstatus(
            nodes_data,
            reports_data,
            events_data,
            sort=False,
            get_status='notall')
        # ('certname', 'latestCatalog', 'latestReport', 'latestFacts', 'success', 'noop', 'failure', 'skipped')
        failed_expected = [
            ('failed-node.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['failed-node']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['failed-node']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['failed-node']['facts'])),
                 'Y-m-d H:i:s'), 5, 0, 20, 10, 'failed'),
            ('missmatch-node1.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node1']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node1']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node1']['facts'])),
                 'Y-m-d H:i:s'), 5, 0, 20, 10, 'failed')
        ]

        changed_expected = [
            ('missmatch-node2.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node2']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node2']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node2']['facts'])),
                 'Y-m-d H:i:s'), 25, 0, 0, 0, 'changed'),
            ('changed-node.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['changed-node']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['changed-node']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['changed-node']['facts'])),
                 'Y-m-d H:i:s'), 78, 0, 0, 0, 'changed'),
        ]

        unreported_expected = [
            ('unreported-node.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['unreported-node']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['unreported-node']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['unreported-node']['facts'])),
                 'Y-m-d H:i:s'), 0, 0, 0, 0, 'unchanged')
        ]

        missmatch_expected = [
            ('missmatch-node1.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node1']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node1']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node1']['facts'])),
                 'Y-m-d H:i:s'), 5, 0, 20, 10, 'failed'),
            ('missmatch-node2.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node2']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node2']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node2']['facts'])),
                 'Y-m-d H:i:s'), 25, 0, 0, 0, 'changed'),
            ('missmatch-node3.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node3']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node3']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node3']['facts'])),
                 'Y-m-d H:i:s'), 0, 50, 0, 0, 'pending')
        ]

        pending_expected = [
            ('missmatch-node3.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node3']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node3']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['missmatch-node3']['facts'])),
                 'Y-m-d H:i:s'), 0, 50, 0, 0, 'pending'),
            ('pending-node.example.com',
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['pending-node']['catalog'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['pending-node']['report'])),
                 'Y-m-d H:i:s'),
             filters.date(
                 localtime(
                     json_to_datetime(
                         nodes_timestamps['pending-node']['facts'])),
                 'Y-m-d H:i:s'), 0, 100, 0, 0, 'pending')
        ]
        # Sort lists so its easier to verify...
        failed_list.sort(key=lambda tup: tup[0])
        failed_expected.sort(key=lambda tup: tup[0])
        changed_list.sort(key=lambda tup: tup[0])
        changed_expected.sort(key=lambda tup: tup[0])
        unreported_list.sort(key=lambda tup: tup[0])
        unreported_expected.sort(key=lambda tup: tup[0])
        missmatch_list.sort(key=lambda tup: tup[0])
        missmatch_expected.sort(key=lambda tup: tup[0])
        pending_list.sort(key=lambda tup: tup[0])
        pending_expected.sort(key=lambda tup: tup[0])
        if failed_list != failed_expected:
            self.fail(msg='Failed list does not match expectations.')
        if changed_list != changed_expected:
            self.fail(msg='Changed list does not match expectations.')
        if unreported_list != unreported_expected:
            self.fail(msg='Unreported list does not match expectations.')
        if missmatch_list != missmatch_expected:
            self.fail(msg='Missmatching list does not match expectations.')
        if pending_list != pending_expected:
            self.fail(msg='Pending list does not match expectations.')
        self.assertTrue(True)
Example #9
0
    def test_nodes_merged_data(self):
        nodes_timestamps = {
            'failed-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node1': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=55)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node2': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=55)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node3': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=50)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'unreported-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=127)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=126)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=125)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'changed-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'unchanged-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=25)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=24)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=23)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'pending-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=16)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=13)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
        }

        nodes_data = {
            'failed-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['failed-node']['catalog'],
                'certname': 'failed-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['failed-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['failed-node']['report']
            },
            'missmatch-node1.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['missmatch-node1']['catalog'],
                'certname': 'missmatch-node1.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['missmatch-node1']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['missmatch-node1']['report']
            },
            'missmatch-node2.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['missmatch-node2']['catalog'],
                'certname': 'missmatch-node2.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['missmatch-node2']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['missmatch-node2']['report']
            },
            'missmatch-node3.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['missmatch-node3']['catalog'],
                'certname': 'missmatch-node3.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['missmatch-node3']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['missmatch-node3']['report']
            },
            'unreported-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['unreported-node']['catalog'],
                'certname': 'unreported-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['unreported-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['unreported-node']['report']
            },
            'changed-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['changed-node']['catalog'],
                'certname': 'changed-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['changed-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['changed-node']['report']
            },
            'unchanged-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['unchanged-node']['catalog'],
                'certname': 'unchanged-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['unchanged-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['unchanged-node']['report'],
            },
            'pending-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['pending-node']['catalog'],
                'certname': 'pending-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['pending-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['pending-node']['report'],
            }
        }

        events_data = {
            'changed-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {'title': 'changed-node.example.com'},
                'subject-type': 'certname',
                'successes': 78
            },
            'pending-node.example.com': {
                'failures': 0,
                'noops': 100,
                'skips': 0,
                'subject': {'title': 'pending-node.example.com'},
                'subject-type': 'certname',
                'successes': 0
            },
            'failed-node.example.com': {
                'failures': 20,
                'noops': 0,
                'skips': 10,
                'subject': {'title': 'failed-node.example.com'},
                'subject-type': 'certname',
                'successes': 5
            },
            'unreported-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {'title': 'unreported-node.example.com'},
                'subject-type': 'certname',
                'successes': 0
            },
            'unchanged-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {'title': 'unchanged-node.example.com'},
                'subject-type': 'certname',
                'successes': 0
            },
            'missmatch-node1.example.com': {
                'failures': 20,
                'noops': 0,
                'skips': 10,
                'subject': {'title': 'missmatch-node1.example.com'},
                'subject-type': 'certname',
                'successes': 5
            },
            'missmatch-node2.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {'title': 'missmatch-node2.example.com'},
                'subject-type': 'certname',
                'successes': 25
            },
            'missmatch-node3.example.com': {
                'failures': 0,
                'noops': 50,
                'skips': 0,
                'subject': {'title': 'missmatch-node3.example.com'},
                'subject-type': 'certname',
                'successes': 0
            }
        }

        reports_data = {
            'changed-node.example.com': {
                'status': 'changed',
            },
            'pending-node.example.com': {
                'status': 'pending',
            },
            'failed-node.example.com': {
                'status': 'failed',
            },
            'missmatch-node1.example.com': {
                'status': 'failed',
            },
            'missmatch-node2.example.com': {
                'status': 'changed',
            },
            'missmatch-node3.example.com': {
                'status': 'pending',
            },
            'unreported-node.example.com': {
                'status': 'unchanged',
            },
            'unchanged-node.example.com': {
                'status': 'unchanged',
            }
        }

        merged_list = dictstatus(nodes_data,
                                 reports_data,
                                 events_data,
                                 sort=False,
                                 get_status='all')
        # ('certname', 'latestCatalog', 'latestReport', 'latestFacts', 'success', 'noop', 'failure', 'skipped')
        merged_expected = [
            (
                'failed-node.example.com',
                filters.date(localtime(json_to_datetime(nodes_timestamps['failed-node']['catalog'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['failed-node']['report'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['failed-node']['facts'])),
                             'Y-m-d H:i:s'),
                5, 0, 20, 10, 'failed'
            ),
            (
                'missmatch-node1.example.com',
                filters.date(localtime(json_to_datetime(nodes_timestamps['missmatch-node1']['catalog'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['missmatch-node1']['report'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['missmatch-node1']['facts'])),
                             'Y-m-d H:i:s'),
                5, 0, 20, 10, 'failed'
            ),
            (
                'missmatch-node2.example.com',
                filters.date(localtime(json_to_datetime(nodes_timestamps['missmatch-node2']['catalog'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['missmatch-node2']['report'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['missmatch-node2']['facts'])),
                             'Y-m-d H:i:s'),
                25, 0, 0, 0, 'changed'
            ),
            (
                'missmatch-node3.example.com',
                filters.date(localtime(json_to_datetime(nodes_timestamps['missmatch-node3']['catalog'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['missmatch-node3']['report'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['missmatch-node3']['facts'])),
                             'Y-m-d H:i:s'),
                0, 50, 0, 0, 'pending'
            ),
            (
                'unreported-node.example.com',
                filters.date(localtime(json_to_datetime(nodes_timestamps['unreported-node']['catalog'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['unreported-node']['report'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['unreported-node']['facts'])),
                             'Y-m-d H:i:s'),
                0, 0, 0, 0, 'unchanged'
            ),
            (
                'changed-node.example.com',
                filters.date(localtime(json_to_datetime(nodes_timestamps['changed-node']['catalog'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['changed-node']['report'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['changed-node']['facts'])),
                             'Y-m-d H:i:s'),
                78, 0, 0, 0, 'changed'
            ),
            (
                'unchanged-node.example.com',
                filters.date(localtime(json_to_datetime(nodes_timestamps['unchanged-node']['catalog'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['unchanged-node']['report'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['unchanged-node']['facts'])),
                             'Y-m-d H:i:s'),
                0, 0, 0, 0, 'unchanged'
            ),
            (
                'pending-node.example.com',
                filters.date(localtime(json_to_datetime(nodes_timestamps['pending-node']['catalog'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['pending-node']['report'])),
                             'Y-m-d H:i:s'),
                filters.date(localtime(json_to_datetime(nodes_timestamps['pending-node']['facts'])),
                             'Y-m-d H:i:s'),
                0, 100, 0, 0, 'pending'
            )]
        # failed_list, changed_list, unreported_list, mismatch_list, pending_list
        merged_list.sort(key=lambda tup: tup[0])
        merged_expected.sort(key=lambda tup: tup[0])
        self.assertEqual(merged_list, merged_expected)
Example #10
0
    def test_nodes_seperated_data(self):
        nodes_timestamps = {
            'failed-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node1': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=55)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node2': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=55)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'missmatch-node3': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=50)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'unreported-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=127)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=126)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=125)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'changed-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=11)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=9)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'unchanged-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=25)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=24)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=23)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
            'pending-node': {
                'catalog': ((datetime.utcnow() - timedelta(minutes=16)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'facts': ((datetime.utcnow() - timedelta(minutes=13)).strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
                'report': ((datetime.utcnow() - timedelta(minutes=10)).strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
            },
        }

        nodes_data = {
            'failed-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['failed-node']['catalog'],
                'certname': 'failed-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['failed-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['failed-node']['report']
            },
            'missmatch-node1.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['missmatch-node1']['catalog'],
                'certname': 'missmatch-node1.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['missmatch-node1']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['missmatch-node1']['report']
            },
            'missmatch-node2.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['missmatch-node2']['catalog'],
                'certname': 'missmatch-node2.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['missmatch-node2']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['missmatch-node2']['report']
            },
            'missmatch-node3.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['missmatch-node3']['catalog'],
                'certname': 'missmatch-node3.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['missmatch-node3']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['missmatch-node3']['report']
            },
            'unreported-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['unreported-node']['catalog'],
                'certname': 'unreported-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['unreported-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['unreported-node']['report']
            },
            'changed-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['changed-node']['catalog'],
                'certname': 'changed-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['changed-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['changed-node']['report']
            },
            'unchanged-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['unchanged-node']['catalog'],
                'certname': 'unchanged-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['unchanged-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['unchanged-node']['report'],
            },
            'pending-node.example.com': {
                'catalog_environment': 'production',
                'catalog_timestamp': nodes_timestamps['pending-node']['catalog'],
                'certname': 'pending-node.example.com',
                'deactivated': None,
                'facts_environment': 'production',
                'facts_timestamp': nodes_timestamps['pending-node']['facts'],
                'report_environment': 'production',
                'report_timestamp': nodes_timestamps['pending-node']['report'],
            }}

        events_data = {
            'changed-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {'title': 'changed-node.example.com'},
                'subject-type': 'certname',
                'successes': 78
            },
            'pending-node.example.com': {
                'failures': 0,
                'noops': 100,
                'skips': 0,
                'subject': {'title': 'pending-node.example.com'},
                'subject-type': 'certname',
                'successes': 0
            },
            'unreported-node.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {'title': 'unreported-node.example.com'},
                'subject-type': 'certname',
                'successes': 0
            },
            'failed-node.example.com': {
                'failures': 20,
                'noops': 0,
                'skips': 10,
                'subject': {'title': 'failed-node.example.com'},
                'subject-type': 'certname',
                'successes': 5
            },
            'missmatch-node1.example.com': {
                'failures': 20,
                'noops': 0,
                'skips': 10,
                'subject': {'title': 'missmatch-node1.example.com'},
                'subject-type': 'certname',
                'successes': 5
            },
            'missmatch-node2.example.com': {
                'failures': 0,
                'noops': 0,
                'skips': 0,
                'subject': {'title': 'missmatch-node2.example.com'},
                'subject-type': 'certname',
                'successes': 25
            },
            'missmatch-node3.example.com': {
                'failures': 0,
                'noops': 50,
                'skips': 0,
                'subject': {'title': 'missmatch-node3.example.com'},
                'subject-type': 'certname',
                'successes': 0
            }
        }
        reports_data = {
            'changed-node.example.com': {
                'status': 'changed',
            },
            'pending-node.example.com': {
                'status': 'unchanged',
            },
            'failed-node.example.com': {
                'status': 'failed',
            },
            'unreported-node.example.com': {
                'status': 'unchanged',
            },
            'missmatch-node1.example.com': {
                'status': 'failed',
            },
            'missmatch-node2.example.com': {
                'status': 'changed',
            },
            'missmatch-node3.example.com': {
                'status': 'unchanged',
            }
        }
        failed_list, changed_list, unreported_list, missmatch_list, pending_list = dictstatus(nodes_data,
                                                                                              reports_data,
                                                                                              events_data,
                                                                                              sort=False,
                                                                                              get_status='notall')
        # ('certname', 'latestCatalog', 'latestReport', 'latestFacts', 'success', 'noop', 'failure', 'skipped')
        failed_expected = [(
            'failed-node.example.com',
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['failed-node']['catalog'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['failed-node']['report'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['failed-node']['facts'])),
                'Y-m-d H:i:s'),
            5, 0, 20, 10, 'failed'), (
            'missmatch-node1.example.com',
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node1']['catalog'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node1']['report'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node1']['facts'])),
                'Y-m-d H:i:s'),
            5, 0, 20, 10, 'failed')]

        changed_expected = [
            (
                'missmatch-node2.example.com',
                filters.date(
                    localtime(json_to_datetime(nodes_timestamps['missmatch-node2']['catalog'])),
                    'Y-m-d H:i:s'),
                filters.date(
                    localtime(json_to_datetime(nodes_timestamps['missmatch-node2']['report'])),
                    'Y-m-d H:i:s'),
                filters.date(
                    localtime(json_to_datetime(nodes_timestamps['missmatch-node2']['facts'])),
                    'Y-m-d H:i:s'),
                25, 0, 0, 0, 'changed'
            ),
            (
                'changed-node.example.com',
                filters.date(
                    localtime(json_to_datetime(nodes_timestamps['changed-node']['catalog'])),
                    'Y-m-d H:i:s'),
                filters.date(
                    localtime(json_to_datetime(nodes_timestamps['changed-node']['report'])),
                    'Y-m-d H:i:s'),
                filters.date(
                    localtime(json_to_datetime(nodes_timestamps['changed-node']['facts'])),
                    'Y-m-d H:i:s'),
                78, 0, 0, 0, 'changed'
            ),
        ]

        unreported_expected = [(
            'unreported-node.example.com',
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['unreported-node']['catalog'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['unreported-node']['report'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['unreported-node']['facts'])),
                'Y-m-d H:i:s'),
            0, 0, 0, 0, 'unchanged')]

        missmatch_expected = [(
            'missmatch-node1.example.com',
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node1']['catalog'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node1']['report'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node1']['facts'])),
                'Y-m-d H:i:s'),
            5, 0, 20, 10, 'failed'), (
            'missmatch-node2.example.com',
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node2']['catalog'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node2']['report'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node2']['facts'])),
                'Y-m-d H:i:s'),
            25, 0, 0, 0, 'changed'), (
            'missmatch-node3.example.com',
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node3']['catalog'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node3']['report'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node3']['facts'])),
                'Y-m-d H:i:s'),
            0, 50, 0, 0, 'pending')]

        pending_expected = [(
            'missmatch-node3.example.com',
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node3']['catalog'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node3']['report'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['missmatch-node3']['facts'])),
                'Y-m-d H:i:s'),
            0, 50, 0, 0, 'pending'), (
            'pending-node.example.com',
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['pending-node']['catalog'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['pending-node']['report'])),
                'Y-m-d H:i:s'),
            filters.date(
                localtime(json_to_datetime(nodes_timestamps['pending-node']['facts'])),
                'Y-m-d H:i:s'),
            0, 100, 0, 0, 'pending')]
        # Sort lists so its easier to verify...
        failed_list.sort(key=lambda tup: tup[0])
        failed_expected.sort(key=lambda tup: tup[0])
        changed_list.sort(key=lambda tup: tup[0])
        changed_expected.sort(key=lambda tup: tup[0])
        unreported_list.sort(key=lambda tup: tup[0])
        unreported_expected.sort(key=lambda tup: tup[0])
        missmatch_list.sort(key=lambda tup: tup[0])
        missmatch_expected.sort(key=lambda tup: tup[0])
        pending_list.sort(key=lambda tup: tup[0])
        pending_expected.sort(key=lambda tup: tup[0])
        if failed_list != failed_expected:
            self.fail(msg='Failed list does not match expectations.')
        if changed_list != changed_expected:
            self.fail(msg='Changed list does not match expectations.')
        if unreported_list != unreported_expected:
            self.fail(msg='Unreported list does not match expectations.')
        if missmatch_list != missmatch_expected:
            self.fail(msg='Missmatching list does not match expectations.')
        if pending_list != pending_expected:
            self.fail(msg='Pending list does not match expectations.')
        self.assertTrue(True)
Example #11
0
def dashboard(request, certname=None):
    context = default_context
    if request.method == 'GET':
        if 'source' in request.GET:
            source = request.GET.get('source')
            set_server(request, source)
    if request.method == 'POST':
        request.session['django_timezone'] = request.POST['timezone']
        return redirect(request.POST['url'])

    source_url, source_certs, source_verify = get_server(request)
    puppet_run_time = get_server(request, type='run_time')
    events_params = {
        'query':
            {
                1: '["and",["=","latest-report?",true],["in", "certname",["extract", "certname",["select-nodes",["null?","deactivated",true]]]]]'
            },
        'summarize-by': 'certname',
    }
    nodes_params = {
        'limit': 25,
        'order-by': {
            'order-field': {
                'field': 'report-timestamp',
                'order': 'desc',
            },
            'query-field': {'field': 'certname'},
        },
    }

    jobs = {
        'population': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'population',
            'path': '/metrics/mbean/com.puppetlabs.puppetdb.query.population:type=default,name=num-nodes',
        },
        'tot_resource': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'tot_resource',
            'path': '/metrics/mbean/com.puppetlabs.puppetdb.query.population:type=default,name=num-resources',
        },
        'avg_resource': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'avg_resource',
            'path': '/metrics/mbean/com.puppetlabs.puppetdb.query.population:type=default,name=avg-resources-per-node',
        },
        'all_nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'all_nodes',
            'path': '/nodes',
        },
        'events': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'id': 'event-counts',
            'path': 'event-counts',
            'api_version': 'v4',
            'params': events_params,
        },
        'nodes': {
            'url': source_url,
            'certs': source_certs,
            'verify': source_verify,
            'api_version': 'v4',
            'id': 'nodes',
            'path': '/nodes',
            'params': nodes_params,
        },
    }
    puppetdb_results = run_puppetdb_jobs(jobs)
    # Dashboard to show nodes of "recent, failed, unreported or changed"
    dashboard_show = request.GET.get('show', 'recent')

    # Assign vars from the completed jobs
    puppet_population = puppetdb_results['population']
    # Total resources managed by puppet metric
    total_resources = puppetdb_results['tot_resource']
    # Average resource per node metric
    avg_resource_node = puppetdb_results['avg_resource']
    # Information about all active nodes in puppet
    all_nodes_list = puppetdb_results['all_nodes']
    # All available events for the latest puppet reports
    event_list = puppetdb_results['event-counts']
    node_list = puppetdb_results['nodes']

    failed_list, changed_list, unreported_list, mismatch_list, pending_list = dictstatus(all_nodes_list,
                                                                                         event_list,
                                                                                         sort=True,
                                                                                         sortby='latestReport',
                                                                                         get_status='notall',
                                                                                         puppet_run_time=puppet_run_time)
    pending_list = [x for x in pending_list if x not in unreported_list]
    changed_list = [x for x in changed_list if
                    x not in unreported_list and x not in failed_list and x not in pending_list]
    failed_list = [x for x in failed_list if x not in unreported_list]
    unreported_list = [x for x in unreported_list if x not in failed_list]

    if dashboard_show == 'recent':
        merged_nodes_list = dictstatus(
            node_list, event_list, sort=False, get_status="all", puppet_run_time=puppet_run_time)
    elif dashboard_show == 'failed':
        merged_nodes_list = failed_list
    elif dashboard_show == 'unreported':
        merged_nodes_list = unreported_list
    elif dashboard_show == 'changed':
        merged_nodes_list = changed_list
    elif dashboard_show == 'failed_catalogs':
        merged_nodes_list = mismatch_list
    elif dashboard_show == 'pending':
        merged_nodes_list = pending_list
    else:
        merged_nodes_list = dictstatus(
            node_list, event_list, sort=False, get_status="all", puppet_run_time=puppet_run_time)

    node_unreported_count = len(unreported_list)
    node_fail_count = len(failed_list)
    node_change_count = len(changed_list)
    node_off_timestamps_count = len(mismatch_list)
    node_pending_count = len(pending_list)
    context['node_list'] = merged_nodes_list
    context['certname'] = certname
    context['show_nodes'] = dashboard_show
    context['population'] = puppet_population['Value']
    context['total_resource'] = total_resources['Value']
    context['avg_resource'] = "{:.2f}".format(avg_resource_node['Value'])
    context['failed_nodes'] = node_fail_count
    context['changed_nodes'] = node_change_count
    context['unreported_nodes'] = node_unreported_count
    context['weird_timestamps'] = node_off_timestamps_count
    context['pending_nodes'] = node_pending_count

    return render(request, 'pano/index.html', context)