Monitoring the network connections on your Windows servers can be crucial to examine server load and investigate bottlenecks and anomalies. There are many ways to monitor your network connections. This blog post will go into detail of some of the tools that can be used to achieve optimal monitoring of your Windows network connections.
How To monitor your Windows Network Connections?
In the Windows Performance Monitor, you can find several counters for all kinds network connections. This set of counters is available for TCPv4 and TCPv6 connections.
|Counter Name||Counter Description|
|Connection Failures||Connection Failures is the number of times TCP connections have made a direct transition to the CLOSED state from the SYN-SENT state or the SYN-RCVD state, plus the number of times TCP connections have made a direct transition to the LISTEN state from the SYN-RCVD state.|
|Connections Active||Connections Active is the number of times TCP connections have made a direct transition to the SYN-SENT state from the CLOSED state. In other words, it shows a number of connections which are initiated by the local computer. The value is a cumulative total.|
|Connections Established||Connections Established is the number of TCP connections for which the current state is either ESTABLISHED or CLOSE-WAIT.|
|Connections Passive||Connections Passive is the number of times TCP connections have made a direct transition to the SYN-RCVD state from the LISTEN state. In other words, it shows a number of connections to the local computer, which are initiated by remote computers. The value is a cumulative total.|
|Connections Reset||Connections Reset is the number of times TCP connections have made a direct transition to the CLOSED state from either the ESTABLISHED state or the CLOSE-WAIT state.|
|Segments Received/sec||Segments Received/sec is the rate at which segments are received, including those received in error. This count includes segments received on currently established connections.|
|Segments Retransmitted/sec||Segments Retransmitted/sec is the rate at which segments are retransmitted, that is, segments transmitted containing one or more previously transmitted bytes.|
|Segments Sent/sec||Segments Sent/sec is the rate at which segments are sent, including those on current connections, but excluding those containing only retransmitted bytes.|
|Segments/sec||Segments/sec is the rate at which TCP segments are sent or received using the TCP protocol.|
At the moment there seems to be no Performance Monitor counter available in Windows to show the UDP connection count. Although the Windows Performance Monitor is an easy choice to have a quick glance at how many TCP connections are currently active, it is not an optimal tool to use for debugging or alerting. The PerfMon user interface also hasn’t changed much over the years.
This means that we will have to look at other options, such as Netstat:
Netstat is a command-line tool that displays very detailed information about your network connections, both incoming and outgoing, routing tables, network interfaces and network protocol statistics.
It is mostly used for finding problems in the network and to determine the amount of traffic on the network as a performance measurement.
Although Netstat is the perfect tool for looking in real-time at your network connections, you will need some way to graph the Netstat values. Being able to analyze the connection count over time really helps with getting a better understanding of what your servers and applications are doing.
As I saw multiple plugins to check network connections with Netstat on Linux hosts, but not on Windows hosts, I decided to write a Powershell script which uses Netstat to monitor your TCP and UDP network connections on Windows hosts.
How to monitor your network connections with Nagios?
- Download the latest version of check_ms_win_network_connections on GitHub.
- Put the script in the NSClient++ scripts folder, preferably in a subfolder Powershell.
- In the nsclient.ini configuration file, define the script like this:
1check_ms_win_network_connections=cmd /c echo scripts/powershell/check_ms_win_network_connections.ps1 $ARG1$; exit $LastExitCode | powershell.exe -command -
- Make a command in Nagios like this:
1check_ms_win_network_connections => $USER1$/check_nrpe -H $HOSTADDRESS$ -p 5666 -t 60 -c check_ms_win_network_connections $ARG1$
- Configure your service in Nagios. Make use of the above created command. Configure something similar like this as $ARG1$:
1-a '-H localhost -wl 2 -cl 0 -wh 10 -ch 15'
The script initiates a ‘netstat -ano’ , which will display all active network connections with their respective ip addresses, port number and the corresponding process id’s, parse the results and apply the optional filters.
This could of course also be accomplished by just retrieving the ‘\TCPv4Connections Established’ performance countera and it’s UDP variant, but the real strength of the script are it’s parameters. If you think your systems have been compromised by a virus or other malicious software, you can distribute the check_ms_network_connections plugin to all Windows servers and then check your network connections for a given process, port or ip address. This could quickly result in an overview of all impacted systems.
Because the Powershell command get-process doesn’t add file extensions, the -P parameter also does not need it’s file extensions eg ‘.exe’. For example in order to look for all connections made by svchost.exe, the parameters would look like this: -H server.fqdn -P svchost
Another usage example could be the need to monitor a server that needs a continuous link with another server. By specifying, the -wl and -cl parameters like this
-H server.fqdn -wl 2 -cl 0 -wh 10 -ch 15 , you should get a warning alert when the amount of TCP connections drops below 2 and a critical alert when there is no TCP connection with the remote server.
Please note that when using different filter parameters, ‘or’ is used, not ‘and’. So if any of the filters apply’s, the connection should be added.
If you don’t want to filter on IP address or port, I suggest you use the ‘-c’ parameter, which improves performance a lot. If you are running the plugin on a server with a very high amount of connections, I also suggest using the -c parameter.
The ‘-c’ parameter will execute (netstat -abn -proto TCP).count which is way faster then having to loop through each individual connection. It does imply you will get less information, as it only counts the active TCP connections.
The result of using Nagios XI to monitor your network connections looks like this:
A third option is to use a TIG stack, which will use Telegraf to query the counters from PerfMon and sends them to an InfluxDB time series database. Visualization is done with Grafana.
The Telegraf agent configuration file needs this input:
ObjectName = "TCPv4"
Counters = ["Connection Failures","Connections Active","Connections Established","Connections Passive","Connections Reset"]
Instances = ["------"]
Measurement = "win_connections"
Grafana allows you to create a query which will show all values for all hosts with a certain tag. With the help of templates, it becomes very easy to create beautiful graphs with filterable, sortable min, max, avg and current values o all your network connections counters. And this with a one second granular interval.
A disadvantage of using Telegraf is that you are limited to using PerfMon counters. This means it’s not possible to get the UDP connection count. There seems to be a way to execute Powershell scripts with telegraf, but my guess is that the resulting load will be too high to execute this with a one second interval.
As you can seen there are multiple options to monitor your Windows network connections. I’ll try to extend this documentation with some alerting examples.