summaryrefslogtreecommitdiff
path: root/monitoring/datadog_event.py
blob: 5d083eab0f776a05da313d96c6038dc8616da535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Author: Artūras 'arturaz' Šlajus <x11@arturaz.net>
#
# This module is proudly sponsored by iGeolise (www.igeolise.com) and
# Tiny Lab Productions (www.tinylabproductions.com).
#
# 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: datadog_event
short_description: Posts events to DataDog  service
description:
- "Allows to post events to DataDog (www.datadoghq.com) service."
- "Uses http://docs.datadoghq.com/api/#events API."
version_added: "1.3"
author: "Artūras `arturaz` Šlajus (@arturaz)"
notes: []
requirements: []
options:
    api_key:
        description: ["Your DataDog API key."]
        required: true
        default: null
    title:
        description: ["The event title."]
        required: true
        default: null
    text:
        description: ["The body of the event."]
        required: true
        default: null
    date_happened:
        description:
        - POSIX timestamp of the event.
        - Default value is now.
        required: false
        default: now
    priority:
        description: ["The priority of the event."]
        required: false
        default: normal
        choices: [normal, low]
    tags:
        description: ["Comma separated list of tags to apply to the event."]
        required: false
        default: null
    alert_type:
        description: ["Type of alert."]
        required: false
        default: info
        choices: ['error', 'warning', 'info', 'success']
    aggregation_key:
        description: ["An arbitrary string to use for aggregation."]
        required: false
        default: null
    validate_certs:
        description:
            - If C(no), SSL certificates will not be validated. This should only be used
              on personally controlled sites using self-signed certificates.
        required: false
        default: 'yes'
        choices: ['yes', 'no']
        version_added: 1.5.1
'''

EXAMPLES = '''
# Post an event with low priority
datadog_event: title="Testing from ansible" text="Test!" priority="low"
               api_key="6873258723457823548234234234"
# Post an event with several tags
datadog_event: title="Testing from ansible" text="Test!"
               api_key="6873258723457823548234234234"
               tags=aa,bb,#host:{{ inventory_hostname }}
'''

import socket

def main():
    module = AnsibleModule(
        argument_spec=dict(
            api_key=dict(required=True),
            title=dict(required=True),
            text=dict(required=True),
            date_happened=dict(required=False, default=None, type='int'),
            priority=dict(
                required=False, default='normal', choices=['normal', 'low']
            ),
            tags=dict(required=False, default=None, type='list'),
            alert_type=dict(
                required=False, default='info',
                choices=['error', 'warning', 'info', 'success']
            ),
            aggregation_key=dict(required=False, default=None),
            source_type_name=dict(
                required=False, default='my apps',
                choices=['nagios', 'hudson', 'jenkins', 'user', 'my apps',
                         'feed', 'chef', 'puppet', 'git', 'bitbucket', 'fabric',
                         'capistrano']
            ),
            validate_certs = dict(default='yes', type='bool'),
        )
    )

    post_event(module)

def post_event(module):
    uri = "https://app.datadoghq.com/api/v1/events?api_key=%s" % module.params['api_key']

    body = dict(
        title=module.params['title'],
        text=module.params['text'],
        priority=module.params['priority'],
        alert_type=module.params['alert_type']
    )
    if module.params['date_happened'] != None:
        body['date_happened'] = module.params['date_happened']
    if module.params['tags'] != None:
        body['tags'] = module.params['tags']
    if module.params['aggregation_key'] != None:
        body['aggregation_key'] = module.params['aggregation_key']
    if module.params['source_type_name'] != None:
        body['source_type_name'] = module.params['source_type_name']

    json_body = module.jsonify(body)
    headers = {"Content-Type": "application/json"}

    (response, info) = fetch_url(module, uri, data=json_body, headers=headers)
    if info['status'] == 202:
        response_body = response.read()
        response_json = module.from_json(response_body)
        if response_json['status'] == 'ok':
            module.exit_json(changed=True)
        else:
            module.fail_json(msg=response)
    else:
        module.fail_json(**info)

# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.urls import *
if __name__ == '__main__':
    main()