How to Set Up Zabbix Trigger Actions for Email Alerts and Remote Commands
This guide walks through configuring Zabbix trigger actions to send email notifications, define users and media types, create a Python alert script, enable remote command execution, and automate SSH service recovery when a trigger fires.
Configure Email Alert Action
First create a trigger action that sends an email when a trigger condition is met.
Define the action conditions and the operations to be performed. Steps 1‑5 send a message every 30 minutes using the
zabbix_send.pyscript. If the problem is not acknowledged within two hours, a second set of operations notifies the admin group every 15 minutes, twice.
Define recovery and update operations to notify the admin when the issue is resolved or when its status changes.
Configure Users and Media Types
Set up a Zabbix user with an email address and assign a media type that takes three parameters: recipient, subject, and message body.
Edit
/etc/zabbix/zabbix_server.confto add the script path:
AlertScriptsPath=/usr/lib/zabbix/alertscriptsRestart the Zabbix server and place the
zabbix_send.pyscript in the alertscripts directory with execution permission.
chmod +x zabbix_send.pyCreate a
graphdirectory for chart images and grant write permissions.
mkdir -p /usr/lib/zabbix/alertscripts/graph && chmod 777 -R /usr/lib/zabbix/alertscripts/graphEmail Script (Python)
<code>#!/usr/bin/python
#coding=utf-8
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
import smtplib, sys, os, time, re, requests
user='Admin' # Zabbix user
password='zabbix'
graph_path='/usr/lib/zabbix/alertscripts/graph'
graph_url='http://192.168.179.132/chart.php'
loginurl='http://192.168.179.132/index.php'
host='192.168.179.132'
to_email=sys.argv[1]
subject=sys.argv[2].decode('utf-8')
smtp_host='smtp.163.com'
from_email='[email protected]'
mail_pass='asd1234'
# Functions get_itemid, get_graph, text_to_html, send_mail, run omitted for brevity
</code>Configure Remote Command Action
Enable remote commands on the Zabbix agent:
EnableRemoteCommands=1Allow the Zabbix user to run commands without a password by editing
/etc/sudoers:
<code>Defaults !requiretty
zabbix ALL=(ALL) NOPASSWD: ALL</code>Test the setup with:
<code>zabbix_get -s 192.168.179.132 -k "system.run[sudo df -h]"</code>Create a Restart‑SSH Script
<code>#/bin/bash
systemctl restart sshd</code>Make the script executable:
chmod +x /restart_ssh.shCreate Monitoring Item, Trigger, and Action
Define an item that checks the SSH port, create a trigger that fires when the port is down, and add an action that runs the restart script after a two‑minute delay.
Trigger the alarm by stopping the SSH service:
systemctl stop sshdAfter two minutes Zabbix executes the script, and the service is restarted, which can be verified with:
<code>zabbix_get -s 192.168.179.132 -k net.tcp.port[192.168.179.132,22]</code>The guide encourages readers to share feedback and collaborate.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.