Ejemplo n.º 1
0
 def test_greater_equal_operator(self):
     assert str(GreaterEqualOperator("uptime_days", 3))\
         == '[">=", "uptime_days", 3]'
     assert str(GreaterEqualOperator(
         "start_time",
         "2016-05-11T23:53:29.962Z"))\
         == '[">=", "start_time", "2016-05-11T23:53:29.962Z"]'
     assert str(GreaterEqualOperator(['parameter', 'version'], 4.0))\
         == '[">=", ["parameter", "version"], 4.0]'
     assert str(GreaterEqualOperator("report_timestamp",
                                     datetime.datetime(2016, 6, 11)))\
         == '[">=", "report_timestamp", "2016-06-11 00:00:00"]'
Ejemplo n.º 2
0
def _puppetdb_nodes(rules, master_id, master_address):
    if "facts" not in rules or not rules["facts"]:
        return set()

    if rules["match_type"] == "ALL":
        operator = AndOperator()
    else:
        operator = OrOperator()
    for fact_rule in rules["facts"]:
        rule_op = fact_rule["operator"]
        lhs = ["fact", fact_rule["fact"]]
        rhs = fact_rule["value"]
        if rule_op == "=":
            operator.add(EqualsOperator(lhs, rhs))
        elif rule_op == "!=":
            notop = NotOperator()
            notop.add(EqualsOperator(lhs, rhs))
            operator.add(notop)
        elif rule_op == "~":
            operator.add(RegexOperator(lhs, rhs))
        elif rule_op == "!~":
            notop = NotOperator()
            notop.add(RegexOperator(lhs, rhs))
            operator.add(notop)
        elif rule_op == ">":
            operator.add(GreaterOperator(lhs, rhs))
        elif rule_op == ">=":
            operator.add(GreaterEqualOperator(lhs, rhs))
        elif rule_op == "<":
            operator.add(LessOperator(lhs, rhs))
        elif rule_op == "<=":
            operator.add(LessEqualOperator(lhs, rhs))

    query_str = str(operator).replace("'", '"')

    cert_instance = CertsClass(master_id)
    cert_path = cert_instance.get_cert()
    private_key_path = cert_instance.get_key()

    req = requests.get(
        f"https://{master_address}:8081/pdb/meta/v1/version",
        verify=False,
        cert=(cert_path, private_key_path),
    )
    if req.status_code == 404:
        # PuppetDB <= 2.x
        nodes_uri = f"https://{master_address}:8081/v4/nodes?query={query_str}"
    else:
        # PuppetDB >= 3.x
        nodes_uri = (
            f"https://{master_address}:8081/pdb/query/v4/nodes?query={query_str}"
        )
    # Fetch the matching nodes
    req = requests.get(nodes_uri,
                       verify=False,
                       cert=(cert_path, private_key_path))
    return {node["certname"] for node in req.json()}
Ejemplo n.º 3
0
def _build_query(env, start, end, certname=None):
    """Build a extract query with optional certname and environment."""
    query = ExtractOperator()
    query.add_field(FunctionOperator('count'))
    query.add_field('status')
    subquery = AndOperator()
    subquery.add(GreaterEqualOperator('producer_timestamp', start))
    subquery.add(LessOperator('producer_timestamp', end))
    if certname is not None:
        subquery.add(EqualsOperator('certname', certname))
    if env != '*':
        subquery.add(EqualsOperator('environment', env))
    query.add_query(subquery)
    query.add_group_by("status")
    return query
Ejemplo n.º 4
0
def reports_ajax(env, node_name):
    """Query and Return JSON data to reports Jquery datatable

    :param env: Search for all reports in this environment
    :type env: :obj:`string`
    """
    draw = int(request.args.get('draw', 0))
    start = int(request.args.get('start', 0))
    length = int(request.args.get('length', app.config['NORMAL_TABLE_COUNT']))
    paging_args = {'limit': length, 'offset': start}
    search_arg = request.args.get('search[value]')
    order_column = int(request.args.get('order[0][column]', 0))
    order_filter = REPORTS_COLUMNS[order_column].get(
        'filter', REPORTS_COLUMNS[order_column]['attr'])
    order_dir = request.args.get('order[0][dir]', 'desc')
    order_args = '[{"field": "%s", "order": "%s"}]' % (order_filter, order_dir)
    status_args = request.args.get('columns[1][search][value]', '').split('|')
    date_args = request.args.get('columns[0][search][value]', '')
    max_col = len(REPORTS_COLUMNS)
    for i in range(len(REPORTS_COLUMNS)):
        if request.args.get("columns[%s][data]" % i, None):
            max_col = i + 1

    envs = environments()
    check_env(env, envs)
    reports_query = AndOperator()

    if env != '*':
        reports_query.add(EqualsOperator("environment", env))

    if node_name:
        reports_query.add(EqualsOperator("certname", node_name))

    if search_arg:
        search_query = OrOperator()
        search_query.add(RegexOperator("certname", r"%s" % search_arg))
        search_query.add(RegexOperator("puppet_version", r"%s" % search_arg))
        search_query.add(
            RegexOperator("configuration_version", r"%s" % search_arg))
        reports_query.add(search_query)

    if date_args:
        dates = json.loads(date_args)

        if len(dates) > 0:
            date_query = AndOperator()

            if 'min' in dates:
                date_query.add(GreaterEqualOperator('end_time', dates['min']))

            if 'max' in dates:
                date_query.add(LessEqualOperator('end_time', dates['max']))

            reports_query.add(date_query)

    status_query = OrOperator()
    for status_arg in status_args:
        if status_arg in ['failed', 'changed', 'unchanged']:
            arg_query = AndOperator()
            arg_query.add(EqualsOperator('status', status_arg))
            arg_query.add(EqualsOperator('noop', False))
            status_query.add(arg_query)
            if status_arg == 'unchanged':
                arg_query = AndOperator()
                arg_query.add(EqualsOperator('noop', True))
                arg_query.add(EqualsOperator('noop_pending', False))
                status_query.add(arg_query)
        elif status_arg == 'noop':
            arg_query = AndOperator()
            arg_query.add(EqualsOperator('noop', True))
            arg_query.add(EqualsOperator('noop_pending', True))
            status_query.add(arg_query)

    if len(status_query.operations) == 0:
        if len(reports_query.operations) == 0:
            reports_query = None
    else:
        reports_query.add(status_query)

    if status_args[0] != 'none':
        reports = get_or_abort(puppetdb.reports,
                               query=reports_query,
                               order_by=order_args,
                               include_total=True,
                               **paging_args)
        reports, reports_events = tee(reports)
        total = None
    else:
        reports = []
        reports_events = []
        total = 0

    # Convert metrics to relational dict
    metrics = {}
    for report in reports_events:
        if total is None:
            total = puppetdb.total

        metrics[report.hash_] = {}
        for m in report.metrics:
            if m['category'] not in metrics[report.hash_]:
                metrics[report.hash_][m['category']] = {}
            metrics[report.hash_][m['category']][m['name']] = m['value']

    if total is None:
        total = 0

    return render_template('reports.json.tpl',
                           draw=draw,
                           total=total,
                           total_filtered=total,
                           reports=reports,
                           metrics=metrics,
                           envs=envs,
                           current_env=env,
                           columns=REPORTS_COLUMNS[:max_col])