Linux监控平台介绍

常见开源监控软件:cacti、nagios、zabbix、smokeping、open-falcon等等,其中cacti、smokeping偏向于基础监控,成图非常漂亮。cacti、nagios、zabbix服务端监控中心,需要php环境支持,其中zabbix和cacti都需要mysql作为数据存储,nagios不用存储历史数据,注重服务或者监控项的状态,zabbix会获取服务或者监控项目的数据,会把数据记录到数据库里,从而可以成图。open-falcon为小米公司开发,开源后受到诸多大公司和运维工程师的追捧,适合大企业,滴滴、360、新浪微博、京东等大公司在使用这款监控软件,值得研究。

zabbix监控介绍

Zabbix不仅适合中小型企业,也适合大型企业,它是C/S架构,分为服务队端与客户端,基于C++开发,监控中心支持web界面配置和管理。单server节点可以支持上万台客户端。最新版本3.4,官方文档 https://www.zabbix.com/manuals

Zabbix的5个组件:

1.zabbix-server

zabbix-server是整个监控体系中最核心的组件,它负责接收客户端发送的报告信息,负责所有配置、统计数据及操作数据都由它组织。

2.数据存储

所有的收集信息都存储在这里,比如mysql。

3.web界面

web界面也叫web UI,这是Zabbix监控简单易用的原因之一,因为我们可以在web界面中配置、管理各个肇端 ,运行Web界面需要有php环境支持。

4.zabbix-proxy

zabbix-proxy为可选组件,用于监控节点非常多的分布式环境中,它可以代替zabbix-server的功能,减轻server的压力。

5.zabbix-agent

zabbix-agent为部署在各客户端的组件,用于采集各监控项目的数据,并把采集的数据传输给zabbix-proxy或者zabbix-server。

Zabbix架构流程图

安装zabbix

1.官网下载地址

地址:www.zabbix.com/download

2.下载安装包,实验准备两台机器,两台都要下载。
[root@garytao-01 ~]# wget repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
3.使用rpm安装包(两台机器)
#官网有安装教程
[root@garytao-01 ~]# rpm -ivh zabbix-release-3.2-1.el7.noarch.rpm 
#查看安装的zabbix的yum源
[root@garytao-01 ~]# cat /etc/yum.repos.d/ 
CentOS-Base.repo       CentOS-Debuginfo.repo  CentOS-Media.repo      CentOS-Vault.repo      epel-testing.repo
CentOS-CR.repo         CentOS-fasttrack.repo  CentOS-Sources.repo    epel.repo.1            zabbix.repo
[root@garytao-01 ~]# cat /etc/yum.repos.d/zabbix.repo 
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://repo.zabbix.com/zabbix/3.2/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch 
baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
4.安装zabbix的一些包
[root@garytao-01 ~]# yum install -y zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql

#会连带安装httpd和php
#如果mysql之前没有安装的话,需要根据lamp那一章的mysql安装方法安装mysql

#启动mysql
[root@garytao-01 ~]#  /etc/init.d/mysqld start
5.配置/etc/my.cnf
[root@garytao-01 ~]# vi /etc/my.cnf

增加如下配置内容:

character_set_server = utf8

6.重启mysql服务后,进入mysql命令行,创建zabbix库。
#查看服务进程
[root@garytao-01 mysql]# ss -tnl
#假设如果mysql无法重启,可以查看进程,使用命令杀死假死进程。
[root@garytao-01 mysql]# killall mysqld

#重启成功
[root@garytao-01 ~]# systemctl restart mysql
[root@garytao-01 ~]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS! 

#创建zabbix库
[root@garytao-01 ~]# mysql -uroot -pszyino-123

#创建库
mysql> create database zabbix character set utf8;
Query OK, 1 row affected (0.00 sec)
#创建用户
mysql> grant all on zabbix.* to 'zabbix'@'127.0.0.1' identified by 'admin-zabbix';
Query OK, 0 rows affected (0.01 sec)

