MONITORING ESPHOME NODES WITH NAGIOS (USING MQTT/NODE-RED AS THE MIDDLEMAN)

#!/bin/bash
while true
do
/usr/bin/sudo python3 /home/demouser/nagios/node-check/check-nodes-mqtt/check-nodes-mqtt.py
done
#!/usr/bin/python
import sys
import time
import paho.mqtt.client as mqtt
broker_url = "<IP_Address_of_MQTT_broker>"
broker_port = <MQTT_Broker_port>
def on_connect(client, userdata, flags, rc):
print("Connected With Result Code: {}".format(rc))
def on_message(client, userdata, message):
print("Message Recieved: "+message.payload.decode())
file_name=message.payload.decode()
file_path="/home/demouser/nagios/node-check/logs/"+file_name+".ok"
file1 = open(file_path, 'w')
file1.write(message.payload.decode()+" is up and running\n")
file1.close()
def on_disconnect(client, userdata, rc):
print("Client Got Disconnected")
client = mqtt.Client("Nagios_NodeChecker")
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.username_pw_set(username="<mqtt_username>",password="<mqtt_password>")
client.connect(broker_url, broker_port)
client.subscribe(topic="nagios/node_check", qos=2)
client.message_callback_add("nagios/node_check", on_message)
client.loop_start()
time.sleep(300)
client.loop_stop()
#!/bin/bash
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4
usage1="Usage: $0 -L <log_location> -I <ip_address> -w <warn_if_no_ping_in_last_mins> -c <critical_if_no_ping_in_last_mins>"
exitstatus=$STATE_WARNING #default
while test -n "$1"; do
case "$1" in
-c)
crit=$2
shift
;;
-w)
warn=$2
shift
;;
-I)
ipaddr=$2
shift
;;
-L)
logloc=$2
shift
;;
-h)
echo $usage1;
echo
exit $STATE_UNKNOWN
;;
*)
echo "Unknown argument: $1"
echo $usage1;
echo
exit $STATE_UNKNOWN
;;
esac
shift
done
if [ $(find $logloc/$ipaddr.ok -mmin -$warn | wc -l) -gt 0 ]; then
echo "$ipaddr - OK (signaled within $warn mins)"
exit $STATE_OK
fi
if [ $(find $logloc/$ipaddr.ok -mmin -$crit | wc -l) -gt 0 ]; then
echo "$ipaddr - WARNING (signaled within $crit mins)"
exit $STATE_WARNING
fi
echo "$ipaddr - CRITICAL (no signal within $crit mins)"
exit $STATE_CRITICAL
define service {
use 1min-service
check_interval 60
max_check_attempts 2
host_name <host_which_the_script_runs_on>
service_description NODE CHECK
check_command check_node!-L /home/demouser/nagios/node-check/logs -I 192.168.1.63 -w 90 -c 180
contacts adminemail, admintext
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store