# rpm -Uv http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-agent-3.2.6-1.el7.x86_64.rpm 添加zabbix yum 源
# yum -y install zabbix-agent 安装zabbix-agent
修改配置文件
# cat /etc/zabbix/zabbix_agent.conf | grep -v ^# | grep -v ^$
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=指向zabbix服务器
ListenPort=10050
ListenIP=0.0.0.0
ServerActive=指向zabbix服务器
Hostname=mysql2 #zabbix服务器上配置主机的时候要用的名称
Include=/etc/zabbix/zabbix_agentd.d/*.conf #这一行是指向模板文件的
##添加用来关联mysql的配置文件.my.cnf,
执行mysql mysqladmin脚本命令时不需要帐户密码,也不再提示警告
mysql.sock文件通过find / -name mysql.sock找出路径
$ cat /etc/zabbix/.my.cnf
[mysql]
host = localhost
user = admin
password = zabbix
socket = /var/lib/mysql/mysql.sock
[mysqladmin]
host = localhost
user = admin
password = zabbix
socket = /var/lib/mysql/mysql.sock
##修改zabbix模板配置文件中的mysql路径
这个文件用于制定Zabbix Agent如何获取MySQL数据
我们需要将所有的/var/lib/zabbix修改为当前.my.cnf文件的路径/etc/zabbix。
$ sed -i 's@/var/lib/zabbix@/etc/zabbix@g' /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
$ cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf |grep -v ^#|grep -v ^$
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/etc/zabbix mysql -N | awk '{print $$2}'
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/etc/zabbix mysql -N'
UserParameter=mysql.ping,HOME=/etc/zabbix mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V
##设置完 成之后保存并重启Zabbix Agent
$ systemctl restart zabbix-agent.service
##测试不用帐户密码就可以执行mysqladmin命令
$ mysqladmin ping | grep -c alive
1 << 返回1 就说明ok了
Zabbix qq 邮件报警警告!
Vim /usr/bin/zabbix/alertscripts/sendmail.py
#!/usr/bin/python
import sys
import smtplib
from email.mime.text import MIMEText
_user = "[email protected]"
_pwd = "hupqnxuftigfbeei"
_to = sys.argv[1]
msg = MIMEText(sys.argv[3], 'plain', 'utf-8')
msg["Subject"] = sys.argv[2]
msg["From"] = _user
msg["To"] = _to
try:
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(_user, _pwd)
s.sendmail(_user, _to, msg.as_string())
s.quit()
print "Success!"
except smtplib.SMTPException,e:
print "Falied,%s"%e
cat /etc/zabbix/zabbix_server.conf | grep AlertScriptsPath
把脚本存放在/usr/bin/zabbix/alertscripts 目录下,记得给上X权限
chmod +x /usr/lib/zabbix/alertscripts/mailx.py
chown -R zabbix.zabbix /usr/lib/zabbix/
/usr/local/zabbix/alertscripts/sendmail.sh test@qq.com "测试邮件标题" "测试邮件内容"
测试脚本能不能正常首到邮件!