Skip to content

Instantly share code, notes, and snippets.

@dennisheitmann
Last active February 14, 2022 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennisheitmann/d6345644771ebdd1cc049ebb6a87f7ab to your computer and use it in GitHub Desktop.
Save dennisheitmann/d6345644771ebdd1cc049ebb6a87f7ab to your computer and use it in GitHub Desktop.
PING statistics to MQTT
# MIT License
#
# Copyright (c) 2022 Dennis Heitmann
from icmplib import ping
import paho.mqtt.client
pingHosts = ['192.168.15.194', '192.168.57.111']
mqttServer = '__SERVER__'
mqttPort = 1883
username = '__USER__'
password = '__PASSWORD__'
topic_tree = 'IP-PING/'
def on_connect(client, userdata, flags, rc):
print("Connected with result code: " + str(rc))
def on_message(client, userdata, msg):
print("Message received: " + str(msg))
def on_disconnect(client, userdata, rc):
print("client disconnected ok")
for pingHost in pingHosts:
host = ping(pingHost, count=25, interval=0.5, timeout=1, privileged=False)
client = paho.mqtt.client.Client()
client.username_pw_set(username, password)
client.on_connect = on_connect
client.on_message = on_message
if (host.is_alive):
#print(host.address)
#print(host.avg_rtt)
#print(host.packet_loss)
#print(host.jitter)
try:
#client.on_connect = on_connect
client.connect(mqttServer, mqttPort, 60)
except Exception():
raise
try:
ret = client.publish(topic_tree+host.address+"/avg_rtt", host.avg_rtt)
except Exception():
print(ret)
raise
try:
ret = client.publish(topic_tree+host.address+"/packet_loss", host.packet_loss)
except Exception():
print(ret)
raise
try:
ret = client.publish(topic_tree+host.address+"/jitter", host.jitter)
except Exception():
print(ret)
raise
try:
#client.on_disconnect = on_disconnect
client.disconnect()
except Exception():
raise
else:
print(host.address, end="")
print(" is not alive")
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment