Éric Lemoine | 6a1abe9 | 2016-11-03 14:02:43 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | import re |
| 4 | |
| 5 | _valid_dimension_re = re.compile(r'^[a-z0-9_/-]+$') |
| 6 | _disallowed_dimensions = ('name', 'value', 'hostname', 'member', |
| 7 | 'no_alerting', 'tag_fields') |
| 8 | |
| 9 | |
Éric Lemoine | 7dc1213 | 2016-11-07 16:25:09 +0000 | [diff] [blame] | 10 | def alarm_message_matcher(alarm, triggers): |
Éric Lemoine | 6a1abe9 | 2016-11-03 14:02:43 +0000 | [diff] [blame] | 11 | """ |
| 12 | Return an Heka message matcher expression for a given alarm and a |
| 13 | dict of triggers. |
| 14 | |
| 15 | For example the function may return this: |
| 16 | |
| 17 | Fields[name] == 'cpu_idle' || Fields[name] = 'cpu_wait' |
| 18 | """ |
| 19 | matchers = set() |
| 20 | for trigger_name in alarm.get('triggers', []): |
| 21 | trigger = triggers.get(trigger_name) |
| 22 | if trigger and trigger.get('enabled', True): |
| 23 | for rule in trigger.get('rules', []): |
| 24 | matcher = "Fields[name] == '{}'".format(rule['metric']) |
| 25 | matchers.add(matcher) |
| 26 | return ' || '.join(matchers) |
| 27 | |
| 28 | |
Swann Croiset | eed005a | 2016-11-10 15:37:53 +0100 | [diff] [blame] | 29 | def alarm_activate_alerting(alerting): |
| 30 | return 'true' if alerting in ['enabled', 'enabled_with_notification'] else 'false' |
| 31 | |
| 32 | |
| 33 | def alarm_enable_notification(alerting): |
| 34 | return 'true' if alerting == 'enabled_with_notification' else 'false' |
| 35 | |
| 36 | |
Éric Lemoine | 7dc1213 | 2016-11-07 16:25:09 +0000 | [diff] [blame] | 37 | def alarm_cluster_message_matcher(alarm_cluster): |
| 38 | """ |
| 39 | Return an Heka message matcher expression for a given alarm cluster. |
| 40 | |
| 41 | For example the function may return this: |
| 42 | |
| 43 | Fields[service] == 'rabbitmq-cluster' |
| 44 | """ |
| 45 | matchers = set() |
| 46 | match_items = alarm_cluster.get('match', {}).items() |
| 47 | for match_name, match_value in match_items: |
| 48 | matcher = "Fields[{}] == '{}'".format(match_name, match_value) |
| 49 | matchers.add(matcher) |
| 50 | match_items = alarm_cluster.get('match_re', {}).items() |
| 51 | for match_name, match_value in match_items: |
| 52 | matcher = "Fields[{}] =~ /{}/".format(match_name, match_value) |
| 53 | matchers.add(matcher) |
| 54 | return ' && '.join(matchers) |
| 55 | |
| 56 | |
Éric Lemoine | fc2ae37 | 2016-11-08 13:55:03 +0000 | [diff] [blame] | 57 | def dimensions(alarm_or_alarm_cluster): |
Éric Lemoine | 6a1abe9 | 2016-11-03 14:02:43 +0000 | [diff] [blame] | 58 | """ |
| 59 | Return a dict alarm dimensions. Each dimension is validated, and an |
| 60 | Exception is raised if a dimension is invalid. |
| 61 | |
| 62 | Valid characters are a-z, 0-9, _, - and /. |
| 63 | """ |
Éric Lemoine | fc2ae37 | 2016-11-08 13:55:03 +0000 | [diff] [blame] | 64 | dimensions = alarm_or_alarm_cluster.get('dimension', {}) |
Éric Lemoine | 6a1abe9 | 2016-11-03 14:02:43 +0000 | [diff] [blame] | 65 | for name, value in dimensions.items(): |
| 66 | if name in _disallowed_dimensions: |
| 67 | raise Exception( |
| 68 | '{} is not allowed as a dimension name'.format(name)) |
| 69 | if not _valid_dimension_re.match(name): |
| 70 | raise Exception( |
| 71 | 'Dimension name {} includes disallowed chars'.format(name)) |
| 72 | if not _valid_dimension_re.match(value): |
| 73 | raise Exception( |
| 74 | 'Dimension value {} includes disallowed chars'.format(value)) |
| 75 | return dimensions |
Éric Lemoine | 74f7bd3 | 2016-11-15 13:18:33 +0000 | [diff] [blame^] | 76 | |
| 77 | |
| 78 | def grains_for_mine(grains): |
| 79 | """ |
| 80 | Return grains that need to be sent to Salt Mine. Only the alarm |
| 81 | and alarm cluster data is to be sent to Mine. |
| 82 | """ |
| 83 | filtered_grains = {} |
| 84 | for service_name, service_data in grains.items(): |
| 85 | alarm = service_data.get('alarm') |
| 86 | if alarm: |
| 87 | filtered_grains[service_name] = {'alarm': alarm} |
| 88 | alarm_cluster = service_data.get('alarm_cluster') |
| 89 | if alarm_cluster: |
| 90 | if service_name in filtered_grains: |
| 91 | filtered_grains[service_name].update( |
| 92 | {'alarm_cluster': alarm_cluster}) |
| 93 | else: |
| 94 | filtered_grains[service_name] = \ |
| 95 | {'alarm_cluster': alarm_cluster} |
| 96 | return filtered_grains |