AFAIK, port exhaustion happens when a host is trying to establish a lot of connections to another server and the sessions used up all the available ephemeral ports. e.g.
192.168.1.1:port -> 10.20.30.40:443
This does not seem common but it is an issue for load-balancer. My question is, is there a way to get an early warning that it is getting close?
I know I can use netstat to get all the connections but if there are tens of thousands of connection. It would take quite a bit of resource to run the command, sort the target and count the number of connections sharing the same client IP, server IP and server port. And do it on a regular basis.
Is there an easier way? As the TCP stack is allocating port, could it some how know that it is having trouble hashing an unused port and generate an error or log?