# How do I monitor a tcp port?

**URL:** <https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550>\
**Category:** Icinga 2\
**Created:** [February 6, 2025, 2:45pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550 "2025-02-06T14:45:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![itineed2022](https://community.icinga.com/letter_avatar_proxy/v4/letter/i/47e85d/32.png) [@itineed2022](https://community.icinga.com/u/itineed2022)\
**Post date:** [February 6, 2025, 2:45pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/1 "2025-02-06T14:45:31Z")

</div>

- Version used (`icinga2 --version`)  
Compiler: GNU 11.2.1  
Build host: runner-hh8q3bz2-project-575-concurrent-0  
OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017
- Operating System and version  
Red Hat Linux 7.9
- Enabled features (`icinga2 feature list`)  
Disabled features: compatlog debuglog elasticsearch gelf icingadb influxdb influxdb2 journald livestatus opentsdb perfdata syslog  
Enabled features: api checker command graphite ido-mysql mainlog notification opsgenie

I’m trying to just monitor whether or not a TCP port is responding. Here’s the config I’m trying to use:

```auto
object Service "webproxy_ports" {
    check_command = "tcp" 
    host = "webproxyprodapp01.mycompany.com"
    check_interval = 1m
    vars.tcp_ports = [3128]
    display_name = "Webproxy ports 3128"

    apply Service {
        for (port in vars.tcp_ports) {
            check_command = "tcp" 
            -H "x.x.x.x" 
            -p "$port"
        }
    }
}

```

But it keeps throwing errors that either the port or the host (-p and -H) are wrong. I’ve been through the docs for days and I can’t figure out where I’ve gone wrong.

---

<div class="post-metadata">

**Author:** ![lorenz](https://community.icinga.com/user_avatar/community.icinga.com/lorenz/32/7666_2.png) [@lorenz](https://community.icinga.com/u/lorenz)\
**Post date:** [February 6, 2025, 3:31pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/2 "2025-02-06T15:31:22Z")

</div>

you probably want something like this:

```auto
object Host "host with webproxy" {
  ...
  address = "x.x.x.x"
  vars.tcp_ports = [3128]
}

apply Service "TCP Webproxy " for (port in host.vars.tcp_ports) {
  ...
  check_command = "tcp"
   display_name = "Webproxy ports 3128"
   vars.tcp_port = port
}

```

---

<div class="post-metadata">

**Author:** ![itineed2022](https://community.icinga.com/letter_avatar_proxy/v4/letter/i/47e85d/32.png) [@itineed2022](https://community.icinga.com/u/itineed2022)\
**Post date:** [February 6, 2025, 3:37pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/3 "2025-02-06T15:37:00Z")

</div>

Apparently, “check\_command” cannot be empty on my hosts? I got this error when trying to restart icinga2:

`Error: Validation failed for object 'webprxprdapp01' of type 'Host'; Attribute 'check_command': Attribute must not be empty.`

---

<div class="post-metadata">

**Author:** ![lorenz](https://community.icinga.com/user_avatar/community.icinga.com/lorenz/32/7666_2.png) [@lorenz](https://community.icinga.com/u/lorenz)\
**Post date:** [February 6, 2025, 3:42pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/4 "2025-02-06T15:42:38Z")

</div>

Indeed, I kinda implied that.  
Some basic Icinga2 stuff, you have two kind of “Monitoring Objects”, `Host`s and `Services`. The main difference is, that a `Service` is _always_ attached to a `Host`.  
Therefore you have to have at least one `Host`. Most of the time you’ve got more since you will map existing Machines (Hardware or VM) to `Host` objects in Icinga2.

`Service`s then can be attached to one `Host`. In my example, through some config magic `address` (a `Host` attribute) is used as an address for the `tcp` `CheckCommand`, that’s why I did not configure it in the `Service` apply rule.

---

<div class="post-metadata">

**Author:** ![itineed2022](https://community.icinga.com/letter_avatar_proxy/v4/letter/i/47e85d/32.png) [@itineed2022](https://community.icinga.com/u/itineed2022)\
**Post date:** [February 6, 2025, 3:49pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/5 "2025-02-06T15:49:41Z")

</div>

I think I got it:

```auto
object Host "webprxprdapp01" {
    address = "x.x.x.x"
    vars.tcp_ports = [3128]
    check_command = "hostalive"
}

apply Service "TCP Webproxy" for (port in host.vars.tcp_ports) {
    check_command = "tcp"
    display_name = "Webproxy tcp ports 3128"
    vars.tcp_port = port
}

```

---

<div class="post-metadata">

**Author:** ![lorenz](https://community.icinga.com/user_avatar/community.icinga.com/lorenz/32/7666_2.png) [@lorenz](https://community.icinga.com/u/lorenz)\
**Post date:** [February 6, 2025, 4:05pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/6 "2025-02-06T16:05:17Z")

</div>

Now you should a `Host` " webprxprdapp01" and a `Service` “TCP Webproxy3128” in your webinterface 🙂

---

<div class="post-metadata">

**Author:** ![itineed2022](https://community.icinga.com/letter_avatar_proxy/v4/letter/i/47e85d/32.png) [@itineed2022](https://community.icinga.com/u/itineed2022)\
**Post date:** [February 6, 2025, 4:22pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/7 "2025-02-06T16:22:20Z")

</div>

Thanks [lorenz](https://community.icinga.com/u/lorenz)!

---

<div class="post-metadata">

**Author:** ![jeanm](https://community.icinga.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jeanm](https://community.icinga.com/u/jeanm)\
**Post date:** [February 6, 2025, 4:28pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/8 "2025-02-06T16:28:34Z")

</div>

Hello,  
The use of a static display name “Webproxy tcp ports 3128” leads me to think you will only ever check one port on that host, i.e., port 3128.  
The complexity of the solution above (the for construct “for (port in host.vars.tcp\_ports)”) is made to handle a list of ports.  
Perhaps you should consider simplifying the configuration if you need to check only one port.  
My two cents,  
Jean

---

<div class="post-metadata">

**Author:** ![itineed2022](https://community.icinga.com/letter_avatar_proxy/v4/letter/i/47e85d/32.png) [@itineed2022](https://community.icinga.com/u/itineed2022)\
**Post date:** [February 6, 2025, 4:29pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/9 "2025-02-06T16:29:55Z")

</div>

> [@itineed2022](#):
>
> ```auto
> object Host "webprxprdapp01" {
> address = "x.x.x.x"
> vars.tcp_ports = [3128]
> check_command = "hostalive"
> }
> 
> apply Service "TCP Webproxy" {
> check_command = "tcp"
> display_name = "Webproxy tcp ports 3128"
> vars.tcp_port = 3128
> assign where host.name == "webprxprdapp01"
> }
> 
> ```

Simplified like this?

---

<div class="post-metadata">

**Author:** ![jeanm](https://community.icinga.com/letter_avatar_proxy/v4/letter/j/6a8cbe/32.png) [@jeanm](https://community.icinga.com/u/jeanm)\
**Post date:** [February 6, 2025, 4:53pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/10 "2025-02-06T16:53:07Z")

</div>

I haven’t verified but this should work:

```auto
object Host "webprxprdapp01" {
    address = "x.x.x.x"
    check_command = "hostalive"
}

apply Service "TCP Webproxy" {
    check_command = "tcp"
    display_name = "Webproxy tcp port 3128"
    vars.tcp_port = 3128
    assign where host.name == "webprxprdapp01"
}

```

It can of course be further simplified if you don’t need to “apply” the service to multiple hosts, and wish to define it on just that single host…

---

<div class="post-metadata">

**Author:** ![linuxfabrik](https://community.icinga.com/user_avatar/community.icinga.com/linuxfabrik/32/2683_2.png) [@linuxfabrik](https://community.icinga.com/u/linuxfabrik)\
**Post date:** [February 27, 2025, 2:27pm UTC](https://community.icinga.com/t/how-do-i-monitor-a-tcp-port/14550/11 "2025-02-27T14:27:37Z")

</div>

BTW, there is also a port check in our Linuxfabrik Monitoring Plugins collection: [monitoring-plugins/check-plugins/network-port-tcp at main · Linuxfabrik/monitoring-plugins · GitHub](https://github.com/Linuxfabrik/monitoring-plugins/tree/main/check-plugins/network-port-tcp)
