66
77# Bolt task for enabling/disabling monitoring alerts in SolarWinds
88class MonitoringPrometheusTask < TaskHelper
9- def initialize ( )
9+ def initialize
1010 @http_helper = PuppetX ::Patching ::HTTPHelper . new ( ssl : false , ssl_verify : false )
1111 end
1212
@@ -17,49 +17,48 @@ def get_end_timestamp(duration, units)
1717 when 'hours'
1818 offset = 3600
1919 when 'days'
20- offset = 86400
20+ offset = 86_400
2121 when 'weeks'
22- offset = 604800
22+ offset = 604_800
2323 end
2424
25- return ( Time . now . utc + duration * offset ) . iso8601
25+ ( Time . now . utc + duration * offset ) . iso8601
2626 end
2727
28- # Create a silence for every target that starts now and ends after the given duration
28+ # Create a silence for every target that starts now and ends after the given duration
2929 def create_silences ( targets , duration , units , prometheus_server )
3030 silence_ids = [ ]
31- for target in targets
31+ targets . each do | target |
3232 payload = {
33- matchers : [ { name : 'alias' , value : target , isRegex : false } ] ,
33+ matchers : [ { name : 'alias' , value : target , isRegex : false } ] ,
3434 startsAt : Time . now . utc . iso8601 ,
3535 endsAt : get_end_timestamp ( duration , units ) ,
3636 comment : "Silencing alerts on #{ target } for patching" ,
37- createdBy : 'patching'
37+ createdBy : 'patching' ,
3838 }
39- headers = { 'Content-Type' => 'application/json' }
39+ headers = { 'Content-Type' => 'application/json' }
4040 res = @http_helper . post ( "http://#{ prometheus_server } :9093/api/v2/silences" ,
4141 body : payload . to_json ,
4242 headers : headers )
4343
4444 silence_ids . push ( ( JSON . parse res . body ) [ 'silenceID' ] )
4545 end
4646
47- return silence_ids
47+ silence_ids
4848 end
4949
5050 # Remove all silences for targets that were created by 'patching'
5151 def remove_silences ( targets , prometheus_server )
5252 res = @http_helper . get ( "http://#{ prometheus_server } :9093/api/v2/silences" )
5353 silences = res . body
5454
55- for silence in JSON . parse silences
55+ ( JSON . parse silences ) . each do | silence |
5656 # Verify that the current silence is for one of the given targets
5757 # All silences created by this task will have exactly one matcher
58- if silence [ 'matchers' ] [ 0 ] [ 'name' ] == 'alias' && targets . include? ( silence [ 'matchers' ] [ 0 ] [ 'value' ] )
59- # Remove only silences that are active and were created by 'patching'
60- if silence [ 'status' ] [ 'state' ] == 'active' && silence [ 'createdBy' ] == 'patching'
61- @http_helper . delete ( "http://#{ prometheus_server } :9093/api/v2/silence/#{ silence [ 'id' ] } " )
62- end
58+ next if silence [ 'matchers' ] [ 0 ] [ 'name' ] != 'alias' || !targets . include? ( silence [ 'matchers' ] [ 0 ] [ 'value' ] )
59+ # Remove only silences that are active and were created by 'patching'
60+ if silence [ 'status' ] [ 'state' ] == 'active' && silence [ 'createdBy' ] == 'patching'
61+ @http_helper . delete ( "http://#{ prometheus_server } :9093/api/v2/silence/#{ silence [ 'id' ] } " )
6362 end
6463 end
6564 end
@@ -70,7 +69,7 @@ def task(targets: nil,
7069 prometheus_server : nil ,
7170 silence_duration : nil ,
7271 silence_units : nil ,
73- **kwargs )
72+ **_kwargs )
7473 # targets can be either an array or a string with a single target
7574 # Check if a single target was given and convert it to an array if it was
7675 if targets . is_a? String
0 commit comments