def test_with_add_function_operator(self): op = ExtractOperator() op.add_field(FunctionOperator('to_string', 'producer_timestamp', 'FMDAY')) op.add_field(FunctionOperator('count')) op.add_group_by(FunctionOperator('to_string', 'producer_timestamp', 'FMDAY')) assert str(op) == '["extract", '\ '[["function", "to_string", "producer_timestamp", "FMDAY"], '\ '["function", "count"]], '\ '["group_by", '\ '["function", "to_string", "producer_timestamp", "FMDAY"]]]' assert repr(op) == 'Query: ["extract", '\ '[["function", "to_string", "producer_timestamp", "FMDAY"], '\ '["function", "count"]], '\ '["group_by", '\ '["function", "to_string", "producer_timestamp", "FMDAY"]]]' assert str(op) == '["extract", ' \ '[["function", "to_string", "producer_timestamp", "FMDAY"], '\ '["function", "count"]], '\ '["group_by", '\ '["function", "to_string", "producer_timestamp", "FMDAY"]]]'
def main(): """main entry point""" args = get_args() logging.basicConfig(level=get_log_level(args.verbose)) failed_nodes = [] pdb = connect() nodes = defaultdict(dict) max_age = datetime.utcnow() - timedelta(hours=args.max_age) extract = ExtractOperator() extract.add_field(['certname', FunctionOperator('count'), 'status']) extract.add_group_by(['certname', 'status']) extract.add_query(GreaterOperator('receive_time', max_age.isoformat())) # pypuppetdb does have a `reports` method which wraps `_query`. however it yields # results of type pypuppetdb.Report which expects a number of parameters e.g. hash # to be present in the result payload. however we don't extract theses values and # therefore have to resort to the more powerful private method reports = pdb._query('reports', query=extract) # pylint: disable=protected-access for report in reports: nodes[report['certname']][report['status']] = report['count'] if args.dev: failed_nodes = [hostname for hostname, node in nodes.items() if not node.get('unchanged', 0)] else: for fqdn, node in nodes.items(): # skip hosts with no unchanged reports: if node.get('unchanged', 0): continue # skip staging servers: # - hostname starting labstest* # - hostname ending dev or dev\d{4} # - hostname ending test or test\d{4} if (fqdn.startswith('labtest') or search(r'(:?dev|test)(:?\d{4})?$', fqdn.split('.')[0]) is not None): logger.debug('%s: Skipping staging host', fqdn) continue failed_nodes.append(fqdn) if len(failed_nodes) >= args.critical: print('CRITICAL: the following ({}) node(s) change every puppet run: {}'.format( len(failed_nodes), ', '.join(failed_nodes))) return 2 if len(failed_nodes) >= args.warning: print('WARNING: the following ({}) node(s) change every puppet run: {}'.format( len(failed_nodes), ', '.join(failed_nodes))) return 1 print('OK: all nodes running as expected') return 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
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('start_time', start)) subquery.add(LessOperator('start_time', 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
def main(): """main entry point""" args = get_args() logging.basicConfig(level=get_log_level(args.verbose)) pdb = connect() nodes = defaultdict(dict) max_age = datetime.utcnow() - timedelta(hours=args.max_age) extract = ExtractOperator() extract.add_field(['certname', FunctionOperator('count'), 'status']) extract.add_group_by(['certname', 'status']) extract.add_query(GreaterOperator('receive_time', max_age.isoformat())) # pypuppetdb does have a `reports` method which wraps `_query`. however it yields # results of type pypuppetdb.Report which expects a number of parameters e.g. hash # to be present in the result payload. however we don't extract theses values and # therefore have to resort to the more powerful private method reports = pdb._query('reports', query=extract) # pylint: disable=protected-access for report in reports: nodes[report['certname']][report['status']] = report['count'] failed_nodes = [ hostname for hostname, node in nodes.items() if not node.get('unchanged', 0) ] if len(failed_nodes) >= args.critical: print( 'CRITICAL: the following ({}) node(s) change every puppet run: {}'. format(len(failed_nodes), ', '.join(failed_nodes))) return 2 if len(failed_nodes) >= args.warning: print( 'WARNING: the following ({}) node(s) change every puppet run: {}'. format(len(failed_nodes), ', '.join(failed_nodes))) return 1 print('OK: all nodes running as expected') return 0
def test_with_add_group_by(self): op = ExtractOperator() op.add_field(['certname', 'fact_environment', 'catalog_environment']) op.add_query(EqualsOperator('domain', 'example.com')) op.add_group_by(["fact_environment", "catalog_environment"]) with pytest.raises(APIError): op.add_group_by({"deactivated": False}) assert repr(op) == 'Query: ["extract", '\ '["certname", "fact_environment", "catalog_environment"], '\ '["=", "domain", "example.com"], '\ '["group_by", "fact_environment", "catalog_environment"]]' assert str(op) == '["extract", '\ '["certname", "fact_environment", "catalog_environment"], '\ '["=", "domain", "example.com"], '\ '["group_by", "fact_environment", "catalog_environment"]]' assert str(op) == '["extract", ' \ '["certname", "fact_environment", "catalog_environment"], '\ '["=", "domain", "example.com"], '\ '["group_by", "fact_environment", "catalog_environment"]]'