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 1 commit
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
8 changes: 7 additions & 1 deletion lib/dogapi/v1/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,13 @@ def get_all_monitors(options = {})
# 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

# :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].join(',')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ruby idiom for a single-line if would be:

params[:tags] = options[:tags].join(',') if options[:tags]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

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 [], monitors.map{|m| m if m['id'] == monitor_id1}.compact[0]
assert_equal [], 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