mysql> 
7.导入数据
#进入目录
[root@garytao-01 ~]# cd /usr/share/doc/zabbix-server-mysql-3.2.11/
[root@garytao-01 zabbix-server-mysql-3.2.11]# ls
AUTHORS  ChangeLog  COPYING  create.sql.gz  NEWS  README

#解压压缩包
[root@garytao-01 zabbix-server-mysql-3.2.11]# gzip -d create.sql.gz 
[root@garytao-01 zabbix-server-mysql-3.2.11]# ls
AUTHORS  ChangeLog  COPYING  create.sql  NEWS  README

#导入数据
[root@garytao-01 zabbix-server-mysql-3.2.11]# mysql -uroot -pszyino-123 zabbix < create.sql 
Warning: Using a password on the command line interface can be insecure.
[root@garytao-01 zabbix-server-mysql-3.2.11]# cd
8.启动 zabbix-server服务
#启动zabbix
[root@garytao-01 ~]# systemctl start zabbix-server

#启动httpd,先检查看nginx服务有没启动,启动了关掉。
[root@garytao-01 ~]# ps aux |grep nginx
root        830  0.0  0.2  46860  2276 ?        Ss   1月29   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    10890  0.0  0.3  48784  3780 ?        S    00:00   0:00 nginx: worker process
nobody    10891  0.0  0.3  48784  3780 ?        S    00:00   0:00 nginx: worker process
root      14512  0.0  0.0 112680   976 pts/0    R+   14:19   0:00 grep --color=auto nginx
[root@garytao-01 ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl):                            [  确定  ]
[root@garytao-01 ~]# systemctl start httpd
[root@garytao-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      896/rpc.mountd      
tcp        0      0 0.0.0.0:38964           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      822/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1596/master         
tcp        0      0 0.0.0.0:38329           0.0.0.0:*               LISTEN      795/rpc.statd       
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::3306                 :::*                    LISTEN      14401/mysqld        
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::80                   :::*                    LISTEN      14538/httpd         
tcp6       0      0 :::20048                :::*                    LISTEN      896/rpc.mountd      
tcp6       0      0 :::22                   :::*                    LISTEN      822/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1596/master         
tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
tcp6       0      0 :::38115                :::*                    LISTEN      -                   
tcp6       0      0 :::47721                :::*                    LISTEN      795/rpc.statd    
9.加入服务,设置开机启动
[root@garytao-01 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@garytao-01 ~]# systemctl enable zabbix-server
Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-server.service to /usr/lib/systemd/system/zabbix-server.service.

#如果有安装nginx,需要关闭开机启动
[root@garytao-01 ~]# systemctl disable nginx
nginx.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig nginx off
[root@garytao-01 ~]# chkconfig nginx off

#查看监听端口,发现没有监听端口
[root@garytao-01 ~]# ps aux |grep zabbix
zabbix    14507  0.0  0.3 256080  3452 ?        S    14:18   0:00 /usr/sbin/zabbix_server -c /etc/zabbix/zabbix_server.conf
root      14677  0.0  0.0 112680   976 pts/0    R+   14:38   0:00 grep --color=auto zabbix
[root@garytao-01 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      896/rpc.mountd      
tcp        0      0 0.0.0.0:38964           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      822/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1596/master         
tcp        0      0 0.0.0.0:38329           0.0.0.0:*               LISTEN      795/rpc.statd       
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
tcp6       0      0 :::3306                 :::*                    LISTEN      14401/mysqld        
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::80                   :::*                    LISTEN      14538/httpd         
tcp6       0      0 :::20048                :::*                    LISTEN      896/rpc.mountd      
tcp6       0      0 :::22                   :::*                    LISTEN      822/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1596/master         
tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
tcp6       0      0 :::38115                :::*                    LISTEN      -                   
tcp6       0      0 :::47721                :::*                    LISTEN      795/rpc.statd 
查看日志
[root@garytao-01 ~]# less /var/log/zabbix/zabbix_server.log

无法连接mysql,如下图:

修改配置文件:

[root@garytao-01 ~]# vim /etc/zabbix/zabbix_server.conf 

增加如下配置:

DBHost=127.0.0.1  //在DBName=zabbix上面增加

DBPassword=admin-zabbix  //在DBuser下面增加

编辑配置文件之后,需要重启服务,重新加载配置文件。

#重启
[root@garytao-01 ~]# systemctl restart zabbix-server
#查看启动服务,正常需要启动如下服务
[root@garytao-01 ~]# ps aux |grep zabbix
zabbix    14777  1.1  0.4 256260  4120 ?        S    15:12   0:00 /usr/sbin/zabbix_server -c /etc/zabbix/zabbix_server.conf
zabbix    14780  0.0  0.2 256260  2468 ?        S    15:12   0:00 /usr/sbin/zabbix_server: configuration syncer [waiting 60 sec for processes]
zabbix    14781  0.0  0.2 256260  2676 ?        S    15:12   0:00 /usr/sbin/zabbix_server: db watchdog [synced alerts config in 0.033642 sec, idle 60 sec]
zabbix    14782  0.3  0.5 363204  5188 ?        S    15:12   0:00 /usr/sbin/zabbix_server: poller #1 [got 0 values in 0.000003 sec, idle 5 sec]
zabbix    14783  0.3  0.5 363204  5188 ?        S    15:12   0:00 /usr/sbin/zabbix_server: poller #2 [got 0 values in 0.000005 sec, idle 5 sec]
zabbix    14784  0.4  0.5 363204  5188 ?        S    15:12   0:00 /usr/sbin/zabbix_server: poller #3 [got 0 values in 0.000005 sec, idle 5 sec]
zabbix    14785  0.3  0.5 363204  5188 ?        S    15:12   0:00 /usr/sbin/zabbix_server: poller #4 [got 0 values in 0.000004 sec, idle 5 sec]
zabbix    14786  0.3  0.5 363204  5188 ?        S    15:12   0:00 /usr/sbin/zabbix_server: poller #5 [got 0 values in 0.000006 sec, idle 5 sec]
zabbix    14787  0.3  0.5 363204  5188 ?        S    15:12   0:00 /usr/sbin/zabbix_server: unreachable poller #1 [got 0 values in 0.000004 sec, idle 5 sec]
zabbix    14788  0.0  0.3 256260  3584 ?        S    15:12   0:00 /usr/sbin/zabbix_server: trapper #1 [processed data in 0.000000 sec, waiting for connection]
zabbix    14789  0.0  0.3 256260  3584 ?        S    15:12   0:00 /usr/sbin/zabbix_server: trapper #2 [processed data in 0.000000 sec, waiting for connection]
zabbix    14790  0.0  0.3 256260  3584 ?        S    15:12   0:00 /usr/sbin/zabbix_server: trapper #3 [processed data in 0.000000 sec, waiting for connection]
zabbix    14791  0.0  0.3 256260  3584 ?        S    15:12   0:00 /usr/sbin/zabbix_server: trapper #4 [processed data in 0.000000 sec, waiting for connection]
zabbix    14794  0.0  0.3 256260  3584 ?        S    15:12   0:00 /usr/sbin/zabbix_server: trapper #5 [processed data in 0.000000 sec, waiting for connection]
zabbix    14796  0.0  0.2 258832  2616 ?        S    15:12   0:00 /usr/sbin/zabbix_server: icmp pinger #1 [got 0 values in 0.000004 sec, idle 5 sec]
zabbix    14797  0.0  0.2 256260  2684 ?        S    15:12   0:00 /usr/sbin/zabbix_server: alerter [sent alerts: 0 success, 0 fail in 0.005679 sec, idle 30 sec]
zabbix    14799  0.0  0.2 256260  2464 ?        S    15:12   0:00 /usr/sbin/zabbix_server: housekeeper [startup idle for 30 minutes]
zabbix    14800  0.0  0.2 256332  2904 ?        S    15:12   0:00 /usr/sbin/zabbix_server: timer #1 [processed 0 triggers, 0 events in 0.000256 sec, 0 maintenances in 0.000000 sec, idle 30 sec]
zabbix    14801  0.0  0.2 256260  2832 ?        S    15:12   0:00 /usr/sbin/zabbix_server: http poller #1 [got 0 values in 0.000844 sec, idle 5 sec]
zabbix    14802  0.2  0.5 360624  5028 ?        S    15:12   0:00 /usr/sbin/zabbix_server: discoverer #1 [processed 0 rules in 0.003493 sec, idle 60 sec]
zabbix    14805  0.0  0.2 256260  2816 ?        S    15:12   0:00 /usr/sbin/zabbix_server: history syncer #1 [synced 0 items in 0.000002 sec, idle 1 sec]
zabbix    14812  0.0  0.2 256260  2816 ?        S    15:12   0:00 /usr/sbin/zabbix_server: history syncer #2 [synced 0 items in 0.000001 sec, idle 1 sec]
zabbix    14815  0.0  0.2 256260  2816 ?        S    15:12   0:00 /usr/sbin/zabbix_server: history syncer #3 [synced 0 items in 0.000002 sec, idle 1 sec]
zabbix    14817  0.0  0.2 256260  2816 ?        S    15:12   0:00 /usr/sbin/zabbix_server: history syncer #4 [synced 0 items in 0.000004 sec, idle 1 sec]
zabbix    14818  0.0  0.3 256260  3760 ?        S    15:12   0:00 /usr/sbin/zabbix_server: escalator #1 [processed 0 escalations in 0.000721 sec, idle 3 sec]
zabbix    14820  0.0  0.3 256260  3756 ?        S    15:12   0:00 /usr/sbin/zabbix_server: proxy poller #1 [exchanged data with 0 proxies in 0.000010 sec, idle 5 sec]
zabbix    14821  0.0  0.2 256260  2584 ?        S    15:12   0:00 /usr/sbin/zabbix_server: self-monitoring [processed data in 0.000003 sec, idle 1 sec]
zabbix    14822  0.0  0.2 256260  2804 ?        S    15:12   0:00 /usr/sbin/zabbix_server: task manager [processed 0 task(s) in 0.000936 sec, idle 5 sec]
root      14828  0.0  0.0 112680   976 pts/0    R+   15:12   0:00 grep --color=auto zabbix
#查看监听端口
[root@garytao-01 ~]# netstat -lntp |grep zabbix
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      14777/zabbix_server 
tcp6       0      0 :::10051                :::*                    LISTEN      14777/zabbix_server 
10.web界面下面配置zabbix
  • 浏览器访问:172.16.111.100/zabbix

ok说明正常,Fail是有问题的,需要到配置文件里配置一下.

#修改配置文件,设置时区
[root@garytao-01 ~]# vi /etc/php.ini

#编辑完成文件后,重启httpd
[root@garytao-01 ~]# systemctl restart httpd

配置图:

刷新浏览器

Database port默认是0就是mysql的3306端口,密码就是我们创建zabbix用户时的密码

安装成功

  • 登录后台配置

使用默认用户名admin 密码zabbix登录

进入后台第一件事情就是修改密码

备注:修改完成密码后,设置成中文显示,保存后刷新浏览器。

zabbix客户端安装

1.下载zabbix的yum源
#下载yum源
[root@garytao-02 ~]# wget repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm

#安装yum源
[root@garytao-02 ~]# rpm -ivh zabbix-release-3.2-1.el7.noarch.rpm

#安装
[root@garytao-02 ~]# yum install -y zabbix-agent
2.编辑配置文件
[root@garytao-02 ~]# vi /etc/zabbix/zabbix_agentd.conf

修改如下内容:

 Server=127.0.0.1修改为Server=172.16.111.100 //定义服务端的ip(被动模式)
 ServerActive=127.0.0.1修改为ServerActive=172.16.111.100 //定义服务端的ip(主动模式)
Hostname=Zabbix server修改为Hostname=admin-123 //这是自定义的主机名,一会还需要在web界面下设置同样的主机名

3.启动服务,设置开机启动
[root@garytao-02 ~]# systemctl start zabbix-agent
[root@garytao-02 ~]# ps aux |grep zabbix
zabbix     7400  0.0  0.1  80604  1256 ?        S    16:29   0:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix     7401  0.0  0.1  80604  1292 ?        S    16:29   0:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix     7402  0.0  0.1  80604  1836 ?        S    16:29   0:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix     7403  0.0  0.1  80604  1836 ?        S    16:29   0:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix     7404  0.0  0.1  80604  1836 ?        S    16:29   0:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix     7405  0.0  0.2  80604  2176 ?        S    16:29   0:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root       7413  0.0  0.0 112676   976 pts/0    R+   16:30   0:00 grep --color=auto zabbix
[root@garytao-02 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      3893/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      758/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1221/master         
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      7400/zabbix_agentd  
tcp6       0      0 :::3306                 :::*                    LISTEN      1126/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      758/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1221/master         
tcp6       0      0 :::10050                :::*                    LISTEN      7400/zabbix_agentd  

#设置开机启动: 
[root@garytao-02 ~]# systemctl enable zabbix-agent

忘记Admin密码如何做

1.进入mysql命令行,选择zabbix库
[root@garytao-01 ~]# mysql -uroot -pszyino-123
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 585
Server version: 5.6.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> use zabbix
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> desc users;
+----------------+---------------------+------+-----+---------+-------+
| Field          | Type                | Null | Key | Default | Extra |
+----------------+---------------------+------+-----+---------+-------+
| userid         | bigint(20) unsigned | NO   | PRI | NULL    |       |
| alias          | varchar(100)        | NO   | UNI |         |       |
| name           | varchar(100)        | NO   |     |         |       |
| surname        | varchar(100)        | NO   |     |         |       |
| passwd         | char(32)            | NO   |     |         |       |
| url            | varchar(255)        | NO   |     |         |       |
| autologin      | int(11)             | NO   |     | 0       |       |
| autologout     | int(11)             | NO   |     | 900     |       |
| lang           | varchar(5)          | NO   |     | en_GB   |       |
| refresh        | int(11)             | NO   |     | 30      |       |
| type           | int(11)             | NO   |     | 1       |       |
| theme          | varchar(128)        | NO   |     | default |       |
| attempt_failed | int(11)             | NO   |     | 0       |       |
| attempt_ip     | varchar(39)         | NO   |     |         |       |
| attempt_clock  | int(11)             | NO   |     | 0       |       |
| rows_per_page  | int(11)             | NO   |     | 50      |       |
+----------------+---------------------+------+-----+---------+-------+
16 rows in set (0.01 sec)

mysql> 
2.修改密码
mysql> update users set passwd=md5('szyino-123') where alias='Admin';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from users;
+--------+-------+--------+---------------+----------------------------------+-----+-----------+------------+-------+---------+------+---------+----------------+------------+---------------+---------------+
| userid | alias | name   | surname       | passwd                           | url | autologin | autologout | lang  | refresh | type | theme   | attempt_failed | attempt_ip | attempt_clock | rows_per_page |
+--------+-------+--------+---------------+----------------------------------+-----+-----------+------------+-------+---------+------+---------+----------------+------------+---------------+---------------+
|      1 | Admin | Zabbix | Administrator | 251fa94c65a7c8ab5760dd6398159841 |     |         1 |          0 | zh_CN |      30 |    3 | default |              0 |            |             0 |            50 |
|      2 | guest |        |               | d41d8cd98f00b204e9800998ecf8427e |     |         0 |        900 | en_GB |      30 |    1 | default |              0 |            |             0 |            50 |
+--------+-------+--------+---------------+----------------------------------+-----+-----------+------------+-------+---------+------+---------+----------------+------------+---------------+---------------+
2 rows in set (0.00 sec)

#更改完成后使用新密码登录zabbix验证。