Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Ambos lados, revisión anterior Revisión previa
Próxima revisión
Revisión previa
integracion_de_telegram_en_alertas_de_zabbix [2016/05/12 16:29]
10.12.12.129
integracion_de_telegram_en_alertas_de_zabbix [2017/03/28 13:06] (actual)
10.12.12.129
Línea 27: Línea 27:
   ​   ​
 BotFather nos preguntará por el nombre y un subnombreque queremos darle al bot. BotFather nos preguntará por el nombre y un subnombreque queremos darle al bot.
-Éste tiene que acabar con la palabra bot. En nuestro caso hemos elegido ​Merkatu_bot+Éste tiene que acabar con la palabra bot. En nuestro caso hemos elegido ​zabbixmerkatu_bot
  
-  Merkatu_bot+ ​zabbixmerkatu_bot
   ​   ​
-Cuando registre el nuevo bot, nos devolverá el token que utilizaremos para añadirlo a nuestro script de python que haremos en el servidor más tarde. (ejemplo el token que nos han dado para Merkatu_bot):+Cuando registre el nuevo bot, nos devolverá el token que utilizaremos para añadirlo a nuestro script de python que haremos en el servidor más tarde. (ejemplo el token que nos han dado para zabbixmerkatu_bot):
  
-  Use this token to access the HTTP API: 230299965:AAHRfFy24Zj4yE2KEbJ_OHvGsDnQo8smQH4+  Use this token to access the HTTP API: 348816857:AAFnvD017g6PhfbIkHkaNbSi1bCjstXTEFo
   ​   ​
 Podemos salir de la conversación con GodFather, ya tenemos el token. Podemos salir de la conversación con GodFather, ya tenemos el token.
Línea 52: Línea 52:
   /cancel – Cancel the current operation.   /cancel – Cancel the current operation.
  
 +**IMPORTANTE:​ Hay que añadir al bot al grupo de chat y enviarle un mensaje**
 ===== Configurar Zabbix Server ===== ===== Configurar Zabbix Server =====
  
Línea 77: Línea 78:
 ===== Configuración Script ===== ===== Configuración Script =====
  
 +Revisamos la configuración de Zabbix para saber cual es el path de los alertscripts. Si es necesario, lo cambiamos por la ruta donde dejemos el script:
 +
 +  vim /​etc/​zabbix/​zabbix-server.conf
 +  ​
 +Supongamos que tenemos definida la ruta: /​var/​lib/​zabbix.
 +
 +En esta carpeta creamos el fichero por ejemplo que se llame telegram-bot.py
 +
 +  vim telegram-bot.py
 +  ​
 +Pegamos el siguiente contenido, cambiando nuestro token:
 +
 +  #​!/​usr/​bin/​python3.4 ​ #Le indicamos donde se encuentra el interprete de python
 +  import telebot,sys
 +  BOT_TOKEN='​348816857:​AAFnvD017g6PhfbIkHkaNbSi1bCjstXTEFo' ​ #cambiamos por el nuestro
 +  DESTINATION=sys.argv[1] ​ #Este argumento lo programamos en el front como {ALERT.SENDTO}
 +  SUBJECT=sys.argv[2] ​  #Este argumento lo programamos en el front como {ALERT.SUBJECT}
 +  MESSAGE=sys.argv[3] ​  #Este argumento lo programamos en el front como {ALERT.MESSAGE}
 +  MESSAGE = MESSAGE.replace('/​n','​\n'​)
 +  tb = telebot.TeleBot(BOT_TOKEN)
 +  tb.send_message(DESTINATION,​SUBJECT + '​\n'​ + MESSAGE)
 +  ​
 +Le damos permisos de ejecución y cambiamos de propietario para que lo ejecute el usuario zabbix
 +
 +  chmod +x telegram-bot.py
 +  chown zabbix:​zabbix telegram-bot.py
 +  ​
 +Listo
 +
 +===== Configuración en el Front =====
 +
 +Accedemos a la GUI del server: ​
 +
 +[[http://​zabbix.merkatu.info:​8888/​zabbix]]
 +
 +Vamos a la pestaña de Administración >> Media Types >> Create Media-type
 +
 +Name -> Le damos un nombre descriptivo
 +
 +Type -> Le decimos que sea un script
 +
 +Script name -> Le indicamos exactamente el nombre que hemos puesto al script (telegram-bot.py)
 +
 +Añadimos los siguientes parámetros:​
 +{ALERT.SENDTO}
 +{ALERT.SUBJECT}
 +{ALERT.MESSAGE}
 +
 +Marcamos la casilla de enable, damos en update y salimos.
 +
 +
 +Ahora nos vamos a Users >> Admin >> Media >> Add
 +
 +Type -> Desplegamos y seleccionamos nuestro script
 +
 +Sendto -> El id de nuestro grupo en telegram (-139912990)
 +
 +When enable -> Programacion de alertas
 +
 +Use it severity -> Que tipo de alertas queremos que lleguen al chat
 +
 +Marcamos casilla enable y le damos a update.
 +
 +
 +===== Para conocer los ids de los grupos y usuarios de telegram =====
 +
 +==== UserID ====
 +
 +
 +If you want to get a user id, send a message from this user to the bot. Reload the page and the user id will be shown.
 +
 +Example:
 +
 +  "​message":​{"​message_id":​59,"​from":​{"​id":​9083329,"​first_name":"​Gabriel","​last_name":"​R F","​username":"​GabrielRF"​},"​chat":​{"​id":​9083329,"​first_name":"​Gabriel","​last_name":"​R F","​username":"​GabrielRF","​type":"​private"​},"​date":​1446911853,"​text":"​\/​start"​}}]}
 +
 +In this case, the user id is 9083329. So, on the step Users, the field Send to would be 9083329.
 +
 +==== GroupID ====
 +
 +If you prefer to have your bot working on a group, then create the group and add the bot to it. Reload the page and you will see a message like:
 +
 +  "​message":​{"​message_id":​60,"​from":​{"​id":​9083329,"​first_name":"​Gabriel","​last_name":"​R F","​username":"​GabrielRF"​},"​chat":​{"​id":​-57169325,"​title":"​q31231","​type":"​group"​},"​date":​1446912067,"​group_chat_created":​true}},​{"​update_id":​727527785,​
 +In this case, the group id is -57169325. So, on the step Users, the field Send to would be -57169325.
 +
 +Note that a group id is always negative. Only user's id are positive.