Zabbix学习笔记(yum源安装)

Zabbix学习笔记(yum源安装)

链接:https://pan.baidu.com/s/19RXhumkB-ulpI4BGOa5b_A
提取码:115h
复制这段内容后打开百度网盘手机App,操作更方便哦

1.环境简述

主机名 主机IP
Zabbix 192.168.200.10
Tomcat 192.168.200.11
[root@Zabbix ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@Zabbix ~]# uname -r
3.10.0-957.12.1.el7.x86_64

2. 安装LAMP环境

2.1 安装apache

#yum源安装apache
[root@Zabbix ~]# yum install -y httpd

#设置开机自启动
[root@Zabbix ~]# systemctl enable httpd

#启动httpd服务
[root@Zabbix ~]# systemctl start httpd

2.2 安装mysql社区版mariadb

#yum源安装mariadb
[root@Zabbix ~]# yum install -y mariadb mariadb-server

#设置开机自启动
[root@Zabbix ~]# systemctl enable mariadb

#启动mariadb服务
[root@Zabbix ~]# systemctl start mariadb

2.3 安装php环境

[root@Zabbix ~]# yum install -y php php-mysql

3. 安装zabbix

3.1 安装zabbix-server端

3.1.1 下载包

[root@Zabbix ~]# rpm -ivh http://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm

3.1.2 安装zabbix的包

[root@Zabbix ~]# yum install -y zabbix-server-mysql zabbix-get zabbix-web zabbix-web-mysql zabbix-agent zabbix-sender

3.1.3 创建一个zabbix库并设置为utf8的字符编码格式

