Notificaciones en Telegram: Servidor caído!
Hace tiempo que uso este script en python para enviarme notificaciones en Telegram cuando mi servidor web está caído. Cabrea… pero está bien tener un log instantáneo.
Hice unas pequeñas modificaciones, lo subí a github y ahora lo comparto con vosotros:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
#! /usr/bin/python2.7 # -*- coding:utf-8 -*- # hackaffeine.com - Santi import subprocess import time import urllib2 import argparse import re import os import fileinput ########## #Time among checks CHECK_TIME = 60 #Websites to check WEB = 'http://your_website' #Telegram username to send notifications USER = 'User' ########## ##### REPOSITORY= "https://github.com/vysheng/tg.git" ##### parser = argparse.ArgumentParser(description = "Script to check websites status and send notifications with Telegram", epilog="Please report any bugs or suggestions to santisjb@gmail.com") parser.add_argument("-n", "--noinstall", action="store_true", help="Use it to start without Telegram installation") args = parser.parse_args() def installTelegram(path,repository): subprocess.call("git clone --recursive " + repository + " " + path, shell = True) subprocess.call("cd /"+path+" && bash "+path+"/configure ;make", shell = True) subprocess.call("ln -s " + path + "/bin/telegram-cli /usr/bin/telegram-cli", shell = True) subprocess.call("mkdir /etc/telegram-cli", shell = True) subprocess.call("cp "+path+"/tg-server.pub /etc/telegram-cli/server.pub", shell = True) def installDependencies(): subprocess.call('apt-get update', shell = True) subprocess.call('apt-get -y install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev make git', shell = True) def main(): while True: try: f = urllib2.urlopen(WEB) code = f.getcode() except: code = "error" if code != 200: MSG = time.strftime("%H:%M:%S - %d/%m/%Y - ")+str(code)+" in "+WEB+" \n" subprocess.call('telegram-cli -WR -U root -e "msg ' + USER + ' ' + MSG + ' " ',shell=True) time.sleep(CHECK_TIME) if __name__ == '__main__': if(args.noinstall == False): telegram = raw_input("Do you want to install Telegram?(yes/no): ") if (telegram == "yes"): path = raw_input("Where do you want to install Telegram?(path): ") path += "/tg" Dependencies = raw_input("some dependencies needed, install?(yes/no): ") if (Dependencies == "yes"): installDependencies() installTelegram(path, REPOSITORY) main() elif (telegram == "no"): main() else: print ("something wrong!") else: main() |
Necesitamos ejecutarlo como root para instalar dependencias.
Simplemente tenemos que editar los siguientes datos:
1 2 3 4 5 6 7 8 |
########## #Time among checks CHECK_TIME = 60 #Websites to check WEB = 'http//your_website' #Telegram username to send notifications USER = 'User' ########## |
Cuidado con el check_time, no nos bloqueé nuestro proveedor.
y para ejecutarlo:
1 |
python checkurl.py |
Si queremos ejecutarlo sin la instalación de Telegram podemos usar el argumento -n :
1 |
python checkurl.py -n |
Por último para dejar las notificaciones en Telegram activadas necesitamos añadir el script en el rc.local:
1 |
/path/checkurl.py -n > /dev/null |
Como es de esperar, necesitamos hacer todo esto en un dispositivo que esté las 24 horas del día encendido (PC, Raspberry Pi, etc) si queremos tener un servicio completamente funcional.
Existen algunos servicios web para notificar mediante correo electrónico estos problemas; esta simplemente es otra forma más!
Cualquier fallo o sugerencia comentad!