Skip to content

Commit e6418f3

Browse files
Add network deep diagnostics: NIC stats, conntrack, IRQ, softnet, TCP extended (#8)
* Add network deep diagnostics: NIC stats, conntrack, IRQ distribution, softnet, TCP extended New data collection in network collector (Tier 1, procfs/sysfs): NIC hardware details per interface: - Driver, speed, queue count (rx/tx) - Ring buffer current/max (ethtool -g) - RX discards and buffer errors (ethtool -S) - RPS enabled status, bond slave detection Conntrack table stats: - count/max/usage% from /proc/sys/net/netfilter/ - Anomaly detection at 70%/90% usage IRQ distribution: - Per-CPU NET_RX softirq delta (two-point sampling) - Detects IRQ imbalance across CPUs Softnet stats: - Per-CPU processed/dropped/time_squeeze from /proc/net/softnet_stat - Anomaly detection for softnet drops Extended TCP stats from /proc/net/netstat TcpExt: - ListenOverflows, ListenDrops (accept queue overflow) - PruneCalled (TCP memory pressure) - TCPOFOQueue (out-of-order packets) - TCPAbortOnMemory Additional sysctl reads: - tcp_mem, tcp_max_tw_buckets, tcp_keepalive_time, netdev_budget New anomaly thresholds: - conntrack_usage_pct (70%/90%) - softnet_dropped (1/10) - listen_overflows (1/100) - nic_rx_discards (100/10000) New recommendations: - Conntrack near full → increase nf_conntrack_max - Ring buffer too small with rx_discards → ethtool -G - ListenOverflows → add reuseport to nginx - PruneCalled → increase tcp_mem - conntrack, ring buffer, listen overflow explanations for MCP All existing tests pass. New fields use omitempty for backward compatibility. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * Review & expand network deep diagnostics: 32 anomaly rules, rate-based detection, full coverage Code review fixes: - Fix double sleep in IRQ distribution (was doubling collection time) - Fix sysctl syntax for tcp_mem recommendation - Fill ConntrackStats.Drops/InsertFailed/EarlyDrop from procfs - Read NIC speed and MTU from sysfs - Parameterize sysRoot for testability (NewNetworkCollectorFull) New data sources: - UDP stats from /proc/net/snmp (RcvbufErrors, SndbufErrors, InErrors) - Socket memory from /proc/net/sockstat (TCP orphans, mem pages) - TCP recv-side counters (TCPRcvQDrop, TCPZeroWindowDrop, TCPToZeroWindowAdv) - Listen queue depths from ss -tnl (Recv-Q/Send-Q fill percentage) - ESTABLISHED Recv-Q saturation from ss -tn - NIC offloads (ethtool -k: TSO/GRO/GSO), coalescing (ethtool -c), XPS - 15 new sysctls: rmem_max, wmem_max, netdev_max_backlog, ip_local_port_range, tcp_fin_timeout, tcp_keepalive_intvl/probes, tcp_slow_start_after_idle, tcp_fastopen, tcp_syncookies, tcp_notsent_lowat, default_qdisc, ARP gc_thresh Rate-based detection (two-point sampling): - Merge parseSNMP + parseUDPStats into single file read - Two-point sampling for softnet_stat and /proc/net/netstat - Rate fields: SoftnetDropRate, SoftnetSqueezeRate, ListenOverflowRate, TCPAbortMemRate, UDPRcvbufErrRate, TCPRcvQDropRate, TCPZeroWindowDropRate - Anomaly evaluators use per-second rates instead of cumulative counters Anomaly rules (29 → 32): - tcp_close_wait: CLOSE_WAIT socket accumulation (app not closing) - softnet_time_squeeze: NAPI budget exhausted (rate-based) - tcp_abort_on_memory: connections killed by memory pressure (rate-based) - irq_imbalance: uneven NET_RX distribution with low-traffic guard - udp_rcvbuf_errors: UDP receive buffer overflow (rate-based) - tcp_rcvq_drop: app not reading from ESTABLISHED sockets (rate-based) - tcp_zero_window_drop: receiver window=0, sender dropping (rate-based) - listen_queue_saturation: accept queue fill % from ss -tnl Recommendations: - Add Type field ("fix" vs "optimization") to all 28 recommendations - netdev_max_backlog, rmem_max/wmem_max, ip_local_port_range - tcp_slow_start_after_idle, tcp_fastopen, netdev_budget - UDP rmem_max, ARP gc_thresh for K8s/container environments - Accept queue SO_REUSEPORT with per-socket evidence - TCP zero-window: increase tcp_rmem + profile app - ESTABLISHED Recv-Q saturation: identify slow reader process Tests: 451 pass, 18 new test functions, testdata fixtures for conntrack, softnet_stat, softirqs, netstat, sockstat, enrichNICDetails Documentation (EN + RU): - CLI help (man page): all tiers, deep diagnostics, 32 rules - README: Manual Usage section with jq one-liners, 32 rules, 27 anti-patterns - doc/en+ru chapters 5, 11, 12 fully rewritten Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: dmitriimaksimovdevelop <227611064+dmitriimaksimovdevelop@users.noreply.github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 245a919 commit e6418f3

27 files changed

Lines changed: 2655 additions & 304 deletions

README.md

Lines changed: 127 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Most performance tools give you raw numbers. melisai gives you **a diagnosis**.
3131
- Runs Brendan Gregg's [USE Method](https://www.brendangregg.com/usemethod.html) automatically
3232
- Flags anomalies with severity (warning/critical) using field-tested thresholds
3333
- Computes a single **health score** (0-100) so an AI agent can decide what to do next
34-
- Generates a context-aware **AI prompt** with 23 known anti-patterns
34+
- Generates a context-aware **AI prompt** with 27 known anti-patterns
3535
- Works over **MCP** (Model Context Protocol) so Claude/Cursor can diagnose a server interactively
3636

3737
---
@@ -82,7 +82,7 @@ melisai mcp # starts stdio JSON-RPC server
8282
| `get_health` | Quick 0-100 score + anomalies. Tier 1 only, no root needed | ~1s |
8383
| `collect_metrics` | Full profile with all BCC/eBPF tools. Args: `profile`, `focus`, `pid` | 10s-60s |
8484
| `explain_anomaly` | Root causes + recommendations for a specific anomaly ID | instant |
85-
| `list_anomalies` | All 23 detectable anomaly metric IDs with descriptions | instant |
85+
| `list_anomalies` | All 29 detectable anomaly metric IDs with descriptions | instant |
8686

8787
### Typical workflow
8888

@@ -108,7 +108,9 @@ Agent melisai
108108
melisai collects metrics at three tiers with automatic fallback:
109109

110110
```
111-
Tier 1: /proc, /sys, ss, dmesg ← always works, no root
111+
Tier 1: /proc, /sys, ss, ethtool, dmesg ← always works, no root
112+
Includes deep network diagnostics: conntrack, softnet,
113+
IRQ distribution, NIC hardware, TCP extended stats
112114
Tier 2: 67 BCC tools (runqlat, bio...) ← root + bcc-tools
113115
Tier 3: native eBPF (cilium/ebpf) ← root + kernel ≥ 5.8 + BTF
114116
```
@@ -126,7 +128,7 @@ The report includes:
126128
| `summary.resources` | USE metrics per resource (utilization, saturation, errors) |
127129
| `summary.recommendations[]` | Copy-paste sysctl commands with citations |
128130
| `categories.*` | Raw data: histograms, events, stack traces per subsystem |
129-
| `ai_context.prompt` | Dynamic prompt with system context and 23 anti-patterns |
131+
| `ai_context.prompt` | Dynamic prompt with system context and 27 anti-patterns |
130132

131133
---
132134

@@ -212,7 +214,7 @@ Abbreviated JSON:
212214

213215
## Anomaly Detection
214216

215-
20 threshold rules based on Gregg's recommended values:
217+
29 threshold rules based on Gregg's recommended values:
216218

217219
| Metric | Warning | Critical | Source |
218220
|--------|---------|----------|--------|
@@ -229,10 +231,130 @@ Abbreviated JSON:
229231
| biolatency_p99_ssd | 5ms | 25ms | BCC histogram |
230232
| biolatency_p99_hdd | 50ms | 200ms | BCC histogram |
231233
| cpu_throttling | 100 | 1000 periods | cgroup cpu.stat |
234+
| conntrack_usage_pct | 70% | 90% | /proc/sys/net/netfilter/ |
235+
| softnet_dropped | 1 | 10 | /proc/net/softnet_stat |
236+
| listen_overflows | 1 | 100 | /proc/net/netstat |
237+
| nic_rx_discards | 100 | 10000 | ethtool -S |
238+
| tcp_close_wait | 1 | 100 | ss |
239+
| softnet_time_squeeze | 1 | 100 | /proc/net/softnet_stat |
240+
| tcp_abort_on_memory | 1 | 10 | /proc/net/netstat |
241+
| irq_imbalance | 5x ratio | 20x ratio | /proc/softirqs |
242+
| udp_rcvbuf_errors | 1 | 100 | /proc/net/snmp |
232243
| ... and 7 more (PSI, cache miss, DNS, container memory, network errors) | | | |
233244

234245
---
235246

247+
## Manual Usage (without AI)
248+
249+
melisai works perfectly as a standalone CLI tool — no AI agent required.
250+
251+
### Getting Help
252+
253+
```bash
254+
# General help — all commands and capabilities
255+
melisai --help
256+
257+
# Detailed help for collect (profiles, flags, examples)
258+
melisai collect --help
259+
260+
# Help for other commands
261+
melisai diff --help
262+
melisai install --help
263+
melisai mcp --help
264+
melisai capabilities --help
265+
```
266+
267+
### Typical Manual Workflow
268+
269+
```bash
270+
# 1. Quick health check — see if something is obviously wrong
271+
sudo melisai collect --profile quick -o quick.json
272+
273+
# 2. Read the summary
274+
cat quick.json | python3 -m json.tool | head -30
275+
# or use jq:
276+
jq '.summary' quick.json
277+
278+
# 3. Check health score and anomalies
279+
jq '.summary.health_score' quick.json # 0-100
280+
jq '.summary.anomalies[]' quick.json # what's wrong
281+
jq '.summary.recommendations[].title' quick.json # what to fix
282+
283+
# 4. Deep dive into network
284+
sudo melisai collect --profile standard --focus network -o net.json
285+
jq '.categories.network[0].data.conntrack' net.json # conntrack usage
286+
jq '.categories.network[0].data.softnet_stats' net.json # per-CPU drops
287+
jq '.categories.network[0].data.listen_overflows' net.json # accept queue
288+
289+
# 5. Profile a specific process
290+
sudo melisai collect --profile standard --pid $(pgrep nginx) -o nginx.json
291+
292+
# 6. Compare before/after a change
293+
sudo melisai collect --profile quick -o before.json
294+
# ... apply your fix ...
295+
sudo melisai collect --profile quick -o after.json
296+
melisai diff before.json after.json # human-readable
297+
melisai diff before.json after.json -o diff.json # JSON diff
298+
299+
# 7. Check what tools are available
300+
melisai capabilities
301+
```
302+
303+
### Interpreting the Report
304+
305+
The JSON report has four main sections:
306+
307+
| Section | How to read it |
308+
|---------|----------------|
309+
| `summary.health_score` | 90-100 = healthy, 70-89 = some issues, <70 = needs attention |
310+
| `summary.anomalies` | Each has `severity` (warning/critical), `metric`, `message` |
311+
| `summary.recommendations` | Copy-paste the `commands` field to fix issues |
312+
| `categories.network[0].data` | Raw metrics — interfaces, TCP, conntrack, softnet, etc. |
313+
314+
### Network Deep Diagnostics — Manual Inspection
315+
316+
```bash
317+
# Conntrack table usage
318+
jq '.categories.network[0].data.conntrack' report.json
319+
# {"count": 15000, "max": 65536, "usage_pct": 22.9}
320+
321+
# Softnet drops (per-CPU) — any "dropped" > 0 is bad
322+
jq '.categories.network[0].data.softnet_stats[] | select(.dropped > 0)' report.json
323+
324+
# Listen overflows (accept queue full)
325+
jq '.categories.network[0].data | {listen_overflows, listen_drops}' report.json
326+
327+
# NIC ring buffer (is it maxed out?)
328+
jq '.categories.network[0].data.interfaces[] | {name, driver, ring_rx_current, ring_rx_max, rx_discards}' report.json
329+
330+
# IRQ imbalance (check if one CPU handles all network interrupts)
331+
jq '.categories.network[0].data.irq_distribution' report.json
332+
333+
# TCP memory pressure
334+
jq '.categories.network[0].data | {prune_called, tcp_abort_on_memory, tcp_mem}' report.json
335+
```
336+
337+
### Useful jq One-Liners
338+
339+
```bash
340+
# All critical anomalies
341+
jq '.summary.anomalies[] | select(.severity == "critical")' report.json
342+
343+
# All recommendations with commands
344+
jq '.summary.recommendations[] | {title, commands}' report.json
345+
346+
# USE metrics for all resources
347+
jq '.summary.resources' report.json
348+
349+
# Top CPU-consuming processes
350+
jq '.categories.process[0].data.top_by_cpu[:5]' report.json
351+
352+
# BCC histogram percentiles
353+
jq '.categories.cpu[].histograms[]? | {name, p50, p99, max}' report.json
354+
```
355+
356+
---
357+
236358
## Architecture
237359

238360
```

cmd/melisai/main.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,29 @@ Collects metrics via BPF/eBPF tools (Brendan Gregg's ecosystem),
3737
procfs/sysfs, and standard utilities. Produces structured JSON
3838
reports optimized for AI-driven diagnostics and optimization.
3939
40-
Tier 1: /proc, /sys, ss, dmesg (always works, no root)
41-
Tier 2: BCC tools — runqlat, biolatency, tcpconnlat, etc. (needs root + tools)
42-
Tier 3: Native eBPF (cilium/ebpf) — zero dependencies (needs root + kernel ≥ 4.15)`,
40+
Data collection tiers (automatic fallback):
41+
42+
Tier 1: /proc, /sys, ss, ethtool, dmesg (always works, no root)
43+
CPU, memory, disk, network (deep: conntrack, softnet, IRQ,
44+
NIC ring buffers, TCP extended stats), process, container.
45+
Tier 2: 67 BCC tools — runqlat, biolatency, tcpconnlat, etc.
46+
(needs root + bcc-tools installed)
47+
Tier 3: Native eBPF (cilium/ebpf) — tcpretrans kprobe
48+
(needs root + kernel ≥ 5.8 + BTF)
49+
50+
Network deep diagnostics (Tier 1):
51+
- Conntrack table usage and drops (/proc/sys/net/netfilter/)
52+
- Softnet per-CPU stats: drops, time_squeeze (/proc/net/softnet_stat)
53+
- IRQ distribution: NET_RX delta per CPU (/proc/softirqs)
54+
- NIC hardware: driver, speed, MTU, queues, ring buffer, RPS, bond
55+
- TCP extended: ListenOverflows, ListenDrops, PruneCalled, TCPAbortOnMemory
56+
- UDP stats: RcvbufErrors, SndbufErrors (/proc/net/snmp)
57+
- Socket memory: orphans, TCP mem pages (/proc/net/sockstat)
58+
- 20+ sysctls: rmem_max, netdev_max_backlog, ip_local_port_range,
59+
tcp_fastopen, tcp_notsent_lowat, default_qdisc, and more
60+
61+
29 anomaly detection rules, health score (0-100), actionable recommendations.
62+
MCP server for interactive diagnostics from Claude Desktop / Cursor.`,
4363
Version: version,
4464
}
4565

@@ -60,7 +80,22 @@ Tier 3: Native eBPF (cilium/ebpf) — zero dependencies (needs root + kernel ≥
6080
collectCmd := &cobra.Command{
6181
Use: "collect",
6282
Short: "Collect system performance metrics",
63-
Long: "Run all available collectors and produce a structured JSON report.",
83+
Long: `Run all available collectors and produce a structured JSON report.
84+
85+
Collectors run in two phases to avoid observer effect:
86+
Phase 1: Tier 1 (procfs/sysfs) — CPU, memory, disk, network, process, container
87+
Phase 2: Tier 2/3 (BCC/eBPF) — histograms, events, stack traces
88+
89+
Network collector includes deep diagnostics:
90+
conntrack, softnet stats, IRQ distribution, NIC hardware details,
91+
TCP extended counters (ListenOverflows, PruneCalled, etc.)
92+
93+
Profiles: quick (10s), standard (30s), deep (60s)
94+
95+
Examples:
96+
melisai collect --profile quick -o report.json
97+
melisai collect --profile standard --focus network -o net.json
98+
melisai collect --profile deep --pid 12345 -o app.json`,
6499
RunE: func(cmd *cobra.Command, args []string) error {
65100
cfg := collector.DefaultConfig()
66101
cfg.Profile = collectProfile

0 commit comments

Comments
 (0)