Heim  >  Artikel  >  Backend-Entwicklung  >  Schreiben eines Telegramm- und E-Mail-Benachrichtigungsskripts für Zabbix

Schreiben eines Telegramm- und E-Mail-Benachrichtigungsskripts für Zabbix

WBOY
WBOYOriginal
2024-09-06 06:02:32907Durchsuche

Writing a Telegram and Email Notifications Script for Zabbix

Monitoring your whole infrastructure is very essential specially in IT infrastructures. I recently learn creating and writing scripts for monitoring like this. I will share you how I make telegram and email notifications script. This script sends an telegram and email notification whenever a problem in the infrastructure occurs.

What is Zabbix

Zabbix is an open source software that monitors numerous parameters of a network and the health and integrity of servers, virtual machines, applications, services, databases, websites, the cloud and more. Zabbix uses a flexible notification mechanism that allows users to configure email-based alerts for virtually any event. This allows a fast reaction to server problems. Zabbix offers excellent reporting and data visualization features based on the stored data. This makes Zabbix ideal for capacity planning.

Telegram Notification Script

Log in to your Zabbix proxy/server via SSH and go to "externalscripts" directory.

cd /usr/lib/zabbix/externalscripts

Create the script for Telegram bot. Set up your telegram bot here.

sudo vim telegram-notif.sh

Paste the script below

#!/bin/bash

# Telegram Bot API Token
BOT_TOKEN="YOUR_BOT_TOKEN"

# Telegram Chat ID
CHAT_ID="YOUR_TELEGRAM_CHAT_ID"

# Message to be sent
MESSAGE="$1"

# Sends the telegram message
curl -s -X POST "https://api.telegram.org/bot$BOT_TOKEN/sendMessage" -d "chat_id=$CHAT_ID&text=$MESSAGE"

Save your script.

:wq

Convert it to an executable file

chmod +x telegram-notif.sh

Email Notification Script

In the same directory, create a python file.

sudo vim email-notif.py

Paste this code. This code requires an app password from your gmail account. Create your app password here.

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import sys

# Get the alert message from command-line arguments
alert_message = sys.argv[1]

# Email account credentials
gmail_user = 'YOUR_EMAIL_ADDRESS'
gmail_password = 'YOUR_APP_PASSWORD'  # Use your App Password here

# Create the message
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = 'YOUR_RECIPIENT_EMAIL_ADDRESS'  # Recipient's email address
msg['Subject'] = 'Alert Message Subject'  # Subject of the email
message = alert_message
msg.attach(MIMEText(message, 'plain'))

# Attempt to send the email
try:
    server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
    server.ehlo()
    server.login(gmail_user, gmail_password)
    server.send_message(msg)
    server.close()

    print('Email sent!')
except Exception as e:
    print('Failed to send email:', e)

Don't forget to save and make this an executable file

:wq
chmod +x email-notif.py

This script can be integrated in Zabbix to notify issues that needs to be addressed as quickly as possible.

In some instances, running a python script in a server requires root permissions(sudo). To allow Zabbix run the python script with sudo, you must edit the visudo.

sudo visudo

Find the "User privilege specification" and paste this line of code under it.

zabbix  ALL=(ALL) NOPASSWD: /usr/lib/zabbix/externalscripts/your_script.py

So it will look like this

# User privilege specification
root    ALL=(ALL:ALL) ALL
zabbix  ALL=(ALL) NOPASSWD: /usr/bin/python3
zabbix  ALL=(ALL) NOPASSWD: /usr/lib/zabbix/externalscripts/your_script.py

Thanks for reading!

Das obige ist der detaillierte Inhalt vonSchreiben eines Telegramm- und E-Mail-Benachrichtigungsskripts für Zabbix. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:Django Redirects App-TutorialNächster Artikel:Django Redirects App-Tutorial