[root@Zabbix ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| zabbix             |
+--------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> 

3.1.4 创建账户并且授权设置密码

MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.1.5 导入表

#切换到此目录下
[root@Zabbix ~]# cd /usr/share/doc/zabbix-server-mysql-4.4.1/

#进行解压
[root@Zabbix zabbix-server-mysql-4.4.1]# gunzip create.sql.gz
#对表进行导入
[root@Zabbix zabbix-server-mysql-4.4.1]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.64-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use zabbix;
Database changed

MariaDB [zabbix]> source create.sql;
#以下省略若干。。。

3.1.6 配置zabbix server配置文件

#修改zabbix配置文件
[root@Zabbix zabbix-server-mysql-4.4.1]# cd /etc/zabbix/
[root@Zabbix zabbix]# vim zabbix_server.conf 
[root@Zabbix zabbix]# cat -n zabbix_server.conf | sed -n "91p;100p;116p;125p;133p"
    91	DBHost=localhost
   100	DBName=zabbix
   116	DBUser=zabbix
   125	DBPassword=zabbix
   133	DBSocket=/var/lib/mysql/mysql.sock
#运行zabbix-server服务
[root@Zabbix zabbix]# systemctl start zabbix-server

#开机自启zabbix-server服务
[root@Zabbix zabbix]# systemctl enable zabbix-server

3.1.7 配置php

#配置时间为上海
[root@Zabbix zabbix]# cd /etc/httpd/conf.d
[root@Zabbix conf.d]# vim zabbix.conf 
[root@Zabbix conf.d]# cat -n zabbix.conf | sed -n "21p"
    21	        php_value date.timezone Asia/Shanghai

#重启httpd服务
[root@Zabbix conf.d]# systemctl restart httpd

3.1.8 登陆zabbix网址设置http://192.168.200.10/zabbix

image.png-129.3kB

image.png-206.7kB

image.png-177.4kB

image.png-154.1kB

image.png-197.6kB

image.png-136.8kB

image.png-66.3kB

image.png-373.4kB

image.png-119.1kB

image.png-395.4kB

3.1.9 修改zabbix中文乱码

image.png-537.4kB

image.png-780.4kB

#替换字符文件(可以从自己win电脑拷一份)
[root@Zabbix ~]# whereis zabbix
zabbix: /usr/lib/zabbix /etc/zabbix /usr/share/zabbix
[root@Zabbix ~]# cd /usr/share/zabbix/
[root@Zabbix zabbix]# mkdir fonts
[root@Zabbix zabbix]# cd fonts/
[root@Zabbix fonts]# rz   #上传字符

[root@Zabbix fonts]# ll
总用量 19188
-rw-r--r-- 1 root root 19647736 9月  15 2018 msyh.ttc
#切换至目录/etc/alternatives/ ,可以看到zabbix-web-font文件实际上是一个软连接,链接的文件实际位置是:/usr/share/fonts/dejavu/DejaVuSans.ttf 

[root@Zabbix fonts]# ll /etc/alternatives/zabbix-web-font
lrwxrwxrwx 1 root root 38 11月 12 15:40 /etc/alternatives/zabbix-web-font -> /usr/share/fonts/dejavu/DejaVuSans.ttf
[root@Zabbix fonts]# rm -rf  /etc/alternatives/zabbix-web-font 
[root@Zabbix fonts]# ln -s /usr/share/zabbix/fonts/msyh.ttc /etc/alternatives/zabbix-web-font
[root@Zabbix fonts]# ll /etc/alternatives/zabbix-web-font
lrwxrwxrwx 1 root root 32 11月 12 17:37 /etc/alternatives/zabbix-web-font -> /usr/share/zabbix/fonts/msyh.ttc
#重启zabbix服务
[root@Zabbix fonts]# systemctl restart zabbix-server

image.png-586kB

3.2 安装Zabbix-Server端agent服务

3.2.1 安装agent命令

[root@Zabbix conf.d]# rpm -ivh http://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.1-1.el7.x86_64.rpm

3.2.2 修改Agent端的配置文件

[root@Zabbix conf.d]# cd /etc/zabbix/
[root@Zabbix zabbix]# vim zabbix_agentd.conf 
[root@Zabbix zabbix]# cat -n zabbix_agentd.conf | sed -n "13p;32p;98p;106p;123p;150p;290p;309p"
    13	PidFile=/var/run/zabbix/zabbix_agentd.pid
    32	LogFile=/var/log/zabbix/zabbix_agentd.log
    98	Server=127.0.0.1,192.168.200.10
   106	ListenPort=10050
   123	StartAgents=3
   150	Hostname=Zabbix
   290	Include=/etc/zabbix/zabbix_agentd.d/*.conf
   309	UnsafeUserParameters=1

3.2.3 启动Zabbix-Server端的agent服务

#启动agent命令
[root@Zabbix zabbix]# systemctl start zabbix-agent

#设置开机自启动
[root@Zabbix zabbix]# systemctl enable zabbix-agent

#查看进程
[root@Zabbix zabbix]# netstat -antup | grep zabbix_agent
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      2681/zabbix_agentd  
tcp6       0      0 :::10050                :::*                    LISTEN      2681/zabbix_agentd 

4. Zabbix-Agent端之Tomcat基础安装

4.1 jdk的解压安装

[root@Tomcat ~]# cd /root/yuanmabao/
[root@Tomcat yuanmabao]# tar xf jdk-8u60-linux-x64.tar.gz -C /usr/
[root@Tomcat yuanmabao]# useradd tomcat
[root@Tomcat yuanmabao]# chown -R tomcat.tomcat /usr/jdk1.8.0_60
[root@Tomcat yuanmabao]# ll -d /usr/jdk1.8.0_60
drwxr-xr-x 8 tomcat tomcat 255 8月   5 2015 /usr/jdk1.8.0_60
[root@Tomcat yuanmabao]# ln -s /usr/jdk1.8.0_60 /usr/jdk
[root@Tomcat yuanmabao]# ll -d /usr/jdk
lrwxrwxrwx 1 root root 16 11月  8 16:43 /usr/jdk -> /usr/jdk1.8.0_60

[root@Tomcat yuanmabao]# chown -h tomcat.tomcat /usr/jdk
[root@Tomcat yuanmabao]# ll -d /usr/jdk
lrwxrwxrwx 1 tomcat tomcat 16 11月  8 16:43 /usr/jdk -> /usr/jdk1.8.0_60

4.2 配置java环境变量

[root@Tomcat yuanmabao]# sed -i.ori '$a export JAVA_HOME=/usr/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' /etc/profile

[root@Tomcat yuanmabao]# tail -3 /etc/profile
export JAVA_HOME=/usr/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
[root@Tomcat yuanmabao]# source /etc/profile
[root@Tomcat yuanmabao]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

4.3 安装Tomcat

[root@Tomcat yuanmabao]# tar xf apache-tomcat-8.0.27.tar.gz -C /usr/
[root@Tomcat yuanmabao]# chown -R tomcat.tomcat /usr/apache-tomcat-8.0.27
[root@Tomcat yuanmabao]# ll -d /usr/apache-tomcat-8.0.27
drwxr-xr-x 9 tomcat tomcat 160 11月  8 16:48 /usr/apache-tomcat-8.0.27
[root@Tomcat yuanmabao]# ln -s /usr/apache-tomcat-8.0.27 /usr/tomcat
[root@Tomcat yuanmabao]# ll -d /usr/tomcat
lrwxrwxrwx 1 root root 25 11月  8 16:48 /usr/tomcat -> /usr/apache-tomcat-8.0.27

[root@Tomcat yuanmabao]# chown -h tomcat.tomcat /usr/tomcat
[root@Tomcat yuanmabao]# ll -d /usr/tomcat
lrwxrwxrwx 1 tomcat tomcat 25 11月  8 16:48 /usr/tomcat -> /usr/apache-tomcat-8.0.27

4.4 配置Tomcat环境变量

[root@Tomcat yuanmabao]# echo 'export TOMCAT_HOME=/usr/tomcat' >> /etc/profile
[root@Tomcat yuanmabao]# tail -1 /etc/profile
export TOMCAT_HOME=/usr/tomcat
[root@Tomcat yuanmabao]# source /etc/profile
[root@Tomcat yuanmabao]# java -version
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

4.5 修改tomcat日志catalina.out默认路径

[root@Tomcat yuanmabao]# cd /usr/tomcat/bin/
[root@Tomcat bin]# vim catalina.sh 
[tomcat@Tomcat bin]$ cat catalina.sh | sed -n '198,200p'
if [ -z "$CATALINA_OUT" ] ; then
  CATALINA_OUT=/data/Tomcat_Logs/log/catalina.out
fi

[root@Tomcat bin]# mkdir -p /data/Tomcat_Logs/log
[root@Tomcat bin]# cd /data/
[root@Tomcat data]# chown -R tomcat.tomcat Tomcat_Logs
[root@Tomcat data]# ll -d Tomcat_Logs/
drwxr-xr-x 3 tomcat tomcat 17 11月  8 16:55 Tomcat_Logs/

4.6 启动tomcat服务

[root@Tomcat data]# su - tomcat
[tomcat@Tomcat ~]$ /usr/tomcat/bin/startup.sh 

[tomcat@Tomcat ~]$ ps -ef | grep java
tomcat     7482      1 99 17:42 pts/0    00:00:01 /usr/jdk/bin/java -Djava.util.logging.config.file=/usr/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/tomcat/endorsed -classpath /usr/tomcat/bin/bootstrap.jar:/usr/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/tomcat -Dcatalina.home=/usr/tomcat -Djava.io.tmpdir=/usr/tomcat/temp org.apache.catalina.startup.Bootstrap start
tomcat     7499   7448  0 17:42 pts/0    00:00:00 grep --color=auto java

5 Zabbix-Agent端的agent安装

5.1 安装agent命令

[root@Tomcat ~]# rpm -ivh http://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.1-1.el7.x86_64.rpm

5.2 修改Zabbix-Agen端的配置文件

[root@Tomcat ~]# cd /etc/zabbix/
[root@Tomcat zabbix]# vim zabbix_agentd.conf 
[root@Tomcat zabbix]# cat -n zabbix_agentd.conf | sed -n "13p;32p;98p;106p;123p;150p;290p;309p"
    13	PidFile=/var/run/zabbix/zabbix_agentd.pid
    32	LogFile=/var/log/zabbix/zabbix_agentd.log
    98	Server=192.168.200.10
   106	ListenPort=10050
   123	StartAgents=3
   150	Hostname=Zabbix
   290	Include=/etc/zabbix/zabbix_agentd.d/*.conf
   309	UnsafeUserParameters=1

5.3 启动Zabbix-Agen端的agent服务

#启动agent命令
[root@Tomcat zabbix]# systemctl start zabbix-agent

#设置开机自启动
[root@Tomcat zabbix]# systemctl enable zabbix-agent

#查看进程
[root@Tomcat zabbix]# netstat -antup | grep zabbix_agent
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      1557/zabbix_agentd  
tcp6       0      0 :::10050                :::*                    LISTEN      1557/zabbix_agentd  

5.4 测试zabbix server监控

#在监控端zabbix_server端进行如下操作
[root@Zabbix ~]# zabbix_get -s 192.168.200.10 -p 10050 -k "system.uptime"
1692
[root@Zabbix ~]# zabbix_get -s 192.168.200.11 -p 10050 -k "system.uptime"
1696


#参数详解
-s 是指定zabbix agent端的IP地址
-p 是指定zabbix agent端的监听端口
-k 是监控项,即item
如果有输出结果,表面zabbix server可以从zabbix agent获取数据,配置成功。

6. zabbix图形化监控简介

6.1 创建一个主机群组

image.png-120.8kB

image.png-107.9kB

image.png-48.4kB

6.2 创建一个主机

image.png-211.4kB

image.png-253.6kB

image.png-190kB

6.3 创建自定义监控脚本

[root@Tomcat zabbix]# cd /etc/zabbix/
[root@Tomcat zabbix]# mkdir scripts
[root@Tomcat zabbix]# cd scripts
[root@Tomcat scripts]# vim thread.sh
[root@Tomcat scripts]# cat thread.sh 
#/bin/bash

pid=`ps -ef | grep jav[a] | awk '{print $2}'`
grep -i 'thread' /proc/$pid/status | awk '{print $2}'
[root@Tomcat scripts]# chmod +x thread.sh 
[root@Tomcat scripts]# ll -d thread.sh 
-rwxr-xr-x 1 root root 112 11月 10 23:54 thread.sh

6.4 创建键值

[root@Tomcat scripts]# cd /etc/zabbix/zabbix_agentd.d
[root@Tomcat zabbix_agentd.d]# vim thread.conf
[root@Tomcat zabbix_agentd.d]# cat thread.conf 
UserParameter=thread.num,/usr/bin/sh /etc/zabbix/scripts/thread.sh
#重启zabbix才会生效
[root@Tomcat zabbix_agentd.d]# systemctl restart zabbix-agent

#检查server是否可以调取到键值
[root@Zabbix ~]# zabbix_get -s 192.168.200.11 -p 10050 -k "thread.num"
19

6.5 自定义一个监控项

image.png-192.2kB

image.png-237.2kB

image.png-239.4kB

image.png-67.9kB

6.6 自定义一个监控项的图形

image.png-246.9kB

image.png-143.2kB

image.png-410.9kB

image.png-192.1kB

image.png-533.1kB

6.7 Zabbix触发器与表达式的编写方法

image.png-194.3kB

image.png-337.5kB

image.png-368.5kB

image.png-61.4kB