> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/tsr/latest/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/tsr/latest/configuration/natevent.md).

# Set up TSR Sink for NatEvent IPFIX messages

Traffic Stream Reporter (TSR) that handles NatEvent IPFIX messages, stores information about Network Address Translation (NAT) events.
It stores internal IP source addresses and ports, the public IP source addresses and ports (NAT binding), the IP destination addresses and ports, and the used protocol.
Additionally, IMSI, the NAT event (start, stop), the start and end time of the NAT binding are stored.

## Prerequisites

- [Kubernetes 1.18](https://kubernetes.io/docs/tasks/tools/) or newer
- [Helm client](https://helm.sh/docs/intro/install/) 3.9.2 or newer
- MySQL database

## Steps

1. Create a namespace for your TSR installation:

   ```bash
   kubectl create namespace tsr
   ```

2. Create a TSR Sink configuration file that specifies the port on which Sink receives the IPFIX packets. The configuration file also contains a reference to your deployed MySQL database that stores data into the `natev_$day` table. 

   ```yaml
     db:
       host: db
       port: 3306
       user: ipfix
       pass: ipfix
       db: tsr
       engine: InnoDB
       persist:
         duration: 5s
      
     buffer:
        persist: 500
     ipfix:
       debug: true
       port: 4739
       type: natEvent
   ```

3. Install TSR Sink on the cluster:

   ```bash
   helm install tsr-sink {REPOSITORY_NAME}/tsr-sink -f {FILENAME}.yaml --namespace tsr
   ```

4. Fetch all natevents from the database with the SQL statement below. The table name is `natev_YEAR_MONTH_DAY`.

   ```sql
   SELECT
       mobileimsi as imsi,
       ip_src,
       ip_dst,
       src_port,
       dst_port,
       ip_proto,
       nat_event,
       post_nat_ip_src,
       post_nat_port_src,
       timestamp_start,
       timestamp_end
   FROM  natev_2024_03_01;
   ```
## Trooubleshooting

Depending on the number of the incomming IPFIX messages, there are a few settings that are helpful to handle higher load.

In the configuration in step 2, it persists every 5 seconds (**db.persist.duration**) or 500 messages (**buffer.persist**), whichever conditions is met first.
It can be beneficial to increase these values, so that fewer statements are sent to the database.
There is also the option to change the storage engine of the database to `ARCHIVE`.
Compared to `InnoDB`, inserting data with the `ARCHIVE` setting is faster but querying the data is slower.
