docs/querying.md

Querying the Database

Each dataset has its own SQLite database at data/<dataset>/netflow.sqlite.

Direct Access

bash
1sqlite3 data/<dataset>/netflow.sqlite

Useful SQLite commands:

text
1.tables
2.schema netflow_stats
3.schema ip_stats

Schema Overview

netflow_stats — per-router flow/packet/byte counts, bucketed by file timestamp:

  • file_path, router, timestamp (Unix timestamp)
  • flows, flows_tcp, flows_udp, flows_icmp, flows_other
  • packets, packets_tcp, packets_udp, packets_icmp, packets_other
  • bytes, bytes_tcp, bytes_udp, bytes_icmp, bytes_other
  • first_timestamp, last_timestamp, msec_first, msec_last
  • Indexed for efficient router/time-range queries

ip_stats — unique source/destination IP counts per router per time bucket:

  • router, granularity, bucket_start, bucket_end
  • granularity is one of 5m, 30m, 1h, 1d
  • sa_ipv4_count, da_ipv4_count, sa_ipv6_count, da_ipv6_count

Example Queries

Daily flow/packet summary for a time window:

sql
1SELECT
2 DATE(timestamp, 'unixepoch') AS day,
3 router,
4 SUM(flows) AS flows,
5 SUM(packets) AS packets,
6 SUM(bytes) AS bytes
7FROM netflow_stats
8WHERE router IN ('router1', 'router2')
9 AND timestamp BETWEEN strftime('%s', '2025-01-01') AND strftime('%s', '2025-01-08')
10GROUP BY day, router
11ORDER BY day, router;

30-minute protocol breakdown for a single day:

sql
1SELECT
2 strftime('%Y-%m-%d %H:%M', timestamp - (timestamp % 1800), 'unixepoch') AS bucket,
3 SUM(flows_tcp) AS flows_tcp,
4 SUM(flows_udp) AS flows_udp,
5 SUM(flows_icmp) AS flows_icmp
6FROM netflow_stats
7WHERE router = 'router1'
8 AND timestamp BETWEEN strftime('%s', '2025-01-03') AND strftime('%s', '2025-01-04')
9GROUP BY timestamp - (timestamp % 1800)
10ORDER BY timestamp - (timestamp % 1800);

Per-router IP counts:

sql
1SELECT router, bucket_start, sa_ipv4_count, da_ipv4_count, sa_ipv6_count, da_ipv6_count
2FROM ip_stats
3WHERE granularity = '1h'
4 AND router IN ('router1', 'router2')
5 AND bucket_start BETWEEN strftime('%s', '2025-01-01') AND strftime('%s', '2025-01-02')
6ORDER BY router, bucket_start;