Module: Sensu::Settings::Validators::Check
- Included in:
- Sensu::Settings::Validators
- Defined in:
- lib/sensu/settings/validators/check.rb
Instance Method Summary collapse
-
#validate_check(check) ⇒ Object
Validate a Sensu check definition.
-
#validate_check_aggregate(check) ⇒ Object
Validate check aggregate.
-
#validate_check_cron(check) ⇒ Object
Validate check cron.
-
#validate_check_execution(check) ⇒ Object
Validate check execution.
-
#validate_check_flap_detection(check) ⇒ Object
Validate check flap detection.
-
#validate_check_handling(check) ⇒ Object
Validate check handling.
-
#validate_check_hook_execution(check, hook) ⇒ Object
Validate check hook execution.
-
#validate_check_hooks(check) ⇒ Object
Validate check hooks.
-
#validate_check_name(check) ⇒ Object
Validate check name.
-
#validate_check_proxy_requests(check) ⇒ Object
Validate check proxy requests.
-
#validate_check_scheduling(check) ⇒ Object
Validate check scheduling.
-
#validate_check_source(check) ⇒ Object
Validate check source.
-
#validate_check_subdue(check) ⇒ Object
Validate check subdue.
-
#validate_check_truncate_output(check) ⇒ Object
Validate check truncate output.
-
#validate_check_ttl(check) ⇒ Object
Validate check ttl.
Instance Method Details
#validate_check(check) ⇒ Object
Validate a Sensu check definition.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/sensu/settings/validators/check.rb', line 236 def validate_check(check) validate_check_name(check) validate_check_execution(check) validate_check_source(check) if check[:source] validate_check_scheduling(check) validate_check_proxy_requests(check) if check[:proxy_requests] validate_check_handling(check) validate_check_ttl(check) if check[:ttl] validate_check_aggregate(check) validate_check_flap_detection(check) validate_check_hooks(check) if check[:hooks] validate_check_truncate_output(check) validate_check_subdue(check) if check[:subdue] end |
#validate_check_aggregate(check) ⇒ Object
Validate check aggregate. Validates: aggregate
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/sensu/settings/validators/check.rb', line 146 def validate_check_aggregate(check) if check[:aggregates] if is_an_array?(check[:aggregates]) items_must_be_strings(check[:aggregates], /\A[\w\.:|-]+\z/) || invalid(check, "check aggregates items must be strings without spaces or special characters") else invalid(check, "check aggregates must be an array") end end if check[:aggregate] if is_a_string?(check[:aggregate]) must_match_regex(/\A[\w\.:|-]+\z/, check[:aggregate]) || invalid(check, "check aggregate cannot contain spaces or special characters") else must_be_boolean(check[:aggregate]) || invalid(check, "check aggregate must be a string (name) or boolean") end end end |
#validate_check_cron(check) ⇒ Object
Validate check cron. Validates: cron
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/sensu/settings/validators/check.rb', line 52 def validate_check_cron(check) must_be_a_string(check[:cron]) || invalid(check, "check cron must be a string") begin cron_parser = CronParser.new(check[:cron]) cron_parser.next(Time.now) rescue ArgumentError invalid(check, "check cron string must use the cron syntax") end end |
#validate_check_execution(check) ⇒ Object
Validate check execution. Validates: command, extension, timeout
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sensu/settings/validators/check.rb', line 22 def validate_check_execution(check) must_be_a_string_if_set(check[:command]) || invalid(check, "check command must be a string") must_be_a_string_if_set(check[:extension]) || invalid(check, "check extension must be a string") (!check[:command].nil? ^ !check[:extension].nil?) || invalid(check, "either check command or extension must be set") must_be_a_numeric_if_set(check[:timeout]) || invalid(check, "check timeout must be numeric") must_be_boolean_if_set(check[:stdin]) || invalid(check, "check stdin must be boolean") end |
#validate_check_flap_detection(check) ⇒ Object
Validate check flap detection. Validates: low_flap_threshold, high_flap_threshold
170 171 172 173 174 175 176 177 |
# File 'lib/sensu/settings/validators/check.rb', line 170 def validate_check_flap_detection(check) if either_are_set?(check[:low_flap_threshold], check[:high_flap_threshold]) must_be_an_integer(check[:low_flap_threshold]) || invalid(check, "check low flap threshold must be an integer") must_be_an_integer(check[:high_flap_threshold]) || invalid(check, "check high flap threshold must be an integer") end end |
#validate_check_handling(check) ⇒ Object
Validate check handling. Validates: handler, handlers
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/sensu/settings/validators/check.rb', line 116 def validate_check_handling(check) must_be_a_string_if_set(check[:handler]) || invalid(check, "check handler must be a string") must_be_an_array_if_set(check[:handlers]) || invalid(check, "check handlers must be an array") if is_an_array?(check[:handlers]) items_must_be_strings(check[:handlers]) || invalid(check, "check handlers must each be a string") end end |
#validate_check_hook_execution(check, hook) ⇒ Object
Validate check hook execution. Validates: command, timeout
184 185 186 187 188 189 190 191 |
# File 'lib/sensu/settings/validators/check.rb', line 184 def validate_check_hook_execution(check, hook) must_be_a_string(hook[:command]) || invalid(check, "check hook command must be a string") must_be_a_numeric_if_set(hook[:timeout]) || invalid(check, "check hook timeout must be numeric") must_be_boolean_if_set(hook[:stdin]) || invalid(check, "check hook stdin must be boolean") end |
#validate_check_hooks(check) ⇒ Object
Validate check hooks. Validates: hooks
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/sensu/settings/validators/check.rb', line 197 def validate_check_hooks(check) if check[:hooks].is_a?(Hash) check[:hooks].each do |key, hook| must_be_either(%w[ok warning critical unknown], key.to_s) || (0..255).map {|i| i.to_s}.include?(key.to_s) || key.to_s == "non-zero" || invalid(check, "check hook key must be a severity, status, or 'non-zero'") validate_check_hook_execution(check, hook) end else invalid(check, "check hooks must be a hash") end end |
#validate_check_name(check) ⇒ Object
Validate check name. Validates: name
11 12 13 14 15 16 |
# File 'lib/sensu/settings/validators/check.rb', line 11 def validate_check_name(check) must_be_a_string(check[:name]) || invalid(check, "check name must be a string") must_match_regex(/\A[\w\.-]+\z/, check[:name]) || invalid(check, "check name cannot contain spaces or special characters") end |
#validate_check_proxy_requests(check) ⇒ Object
Validate check proxy requests. Validates: proxy_requests (client_attributes, splay, splay_coverage)
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/sensu/settings/validators/check.rb', line 94 def validate_check_proxy_requests(check) if is_a_hash?(check[:proxy_requests]) proxy_requests = check[:proxy_requests] must_be_a_hash(proxy_requests[:client_attributes]) || invalid(check, "check proxy_requests client_attributes must be a hash") must_be_boolean_if_set(proxy_requests[:splay]) || invalid(check, "check proxy_requests splay must be boolean") if proxy_requests[:splay_coverage] (must_be_an_integer(proxy_requests[:splay_coverage]) && proxy_requests[:splay_coverage] > 0 && proxy_requests[:splay_coverage] < 100) || invalid(check, "check proxy_requests splay_coverage must be an integer greater than 0 and less than 100") end else invalid(check, "check proxy_requests must be a hash") end end |
#validate_check_scheduling(check) ⇒ Object
Validate check scheduling. Validates: publish, interval, standalone, subscribers
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/sensu/settings/validators/check.rb', line 67 def validate_check_scheduling(check) must_be_boolean_if_set(check[:publish]) || invalid(check, "check publish must be boolean") unless check[:publish] == false if check[:cron] validate_check_cron(check) else (must_be_an_integer(check[:interval]) && check[:interval] > 0) || invalid(check, "check interval must be an integer greater than 0") end end must_be_boolean_if_set(check[:standalone]) || invalid(check, "check standalone must be boolean") unless check[:standalone] if is_an_array?(check[:subscribers]) items_must_be_strings(check[:subscribers]) || invalid(check, "check subscribers must each be a string") else invalid(check, "check subscribers must be an array") end end end |
#validate_check_source(check) ⇒ Object
Validate check source. Validates: source
39 40 41 42 43 44 45 46 |
# File 'lib/sensu/settings/validators/check.rb', line 39 def validate_check_source(check) if is_a_string?(check[:source]) must_match_regex(/\A[\w\.-]*([:]{3}[\w\|\.-]+[:]{3}[\w\.-]*|[\w\.-]+)\z/, check[:source]) || invalid(check, "check source cannot contain spaces, special characters, or invalid tokens") else invalid(check, "check source must be a string") end end |
#validate_check_subdue(check) ⇒ Object
Validate check subdue. Validates: subdue
229 230 231 |
# File 'lib/sensu/settings/validators/check.rb', line 229 def validate_check_subdue(check) validate_time_windows(check, "check", :subdue) end |
#validate_check_truncate_output(check) ⇒ Object
Validate check truncate output. Validates: truncate_output, truncate_output_length
215 216 217 218 219 220 221 222 223 |
# File 'lib/sensu/settings/validators/check.rb', line 215 def validate_check_truncate_output(check) must_be_boolean_if_set(check[:truncate_output]) || invalid(check, "check truncate_output must be boolean") if check[:truncate_output_length] (must_be_an_integer(check[:truncate_output_length]) && check[:truncate_output_length] > 0) || invalid(check, "check truncate_output_length must be an integer greater than 0") end end |
#validate_check_ttl(check) ⇒ Object
Validate check ttl. Validates: ttl, ttl_status
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/sensu/settings/validators/check.rb', line 131 def validate_check_ttl(check) if is_an_integer?(check[:ttl]) check[:ttl] > 0 || invalid(check, "check ttl must be greater than 0") else invalid(check, "check ttl must be an integer") end must_be_an_integer_if_set(check[:ttl_status]) || invalid(check, "check ttl_status must be an integer") end |