Linux下常用的系统监控软件有Nagios、Cacti、Zabbix、Monit等,这些开源的软件,可以帮助我们更好的
管理机器,在第一时间内发现,并警告系统维护人员。
今天开始研究下Zabbix,使用Zabbix的目的,是为了能够更好的监控mysql数据库服务器,并且能够生成
图形报表,虽然Nagios也能够生成图形报表,但没有Zabbix这么强大。
首先,我们先来介绍下Zabblx:
Zabbix简介:
zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。
zabbix由zabbix server与可选组件zabbix agent两部门组成。
zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视。
zabbix agent需要安装在被监视的目标服务器上,它主要完成对硬件信息或与操作系统有关的内存,CPU等信息的收集。
zabbix的主要特点:
- 安装与配置简单,学习成本低
- 支持多语言(包括中文)
- 免费开源
- 自动发现服务器与网络设备
- 分布式监视以及WEB集中管理功能
- 可以无agent监视
- 用户安全认证和柔软的授权方式
- 通过WEB界面设置或查看监视结果
- email等通知功能
Zabbix主要功能:
CPU负荷
内存使用
磁盘使用
网络状况
端口监视
日志监视
官方也提供了安装资料:http://www.zabbix.com/wiki/howto/monitor
实验环境:
server3: 172.25.38.3 agent端 企业6
loaclhost: 172.25.38.11 企业7
zabbix的安装:
[root@localhost ~]# ls
fping-3.10-1.el7.x86_64.rpm
iksemel-1.4-2.el7.centos.x86_64.rpm
php-bcmath-5.4.16-21.el7.x86_64.rpm
php-common-5.4.16-21.el7.x86_64.rpm
php-mbstring-5.4.16-21.el7.x86_64.rpm
tidb-ansible-master.zip
tidb-latest-linux-amd64
tidb-latest-linux-amd64.tar.gz
zabbix-agent-3.4.6-1.el7.x86_64.rpm
zabbix-get-3.4.6-1.el7.x86_64.rpm
zabbix-java-gateway-3.4.6-1.el7.x86_64.rpm
zabbix-proxy-mysql-3.4.6-1.el7.x86_64.rpm
zabbix-server-mysql-3.4.6-1.el7.x86_64.rpm
zabbix-web-3.4.6-1.el7.noarch.rpm
zabbix-web-mysql-3.4.6-1.el7.noarch.rpm
[root@localhost ~]# yum install -y zabbix-server-mysql-3.4.6-1.el7.x86_64.rpm zabbix-web-3.4.6-1.el7.noarch.rpm zabbix-web-mysql-3.4.6-1.el7.noarch.rpm fping-3.10-1.el7.x86_64.rpm iksemel-1.4-2.el7.centos.x86_64.rpm php-*
[root@localhost zabbix]# systemctl start mariadb 开启数据库
[root@localhost zabbix]# mysql_secure_installation 初始化安装
Set root password? [Y/n] Y 设置密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@localhost zabbix]# mysql -pwestos
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'westos'; 授权
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit
Bye
[root@localhost zabbix]# ls
web zabbix_java_gateway.conf zabbix_server.conf
zabbix_agentd.conf zabbix_java_gateway_logback.xml
zabbix_agentd.d zabbix_proxy.conf
[root@localhost zabbix]# cd web/
[root@localhost web]# ls
maintenance.inc.php
[root@localhost web]# cd ..
[root@localhost zabbix]# cd zabbix_agentd.d/
[root@localhost zabbix_agentd.d]# ls
userparameter_mysql.conf
[root@localhost zabbix_agentd.d]# cd ..
[root@localhost zabbix]# cd
[root@localhost ~]# cd /etc/zabbix/
[root@localhost zabbix]# ls
web zabbix_java_gateway.conf zabbix_server.conf
zabbix_agentd.conf zabbix_java_gateway_logback.xml
zabbix_agentd.d zabbix_proxy.conf
[root@localhost zabbix]# cd /usr/share/
[root@localhost share]# cd doc/
[root@localhost doc]# cd zabbix-server-mysql-3.4.6/
[root@localhost zabbix-server-mysql-3.4.6]# ls
AUTHORS ChangeLog COPYING create.sql.gz NEWS README
[root@localhost zabbix-server-mysql-3.4.6]# zcat create.sql.gz | mysql -u zabbix -p zabbix
Enter password:
[root@localhost zabbix-server-mysql-3.4.6]# ls
AUTHORS ChangeLog COPYING create.sql.gz NEWS README
[root@localhost zabbix-server-mysql-3.4.6]# cd /etc/zabbix/
[root@localhost zabbix]# ls
web zabbix_java_gateway.conf zabbix_server.conf
zabbix_agentd.conf zabbix_java_gateway_logback.xml
zabbix_agentd.d zabbix_proxy.conf
[root@localhost zabbix]# ls
web zabbix_java_gateway.conf zabbix_server.conf
zabbix_agentd.conf zabbix_java_gateway_logback.xml
zabbix_agentd.d zabbix_proxy.conf
[root@localhost zabbix]# vim zabbix_server.conf
[root@localhost zabbix]# ll /var/lib/mysql/mysql.sock
srwxrwxrwx 1 mysql mysql 0 Aug 14 15:15 /var/lib/mysql/mysql.sock
[root@localhost zabbix]# cd /etc/httpd/
[root@localhost httpd]# ls
conf conf.d conf.modules.d logs modules run
[root@localhost httpd]# cd conf.d/
[root@localhost conf.d]# ls
autoindex.conf php.conf README userdir.conf welcome.conf zabbix.conf
[root@localhost conf.d]# vim zabbix.conf
[root@localhost conf.d]# systemctl start zabbix-server
[root@localhost conf.d]# systemctl start httpd
[root@localhost conf.d]# getenforce
Disabled
[root@localhost conf.d]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@localhost conf.d]# cd
[root@localhost ~]# ls
fping-3.10-1.el7.x86_64.rpm
iksemel-1.4-2.el7.centos.x86_64.rpm
php-bcmath-5.4.16-42.el7.x86_64.rpm
php-mbstring-5.4.16-42.el7.x86_64.rpm
zabbix-agent-3.4.6-1.el7.x86_64.rpm
zabbix-get-3.4.6-1.el7.x86_64.rpm
zabbix-java-gateway-3.4.6-1.el7.x86_64.rpm
zabbix-proxy-mysql-3.4.6-1.el7.x86_64.rpm
zabbix-server-mysql-3.4.6-1.el7.x86_64.rpm
zabbix-web-3.4.6-1.el7.noarch.rpm
zabbix-web-mysql-3.4.6-1.el7.noarch.rpm
[root@localhost ~]# yum install zabbix-agent-3.4.6-1.el7.x86_64.rpm
[root@localhost ~]# systemctl start zabbix-agent
在浏览器进行安装配置zabbix:
在localhost添加解析:
[root@localhost ~]# hostname
server1
[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.38.11 server1
172.25.38.12 test2
172.25.38.13 test3
172.25.38.14 test4
172.25.38.15 test5
172.25.38.16 test6
172.25.38.3 server3
在agent端(server3)添加解析:
[root@server3 ~]# hostname
server3
[root@server3 ~]# vim /etc/hosts
[root@server3 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.38.11 server1
172.25.38.2 server2
172.25.38.3 server3
172.25.38.4 server4
172.25.38.5 server5
172.25.38.6 server6
[root@server3 ~]#
[root@server3 ~]# ls
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
zabbix-agent-3.4.6-1.el6.x86_64.rpm
[root@server3 ~]# rpm -ivh zabbix-agent-3.4.6-1.el6.x86_64.rpm
warning: zabbix-agent-3.4.6-1.el6.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing... ########################################### [100%]
1:zabbix-agent ########################################### [100%]
[root@server3 ~]# cd /etc/zabbix/
[root@server3 zabbix]# ls
zabbix_agentd.conf zabbix_agentd.d
[root@server3 zabbix]# vim zabbix_agentd.conf 写入agent-server的IP,也就是localhost的IP
[root@server3 zabbix]# /etc/init.d/zabbix-agent start 开启服务
Starting Zabbix agent: [ OK ]
[root@server3 zabbix]# netstat -antlp 可以看到10050端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 915/redis-server 0.
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 908/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 993/master
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 1118/zabbix_agentd
tcp 0 0 172.25.38.3:22 172.25.38.250:38770 ESTABLISHED 1042/sshd
tcp 0 0 :::22 :::* LISTEN 908/sshd
tcp 0 0 ::1:25 :::* LISTEN 993/master
tcp 0 0 :::10050 :::* LISTEN 1118/zabbix_agentd
在网页测试,之后的几个实验使用172.25.254.0/24网段进行实验:
1.手动创建主机:
2.自动注册:
3.自动发现:
1、获取Token:
[root@localhost ~]# vim zabbix-api.sh
[root@localhost ~]# chmod +x zabbix-api.sh 加入权限
[root@localhost ~]# ./zabbix-api.sh 调用脚本
{
"id": 1,
"jsonrpc": "2.0",
"result": "03a36ed5f6882ca93a201983ddb9830f"
}
[root@localhost ~]# cat zabbix-api.sh
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "westos"
},
"id": 1,
"auth": null
}' http://172.25.254.11/zabbix/api_jsonrpc.php | python -m json.tool
get主机:
[root@localhost ~]# vim zabbix-api.sh
[root@localhost ~]# chmod +x zabbix-api.sh
[root@localhost ~]# ./zabbix-api.sh
{
"id": 1,
"jsonrpc": "2.0",
"result": [
{
"host": "Zabbix server",
"hostid": "10084"
},
{
"host": "server3",
"hostid": "10255"
}
]
}
[root@localhost ~]# cat zabbix-api.sh
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.get",
"params": {
"output": ["host"]
},
"auth": "03a36ed5f6882ca93a201983ddb9830f",
"id": 1
}' http://172.25.254.11/zabbix/api_jsonrpc.php | python -m json.tool
删除主机:
[root@localhost ~]# vim zabbix-api.sh 删除server3
[root@localhost ~]# chmod +x zabbix-api.sh
[root@localhost ~]# ./zabbix-api.sh
{
"id": 1,
"jsonrpc": "2.0",
"result": {
"hostids": [
"10255"
]
}
}
[root@localhost ~]# cat zabbix-api.sh
curl -s -X POST -H 'Content-Type:application/json' -d '
{
"jsonrpc": "2.0",
"method": "host.delete",
"params": [
"10255"
],
"auth": "03a36ed5f6882ca93a201983ddb9830f",
"id": 1
}' http://172.25.254.11/zabbix/api_jsonrpc.php | python -m json.tool
在网页查看server3虚拟机已经被删除: