Parsing diagnose sys session list: fgt-session-parser and the proto_state I Got Wrong
I wrote fgt-session-parser while travelling with patchy signal, as a small project that needed nothing from the internet. It reads the output of diagnose sys session list and turns it into a table, a per-policy summary, or JSON. The repo is at github.com/MichealGarner/fgt-session-parser.
The tool is simple. The more useful result was that building it showed me mistakes in a post I had already published, and then showed me a mistake in my own first fix.
Why parse session output at all
diagnose sys session list prints about a dozen lines per session. On a busy unit that is thousands of lines, and the fields you want (protocol, the original tuple, NAT, policy, state, bytes) are scattered across them. You can filter on the FortiGate itself, and for a first look you should. But once the output is in a file, or you want to sort by bytes, or compare two captures, you need it in a structure.
The parser reads the session info: header, the statistic(...) line and the hook= lines, and ignores what it does not recognise. Unknown keys are kept, not rejected, because FortiOS adds fields between versions and a parser that fails on a new key is useless the first time you upgrade.
Using it
Install it into a virtual environment, not your global Python:
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -e ".[dev]"
Then feed it a saved capture, or pipe to it with -:
fgt-sessions capture.txt
fgt-sessions capture.txt --policy 5
fgt-sessions capture.txt --src 10.10.1.0/24 --proto tcp --top 10
fgt-sessions capture.txt --older-than 600
fgt-sessions capture.txt --summary
fgt-sessions capture.txt --json
--summary groups by policy ID and sorts by total bytes, which is the quickest way to see which policy is carrying the traffic. --older-than and --newer-than filter on the session’s duration. --json gives you something to pass to jq or another script.
Exit codes follow the other tools on this site: 0 for success, 1 if the input cannot be read, 2 if there was content but no sessions could be parsed. A script can tell an empty capture from a broken one.
Where I got it wrong
I wrote the first version of the parser and its test fixtures from memory, because I had no connection to check against. Three of my assumptions about the format were wrong, and I only found out when I captured a real session from my 70G and ran it through the parser:
vd=0:20is an index and an ID, not a number. My code expected an integer.- Sessions to or from the FortiGate itself show
policy_id=4294967295, which means no policy. My code treated it as a real policy ID. - The output ends with a
total session: Nline that has no=in it.
I fixed all three. The real captures are now test fixtures, with public addresses and MACs replaced. The other fixtures are synthetic, or assembled from lines I had already published.
The proto_state problem
The part that mattered more came from the TCP state names. I wanted the table to show ESTABLISHED instead of a number, so I needed the mapping, and two of my own posts disagreed about it.
My NSE4 CLI reference, Part 2 had a table with SYN_SENT at 01 and ESTABLISHED at 03. My live session on FortiOS 7.6.6 showed proto_state=01 on an established SSH session, so the post was wrong. A Fortinet knowledge base article gave a table that put ESTABLISHED at 1, which matched, and I wrote that into the parser.
That was still not the whole story. When I went back to the documentation to cite it properly, the Administration Guide’s “Using a session table” page describes the TCP value as two digits, because FortiGate tracks both directions of the session. The CLI reference lists the state numbers: 0 NONE, 1 ESTABLISHED, 2 SYN_SENT, 3 SYN_RECV, 4 FIN_WAIT, 5 TIME_WAIT, 6 CLOSE, 7 CLOSE_WAIT, 8 LAST_ACK and 9 LISTEN. The guide’s own forwarded TCP example shows 11. It does not say which digit is which direction.
So I tested it on the unit. Two captures from the 70G, both FortiOS 7.6.6:
- Twenty sessions across three VDOMs. All ten TCP sessions read
01: local, forwarded, SNAT’d and NP-offloaded alike. The guide’s11did not reproduce. UDP matched the guide: the four sessions with no reply read00, and the six with replies read01. - A half-closed session. From my jumpbox I opened a TCP connection to port 22 on a host in another segment and sent nothing. sshd gave up on the unauthenticated connection and closed it from the server side, while my client held its end open.
proto_statewent from01to07, andexpiredropped well below the 3600-second timeout, which is the shorter half-close timer taking over. About two minutes later the session was gone.
That is consistent with Fortinet’s definition of CLOSE_WAIT: the remote side started the close and the FortiGate is waiting for the local side. It also shows the second digit moving from 1 to 7. The first digit was 0 in every TCP session I looked at, including this one.
Here is what I can and cannot say. On this unit a TCP value behaves like 0 plus one state digit. What the first digit is for, I do not know, and I have not worked out which digit is which direction. Settling that would need a capture where the client closes first. The parser decodes each digit with the CLI reference names and joins them, so 01 reads NONE/ESTABLISHED and 07 reads NONE/CLOSE_WAIT. It does not label direction. For UDP and ICMP it shows the raw value.
What else was wrong in the post
Checking the proto_state table made me read the rest of that section of the NSE4 post against real output and the documentation, and it did not hold up:
- The “live output breakdown” used an invented format, with
orig src:andreply src:lines and fields such asips_bypassthat do not appear in real 7.6.6 output. The real format hashook=pre dir=org act=nooplines, astatisticline,vd=0:20and atotal session: Nfooter. - The filter command list used
policy-id. The real option ispolicy, and VDOMs are selected withvd(an index) orvd-name. - The sample output for running
diagnose sys session filterwith no arguments was invented. On my unit the real output is asession filter:block that lists every criterion, withanyfor anything unset, and a single port or address shown as a range (dest port: 22-22). Fortinet’s CLI troubleshooting cheat sheet describes that form as showing the session filters, if set, which matches. - The CLOSE_WAIT explanation described the wrong side of the close. Fortinet’s own definition is that the remote side started the close and the FortiGate is waiting for the local side to finish.
I rewrote those sections. The updated post marks the session block as illustrative, since the addresses are made up to fit the scenario, says which details I confirmed on a unit, and links the CLI Reference, the Administration Guide and the cheat sheet.
What is still unverified
The parser has been run against synthetic samples, lines from my own earlier posts, and three real 7.6.6 captures. That leaves gaps. I have not run it against ICMP, IPsec or IPv6 sessions from a real unit. I have not confirmed which proto_state digit is which direction, or what the first digit means. The capture also showed hook names pre, post, in and out, and I have not seen others. The repo’s NOTES.md lists these. If you run it against something unusual and it fails or mislabels a session, send me the block and I will add it as a fixture.
The general point
I could have written the whole post and the parser from memory and the documentation, and both would have looked fine. What exposed the problem was feeding real output into code that has to be right, because a parser cannot be vague about a field the way prose can. Even then, the first fix was only half right. The documentation caught part of it and the unit caught the rest, and a few things still need another capture. If you publish CLI output, capture it from a unit before you write the explanation, and then read the vendor’s guide for the field you think you understand.
Fortinet references: the FortiOS CLI Reference for diagnose sys, the Administration Guide page Using a session table, and the CLI troubleshooting cheat sheet.