Article Directory
1. Introduction to zabbix
zabbix官网:https://www.zabbix.com
zabbix official website Chinese manual
1.1 Introduction to zabbix
- Zabbix was founded by Alexei Vladishev, and the company established by him, Zabbix SIA, is actively developing, updating and maintaining, and providing users with technical support services.
- Zabbix is an enterprise-level distributed open source monitoring solution.
- Zabbix software can monitor the health and integrity of numerous network parameters and servers. Zabbix uses a flexible alert mechanism, allowing users to configure email-based alerts for almost any event. This allows users to quickly respond to server problems. Zabbix provides excellent report and data visualization functions based on stored data. These features make Zabbix an ideal choice for capacity planning.
- Zabbix supports active polling (polling) and passive capture (trapping). All Zabbix reports, statistics and configuration parameters can be accessed through the web-based front-end page. The web-based front-end page ensures that you can access your monitored network status and server health from anywhere. With proper configuration, Zabbix can play an important role in monitoring IT infrastructure. The same applies to small organizations with a small number of servers or large enterprises with a large number of servers.
- Zabbix is free. Zabbix is written and released under the second edition of the GPL General Public License. This means that the product source code is released for free and is available for public use
1.2 zabbix function
Features-official website documentation
1.3 zabbix architecture
Server
- Zabbix server is the core component of Zabbix software, and the agent reports availability, system integrity information and statistics to it. The server is also the core repository that stores all configuration information, statistics, and operation information.
database
- All configuration information and data collected by Zabbix are stored in the database.
Web interface
- In order to easily access Zabbix from any place and any platform, we provide a web-based interface. This interface is part of the Zabbix server and usually (but not necessarily) runs on the same physical machine as the Zabbix server.
Proxy
- Zabbix proxy can collect performance and availability data instead of Zabbix server. The deployment of Zabbix proxy in Zabbix is an optional part; but the deployment of proxy can well share the load of a single Zabbix server.
Agent
- Zabbix agents are deployed on the monitored target to actively monitor local resources and applications, and send the collected data to the Zabbix server.
data flow
- In addition, it is also very important for Zabbix to use Zabbix to understand the overall data flow inside Zabbix. First, in order to create a monitoring item to collect data, you must first create a host. Second, at the other end of the task, there must be monitoring items to create triggers, and triggers must be used to create actions. Therefore, if you want to receive an alarm like "The CPU load on X servers is too high", you must first create a host entry for Server X, secondly create a monitoring item for monitoring its CPU, and finally create a trigger The device is used to trigger the action of high CPU load and send it to your mailbox. Although these steps may seem tedious, the actual operation is very simple when using templates. It is precisely because of this design that the configuration of Zabbix becomes more flexible and easy to use.
1.4 Commonly used terms
Commonly used terms on zabbix official website
2. Server installation and configuration
server1:192.168.17.1
2.1 Configure software warehouse
vim /etc/yum.repos.d/zabbix.repo
: You can use the official website yum source, here is the Aliyun mirroring website, which is fast!
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=0
2.2 zabbix-server and database installation and configuration
- Install MySQL database
yum install -y zabbix-server-mysql
yum install -y mariadb-server
- Open database service
systemctl start mariadb
systemctl enable mariadb
- Create a database:
mysql
mysql
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> create user 'zabbix'@'localhost' identified by '密码';
mysql> grant all privileges on zabbix.* to 'zabbix'@'localhost';
mysql> quit;
- Import Data
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p密码 zabbix
- Configuration database
vim /etc/zabbix/zabbix_server.conf
DBHost=localhost#默认
DBName=zabbix#默认
DBUser=zabbix#默认
DBPassword=westos
- Open zabbix service
systemctl enable --now zabbix-server
If it fails to open, you can view the zabbix log file: /var/log/zabbix/zabbix_server.log
2.3 Install zabbix-agent
yum install -y zabbix-agent
systemctl enable --now zabbix-agent
netstat -antlupe | grep zabbix
: View port
2.4 Front-end configuration
- installation
yum list php
: View the php version as 5.4.16-46.el7
yum install -y zabbix-web-mysql
yum install -y php-mbstring-5.4.16-46.el7.x86_64.rpm
(Alibaba Cloud can download)
yum install -y php-bcmath-5.4.16-46.el7.x86_64.rpm
(Alibaba Cloud can download)
- Modify time zone
vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Shanghai
- Modify font
cd /usr/share/zabbix/assets/fonts/
: Download simkai.ttf to this directory
vim /usr/share/zabbix/include/defines.inc.php
执行:%s/graphfont/simkai/g
- Start Apache
systemctl enable --now httpd
- Visit : http://172.25.17.1/zabbix/
3. Agent configuration (monitored host)
server2:192.168.17.2
server3:192.168.17.3
- Configure software warehouse
vim /etc/yum.repos.d/zabbix.repo
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=0
- Install zabbix-agent (all agents must be installed)
yum install -y zabbix-agent
- Configuration
vim /etc/zabbix/zabbix_agentd.conf
: Edit the main configuration file
Server=192.168.17.1#监控系统server端的IP
ServerActive=192.168.17.1#监控系统server端的IP
Hostname=#agent端 本机的hostname
vim /etc/hosts
: Configuration analysis
192.168.17.1 server1
192.168.17.2 server2
192.168.17.3 server3
- Open service
systemctl enable --now zabbix-agent.service
- Log file :/var/log/zabbix/zabbix_agentd.log
4. Manually add the monitored host
Detailed explanation of all configurations in official documents
- Visit : http://172.25.17.1/zabbix/
- Select 1. "Configuration"-2. "Host"-3. "Create Host"
- Create host
- Add a template to the host
- After a while, after the ZBX icon of the added host turns green, the addition is successful and the monitoring is successful!
5. Automatically add monitored hosts
5.1 Server automatic discovery
- Enable "Auto Discovery" action
- Enable "Auto Discovery" rule
- Detection: successful automatic discovery of server3
5.2 Automatic registration on the agent side
- Disable automatic discovery, so as not to interfere with this experiment
- Due to limited hosts, delete server3
- Create "Auto Registration"
- Restart the agent service of server3
systemctl restart zabbix-agent.service
- Check, server3 is automatically registered successfully!
6. API
The Zabbix API allows you to retrieve and modify Zabbix configuration programmatically and provides access to historical data. It is widely used:
- Create a new application to use Zabbix;
- Integrate Zabbix with third-party software;
- Automate routine tasks.
Zabbix API is a web-based API provided as part of the web front end. It uses the JSON-RPC 2.0 protocol, which means two things:
- The API contains a set of independent methods;
- The request and response between the client and the API are encoded in JSON format.
structure
- get: retrieve data
- create: Create data
- update: update data
- delete: delete data
6.1 Obtain the authentication token user.login
Authentication attributes:
- jsonrpc: The version of the JSON-RPC protocol used by the API; the JSON-RPC version implemented by the Zabbix API is 2.0;
- method: the name of the API method being called;
- params: the parameters that will be passed to the API method;
- id: any identifier requested;
- auth: user authentication token; because we don’t have one yet, its setting is null
API response object:
- jsonrpc: the version of the JSON-RPC protocol;
- result: the data returned by the method (ie the user authentication token)
- id: the identifier of the corresponding request
Example
curl -s -XPOST http://192.168.17.1/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d
'{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}' | python -m json.tool
6.2 Retrieve host host.get
vim get_zabbix_api.sh
curl -s -XPOST http://192.168.17.1/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": [
"hostid",
"host"
],
"selectInterfaces": [
"interfaceid",
"ip"
]
},
"id": 1,
"auth": "身份令牌"
}' | python -m json.tool
6.3 Delete host host.delete
vim del_zabbix_api.sh
curl -s -XPOST http://192.168.17.1/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"删除的主机id"
],
"id": 1,
"auth": "身份令牌"
}' | python -m json.tool
6.4 Create host host.create
vim create_zabbix_api.sh
curl -s -XPOST http://192.168.17.1/zabbix/api_jsonrpc.php -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
"host": "server3",
"interfaces": [
{
"type": 1,
"main": 1,
"useip": 1,
"ip": "192.168.17.3",
"dns": "",
"port": "10050"
}
],
"groups": [
{
"groupid": "2"#主机群组id
}
],
"templates": [
{
"templateid": "10001"#模板id
}
],
"inventory_mode": 0,
"inventory": {
"macaddress_a": "01234",
"macaddress_b": "56768"
}
},
"auth": "身份令牌",
"id": 1
}' | python -m json.tool
7. Add httpd template
server2:192.168.17.2
- Configure httpd on the agent side
yum install -y httpd
systemctl enable --now httpd
echo server2 > /var/www/html/index.html
- Add template
8. Create nginx monitoring items
- Install source nginx
tar zxf nginx-1.18.0.tar.gz
cd nginx-1.18.0
yum install -y gcc pcre-devel openssl-devel
:安装gcc、pcre-devel、openssl-devel
vim auto/cc/gcc`
#CFLAGS="$CFLAGS -g"
#注释此行(127行)可以使安装后的二进制文件更小
./configure --prefix=/usr/local/nginx --with-http_ssl_module
: Configure script, specify the installation path and other parameters
make && make install
- Create soft connection
cd /usr/local/nginx/sbin/
:
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
- Configuration start
vim /usr/local/nginx/conf/nginx.conf
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
nginx -t
- Add the key of the monitoring item
vim /etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf
UserParameter=nginx.active,curl -s http://127.0.0.1/status | grep Active | awk '{print $3}'
UserParameter=nginx.accept,curl -s http://127.0.0.1/status | awk 'NR==3{print $1}'
UserParameter=nginx.handled,curl -s http://127.0.0.1/status | awk 'NR==3{print $2}'
UserParameter=nginx.request,curl -s http://127.0.0.1/status | awk 'NR==3{print $3}'
systemctl restart zabbix-agent.service
: Restart service
- Test the key of the monitoring item on the server side
yum install -y zabbix-get
: Server1 install zabbix-get
zabbix_get -s 192.168.17.3 -p 10050 -k "nginx.active"
- Create monitoring items
- Add monitoring items to the graph
- View monitoring results