
Sending Centreon Alerts to Google Home
If we are a bit geeky and have an intelligent assistant at hand called Google Home, you have a monitoring system based on Centreon or Nagios and you want to listen to the alerts there, This is your post!
GOOD, to achieve this we will need to do a few steps on our Centreon monitoring server, we will achieve it thanks to a Python script 3, that allows you to send a text to a Google Home as an argument.
We will take into account having at least Python 3.5:
Sudo yum install RH-Python35 SCL enable RH-Python35 Bash
And we enable it permanently, example of my '/etc/profile.d/enablepython35.sh':
#!/bin/bash
source scl_source enable rh-python35
After that, we will need to restart the Centreon services so that it loads the centreon-engine session again and runs Python3.
Something that we surely don't like, but I have it written down as a requirement, is that the user centreon-centengine must have sudo privileges, We executed 'Visudo’ and add the following line in the corresponding part:
centreon-engine ALL=(ALL) NOPASSWD:ALL
We download this script, which in my case I called 'mensaje_google_home.sh':
#! /usr/bin/python3.5
#
# spoke something on GoogleHome
#
# Use: ./ghome_say [ghome_ip] [text_to_say]
#
#
import sys import pychromecast import os import os.path from gtts import gTTS import time import hashlib ip=sys.argv[1];
say=sys.argv[2];
#********* Retrieve local IP of my RPI3 import socket s = socket.socket(socket.AF_INET, Socket. SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
local_ip=s.getsockname()[0]
s.close()
#**********************
fname=hashlib.md5(say.encode()).hexdigest()+".MP3"; #Create MD5 filename for caching castdevice = pychromecast. Chromecast(IP)
castdevice.wait()
vol_prec=castdevice.status.volume_level castdevice.set_volume(0.0) #set volume 0 for not hear the BEEEP try:
os.mkdir("/var/www/html/mp3_cache/")
except:
pass if not os.path.isfile("/var/www/html/mp3_cache/"+fname):
tts = gTTS(say,lang='es')
tts.save("/var/www/html/mp3_cache/"+fname)
mc = castdevice.media_controller mc.play_media("HTTP://"+local_ip+"/mp3_cache/"+fname, "audio/mp3")
mc.block_until_active()
mc.pause() #prepare audio and pause...
time.sleep(1)
castdevice.set_volume(vol_prec) #setting volume to precedent value time.sleep(0.2)
mc.play() #play the mp3 while not mc.status.player_is_idle:
time.sleep(0.5)
mc.stop()
castdevice.quit_app()
Lo hacemos ejecutable y lo probamos:
chmod +x mensaje_google_home.sh python3 mensaje_google_home.sh DIRECCION_IP_GOOGLE_HOME "Testing, Testing"
Veremos cómo nuestro altavoz inteligente de Google Home nos dirá el texto que pusimos en el ejemplo. GOOD, ahora llevaremos esto a Centreon, This is, we will create a Notification Command for Host alerts and another for Service alerts using the above script.
To do this,, since “Configuration” > “Commands” > “Notifications”, we will create a Command for Google Home to alert our hosts of what happens with the following command line:
python3 /usr/lib/centreon/plugins/mensaje_google_home.sh DIRECCION_IP_GOOGLE_HOME "The host $HOSTNAME$ ($HOSTADDRESS$) is in $HOSTSTATE$ status"
And we do another Command, but this time to alert the Services, and the following line would be enough for us:
python3 /usr/lib/centreon/plugins/mensaje_google_home.sh DIRECCION_IP_GOOGLE_HOME "At $HOSTNAME$ the $SERVICEDESC$ Service is in $SERVICESTATE$ status"
What would be left now would be to associate these newly created notification commands with a user that we have, for example already defined, or to whom we enable notifications. We will export the Centreon configuration and from this moment on we will receive the alerts of our monitoring with our speakers or smart assistants of Google Home or Google Home Mini among others.
As usual, I hope it has been of interest to you and thank you as always for moving it through social networks.