Skip to content

Commit

Permalink
move userip tasks to dailyconnections and modify report
Browse files Browse the repository at this point in the history
  • Loading branch information
dancorrigan1 committed Nov 27, 2024
1 parent 5c67a5f commit f28706d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 144 deletions.
65 changes: 32 additions & 33 deletions tasks/dailyconnections.yml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 = [] -%}
Expand All @@ -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 }}"
Expand Down Expand Up @@ -139,45 +139,44 @@
{%- 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'
register: __add_ip
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 = [] -%}
Expand All @@ -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 }}"

Expand All @@ -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."
...
108 changes: 0 additions & 108 deletions tasks/userips.yml

This file was deleted.

11 changes: 8 additions & 3 deletions templates/report.txt.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand All @@ -27,4 +27,9 @@ User Agent(s):
{% endfor %}
{% endfor %}

{% endfor %}
{% else %}
No connections for user.

{% endif %}
{% endfor %}

0 comments on commit f28706d

Please sign in to comment.