Files
bab-backend-ansible/playbooks/investigate_high_cpu.yml
2026-10-14 12:15:15 -06:00

101 lines
3.2 KiB
YAML

---
- name: Investigate High CPU
hosts: all
become: true
tasks:
- name: Gather information on top CPU consuming processes
ansible.builtin.command:
cmd: 'ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%cpu'
register: processes_cpu
changed_when: false
- name: Gather information on top Memory consuming processes
ansible.builtin.command:
cmd: 'ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%mem'
register: processes_mem
changed_when: false
- name: Set var
ansible.builtin.set_fact:
event_payload: "{{ processes_cpu + processes_mem }}"
- name: Open Incident
hosts: all
tasks:
- name: Create Problem Template # noqa: no-relative-paths
ansible.builtin.template:
mode: '0644'
src: 'cpuhog_ticket.j2'
dest: /tmp/cpuhog_details.txt
delegate_to: localhost
- name: Create SNow Incident
servicenow.itsm.incident:
instance: '{{ snow_instance }}'
state: new
caller: "admin"
short_description: "CPUHog event detected on: {{ alertmanager_instance }}"
description: "A CPUHog was detected on: {{ alertmanager_instance }} that needs to be investigated."
impact: high
urgency: high
delegate_to: localhost
register: incident_result
- name: Trigger EDA webhook
hosts: all
connection: local
gather_facts: false
tasks:
- name: Obtain OAuth token
ansible.builtin.uri:
url: "{{ webhook_base_url }}/api/v1/auth/token"
method: POST
body_format: form-urlencoded
body:
grant_type: client_credentials
client_id: "{{ webhook_client_id }}"
client_secret: "{{ webhook_client_secret }}"
status_code: 200
register: token_response
- name: Post event payload to Event-Driven Ansible trigger endpoint
ansible.builtin.uri:
url: "{{ webhook_base_url }}/api/v1/webhooks/eda/{{ webhook_path }}"
method: POST
body_format: json
headers:
Authorization: "Bearer {{ token_response.json.access_token }}"
Content-Type: application/json
body: "{{ event_payload }}"
status_code: 202
register: webhook_response
- name: Display execution ID
ansible.builtin.debug:
msg: "Workflow execution started: {{ webhook_response.json.execution_id }}"
# - name: Create SNow Problem
# servicenow.itsm.problem:
# instance: '{{ snow_instance }}'
# state: new
# short_description: "{{ alertmanager_annotations.summary | default('CPUHog event detected on: ' ~ alertmanager_instance) }}"
# description: "Generator URL: {{ alertmanager_generator_url }}"
# impact: high
# urgency: high
# attachments:
# - path: /tmp/cpuhog_details.txt
# name: cpuhog_details.txt
# type: 'text/plain'
# register: problem_result
# delegate_to: localhost
# - name: Update Incident
# servicenow.itsm.incident:
# instance: '{{ snow_instance }}'
# state: in_progress
# number: "{{ incident_result.record.number }}"
# other:
# problem_id: "{{ problem_result.record.number }}"
# delegate_to: localhost