<p>When managing a cluster, I might want to know how many tcp connections to one port on each node. This can help me to alert if some node's tcp connections number to some port are too many.</p>
<p>I can use <code>netstat -nat | grep -i &quot;22&quot; | wc -l</code> on a linux node to see how many connections to port 22. But I'm looking for a method that can do this and export the metric on prometheus.</p>
<p><strong>node_exporter</strong> has a metric <strong>node_netstat_Tcp_CurrEstab</strong> that can show tcp connections total number. But it has only a label named <strong>instance</strong> to show the total number on that node. But I want to check by a label like <strong>port</strong>.</p>
<p>In a word, I want to track tcp connections on node 192.168.1.1, port 22.</p>
<p>Anybody knows is there a metric in <strong>node_exporter</strong> or is there a exporter can do this?</p>

54vault
<p>I don't think it is possible through default metrics: netstat collector shows a summary with no detailing. From <a href="https://github.com/prometheus/node_exporter#enabled-by-default" rel="nofollow noreferrer">official docs</a>:</p>
<blockquote>
<p>Exposes network statistics from <code>/proc/net/netstat</code>. This is the same information as <code>netstat -s</code>.</p>
</blockquote>
<p>But you can always utilize a <a href="https://github.com/prometheus/node_exporter#textfile-collector" rel="nofollow noreferrer">textfile collector</a>, and add a script to your cron that will update number of open metrics.</p>
<p>I believe you'll need a command like this:</p>
<pre class="lang-bash prettyprint-override"><code>echo 'custom_established_connections{port=&quot;22&quot;}' $(netstat -nat|grep -P ':22\b.*ESTABLISHED$'|wc -l) &gt; /path/to/directory/role.prom.$$
</code></pre>
