Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick and dirty gateway metrics for telegraf on pfSense
[[inputs.exec]]
commands = ["/usr/local/libexec/telegraf/gateways.py"]
timeout = "5s"
data_format = "influx"
#!/usr/local/bin/python2.7
import glob, os, socket
DPINGER_SOCK_PATH = "/var/run/"
os.chdir(DPINGER_SOCK_PATH)
for sock_name in glob.glob("dpinger*.sock"):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock_path = DPINGER_SOCK_PATH+sock_name
s = sock.connect(sock_path)
line = sock.recv(1024).split('\n', 1)[0]
values = line.split()
print "gateways,gateway_name=" + values[0] + " rtt=" + str(int(values[1])/100.0) + \
",rttsd=" + str(int(values[2])/100.0) + ",loss=" + str(int(values[3])) + "i"
sock.close()
#!/usr/local/bin/python3.7
import glob, os, socket
DPINGER_SOCK_PATH = "/var/run/"
os.chdir(DPINGER_SOCK_PATH)
for sock_name in glob.glob("dpinger*.sock"):
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
sock_path = DPINGER_SOCK_PATH+sock_name
s = sock.connect(sock_path)
line = sock.recv(1024).decode().split('\n', 1)[0]
values = line.split()
print("gateways,gateway_name="+values[0]+" rtt="+str(int(values[1])/100.0)+ \
",rttsd="+str(int(values[2])/100.0)+",loss="+str(int(values[3]))+"i")
sock.close()
@bigjohns97

This comment has been minimized.

Copy link

@bigjohns97 bigjohns97 commented Apr 7, 2020

Trying to use this on the new version of pfsense which is now python 3.7 updating just the python line at the top produces

File "telegraf_gateways.py", line 17 print "gateways,gateway_name=" + values[0] + " rtt=" + str(int(values[1])/100.0) + \ ^ SyntaxError: invalid syntax
Adding parentheses around the print part brings up this error.

Traceback (most recent call last): File "./telegraf_gateways.py", line 15, in <module> line = sock.recv(1024).split('\n', 1)[0] TypeError: a bytes-like object is required, not 'str'
some googling I found that this new byte-like option was introduced in 3.x so I am guessing this script will need to be updated?

@fastjack

This comment has been minimized.

Copy link
Owner Author

@fastjack fastjack commented Apr 11, 2020

I've created a python 3.7 version of this script for pfSense 2.4.5 and above

@maxxoverclocker

This comment has been minimized.

Copy link

@maxxoverclocker maxxoverclocker commented Apr 14, 2020

I've created a python 3.7 version of this script for pfSense 2.4.5 and above

Thank you so much! I was really struggling trying to convert this to Python3.

@bigjohns97

This comment has been minimized.

Copy link

@bigjohns97 bigjohns97 commented Apr 14, 2020

Works like a charm fastjack! thanks!

@fastjack

This comment has been minimized.

Copy link
Owner Author

@fastjack fastjack commented Jul 14, 2020

Reading https://forum.netgate.com/topic/152132/grafana-dashboard-using-telegraf-with-additional-plugins/19 I noticed that some people were getting ping times that were orders of magnitude too small. This problem never occurred to me. I had set the unit of my RTT/RTTsd graph to "Time/miliseconds (ms)" in Grafana.

@ladenraies

This comment has been minimized.

Copy link

@ladenraies ladenraies commented Oct 21, 2020

The script works correctly and the data goes back to grafana. The script stops executing after a while. I have to relaunch the gateway.py script and the data is going up for a while. I just checked my telegraf.conf file. It has not undergone any modification.

This file is automatically generated by pfSense

[agent]
interval = "10s"
round_interval = true

[[inputs.cpu]]
percpu = true
totalcpu = true
fielddrop = ["time_*"]

[[inputs.disk]]
ignore_fs = ["tmpfs", "devtmpfs"]

[[inputs.diskio]]

[[inputs.kernel]]

[[inputs.mem]]

[[inputs.net]]

[[inputs.processes]]

[[inputs.swap]]

[[inputs.system]]

[[inputs.pf]]
[[outputs.influxdb]]
urls = ["http://grafana:8086"]
database = "influx_db"
username = "xxxxx"
password = "xxxxx"

Additional Raw Options

@corgan2222

This comment has been minimized.

Copy link

@corgan2222 corgan2222 commented Feb 17, 2021

any idea how to get the gateway data without the dpinger service?
I had to deactivate the gateway lookup, because of some strange conection problems, as discribed here https://be-virtual.net/pfsense-arpresolve-cant-allocate-llinfo-for-x-x-x-x-on-emx/

@fastjack

This comment has been minimized.

Copy link
Owner Author

@fastjack fastjack commented Feb 19, 2021

@corgan2222 You probably could cobble together something with ping. I used dpinger because it already comes configured through pfSense's admin interface.

But if you're having problems pinging your already configured gateway you can choose a custom ping target (the user interfaces calls it a "Monitor IP"). Just go to System -> Routing -> Gateways and then edit your gateway from there.

@opswhisperer

This comment has been minimized.

Copy link

@opswhisperer opswhisperer commented Mar 30, 2021

Why the /100 - the default output is microseconds and that seems fine?

@fastjack

This comment has been minimized.

Copy link
Owner Author

@fastjack fastjack commented Mar 30, 2021

I believe I had my Grafana already set up for seconds instead of microseconds so I added it here.

@opswhisperer

This comment has been minimized.

Copy link

@opswhisperer opswhisperer commented Mar 30, 2021

Ah okay, that would be /1000 but either way thanks for saving me some time with this. Works well on opnsense too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment