Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tag filter to get_all_monitors #58

Merged
merged 4 commits into from
Dec 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/dogapi/v1/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ def get_monitor(monitor_id, options = {})
# :group_states is an optional list of statuses to filter returned
# groups. If no value is given then no group states will be returned.
# Possible values are: "all", "ok", "warn", "alert", "no data".
if options[:group_states]
params[:group_states] = options[:group_states].join(',')
options.delete :group_states
end
params[:group_states] = options[:group_states].join(',') if options[:group_states]

request(Net::HTTP::Get, "/api/#{API_VERSION}/monitor/#{monitor_id}", params, nil, false)
rescue Exception => e
Expand Down Expand Up @@ -87,8 +84,16 @@ def get_all_monitors(options = {})
# groups. If no value is given then no group states will be returned.
# Possible values are: "all", "ok", "warn", "alert", "no data".
if options[:group_states]
params[:group_states] = options[:group_states].join(',')
options.delete :group_states
params[:group_states] = options[:group_states]
params[:group_states] = params[:group_states].join(',') if params[:group_states].respond_to?(:join)
end

# :tags is an optional list of scope tags to filter the list of monitors
# returned. If no value is given, then all monitors, regardless of
# scope, will be returned.
if options[:tags]
params[:tags] = options[:tags]
params[:tags] = params[:tags].join(',') if params[:tags].respond_to?(:join)
end

request(Net::HTTP::Get, "/api/#{API_VERSION}/monitor", params, nil, false)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_monitors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,26 @@ def test_metric_alerts

query1 = "avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 100"
query2 = "avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 200"
query3 = "avg(last_1h):sum:system.net.bytes_rcvd{host:host1} > 200"

monitor_id1 = dog.monitor('metric alert', query1)[1]['id']
monitor_id2 = dog.monitor('metric alert', query2)[1]['id']
monitor_id3 = dog.monitor('metric alert', query3)[1]['id']
status, monitors = dog.get_all_monitors(:group_states => ['alert', 'warn'])
monitor1 = monitors.map{|m| m if m['id'] == monitor_id1}.compact[0]
monitor2 = monitors.map{|m| m if m['id'] == monitor_id2}.compact[0]
assert_equal monitor1['query'], query1, monitor1['query']
assert_equal monitor2['query'], query2, monitor2['query']

status, monitors = dog.get_all_monitors(:tags => ['host:host1'])
monitor3 = monitors.map{|m| m if m['id'] == monitor_id3}.compact[0]
assert_equal monitor3['query'], query3, monitor3['query']
assert_equal nil, monitors.map{|m| m if m['id'] == monitor_id1}.compact[0]
assert_equal nil, monitors.map{|m| m if m['id'] == monitor_id2}.compact[0]

dog.delete_monitor(monitor_id1)
dog.delete_monitor(monitor_id2)
dog.delete_monitor(monitor_id3)
end

def test_checks
Expand Down