netstat -lntp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 10.81.12.34:7003 0.0.0.0:* LISTEN 54261/skynet
tcp 0 0 10.81.12.34:6013 0.0.0.0:* LISTEN 54261/skynet
tcp 0 0 127.0.0.1:6014 0.0.0.0:* LISTEN 54261/skynet
tcp 0 0 10.81.12.34:6015 0.0.0.0:* LISTEN 54261/skynet
tcp 0 0 10.81.12.34:6016 0.0.0.0:* LISTEN 54261/skynet
tcp 0 0 0.0.0.0:8512 0.0.0.0:* LISTEN 14194/omsa_py
tcp 0 0 0.0.0.0:8513 0.0.0.0:* LISTEN 14194/omsa_py
tcp6 0 0 :::9273 :::* LISTEN 42054/telegraf
I want to monitor whether the ports 6014,7003,8513,9273 are open normally, but I don’t know the monitoring address of the ports. I can use netstat - lntp | grep xxx to get whether this port is open normally. How do I configure it using net_desponse
Can you try to leave the host blank and see what it does? It is supposed to default to localhost but I’m curious what will happen. I.e. address = “:7003” Outside of that I think your best option would be to have a script running that sets an env with the local address and call it as a variable in your config. i.e. address = “$hostip:7003”
1 Like
@tangguangliang, Welcome to the Influxdata community!
To integrate port monitoring with Telegraf, you can use the exec
input plugin, which allows you to run custom scripts and collect their output as metrics. Here’s how to set it up:
- First, create a script that checks your ports and outputs results in a format Telegraf can parse. Save this as
/usr/local/bin/check_ports.sh
:
#!/bin/bash
HOST="10.81.12.34"
TIMEOUT=3
# Check port 6014
nc -z -w $TIMEOUT $HOST 6014
PORT_6014=$?
# Check port 7003
nc -z -w $TIMEOUT $HOST 7003
PORT_7003=$?
# Check port 8513
nc -z -w $TIMEOUT $HOST 8513
PORT_8513=$?
# Check port 9273
nc -z -w $TIMEOUT $HOST 9273
PORT_9273=$?
# Output in influx line protocol format
echo "port_status,host=$HOST,port=6014 status=$PORT_6014"
echo "port_status,host=$HOST,port=7003 status=$PORT_7003"
echo "port_status,host=$HOST,port=8513 status=$PORT_8513"
echo "port_status,host=$HOST,port=9273 status=$PORT_9273"
- Make the script executable:
chmod +x /usr/local/bin/check_ports.sh
- Configure Telegraf to use this script. Edit your Telegraf configuration file (usually at
/etc/telegraf/telegraf.conf
) and add this section:
[[inputs.exec]]
commands = ["/usr/local/bin/check_ports.sh"]
timeout = "5s"
data_format = "influx"
interval = "60s" # Run every minute
- Restart Telegraf to apply the changes:
sudo systemctl restart telegraf
With this setup, Telegraf will run your script every minute and collect the port status as metrics. The status value will be 0 if the port is open or non-zero if it’s closed. These metrics will be sent to whatever output you’ve configured in Telegraf (InfluxDB, Prometheus, etc.).