From f28706d27d14b3f312574b9ae27a575720990006 Mon Sep 17 00:00:00 2001 From: Dan Corrigan Date: Wed, 27 Nov 2024 15:19:02 -0600 Subject: [PATCH] move userip tasks to dailyconnections and modify report --- tasks/dailyconnections.yml | 65 +++++++++++----------- tasks/userips.yml | 108 ------------------------------------- templates/report.txt.j2 | 11 ++-- 3 files changed, 40 insertions(+), 144 deletions(-) delete mode 100644 tasks/userips.yml diff --git a/tasks/dailyconnections.yml b/tasks/dailyconnections.yml index 605c04a..608d2b1 100644 --- a/tasks/dailyconnections.yml +++ b/tasks/dailyconnections.yml @@ -1,16 +1,16 @@ --- -- name: "Daily Connections | Check if Previous Day Log Exists" +- name: "Daily Connections | Check if previous day log exists" ansible.builtin.stat: path: "{{ role_iptvservice__nginx_log_prev_day }}" register: __previous_day_log_stat -- name: "Daily Connections | Assert that Previous Day Log Exists" +- name: "Daily Connections | Assert that previous day log exists" ansible.builtin.assert: that: __previous_day_log_stat.stat.exists | bool msg: "Previous Day Log file {{ role_iptvservice__nginx_log_prev_day }} must exist to run a report." when: not __previous_day_log_stat.stat.exists | bool -- name: "Daily Connections | Previous Day Log Exists" +- name: "Daily Connections | Previous day log exists" when: __previous_day_log_stat.stat.exists | bool block: - name: "Daily Connections | Gather minimal facts" @@ -53,7 +53,7 @@ {{ __full_log.splitlines() | select('search', '(/player_api.php|/xmltv.php|/live)') | select('search', role_iptvservice__firewall_local_ip) }} no_log: true - - name: "Daily Connections | Unique Connected IPs" + - name: "Daily Connections | Unique connected IPs" ansible.builtin.set_fact: __unique_log_lines: >- {%- set unique_ips = [] -%} @@ -68,7 +68,7 @@ {{ unique_lines }} no_log: true - - name: "Daily Connections | Unique Used Upstream ports" + - name: "Daily Connections | Unique used upstream ports" ansible.builtin.set_fact: __used_ports: "{{ __unique_log_lines | map('regex_search', role_iptvservice__firewall_local_ip + ':[0-9]{5}') | map('regex_search', '[0-9]{5}$') | community.general.counter }}" @@ -139,16 +139,16 @@ {%- endfor -%} {{ __unique_user_ips }} - - name: "User IPs | Ensure user_ips directory exists" + - name: "Daily Connections | Ensure user_ips directory exists" ansible.builtin.file: path: "{{ role_iptvservice__iptv_logs_path }}/user_ips" state: directory mode: '0750' - - name: "User IPs | Add IPs to user IP file" + - name: "Daily Connections | Add IPs to user IP file" ansible.builtin.lineinfile: path: '{{ role_iptvservice__iptv_logs_path }}/user_ips/{{ item.0.user }}' - line: '{{ item.1 }}' + line: '{{ item.1 }}' state: present create: true mode: '0644' @@ -156,28 +156,27 @@ loop: "{{ __unique_user_ips | subelements('ip_list') }}" loop_control: label: >- - Adding '{{ item.1 }}' to IP user file for '{{ item.0.user }}' + Checking User: '{{ item.0.user }}' for IP: '{{ item.1 }}' - - name: Debug - ansible.builtin.debug: - var: __unique_user_ips | subelements('ip_list') | zip(__add_ip.results) - - - name: "Check which subelements caused changes" - ansible.builtin.debug: - msg: >- - "item.0: {{ item.0 }} item.1: {{ item.1 }}" - loop: "{{ __unique_user_ips | subelements('ip_list') | zip(__add_ip.results) }}" - - - # - debug: - # var: __add_ip - - meta: end_play - # - name: "Daily Connections | Include User IPs task file" - # ansible.builtin.include_tasks: - # file: userips.yml - # loop: '{{ __unique_user_ips }}' + - name: "Daily Connections | Create list of user IP status" + ansible.builtin.set_fact: + __ip_status: >- + {%- set __ip_status = [] -%} + {%- set user = 'user' -%} + {%- set ip = 'ip' -%} + {%- set new = 'new' -%} + {%- for item in __unique_user_ips | subelements('ip_list') | zip(__add_ip.results) -%} + {%- set __new = 'true' if item.1.changed | bool else 'false' -%} + {%- set item_dict = { + user: (item.0 | first).user, + ip: item.0 | last, + new: __new} + -%} + {%- set _ = __ip_status.append(item_dict) -%} + {%- endfor -%} + {{ __ip_status }} - - name: "Daily Connections | Create list of IP Info" + - name: "Daily Connections | Create list of IP info" ansible.builtin.set_fact: __ip_info: >- {%- set __ip_info = [] -%} @@ -202,25 +201,25 @@ {%- endfor -%} {{ __ip_info }} - - name: "Daily Connections | Ensure Reports directory exists" + - name: "Daily Connections | Ensure reports directory exists" ansible.builtin.file: path: '{{ role_iptvservice__report_path }}' state: directory mode: '0750' - - name: "Daily Connections | Template Report" + - name: "Daily Connections | Template report" ansible.builtin.template: src: report.txt.j2 dest: "{{ role_iptvservice__report_path }}/{{ ansible_date_time.date }}-iptv-monitor.txt" mode: '0600' become: true - - name: "Daily Connections | Slurp Report" + - name: "Daily Connections | Slurp report" ansible.builtin.slurp: src: "{{ role_iptvservice__report_path }}/{{ ansible_date_time.date }}-iptv-monitor.txt" register: __encoded_report - - name: "Daily Connections | Decode Report" + - name: "Daily Connections | Decode report" ansible.builtin.set_fact: __report: "{{ __encoded_report['content'] | b64decode }}" @@ -235,7 +234,7 @@ body: "{{ __report }}" become: true rescue: - - name: "Daily Connections | Fail Playbook" + - name: "Daily Connections | Fail playbook" ansible.builtin.fail: msg: "Something went wrong with the report." ... diff --git a/tasks/userips.yml b/tasks/userips.yml deleted file mode 100644 index c6dad56..0000000 --- a/tasks/userips.yml +++ /dev/null @@ -1,108 +0,0 @@ ---- -- name: "User IPs | Ensure user_ips directory exists" - ansible.builtin.file: - path: "{{ role_iptvservice__iptv_logs_path }}/user_ips" - state: directory - mode: '0750' - -- name: "User IPs | Check if User IP Files Exist" - ansible.builtin.stat: - path: "{{ role_iptvservice__iptv_logs_path }}/user_ips/{{ item.user }}" - register: __user_ip_file - loop_control: - label: >- - Checking for '{{ item.user }}' - index_var: __my_idx - extended: true - extended_allitems: false - -- name: "User IPs | Check if IPs exist in user File" - ansible.builtin.lineinfile: - path: '{{ __user_ip_file.stat.path }}' - regexp: '{{ ip }}' - state: absent - check_mode: true - register: __user_ip_file_check - when: __user_ip_file.stat.exists | bool - loop: '{{ item.ip_list }}' - loop_control: - loop_var: ip - label: >- - Checking '{{ item.user }}' for '{{ ip }}' - index_var: __my_idx - extended: true - extended_allitems: false - -- name: "User IPs | Add new IP to exiting user IP file" - ansible.builtin.lineinfile: - path: '{{ __user_ip_file.stat.path }}' - line: '{{ __ip_file_check.ip }}' - state: present - when: - - __user_ip_file.stat.exists | bool - - __ip_file_check.found == 0 - register: __add_ip - loop: '{{ __user_ip_file_check.results }}' - loop_control: - loop_var: __ip_file_check - label: >- - Adding '{{ __ip_file_check.ip }}' to existing IP user file for '{{ item.user }}' - index_var: __my_idx - extended: true - extended_allitems: false - -- name: "User IPs | Add new IP to new file user IP file" - ansible.builtin.lineinfile: - path: '{{ role_iptvservice__iptv_logs_path }}/user_ips/{{ item.user }}' - line: '{{ __ip_file_check.ip }}' - state: present - create: true - mode: '0644' - when: - - not __user_ip_file.stat.exists | bool - register: __add_ip_new_file - loop: '{{ __user_ip_file_check.results }}' - loop_control: - loop_var: __ip_file_check - label: >- - Adding '{{ __ip_file_check.ip }}' to new IP user file for '{{ item.user }}' - index_var: __my_idx - extended: true - extended_allitems: false - -- name: "User IPs | Set fact about existing user IP file status" - ansible.builtin.set_fact: - __ip_added_existing: >- - {%- set __ip_added_existing = __ip_added_existing | default([]) -%} - {%- set user = 'user' -%} - {%- set ip = 'ip' -%} - {%- set new = 'new' -%} - {%- for __ip in __add_ip.results -%} - {%- set __new = 'true' if __ip.changed | bool else 'false' -%} - {%- set item_dict = { - user: item.user, - ip: __ip.__ip_file_check.ip, - new: __new} - -%} - {%- set __ip_added_existing = __ip_added_existing.append(item_dict) -%} - {%- endfor -%} - {{ __ip_added_existing }} - -- name: "User IPs | Set fact about new user IP file status" - ansible.builtin.set_fact: - __ip_added_new: >- - {%- set __ip_added_new = __ip_added_new | default([]) -%} - {%- set user = 'user' -%} - {%- set ip = 'ip' -%} - {%- set new = 'new' -%} - {%- for __ip in __add_ip_new_file.results -%} - {%- set __new = 'true' if not __user_ip_file.stat.exists | bool else 'false' -%} - {%- set item_dict = { - user: item.user, - ip: __ip.__ip_file_check.ip, - new: __new} - -%} - {%- set __ip_added_new = __ip_added_new.append(item_dict) -%} - {%- endfor -%} - {{ __ip_added_new }} -... diff --git a/templates/report.txt.j2 b/templates/report.txt.j2 index c18cb11..5b05b64 100644 --- a/templates/report.txt.j2 +++ b/templates/report.txt.j2 @@ -13,9 +13,9 @@ User Usage Summary -------------------------- {{ user.user }} on {{ user.provider }} --------------------------- +{% if user.ip_list | length > 0 %} {% for ip in user.ip_list | unique %} - -{% if __ip_added_existing | selectattr('user', '==', user.user) | selectattr('ip', '==', ip) | map(attribute='new') | first == 'true' or __ip_added_new | selectattr('user', '==', user.user) | selectattr('ip', '==', ip) | map(attribute='new') | first == 'true' %}New IP:{% else %}IP:{% endif %} {% if role_iptvservice__known_ips[ip] is defined %}{{ role_iptvservice__known_ips[ip]['name'] }} / {% endif %}{{ ip }}{% if __ip_info | selectattr('ip', '==', ip) | map(attribute='reverse') | first | length > 0 and __ip_info | selectattr('ip', '==', ip) | map(attribute='reverse') | first != 'NXDOMAIN' %} / {{ __ip_info | selectattr('ip', '==', ip) | map(attribute='reverse') | first }} +{% if __ip_status | selectattr('user', '==', user.user) | selectattr('ip', '==', ip) | map(attribute='new') | first == 'true' %}New IP:{% else %}IP:{% endif %} {% if role_iptvservice__known_ips[ip] is defined %}{{ role_iptvservice__known_ips[ip]['name'] }} / {% endif %}{{ ip }}{% if __ip_info | selectattr('ip', '==', ip) | map(attribute='reverse') | first | length > 0 and __ip_info | selectattr('ip', '==', ip) | map(attribute='reverse') | first != 'NXDOMAIN' %} / {{ __ip_info | selectattr('ip', '==', ip) | map(attribute='reverse') | first }} {% else %} / No Reverse DNS {% endif %} IP Org: {{ __ip_info | selectattr('ip', '==', ip) | map(attribute='org') | first }} @@ -27,4 +27,9 @@ User Agent(s): {% endfor %} {% endfor %} -{% endfor %} \ No newline at end of file +{% else %} +No connections for user. + +{% endif %} +{% endfor %} +