<commit_before>#!/usr/bin/env python

""" Quick and dirty example to set flags on host/service checks for icinga2 """

""" 
Quick and dirty program to enable/disable host/service checks for icinga2 for
hostgroups matching a pattern

EXAMPLES:

# Set 'enable_active_checks' to 'true' for all hosts in the hostgroups matching 'webs' for 'prod'
# icinga2 profile
$proganame prod enable_active_checks:true hosts webs 

# Set 'enable_notifications' to 'false' for all services in the hostgroups matching 'webs' for 'prod'
# icinga2 profile
$proganame prod enable_notifications:false services webs 
"""

import os
import sys
import json
import find_hostgroups
from icinga2_api import api

USAGE = 'USAGE: %s host_profile enable|disable hosts|services hostgroup_pattern' %\
        os.path.basename(__file__)

class HostgroupChecksException(Exception):
  """ Exception class for this program """
  pass

def main(args):
  """ enable|disable checks for hosts|services for matching hostgroups """

  if len(args) != 4:
    raise HostgroupChecksException({'message': 'Invalid number of args'})

  icinga2_host_profile = args[0]
  obj = api.Api(profile=icinga2_host_profile)

  action_state = args[1]
  action, state = action_state.split(':')
  if state == 'true':
    state = True
  else:
    state = False
  # no validation for action as of now

  target = args[2]
  if target != 'hosts' and target != 'services':
    raise HostgroupChecksException({'message': 'Invalid action target - %s' % target})

  # find the hostgroups
  hostgroup_pattern = args[3]
  target_hostgroups_ds = find_hostgroups.main([icinga2_host_profile,
                                               'hostgroups', hostgroup_pattern])
  hostgroup_names = [x['name'] for x in target_hostgroups_ds]

  # do the deed
  uri = '/v1/objects/%s' % target
  status = {}

  for hostgroup_name in hostgroup_names:
    if action not in status:
      status[action] = {'success': [], 'failure': []}

    print "STATUS: %s: %s" % (hostgroup_name, action)
    data = {"attrs": {action: state},
            "filter": "\"%s\" in host.groups" % hostgroup_name}
    output = obj.update(uri, data)

    print "STATUS: %s: %s: %s" % (hostgroup_name, action, output['status'].upper())<commit_msg>adding wildcard to apply to all hostgroups<commit_after>#!/usr/bin/env python

""" Quick and dirty example to set flags on host/service checks for icinga2 """

""" 
Quick and dirty program to enable/disable host/service checks for icinga2 for
hostgroups matching a pattern

EXAMPLES:

# Set 'enable_active_checks' to 'true' for all hosts in the hostgroups matching 'webs' for 'prod'
# icinga2 profile
$proganame prod enable_active_checks:true hosts webs 

# Set 'enable_notifications' to 'false' for all services in the hostgroups matching 'webs' for 'prod'
# icinga2 profile
$proganame prod enable_notifications:false services webs 
"""

import os
import sys
import json
import find_hostgroups
from icinga2_api import api

USAGE = 'USAGE: %s host_profile enable|disable hosts|services hostgroup_pattern' %\
        os.path.basename(__file__)

class HostgroupChecksException(Exception):
  """ Exception class for this program """
  pass

def main(args):
  """ enable|disable checks for hosts|services for matching hostgroups """

  if len(args) != 4:
    raise HostgroupChecksException({'message': 'Invalid number of args'})

  icinga2_host_profile = args[0]
  obj = api.Api(profile=icinga2_host_profile)

  action_state = args[1]
  action, state = action_state.split(':')

  if state == 'true':
    state = True
  else:
    state = False
  # no validation for action as of now

  target = args[2]
  if target != 'hosts' and target != 'services':
    raise HostgroupChecksException({'message': 'Invalid action target - %s' % target})

  # find the hostgroups
  hostgroup_pattern = args[3]

  uri = '/v1/objects/%s' % target
  status = {}
  if action not in status:
    status[action] = {'success': [], 'failure': []}

  if hostgroup_pattern == '.+':
    data = {"attrs": {action: state}}
    output = obj.update(uri, data)
    if output['status'] != 'success':
      status[action]['failure'].append(output)
    else:
      status[action]['success'].append(output)
    return status

  target_hostgroups_ds = find_hostgroups.main([icinga2_host_profile,
                                               'hostgroups', hostgroup_pattern])
  hostgroup_names = [x['name'] for x in target_hostgroups_ds]

  # do the deed

  for hostgroup_name in hostgroup_names:
    print "STATUS: %s: %s" % (hostgroup_name, action)
    data = {"attrs": {action: state},
            "filter": "\"%s\" in host.groups" % hostgroup_name}
    output = obj.update(uri, data)

    print "STATUS: %s: %s: %s" % (hostgroup_name, action, output['status'].upper())
{'fraction_non_alphanumeric': 0.11726452695615691, 'fraction_numerical': 0.005873715124816446, 'mean_word_length': 3.8952772073921973, 'pattern_counts': {'":': 5, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4719593', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1450, 'n_words': 456}