▲单独架构的小伙伴看这里:(学习杰哥视频的作业第21-22天)
安装配置zabbix,并实现zabbix监控tomcat,nginx,memcached,redis等,并实现发生报警后发送邮件报警
一、安装配置zabbix
1 安装依赖包:
# yum install gcc libxml2-devel net-snmp net-snmp-devel curl curl-devel php php-bcmath php-mbstring mariadb mariadb-devel java-devel libevent-devel
2 准备数据库:
# mysql
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql>grant all privileges on zabbix.* to zabbix@"192.168.7.%" identified by '123456';
Query OK, 0 rows affected (0.00 sec)
3 编译安装:
# cd /usr/local/src/
# tar -xvf zabbix-4.0.24.tar.gz
# cd zabbix-4.0.24
# useradd zabbix -s /sbin/nologin
# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --enable-java
# make && make install
4 导入数据库:
# mysql -uzabbix -p123456 -h192.168.15.203 zabbix < database/mysql/schema.sql
# mysql -uzabbix -p123456 -h192.168.15.203 zabbix < database/mysql/images.sql
# mysql -uzabbix -p123456 -h192.168.15.203 zabbix < database/mysql/data.sql
5 复制启动脚本:
# cp /usr/local/src/zabbix-4.0.24/misc/init.d/fedora/core/zabbix_server /etc/init.d/
# cp /usr/local/src/zabbix-4.0.24/misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
6 更改启动脚本:
# vim /etc/init.d/zabbix_server
# vim /etc/init.d/zabbix_agent
21 # Zabbix-Directory
22 BASEDIR=/usr/local/zabbix
7 编辑zabbix_server.con配置文件:
# mkdir -pv /var/log/zabbix
# chown zabbix.zabbix /var/log/zabbix -R
# vim /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log
DBHost=192.168.7.122
DBName=zabbix
DBUser=zabbix
DBPassword=123456
DBPort=3306
Timeout=30
LogSlowQueries=3000
# vim /etc/rc.d/rc.local
/usr/local/zabbix/sbin/zabbix_server -c /usr/local/zabbix/etc/zabbix_server.conf
/usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
# chmod +x /etc/rc.d/rc.local
8 安装httpd:
# yum install httpd -y
# mkdir /var/www/html/zabbix
# cp -a /usr/local/src/zabbix-4.0.24/frontends/php/* /var/www/html/zabbix
# systemctl restart httpd
9 访问web
http://192.168.7.121/zabbix 会报错
10 解决报错:
# yum install php-gettext php-session php-ctype php-xmlreader php-xmlwriter php-xml php-net-socket php-gd php-mysql
# vim /etc/php.ini
post_max_size = 16M
max_execution_time = 300
max_input_time = 300
date.timezone = Asia/Shanghai
11 重启http再次访问web
# systemctl restart httpd
12 解决中文乱码
# cd /var/www/html/zabbix/assets/fonts/
将simkai.ttf 上传至该目录
# sed -i "s@DejaVuSans@simkai@g" /var/www/html/zabbix/include/defines.inc.php
二 Zabbix 监控 Tomcat (Zabbix Server 与 Java gateway 安装在一台机器)
1 构建Tomcat客户端(7.123)
# yum install gcc libxml2-devel net-snmp net-snmp-devel curl curl-devel php php-bcmath php-mbstring mariadb-devel java-1.8.0-openjdk-devel -y
2 将二进制文件apache-tomcat-8.5.39.tar.gz 解压到 /apps/tomcat
# vim /apps/tomcat/bin/catalina.sh
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=192.168.7.123"
3 安装Java gateway(7.121)
# vim /usr/local/zabbix/sbin/zabbix_java/settings.sh
LISTEN_IP="0.0.0.0"
LISTEN_PORT=10052
PID_FILE="/tmp/zabbix_java.pid"
START_POLLERS=20
TIMEOUT=30
# /usr/local/zabbix/sbin/zabbix_java/startup.sh
# vim /usr/local/zabbix/etc/zabbix_server.conf
JavaGateway=192.168.7.121
JavaGatewayPort=10052
StartJavaPollers=20
Timeout=30
3 Tomcat端安装zabbix agent (7.123)
# yum install gcc libxml2-devel net-snmp net-snmp-devel curl curl-devel php php-bcmath php-mbstring mariadb mariadb-devel java-devel libevent-devel
# tar xvf zabbix-4.0.1.tar.gz
# cd zabbix-4.0.1/
# ./configure --enable-agent --prefix=/usr/local/zabbix
# make && make install
# cp misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
# vim /etc/init.d/zabbix_agentd
22 BASEDIR=/usr/local/zabbix
72 action $"Starting $BINARY_NAME: " $FULLPATH -c /usr/local/zabbix/etc/zabbix_agentd.conf
# useradd zabbix -s /sbin/nologin
# vim /usr/local/zabbix/etc/zabbix_agentd.conf
LogFile=/tmp/zabbix_agentd.log
DebugLevel=4
Server=192.168.7.121
Hostname=192.168.7.123
# /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
# /etc/init.d/zabbix_agentd restart #脚本问题
22 BASEDIR=/usr/local/zabbix
72 action $"Starting $BINARY_NAME: " $FULLPATH -c /usr/local/zabbix/etc/zabbix_agentd.conf
# vim /etc/rc.d/rc.local
# /apps/apache-tomcat-8.5.39/bin/catalina.sh start
# /usr/local/zabbix/sbin/zabbix_agentd -c /usr/local/zabbix/etc/zabbix_agentd.conf
# chmod +x /etc/rc.d/rc.local
4 配置zabbix webUI对tomcat的监控
三 监控memcache
1 监控脚本内容:
#!/bin/bash
memcached_status(){
M_PORT=$1
M_COMMAND=$2
echo -e "stats\nquit" | nc 127.0.0.1 "$M_PORT" | grep "STAT $M_COMMAND " | awk '{print $3}'
}
main(){
case $1 in
memcached_status) memcached_status $2 $3
;;
esac
}
main $1 $2 $3
2 测试脚本:
# bash memcache.sh memcached_status 11211 curr_connections
3 启动服务
# /etc/init.d/zabbix_agentd restart
4 WEB模式配置zabbix监控memcache
四 zabbix配置邮件报警
1 创建报警媒介类型(目的是指定发件人邮箱)
2 填写邮箱地址及邮箱服务器信息(以QQ邮箱为例)
3 创建zabbix用户指定(目的是指定收件人邮箱),配置后,一定要点“更新”生效
4 定义动作(定义发送邮件的操作)
5 触发报警
6 停掉nginx服务,查看邮件是否发送成功