barkdog 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +2 -0
- data/barkdog.gemspec +26 -0
- data/bin/barkdog +94 -0
- data/lib/barkdog.rb +19 -0
- data/lib/barkdog/client.rb +83 -0
- data/lib/barkdog/constants.rb +5 -0
- data/lib/barkdog/driver.rb +63 -0
- data/lib/barkdog/dsl.rb +9 -0
- data/lib/barkdog/dsl/context.rb +48 -0
- data/lib/barkdog/dsl/context/monitor.rb +23 -0
- data/lib/barkdog/dsl/context/monitor/options.rb +19 -0
- data/lib/barkdog/dsl/converter.rb +84 -0
- data/lib/barkdog/exporter.rb +47 -0
- data/lib/barkdog/ext/string_ext.rb +25 -0
- data/lib/barkdog/logger.rb +30 -0
- data/lib/barkdog/version.rb +3 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9b638cff71a732aae5f42cbd4f60f30e4e674d36
|
4
|
+
data.tar.gz: 89044b32af12f4aee82413d5d8848132582e5479
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b2577c9e00dc2e8758a315bc74cfd25457d395b581c9ec404d6fc388476b44fb2f82d8bf554589eb002c0599b052e9fcda2f1fcbc7a21c86760b24b1250a2c03
|
7
|
+
data.tar.gz: a6bc116e8fc6bffdd0003658713ce24359564959147e71a993768125e77374ad41c1f802713117c916bf31253089c780aa0e017c2eab8e0f2a0fda23a48f5ae6
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Genki Sugawara
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Barkdog
|
2
|
+
|
3
|
+
Barkdog is a tool to manage [Datadog monitors](http://docs.datadoghq.com/guides/monitoring/).
|
4
|
+
|
5
|
+
It defines Datadog monitors using Ruby DSL, and updates monitors according to DSL.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'barkdog'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install barkdog
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```sh
|
26
|
+
export BARKDOG_API_KEY=...
|
27
|
+
export BARKDOG_APP_KEY=...
|
28
|
+
|
29
|
+
barkdog -e -o Barkfile
|
30
|
+
vi Barkfile
|
31
|
+
barkdog -a --dry-run
|
32
|
+
barkdog -a
|
33
|
+
```
|
34
|
+
|
35
|
+
## Help
|
36
|
+
|
37
|
+
```
|
38
|
+
Usage: barkdog [options]
|
39
|
+
--api-key API_KEY
|
40
|
+
--app-key APP_KEY
|
41
|
+
-a, --apply
|
42
|
+
-f, --file FILE
|
43
|
+
--dry-run
|
44
|
+
-e, --export
|
45
|
+
-o, --output FILE
|
46
|
+
--no-color
|
47
|
+
--debug
|
48
|
+
-h, --help
|
49
|
+
```
|
50
|
+
|
51
|
+
## Barkfile example
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
monitor "Check load avg", :type=>"metric alert" do
|
55
|
+
query "avg(last_5m):avg:ddstat.load_avg.1m{host:i-XXXXXXXX} > 1"
|
56
|
+
message "@winebarrel@example.net"
|
57
|
+
options do
|
58
|
+
notify_no_data true
|
59
|
+
no_data_timeframe 2
|
60
|
+
notify_audit true
|
61
|
+
silenced({})
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
data/Rakefile
ADDED
data/barkdog.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'barkdog/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'barkdog'
|
8
|
+
spec.version = Barkdog::VERSION
|
9
|
+
spec.authors = ['Genki Sugawara']
|
10
|
+
spec.email = ['sgwr_dts@yahoo.co.jp']
|
11
|
+
spec.summary = %q{Barkdog is a tool to manage Datadog monitors.}
|
12
|
+
spec.description = %q{Barkdog is a tool to manage Datadog monitors.}
|
13
|
+
spec.homepage = 'https://github.com/winebarrel/barkdog'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'dogapi'
|
22
|
+
spec.add_dependency 'term-ansicolor'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
end
|
data/bin/barkdog
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
$: << File.expand_path("#{File.dirname __FILE__}/../lib")
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'barkdog'
|
6
|
+
require 'optparse'
|
7
|
+
|
8
|
+
Version = Barkdog::VERSION
|
9
|
+
DEFAULT_FILENAME = 'Barkfile'
|
10
|
+
|
11
|
+
mode = nil
|
12
|
+
file = DEFAULT_FILENAME
|
13
|
+
output_file = '-'
|
14
|
+
|
15
|
+
options = {
|
16
|
+
:api_key => ENV['BARKDOG_API_KEY'],
|
17
|
+
:application_key => ENV['BARKDOG_APP_KEY'],
|
18
|
+
:dry_run => false,
|
19
|
+
:color => true,
|
20
|
+
:debug => false,
|
21
|
+
}
|
22
|
+
|
23
|
+
ARGV.options do |opt|
|
24
|
+
begin
|
25
|
+
opt.on('' , '--api-key API_KEY') {|v| options[:api_key] = v }
|
26
|
+
opt.on('' , '--app-key APP_KEY') {|v| options[:application_key] = v }
|
27
|
+
opt.on('-a', '--apply') { mode = :apply }
|
28
|
+
opt.on('-f', '--file FILE') {|v| file = v }
|
29
|
+
opt.on('' , '--dry-run') { options[:dry_run] = true }
|
30
|
+
opt.on('-e', '--export') { mode = :export }
|
31
|
+
opt.on('-o', '--output FILE') {|v| output_file = v }
|
32
|
+
opt.on('' , '--no-color') { options[:color] = false }
|
33
|
+
opt.on('' , '--debug') { options[:debug] = true }
|
34
|
+
|
35
|
+
opt.on('-h', '--help') do
|
36
|
+
puts opt.help
|
37
|
+
exit 1
|
38
|
+
end
|
39
|
+
|
40
|
+
opt.parse!
|
41
|
+
|
42
|
+
unless mode
|
43
|
+
puts opt.help
|
44
|
+
exit 1
|
45
|
+
end
|
46
|
+
|
47
|
+
if not options[:api_key] or not options[:application_key]
|
48
|
+
raise 'api-key and app-key is required'
|
49
|
+
end
|
50
|
+
rescue => e
|
51
|
+
$stderr.puts("[ERROR] #{e.message}")
|
52
|
+
exit 1
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
String.colorize = options[:color]
|
57
|
+
|
58
|
+
begin
|
59
|
+
logger = Barkdog::Logger.instance
|
60
|
+
logger.set_debug(options[:debug])
|
61
|
+
client = Barkdog::Client.new(options)
|
62
|
+
|
63
|
+
case mode
|
64
|
+
when :export
|
65
|
+
if output_file == '-'
|
66
|
+
logger.info('# Export Datadog monitors')
|
67
|
+
puts client.export
|
68
|
+
else
|
69
|
+
logger.info("Export Datadog monitors to `#{output_file}`")
|
70
|
+
open(output_file, 'wb') {|f| f.puts client.export }
|
71
|
+
end
|
72
|
+
when :apply
|
73
|
+
unless File.exist?(file)
|
74
|
+
raise "No Barkfile found (looking for: #{file})"
|
75
|
+
end
|
76
|
+
|
77
|
+
msg = "Apply `#{file}` to Datadog monitors"
|
78
|
+
msg << ' (dry-run)' if options[:dry_run]
|
79
|
+
logger.info(msg)
|
80
|
+
|
81
|
+
updated = client.apply(file)
|
82
|
+
|
83
|
+
unless updated
|
84
|
+
logger.info('No change'.intense_blue)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
rescue => e
|
88
|
+
if options[:debug]
|
89
|
+
raise e
|
90
|
+
else
|
91
|
+
$stderr.puts("[ERROR] #{e.message}".red)
|
92
|
+
exit 1
|
93
|
+
end
|
94
|
+
end
|
data/lib/barkdog.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'dogapi'
|
2
|
+
require 'logger'
|
3
|
+
require 'singleton'
|
4
|
+
require 'term/ansicolor'
|
5
|
+
|
6
|
+
module Barkdog; end
|
7
|
+
|
8
|
+
require 'barkdog/ext/string_ext'
|
9
|
+
require 'barkdog/constants'
|
10
|
+
require 'barkdog/logger'
|
11
|
+
require 'barkdog/client'
|
12
|
+
require 'barkdog/driver'
|
13
|
+
require 'barkdog/dsl'
|
14
|
+
require 'barkdog/dsl/context'
|
15
|
+
require 'barkdog/dsl/context/monitor'
|
16
|
+
require 'barkdog/dsl/context/monitor/options'
|
17
|
+
require 'barkdog/dsl/converter'
|
18
|
+
require 'barkdog/exporter'
|
19
|
+
require 'barkdog/version'
|
@@ -0,0 +1,83 @@
|
|
1
|
+
class Barkdog::Client
|
2
|
+
include Barkdog::Logger::Helper
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
@options = options
|
6
|
+
|
7
|
+
api_key, app_key = @options.values_at(:api_key, :application_key)
|
8
|
+
raise 'API Key does not exist' unless api_key
|
9
|
+
raise 'Application Key does not exist' unless app_key
|
10
|
+
|
11
|
+
@dog = Dogapi::Client.new(api_key, app_key)
|
12
|
+
@driver = Barkdog::Driver.new(@dog, @options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def export(export_options = {})
|
16
|
+
exported = Barkdog::Exporter.export(@dog, @options)
|
17
|
+
Barkdog::DSL.convert(exported, @options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def apply(file)
|
21
|
+
walk(file)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def walk(file)
|
27
|
+
expected = load_file(file)
|
28
|
+
actual = Barkdog::Exporter.export(@dog, @options)
|
29
|
+
walk_monitors(expected, actual)
|
30
|
+
end
|
31
|
+
|
32
|
+
def walk_monitors(expected, actual)
|
33
|
+
updated = false
|
34
|
+
|
35
|
+
expected.each do |name, expected_monitor|
|
36
|
+
actual_monitor = actual.delete(name)
|
37
|
+
|
38
|
+
if actual_monitor
|
39
|
+
updated = walk_monitor(name, expected_monitor, actual_monitor) || updated
|
40
|
+
else
|
41
|
+
updated = @driver.create_monitor(name, expected_monitor) || updated
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
actual.each do |name, actual_monitor|
|
46
|
+
updated = @driver.delete_monitor(name, actual_monitor) || updated
|
47
|
+
end
|
48
|
+
|
49
|
+
updated
|
50
|
+
end
|
51
|
+
|
52
|
+
def walk_monitor(name, expected, actual)
|
53
|
+
updated = false
|
54
|
+
|
55
|
+
Barkdog::FIXED_OPTION_KEYS.each do |key|
|
56
|
+
if expected[key] != actual[key]
|
57
|
+
log(:warn, "#{name}: `#{key}` can not be changed (Changes has been ignored)", :color => :yellow)
|
58
|
+
return updated
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
actual_without_id = actual.dup
|
63
|
+
monitor_id = actual_without_id.delete('id')
|
64
|
+
|
65
|
+
if expected != actual_without_id
|
66
|
+
updated = @driver.update_monitor(name, expected.merge('id' => monitor_id)) || updated
|
67
|
+
end
|
68
|
+
|
69
|
+
updated
|
70
|
+
end
|
71
|
+
|
72
|
+
def load_file(file)
|
73
|
+
if file.kind_of?(String)
|
74
|
+
open(file) do |f|
|
75
|
+
Barkdog::DSL.parse(f.read, file)
|
76
|
+
end
|
77
|
+
elsif [File, Tempfile].any? {|i| file.kind_of?(i) }
|
78
|
+
Barkdog::DSL.parse(file.read, file.path)
|
79
|
+
else
|
80
|
+
raise TypeError, "can't convert #{file} into File"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
class Barkdog::Driver
|
2
|
+
include Barkdog::Logger::Helper
|
3
|
+
|
4
|
+
def initialize(dog, options = {})
|
5
|
+
@dog = dog
|
6
|
+
@options = options
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_monitor(name, attrs)
|
10
|
+
updated = false
|
11
|
+
log(:info, "Create Monitor: #{name}", :color => :cyan)
|
12
|
+
|
13
|
+
unless @options[:dry_run]
|
14
|
+
@dog.monitor(
|
15
|
+
attrs['type'],
|
16
|
+
attrs['query'],
|
17
|
+
:name => name,
|
18
|
+
:message => attrs['message'],
|
19
|
+
:options => attrs['options']
|
20
|
+
)
|
21
|
+
|
22
|
+
updated = true
|
23
|
+
end
|
24
|
+
|
25
|
+
updated
|
26
|
+
end
|
27
|
+
|
28
|
+
def delete_monitor(name, attrs)
|
29
|
+
updated = false
|
30
|
+
log(:info, "Delete Monitor: #{name}", :color => :red)
|
31
|
+
|
32
|
+
unless @options[:dry_run]
|
33
|
+
@dog.delete_monitor(attrs['id'])
|
34
|
+
updated = true
|
35
|
+
end
|
36
|
+
|
37
|
+
updated
|
38
|
+
end
|
39
|
+
|
40
|
+
def update_monitor(name, attrs)
|
41
|
+
updated = false
|
42
|
+
log(:info, "Update Monitor: #{name}", :color => :green)
|
43
|
+
|
44
|
+
attrs.each do |key, value|
|
45
|
+
next if key == 'id'
|
46
|
+
log(:info, " set #{key}=#{value}", :color => :green)
|
47
|
+
end
|
48
|
+
|
49
|
+
unless @options[:dry_run]
|
50
|
+
@dog.update_monitor(
|
51
|
+
attrs['id'],
|
52
|
+
attrs['query'],
|
53
|
+
:name => name,
|
54
|
+
:message => attrs['message'],
|
55
|
+
:options => attrs['options']
|
56
|
+
)
|
57
|
+
|
58
|
+
updated = true
|
59
|
+
end
|
60
|
+
|
61
|
+
updated
|
62
|
+
end
|
63
|
+
end
|
data/lib/barkdog/dsl.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
class Barkdog::DSL::Context
|
2
|
+
def self.eval(dsl, path, options = {})
|
3
|
+
self.new(path, options) {
|
4
|
+
eval(dsl, binding, path)
|
5
|
+
}
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :result
|
9
|
+
|
10
|
+
def initialize(path, options = {}, &block)
|
11
|
+
@path = path
|
12
|
+
@options = options
|
13
|
+
@result = {}
|
14
|
+
instance_eval(&block)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def require(file)
|
20
|
+
barkfile = (file =~ %r|\A/|) ? file : File.expand_path(File.join(File.dirname(@path), file))
|
21
|
+
|
22
|
+
if File.exist?(barkfile)
|
23
|
+
instance_eval(File.read(barkfile), barkfile)
|
24
|
+
elsif File.exist?(barkfile + '.rb')
|
25
|
+
instance_eval(File.read(barkfile + '.rb'), barkfile + '.rb')
|
26
|
+
else
|
27
|
+
Kernel.require(file)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def monitor(name, fixed_options = {}, &block)
|
32
|
+
name = name.to_s
|
33
|
+
|
34
|
+
if @result[name]
|
35
|
+
raise "Monitor `#{name}` is already defined"
|
36
|
+
end
|
37
|
+
|
38
|
+
fixed_options.keys.each do |key|
|
39
|
+
unless Barkdog::FIXED_OPTION_KEYS.include?(key.to_s)
|
40
|
+
raise "Monitor `#{name}`: `#{key}` is invalid fixed option key"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
fixed_options = Hash[fixed_options.map {|k, v| [k.to_s, v] }]
|
45
|
+
attrs = Barkdog::DSL::Context::Monitor.new(name, &block).result
|
46
|
+
@result[name] = fixed_options.merge(attrs)
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Barkdog::DSL::Context::Monitor
|
2
|
+
def initialize(name, &block)
|
3
|
+
@monitor_name = name
|
4
|
+
@result = {}
|
5
|
+
instance_eval(&block)
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :result
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def query(value)
|
13
|
+
@result['query'] = value.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def message(value)
|
17
|
+
@result['message'] = value.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def options(&block)
|
21
|
+
@result['options'] = Barkdog::DSL::Context::Monitor::Options.new(&block).result
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Barkdog::DSL::Context::Monitor::Options
|
2
|
+
def initialize(&block)
|
3
|
+
@result = {}
|
4
|
+
instance_eval(&block)
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_reader :result
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def method_missing(method_name, *args)
|
12
|
+
if args.length == 1
|
13
|
+
value = args.first
|
14
|
+
@result[method_name.to_s] = value
|
15
|
+
else
|
16
|
+
super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
class Barkdog::DSL::Converter
|
2
|
+
def self.convert(exported, options = {})
|
3
|
+
self.new(exported, options).convert
|
4
|
+
end
|
5
|
+
|
6
|
+
def initialize(exported, options = {})
|
7
|
+
@exported = exported
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def convert
|
12
|
+
output_monitors(@exported)
|
13
|
+
#[
|
14
|
+
# output_users(@exported[:users]),
|
15
|
+
# output_groups(@exported[:groups]),
|
16
|
+
# output_roles(@exported[:roles]),
|
17
|
+
# output_instance_profiles(@exported[:instance_profiles]),
|
18
|
+
#].join("\n")
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def output_monitors(monitor_by_name)
|
24
|
+
monitor_by_name.sort_by {|k, v| k }.map {|monitor_name, attrs|
|
25
|
+
next unless target_matched?(monitor_name)
|
26
|
+
output_monitor(monitor_name, attrs)
|
27
|
+
}.select {|i| i }.join("\n")
|
28
|
+
end
|
29
|
+
|
30
|
+
def output_monitor(monitor_name, attrs)
|
31
|
+
fixed_options = Hash[Barkdog::FIXED_OPTION_KEYS.map {|k| [k.to_sym, attrs[k]] }]
|
32
|
+
query = attrs['query']
|
33
|
+
message = attrs['message']
|
34
|
+
monitor_options = attrs['options'] || {}
|
35
|
+
|
36
|
+
if monitor_options.empty?
|
37
|
+
monitor_options = ''
|
38
|
+
else
|
39
|
+
monitor_options = "\n" + output_monitor_options(monitor_options)
|
40
|
+
end
|
41
|
+
|
42
|
+
<<-EOS
|
43
|
+
monitor #{monitor_name.inspect}, #{unbrace(fixed_options.inspect)} do
|
44
|
+
query #{query.inspect}
|
45
|
+
message #{message.inspect}#{
|
46
|
+
monitor_options}
|
47
|
+
end
|
48
|
+
EOS
|
49
|
+
end
|
50
|
+
|
51
|
+
def output_monitor_options(monitor_options)
|
52
|
+
options_body = monitor_options.map {|key, value|
|
53
|
+
value_is_hash = value.is_a?(Hash)
|
54
|
+
value = value.inspect
|
55
|
+
|
56
|
+
if value_is_hash
|
57
|
+
value = unbrace(value).strip
|
58
|
+
value = value.empty? ? '({})' : " #{value}"
|
59
|
+
else
|
60
|
+
value = " #{value}"
|
61
|
+
end
|
62
|
+
|
63
|
+
"#{key}#{value}"
|
64
|
+
}.join("\n ")
|
65
|
+
|
66
|
+
<<-RUBY.chomp
|
67
|
+
options do
|
68
|
+
#{options_body}
|
69
|
+
end
|
70
|
+
RUBY
|
71
|
+
end
|
72
|
+
|
73
|
+
def unbrace(str)
|
74
|
+
str.sub(/\A\s*\{/, '').sub(/\}\s*\z/, '').strip
|
75
|
+
end
|
76
|
+
|
77
|
+
def target_matched?(name)
|
78
|
+
if @options[:target]
|
79
|
+
name =~ @options[:target]
|
80
|
+
else
|
81
|
+
true
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
class Barkdog::Exporter
|
2
|
+
EXCLUDE_KEYS = %w(
|
3
|
+
overall_state
|
4
|
+
creator
|
5
|
+
org_id
|
6
|
+
multi
|
7
|
+
)
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def export(dog, opts = {})
|
11
|
+
self.new(dog, opts).export
|
12
|
+
end
|
13
|
+
end # of class methods
|
14
|
+
|
15
|
+
def initialize(dog, options = {})
|
16
|
+
@dog = dog
|
17
|
+
@options = options
|
18
|
+
end
|
19
|
+
|
20
|
+
def export
|
21
|
+
monitors = @dog.get_all_monitors[1]
|
22
|
+
normalize(monitors)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def normalize(monitors)
|
28
|
+
monitor_by_name = {}
|
29
|
+
|
30
|
+
monitors.each do |m|
|
31
|
+
name = m.delete('name')
|
32
|
+
|
33
|
+
if monitor_by_name[name]
|
34
|
+
raise "Duplicate monitor name exists: #{name}"
|
35
|
+
end
|
36
|
+
|
37
|
+
EXCLUDE_KEYS.each do |key|
|
38
|
+
m.delete(key)
|
39
|
+
end
|
40
|
+
|
41
|
+
monitor_by_name[name] = m
|
42
|
+
end
|
43
|
+
|
44
|
+
monitor_by_name
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class String
|
2
|
+
@@colorize = false
|
3
|
+
|
4
|
+
class << self
|
5
|
+
def colorize=(value)
|
6
|
+
@@colorize = value
|
7
|
+
end
|
8
|
+
|
9
|
+
def colorize
|
10
|
+
@@colorize
|
11
|
+
end
|
12
|
+
end # of class methods
|
13
|
+
|
14
|
+
Term::ANSIColor::Attribute.named_attributes.map do |attribute|
|
15
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
16
|
+
def #{attribute.name}
|
17
|
+
if @@colorize
|
18
|
+
Term::ANSIColor.send(#{attribute.name.inspect}, self)
|
19
|
+
else
|
20
|
+
self
|
21
|
+
end
|
22
|
+
end
|
23
|
+
EOS
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class Barkdog::Logger < ::Logger
|
2
|
+
include Singleton
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
super($stdout)
|
6
|
+
|
7
|
+
self.formatter = proc do |severity, datetime, progname, msg|
|
8
|
+
"#{msg}\n"
|
9
|
+
end
|
10
|
+
|
11
|
+
self.level = INFO
|
12
|
+
end
|
13
|
+
|
14
|
+
def set_debug(value)
|
15
|
+
self.level = value ? DEBUG : INFO
|
16
|
+
end
|
17
|
+
|
18
|
+
module Helper
|
19
|
+
def log(level, message, opts = {})
|
20
|
+
opts = (@options || {}).merge(opts)
|
21
|
+
|
22
|
+
message = "[#{level.to_s.upcase}] #{message}" unless level == :info
|
23
|
+
message << ' (dry-run)' if opts[:dry_run]
|
24
|
+
message = message.send(opts[:color]) if opts[:color]
|
25
|
+
|
26
|
+
logger = opts[:logger] || Barkdog::Logger.instance
|
27
|
+
logger.send(level, message)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barkdog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genki Sugawara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dogapi
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: term-ansicolor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Barkdog is a tool to manage Datadog monitors.
|
70
|
+
email:
|
71
|
+
- sgwr_dts@yahoo.co.jp
|
72
|
+
executables:
|
73
|
+
- barkdog
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- barkdog.gemspec
|
83
|
+
- bin/barkdog
|
84
|
+
- lib/barkdog.rb
|
85
|
+
- lib/barkdog/client.rb
|
86
|
+
- lib/barkdog/constants.rb
|
87
|
+
- lib/barkdog/driver.rb
|
88
|
+
- lib/barkdog/dsl.rb
|
89
|
+
- lib/barkdog/dsl/context.rb
|
90
|
+
- lib/barkdog/dsl/context/monitor.rb
|
91
|
+
- lib/barkdog/dsl/context/monitor/options.rb
|
92
|
+
- lib/barkdog/dsl/converter.rb
|
93
|
+
- lib/barkdog/exporter.rb
|
94
|
+
- lib/barkdog/ext/string_ext.rb
|
95
|
+
- lib/barkdog/logger.rb
|
96
|
+
- lib/barkdog/version.rb
|
97
|
+
homepage: https://github.com/winebarrel/barkdog
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
metadata: {}
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 2.0.14
|
118
|
+
signing_key:
|
119
|
+
specification_version: 4
|
120
|
+
summary: Barkdog is a tool to manage Datadog monitors.
|
121
|
+
test_files: []
|