├── Gemfile ├── LICENSE ├── .gitignore ├── README.md └── monitor-import.rb /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | source "https://rubygems.org" 3 | 4 | gem "dogapi" 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Kurochan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | /.config 4 | /coverage/ 5 | /InstalledFiles 6 | /pkg/ 7 | /spec/reports/ 8 | /spec/examples.txt 9 | /test/tmp/ 10 | /test/version_tmp/ 11 | /tmp/ 12 | 13 | # Used by dotenv library to load environment variables. 14 | # .env 15 | 16 | ## Specific to RubyMotion: 17 | .dat* 18 | .repl_history 19 | build/ 20 | *.bridgesupport 21 | build-iPhoneOS/ 22 | build-iPhoneSimulator/ 23 | 24 | ## Specific to RubyMotion (use of CocoaPods): 25 | # 26 | # We recommend against adding the Pods directory to your .gitignore. However 27 | # you should judge for yourself, the pros and cons are mentioned at: 28 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 29 | # 30 | # vendor/Pods/ 31 | 32 | ## Documentation cache and generated files: 33 | /.yardoc/ 34 | /_yardoc/ 35 | /doc/ 36 | /rdoc/ 37 | 38 | ## Environment normalization: 39 | /.bundle/ 40 | /vendor/bundle 41 | /lib/bundler/man/ 42 | 43 | # for a library or gem, you might want to ignore these files since the code is 44 | # intended to run in multiple environments; otherwise, check them in: 45 | # Gemfile.lock 46 | # .ruby-version 47 | # .ruby-gemset 48 | 49 | # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: 50 | .rvmrc 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | datadog_monitor2terraform 2 | ========== 3 | 4 | Import DataDog monitor rule and generate Terraform resource configuration 5 | 6 | ## Setup 7 | 8 | ```bash 9 | $ git clone https://github.com/kurochan/datadog_monitor2terraform 10 | $ bundle install 11 | ``` 12 | 13 | ## Usage 14 | ``` 15 | $ ruby ./monitor-import.rb [monitor_name] [monitor_id] 16 | ``` 17 | 18 | * `monitor_name` is terraform resource id 19 | * `monitor_id` is written in datadog monitor page url 20 | * https://app.datadoghq.com/monitors#112233 21 | * `112233` is `monitor_id` 22 | 23 | ## example 24 | 25 | ```bash 26 | $ export DATADOG_API_KEY='datadogapikeydatadogapikey' 27 | $ export DATADOG_APP_KEY='datadogappkeydatadogappkeydatadogappkey' 28 | 29 | $ ruby ./monitor-import.rb dynamodb_user_error_count 112233 30 | 31 | resource "datadog_monitor" "dynamodb_user_error_count" { 32 | name = "DynamoDB UserError count is above the Threshold !!" 33 | type = "metric alert" 34 | message = <" { 22 | name = "<%= monitor["name"] %>" 23 | type = "<%= monitor["type"] %>" 24 | message = < 26 | EOF 27 | query = "<%= monitor["query"].gsub('"', '\\"') %>" 28 | <%- if monitor["options"]["escalation_message"] != nil -%> 29 | escalation_message = "<%= monitor["options"]["escalation_message"] %>" 30 | <%- end -%> 31 | <%- if monitor["options"]["thresholds"] != nil && monitor["options"]["thresholds"] != {} -%> 32 | thresholds { 33 | <%- if monitor["options"]["thresholds"]["ok"] != nil -%> 34 | ok = <%= monitor["options"]["thresholds"]["ok"] %> 35 | <%- end -%> 36 | <%- if monitor["options"]["thresholds"]["warning"] != nil -%> 37 | warning = <%= monitor["options"]["thresholds"]["warning"] %> 38 | <%- end -%> 39 | <%- if monitor["options"]["thresholds"]["critical"] != nil -%> 40 | critical = <%= monitor["options"]["thresholds"]["critical"] %> 41 | <%- end -%> 42 | } 43 | <%- end -%> 44 | <%- if monitor["options"]["notify_no_data"] != nil -%> 45 | notify_no_data = <%= monitor["options"]["notify_no_data"] %> 46 | <%- end -%> 47 | <%- if monitor["options"]["no_data_timeframe"] != nil -%> 48 | no_data_timeframe = <%= monitor["options"]["no_data_timeframe"] %> 49 | <%- end -%> 50 | <%- if monitor["options"]["new_host_delay"] != nil -%> 51 | new_host_delay = <%= monitor["options"]["new_host_delay"] %> 52 | <%- end -%> 53 | <%- if monitor["options"]["evaluation_delay"] != nil -%> 54 | evaluation_delay = <%= monitor["options"]["evaluation_delay"] %> 55 | <%- end -%> 56 | <%- if monitor["options"]["renotify_interval"] != nil -%> 57 | renotify_interval = <%= monitor["options"]["renotify_interval"] %> 58 | <%- end -%> 59 | <%- if monitor["options"]["timeout_h"] != nil -%> 60 | timeout_h = <%= monitor["options"]["timeout_h"] %> 61 | <%- end -%> 62 | <%- if monitor["options"]["include_tags"] != nil -%> 63 | include_tags = <%= monitor["options"]["include_tags"] %> 64 | <%- end -%> 65 | <%- if monitor["options"]["require_full_window"] != nil -%> 66 | require_full_window = <%= monitor["options"]["require_full_window"] %> 67 | <%- end -%> 68 | <%- if monitor["options"]["notify_audit"] != nil -%> 69 | notify_audit = <%= monitor["options"]["notify_audit"] %> 70 | <%- end -%> 71 | tags = <%= monitor["tags"] %> 72 | } 73 | --------------------------------------------------------------------------------