<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2014, <NAME> <<EMAIL>>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#

DOCUMENTATION = '''
---
module: sensu_check
short_description: Manage Sensu checks
version_added: 2.0
description:
  - Manage the checks that should be run on a machine by I(Sensu).
  - Most options do not have a default and will not be added to the check definition unless specified.
  - All defaults except I(path), I(state), I(backup) and I(metric) are not managed by this module,
  - they are simply specified for your convenience.
options:
  name:
    description:
      - The name of the check
      - This is the key that is used to determine whether a check exists
    required: true
  state:
    description: Whether the check should be present or not
    choices: [ 'present', 'absent' ]
    required: false
    default: present
  path:
    description:
      - Path to the json file of the check to be added/removed.
      - Will be created if it does not exist (unless I(state=absent)).
      - The parent folders need to exist when I(state=present), otherwise an error will be thrown
    required: false
    default: /etc/sensu/conf.d/checks.json
  backup:
    description:
      - Create a backup file (if yes), including the timestamp information so
      - you can get the original file back if you somehow clobbered it incorrectly.
    choices: [ 'yes', 'no' ]
    required: false
    default: no
  command:
    description:
      - Path to the sensu check to run (not required when I(state=absent))
    required: true
  handlers:
    description:
      - List of handlers to notify when the check fails
    required: false
    default: []
  subscribers:
    description:
      - List of subscribers/channels this check should run for
      - See sensu_subscribers to subscribe a machine to a channel
    required: false
    default: []
  interval:
    description:
      - Check interval in seconds
    required: false
    default: null
  timeout:
    description:
      - Timeout for the check
    required: false
    default: 10
  handle:
    description:
      - Whether the check should be handled or not
    choices: [ 'yes', 'no' ]
    required: false
    default: yes
  subdue_begin:
    description:
      - When to disable handling of check failures
    required: false
    default: null
  subdue_end:
    description:
      - When to enable handling of check failures
    required: false
    default: null
  dependencies:
    description:
      - Other checks this check depends on, if dependencies fail,
      - handling of this check will be disabled
    required: false
    default: []
  metric:
    description: Whether the check is a metric
    choices: [ 'yes', 'no' ]
    required: false
    default: no
  standalone:
    description:
      - Whether the check should be scheduled by the sensu client or server
      - This option obviates the need for specifying the I(subscribers) option
    choices: [ 'yes', 'no' ]
    required: false
    default: no
  publish:
    description:
      - Whether the check should be scheduled at all.
      - You can still issue it via the sensu api
    choices: [ 'yes', 'no' ]
    required: false
    default: yes
  occurrences:
    description:
      - Number of event occurrences before the handler should take action
    required: false
    default: 1
  refresh:
    description:
      - Number of seconds handlers should wait before taking second action
    required: false
    default: null
  aggregate:
    description:
      - Classifies the check as an aggregate check,
      - making it available via the aggregate API
    choices: [ 'yes', 'no' ]
    required: false
    default: no
  low_flap_threshold:
    description:
      - The low threshhold for flap detection
    required: false
    default: null
  high_flap_threshold:
    description:
      - The low threshhold for flap detection
    required: false
    default: null
requirements: [ ]
author: "<NAME> (@andsens)"
'''

EXAMPLES = '''
# Fetch metrics about the CPU load every 60 seconds,
# the sensu server has a handler called 'relay' which forwards stats to graphite
- name: get cpu metrics
  sensu_check: name=cpu_load
               command=/etc/sensu/plugins/system/cpu-mpstat-metrics.rb
               metric=yes handlers=relay subscribers=common interval=60

# Check whether nginx is running
- name: check nginx process
  sensu_check: name=nginx_running
               command='/etc/sensu/plugins/processes/check-procs.rb -f /var/run/nginx.pid'
               handlers=default subscribers=nginx interval=60

# Stop monitoring the disk capacity.
# Note that the check will still show up in the sensu dashboard,
# to remove it completely you need to issue a DELETE request to the sensu api.
- name: check disk
  sensu_check: name=check_disk_capacity
'''


def sensu_check(module, path, name, state='present', backup=False):
    changed = False
    reasons = []

    try:
        import json
    except ImportError:
        import simplejson as json

    stream = None
    try:
        try:
            stream = open(path, 'r')<commit_msg>Fix state & metric description<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# (c) 2014, <NAME> <<EMAIL>>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
#

