Skip to content

Commit

Permalink
NR-164383 feat: roles for HNR alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenruizdegauna committed Jan 8, 2024
1 parent 0d8061e commit ce226fe
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 0 deletions.
21 changes: 21 additions & 0 deletions caos.ansible_roles/roles/infrastructure_hnr_alert/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Create or Delete Infrastructure HNR alerts

```yaml
- name: Create HNR alerts
include_role:
name: caos.ansible_roles.infrastructure_hnr_alert
vars:
action: create
entity_name: "{{ iid }}:{{ inventory_hostname }}"
policy_name: "CAOS packaging tests HNR {{ inventory_hostname }}"
```
```yaml
- name: Delete HNR alerts
include_role:
name: caos.ansible_roles.infrastructure_hnr_alert
vars:
action: delete
entity_name: "{{ iid }}:{{ inventory_hostname }}"
policy_name: "CAOS packaging tests HNR {{ inventory_hostname }}"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
nr_api_key: ""
action: "" # create | delete
infra_alerts_host: "https://staging-infra-api.newrelic.com"
alerts_host: "https://staging-api.newrelic.com"
entity_name: "" # i.e. "{{ iid }}:{{ inventory_hostname }}"
policy_name: "" # i.e. "CAOS packaging tests HNR {{ inventory_hostname }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---

- name: create host not reporting policy
delegate_to: 127.0.0.1
uri:
url: "{{ alerts_host }}/v2/alerts_policies.json"
method: POST
body:
policy:
incident_preference: "PER_POLICY"
name: "{{ policy_name }}"
status_code: 201
body_format: json
headers:
Api-Key: "{{ nr_api_key }}"
register: response_policy
no_log: true

- name: create host not reporting condition
delegate_to: 127.0.0.1
uri:
url: "{{ infra_alerts_host }}/v2/alerts/conditions"
method: POST
body: "{\"data\":{\"type\":\"infra_host_not_reporting\",\"name\":\"CAOS packaging tests HNR {{ entity_name }}\",\"filter\":{\"and\":[{\"is\":{\"entityName\":\"{{ entity_name }}\"}}]},\"enabled\":true,\"violation_close_timer\": 24,\"policy_id\":{{ response_policy.json.policy.id }},\"critical_threshold\":{\"duration_minutes\":5,\"no_trigger_on\":[\"shutdown\"]}}}"
status_code: 201
body_format: json
headers:
Api-Key: "{{ nr_api_key }}"
no_log: true

...
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---

- name: get host not reporting policyId
delegate_to: 127.0.0.1
uri:
url: "{{ alerts_host }}/v2/alerts_policies.json?{{ 'filter[name]=' | urlencode }}{{ policy_name | urlencode }}"
method: GET
return_content: yes
status_code: 200
headers:
Api-Key: "{{ nr_api_key }}"
register: existing_response_policy
no_log: true

- name: fail if more than one policy found
fail:
msg: "more than one policy found"
when: existing_response_policy.json.policies | length > 1

- name: delete host not reporting policy
delegate_to: 127.0.0.1
uri:
url: "{{ alerts_host }}/v2/alerts_policies/{{ existing_response_policy.json.policies[0].id }}"
method: DELETE
status_code: 200
headers:
Api-Key: "{{ nr_api_key }}"
Content-Type: application/json
Accept: application/json
register: existing_response_policy
when: existing_response_policy.json.policies | length == 1
no_log: true

...
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---

- ansible.builtin.include_tasks: alert_{{ action }}.yml

...
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Infrastructure HNR alert violation assert

```yaml
- name: Create HNR alerts
include_role:
name: caos.ansible_roles.infrastructure_hnr_alert
vars:
action: create
entity_name: "{{ iid }}:{{ inventory_hostname }}"
policy_name: "CAOS packaging tests HNR {{ inventory_hostname }}"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
nr_api_key: ""
infra_alerts_host: "https://staging-infra-api.newrelic.com"
alerts_host: "https://staging-api.newrelic.com"
entity_name: "" # i.e. "{{ iid }}:{{ inventory_hostname }}"
policy_name: "" # i.e. "CAOS packaging tests HNR {{ inventory_hostname }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---

- name: list violations
delegate_to: 127.0.0.1
uri:
url: "{{ alerts_host }}/v2/alerts_violations.json?only_open=true"
method: GET
status_code: 200
headers:
Api-Key: "{{ nr_api_key }}"
register: existing_violations
no_log: true

- name: assert violation exists
vars:
violating_entities_query: "[?policy_name=='{{ policy_name }}'].entity.name"
assert:
that: "'{{entity_name}}' in existing_violations.json.violations | json_query(violating_entities_query) | flatten"
when: violation_open

- name: assert violation does not exists
vars:
violating_entities_query: "[?policy_name=='{{ policy_name }}'].entity.name"
assert:
that: "'{{entity_name}}' not in existing_violations.json.violations | json_query(violating_entities_query) | flatten"
when: not violation_open

...

0 comments on commit ce226fe

Please sign in to comment.