diff --git a/roles/bgp_checks/tasks/check_bgp_status.yaml b/roles/bgp_checks/tasks/check_bgp_status.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ba3b3ff9c8d440c90ab8eff4a73c9eeea202bd6b --- /dev/null +++ b/roles/bgp_checks/tasks/check_bgp_status.yaml @@ -0,0 +1,47 @@ +--- + +############################################################################################################### +# The playbook does the following: +# +# - Loads variables from pre and post check results +# - Prints all the pre or post check BGP statistics that were retreived +# - Runs a number of assertions upon these metrics and prints weather a metric has been a 'PASS' or 'FAIL': +# - Checks if bgp_state (peer-state) is 'Established' = PASS +# - Checks if bfd_operstate (bfd-operational-state) is 'UP' = PASS +# - If there is a FAIL, the playbook stops +# +############################################################################################################### + +- name: Load variables from pre and post check results + ansible.builtin.include_vars: + dir: vars + files_matching: "{{ check_id }}.yaml" + +- name: TEST Print PRE interface statistics + ansible.builtin.debug: + var: pre_check + when: verb == 'pre_check' + +- name: TEST Print POST interface statistics + ansible.builtin.debug: + var: post_check + when: verb == 'post_check' + +# SESSION_UP: The BGP session must be up (both IPv4 and IPv6) + +- name: Get bgp state + ansible.builtin.assert: + that: + - "{{ verb }}.{{ check_id }}.bgp_state == 'Established'" + fail_msg: "FAIL: bgp isn't Established" + success_msg: "PASS: bgp state is Established" + failed_when: false + +# BFD: If BFD is configured, it should be UP + +- name: Get bfd state + ansible.builtin.assert: + that: + - "{{ verb }}.{{ check_id }}.bfd_operstate == 'Up'" + fail_msg: "FAIL: bfd isn't Up" + success_msg: "PASS: bfd state is Up" diff --git a/roles/bgp_checks/tasks/compare_pre_post.yaml b/roles/bgp_checks/tasks/compare_pre_post.yaml new file mode 100644 index 0000000000000000000000000000000000000000..e7ab268d881738e15de1e8adaaa9d9ab6f8aec25 --- /dev/null +++ b/roles/bgp_checks/tasks/compare_pre_post.yaml @@ -0,0 +1,74 @@ +--- + +############################################################################################################### +# The playbook does the following: +# +# - Loads variables from pre and post check results +# - Prints all the pre or post check BGP statistics that were retreived +# - Runs a number of assertions upon these metrics and prints weather a metric has been a 'PASS' or 'FAIL': +# - Checks if bgp_state (peer-state) is 'Established' = PASS +# - Checks if bfd_operstate (bfd-operational-state) is 'UP' = PASS +# - If there is a FAIL, the playbook stops +# +############################################################################################################### + +- name: Load variables from pre and post check results + ansible.builtin.include_vars: + dir: vars + files_matching: "{{ check_id }}.yaml" + +- name: TEST Print PRE interface statistics + ansible.builtin.debug: + var: pre_check + when: verb == 'pre_check' + +- name: TEST Print POST interface statistics + ansible.builtin.debug: + var: post_check + when: verb == 'post_check' + +# NLRIs: The NLRIs exchanged must be the same + +- name: Compare NLRIs + ansible.builtin.assert: + that: + - post_check.{{ check_id }}.nlri_peerconf is defined + - pre_check.{{ check_id }}.nlri_peerconf is defined + - post_check.{{ check_id }}.nlri_peer is defined + - pre_check.{{ check_id }}.nlri_peer is defined + - post_check.{{ check_id }}.nlri_peerconf is defined + - pre_check.{{ check_id }}.nlri_peerconf is defined + - post_check.{{ check_id }}.nlri_session is defined + - pre_check.{{ check_id }}.nlri_session is defined + - post_check.{{ check_id }}.nlri_rib is defined + - pre_check.{{ check_id }}.nlri_rib is defined + - "post_check.{{ check_id }}.nlri_peerconf == pre_check.{{ check_id }}.nlri_peerconf" + - "post_check.{{ check_id }}.nlri_peer == pre_check.{{ check_id }}.nlri_peer" + - "post_check.{{ check_id }}.nlri_session == pre_check.{{ check_id }}.nlri_session" + - "post_check.{{ check_id }}.nlri_peernego == pre_check.{{ check_id }}.nlri_peernego" + - "post_check.{{ check_id }}.nlri_rib == pre_check.{{ check_id }}.nlri_rib" + fail_msg: "FAIL: the nlri's exchanged aren't the same" + success_msg: "PASS: the nlri's exchanged are the same" + failed_when: false + +# ROUTES: The number of advertised and received routes must be within 5% difference with the previous state + +- name: Compare the number of advertised routes + ansible.builtin.assert: + that: + - post_check.bgp_adv_routes is defined + - pre_check.bgp_adv_routes is defined + - "((pre_check.{{ check_id }}.bgp_adv_routes - post_check.{{ check_id }}.bgp_adv_routes) / pre_check.{{ check_id }}.bgp_adv_routes) * 100 | abs <= 5" + fail_msg: "FAIL: the number of advertised routes isn't within 5% of difference" + success_msg: "PASS: the number of advertised routes is within 5% of difference" + failed_when: false + +- name: Compare the number of received routes + ansible.builtin.assert: + that: + - post_check.bgp_adv_routes is defined + - pre_check.bgp_adv_routes is defined + - "((pre_check.{{ check_id }}.bgp_rec_routes - post_check.{{ check_id }}.bgp_rec_routes) / pre_check.{{ check_id }}.bgp_rec_routes) * 100 | abs <= 5" + fail_msg: "FAIL: the number of received routes isn't within 5% of difference" + success_msg: "PASS: the number of received routes is within 5% of difference" + failed_when: false diff --git a/roles/bgp_checks/tasks/create_file.yaml b/roles/bgp_checks/tasks/create_file.yaml index c60826747f1bb1491685416ccaaab0276348743c..338df15f7cf37056a8f8e7cdfa1ca0f96c6ba2b6 100644 --- a/roles/bgp_checks/tasks/create_file.yaml +++ b/roles/bgp_checks/tasks/create_file.yaml @@ -14,7 +14,7 @@ - name: Get output file information ansible.builtin.stat: - path: roles/bgp_checks/vars/{{ check_id }}.yaml + path: "{{ results_dir }}{{ check_id }}.yaml" register: file_status - name: Creating a file with results content @@ -46,14 +46,14 @@ {{ 'bgp_rec_active:' }} {{ bgp_rec_active }} {{ 'bgp_rec_holddown:' }} {{ bgp_rec_holddown }} {{ 'bgp_rec_hidden:' }} {{ bgp_rec_hidden }} - dest: "roles/bgp_checks/vars/{{ check_id }}.yaml" + dest: "{{ results_dir }}{{ check_id }}.yaml" mode: '0660' when: - not file_status.stat.exists - name: Add post_check parameters to yaml ansible.builtin.blockinfile: - path: "roles/bgp_checks/vars/{{ check_id }}.yaml" + path: "{{ results_dir }}{{ check_id }}.yaml" marker: "# {mark} -------------- POST CHECKS ----------------------------- #" block: | {{ verb + ':' }} diff --git a/roles/bgp_checks/tasks/main.yaml b/roles/bgp_checks/tasks/main.yaml index 04f048f7b48a1b039a42ab074648f2f964cee3cb..3331fccfe732a340bb18b8982f32fe68e1a08b95 100644 --- a/roles/bgp_checks/tasks/main.yaml +++ b/roles/bgp_checks/tasks/main.yaml @@ -40,7 +40,12 @@ - (verb is not defined) or (neighbor_address is not defined) or (vendor is not defined) or (check_id is not defined) - name: Include task - ansible.builtin.include_tasks: get_bgpstatus.yaml + ansible.builtin.include_tasks: check_bgp_status.yaml + when: + - verb in verbs + +- name: Include task + ansible.builtin.include_tasks: compare_pre_post.yaml when: - verb in verbs diff --git a/roles/bgp_checks/vars/main.yaml b/roles/bgp_checks/vars/main.yaml index ac46f829591872fdfb6e027f382f985f185bdb35..38742aaf93b01b5307a6d5243dbc932e7c03749b 100644 --- a/roles/bgp_checks/vars/main.yaml +++ b/roles/bgp_checks/vars/main.yaml @@ -1,5 +1,8 @@ --- # vars file for L3-BGP-based-checks +results_dir: "/var/tmp/" +max_adv_routes_diff: 10 +max_rec_routes_diff: 10 dryrun: "True" verbs: - "pre_check"