DOCUMENTATION = '''
---
module: sensu_check
short_description: Manage Sensu checks
version_added: 2.0
description:
  - Manage the checks that should be run on a machine by I(Sensu).
  - Most options do not have a default and will not be added to the check definition unless specified.
  - All defaults except I(path), I(state), I(backup) and I(metric) are not managed by this module,
  - they are simply specified for your convenience.
options:
  name:
    description:
      - The name of the check
      - This is the key that is used to determine whether a check exists
    required: true
  state:
    description:
      - Whether the check should be present or not
    choices: [ 'present', 'absent' ]
    required: false
    default: present
  path:
    description:
      - Path to the json file of the check to be added/removed.
      - Will be created if it does not exist (unless I(state=absent)).
      - The parent folders need to exist when I(state=present), otherwise an error will be thrown
    required: false
    default: /etc/sensu/conf.d/checks.json
  backup:
    description:
      - Create a backup file (if yes), including the timestamp information so
      - you can get the original file back if you somehow clobbered it incorrectly.
    choices: [ 'yes', 'no' ]
    required: false
    default: no
  command:
    description:
      - Path to the sensu check to run (not required when I(state=absent))
    required: true
  handlers:
    description:
      - List of handlers to notify when the check fails
    required: false
    default: []
  subscribers:
    description:
      - List of subscribers/channels this check should run for
      - See sensu_subscribers to subscribe a machine to a channel
    required: false
    default: []
  interval:
    description:
      - Check interval in seconds
    required: false
    default: null
  timeout:
    description:
      - Timeout for the check
    required: false
    default: 10
  handle:
    description:
      - Whether the check should be handled or not
    choices: [ 'yes', 'no' ]
    required: false
    default: yes
  subdue_begin:
    description:
      - When to disable handling of check failures
    required: false
    default: null
  subdue_end:
    description:
      - When to enable handling of check failures
    required: false
    default: null
  dependencies:
    description:
      - Other checks this check depends on, if dependencies fail,
      - handling of this check will be disabled
    required: false
    default: []
  metric:
    description:
      - Whether the check is a metric
    choices: [ 'yes', 'no' ]
    required: false
    default: no
  standalone:
    description:
      - Whether the check should be scheduled by the sensu client or server
      - This option obviates the need for specifying the I(subscribers) option
    choices: [ 'yes', 'no' ]
    required: false
    default: no
  publish:
    description:
      - Whether the check should be scheduled at all.
      - You can still issue it via the sensu api
    choices: [ 'yes', 'no' ]
    required: false
    default: yes
  occurrences:
    description:
      - Number of event occurrences before the handler should take action
    required: false
    default: 1
  refresh:
    description:
      - Number of seconds handlers should wait before taking second action
    required: false
    default: null
  aggregate:
    description:
      - Classifies the check as an aggregate check,
      - making it available via the aggregate API
    choices: [ 'yes', 'no' ]
    required: false
    default: no
  low_flap_threshold:
    description:
      - The low threshhold for flap detection
    required: false
    default: null
  high_flap_threshold:
    description:
      - The low threshhold for flap detection
    required: false
    default: null
requirements: [ ]
author: "<NAME> (@andsens)"
'''

EXAMPLES = '''
# Fetch metrics about the CPU load every 60 seconds,
# the sensu server has a handler called 'relay' which forwards stats to graphite
- name: get cpu metrics
  sensu_check: name=cpu_load
               command=/etc/sensu/plugins/system/cpu-mpstat-metrics.rb
               metric=yes handlers=relay subscribers=common interval=60

# Check whether nginx is running
- name: check nginx process
  sensu_check: name=nginx_running
               command='/etc/sensu/plugins/processes/check-procs.rb -f /var/run/nginx.pid'
               handlers=default subscribers=nginx interval=60

# Stop monitoring the disk capacity.
# Note that the check will still show up in the sensu dashboard,
# to remove it completely you need to issue a DELETE request to the sensu api.
- name: check disk
  sensu_check: name=check_disk_capacity state=absent
'''


def sensu_check(module, path, name, state='present', backup=False):
    changed = False
    reasons = []

    try:
        import json
    except ImportError:
        import simplejson as json

    stream = None
    try:
        try:
            stream = open(path, 'r')
{'fraction_non_alphanumeric': 0.0710809871393813, 'fraction_numerical': 0.002954466458116093, 'mean_word_length': 2.8058862433862433, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1456465', 'n_tokens_mistral': 3207, 'n_tokens_neox': 3122, 'n_words': 1377}