1. 程式人生 > >2018-05-24 Linux學習

2018-05-24 Linux學習

Linux學習

19.1 Linux監控平臺介紹

常見開源監控軟件

cacti、nagios、zabbix、smokeping、open-falcon等等
cacti、smokeping偏向於基礎監控,成圖非常漂亮
cacti、nagios、zabbix服務端監控中心,需要php環境支持,其中zabbix和cacti都需要mysql作為數據存儲,nagios不用存儲歷史數據,註重服務或者監控項的狀態,zabbix會獲取服務或者監控項目的數據,會把數據記錄到數據庫裏,從而可以成圖
open-falcon為小米公司開發,開源後受到諸多大公司和運維工程師的追捧,適合大企業,滴滴、360、新浪微博、京東等大公司在使用這款監控軟件,值得研究
後續以介紹zabbix為主

19.2 zabbix監控介紹

C/S架構,基於C++開發,監控中心支持web界面配置和管理
單server節點可以支持上萬臺客戶端
最新版本3.4,官方文檔https://www.zabbix.com/manuals

5個組件
zabbix-server 監控中心,接收客戶端上報信息,負責配置、統計、操作數據
數據存儲 存放數據,比如mysql
web界面 也叫web UI,在web界面下操作配置是zabbix簡單易用的主要原因
zabbix-proxy 可選組件,它可以代替zabbix-server的功能,減輕server的壓力
zabbix-agent 客戶端軟件,負責采集各個監控服務或項目的數據,並上報

19.3-6 安裝zabbix

安裝zabbix

官網下載地址 www.zabbix.com/download
wget repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
rpm -ivh zabbix-release-3.2-1.el7.noarch.rpm
yum install -y zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql
會連帶安裝httpd和php
如果mysql之前沒有安裝的話,需要根據lamp那一章的mysql安裝方法安裝mysql

vim /etc/my.cnf //需要增加配置
character_set_server = utf8
重啟mysqld服務後,進入mysql命令行,創建zabbix庫
create database zabbix character set utf8;
再創建用戶
grant all on zabbix.* to ‘zabbix‘@‘127.0.0.1‘ identified by ‘aming-zabbix‘;
導入數據
cd /usr/share/doc/zabbix-server-mysql-3.2.7
gzip -d create.sql.gz
mysql -uroot -pxxx zabbix < create.sql

systemctl start httpd; systemctl enable httpd
vim /etc/zabbix/zabbix_server.conf //修改或增加
DBHost=127.0.0.1 //在DBName=zabbix上面增加
DBPassword=aming-zabbix //在DBuser下面增加
systemctl start zabbix-server
systemctl enable zabbix-server
netstat -lntp |grep zabbix //查看監聽端口
瀏覽器訪問 http://ip/zabbix/ w eb界面下面配置zabbix
用戶名Admin 密碼zabbix
進入後臺第一件事情就是修改密碼

忘記Admin密碼如何做

進入mysql命令行,選擇zabbix庫
mysql -uroot -p zabbix
update users set passwd=md5(‘newpasswd’) where alias=‘Admin’;
這樣就更改了Admin用戶的密碼

Zabbix客戶端安裝

在客戶端上也需要下載zabbix的yum源
wget repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
rpm -ivh zabbix-release-3.2-1.el7.noarch.rpm
yum install -y zabbix-agent
vim /etc/zabbix/zabbix_agentd.conf //修改如下配置
Server=127.0.0.1 修改為 Server=192.168.133.130 //定義服務端的ip(被動模式)
ServerActive=127.0.0.1 修改為 ServerActive=192.168.133.130 //定義服務端的ip(主動模式)
Hostname=Zabbix server 修改為 Hostname=aming-123 //這是自定義的主機名,一會還需要在web界面下設置同樣的主機名
systemctl start zabbix-agent
systemctl enable zabbix-agent

操作過程

linux-01 服務端, linux-02 客戶端

服務端安裝

[root@linux-01 ~]# wget http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm

[root@linux-01 ~]# rpm -ivh zabbix-release-3.4-2.el7.noarch.rpm 
[root@linux-01 ~]# yum install -y zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql

啟動 MySQL

[root@linux-01 ~]# systemctl start mysqld
[root@linux-01 ~]# ps aux|grep mysql

[root@linux-01 ~]# vim /etc/my.cnf
添加最後一行
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=160
log_bin=aminglinux01
character_set_server = utf8

配置 MySQL

[root@linux-01 ~]# mysql -uroot -paminglinux
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 ‘aming-zabbix‘;
Query OK, 0 rows affected (0.00 sec)

[root@linux-01 ~]# cd /usr/share/doc/zabbix-server-mysql-3.4.8/
[root@linux-01 zabbix-server-mysql-3.4.8]# ls
AUTHORS  ChangeLog  COPYING  create.sql.gz  NEWS  README
[root@linux-01 zabbix-server-mysql-3.4.8]# gzip -d create.sql.gz 
[root@linux-01 zabbix-server-mysql-3.4.8]# ls
AUTHORS  ChangeLog  COPYING  create.sql  NEWS  README
[root@linux-01 zabbix-server-mysql-3.4.8]# mysql -uroot -paminglinux zabbix < create.sql 
Warning: Using a password on the command line interface can be insecure.

啟動 zabbix

[root@linux-01 ~]# systemctl start zabbix-server
Job for zabbix-server.service failed because a configured resource limit was exceeded. See "systemctl status zabbix-server.service" and "journalctl -xe" for details.
[root@linux-01 ~]# getenforce 
Enforcing
[root@linux-01 ~]# setenforce 0
[root@linux-01 ~]# getenforce 
Permissive

[root@linux-01 ~]# systemctl start zabbix-server
[root@linux-01 ~]# systemctl start httpd

開機啟動

[root@linux-01 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

[root@linux-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.

zabbix啟動但沒有監聽端口,修改配置

[root@linux-01 ~]# ps aux |grep zabbix
zabbix    11841  0.0  0.1 259384  3368 ?        S    04:00   0:00 /usr/sbin/zabbix_server -c /etc/zabbix/zabbix_server.conf
root      11990  0.0  0.0 112676   980 pts/0    R+   04:08   0:00 grep --color=auto zabbix

[root@linux-01 ~]# netstat -lnpt
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      1041/rpc.mountd     
tcp        0      0 0.0.0.0:39029           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      978/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1323/master         
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:48259           0.0.0.0:*               LISTEN      980/rpc.statd       
tcp6       0      0 :::3306                 :::*                    LISTEN      11706/mysqld        
tcp6       0      0 :::41514                :::*                    LISTEN      -                   
tcp6       0      0 :::43981                :::*                    LISTEN      980/rpc.statd       
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::80                   :::*                    LISTEN      11888/httpd         
tcp6       0      0 :::20048                :::*                    LISTEN      1041/rpc.mountd     
tcp6       0      0 :::22                   :::*                    LISTEN      978/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1323/master         
tcp6       0      0 :::2049                 :::*                    LISTEN      -   

[root@linux-01 ~]# vim /etc/zabbix/zabbix_server.conf
添加以下 第2第4行

DBHost=localhost

DBHost=127.0.0.1

DBUser=zabbix
DBPassword=aming-zabbix

[root@linux-01 ~]# netstat -lnpt
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      1041/rpc.mountd     
tcp        0      0 0.0.0.0:39029           0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      978/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1323/master         
tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      12062/zabbix_server 
tcp        0      0 0.0.0.0:48259           0.0.0.0:*               LISTEN      980/rpc.statd       
tcp6       0      0 :::3306                 :::*                    LISTEN      11706/mysqld        
tcp6       0      0 :::41514                :::*                    LISTEN      -                   
tcp6       0      0 :::43981                :::*                    LISTEN      980/rpc.statd       
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::80                   :::*                    LISTEN      11888/httpd         
tcp6       0      0 :::20048                :::*                    LISTEN      1041/rpc.mountd     
tcp6       0      0 :::22                   :::*                    LISTEN      978/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1323/master         
tcp6       0      0 :::2049                 :::*                    LISTEN      -                   
tcp6       0      0 :::10051                :::*                    LISTEN      12062/zabbix_server 

[root@linux-01 ~]# ps aux |grep zabbix
zabbix    12062  0.5  0.2 259488  4200 ?        S    04:17   0:00 /usr/sbin/zabbix_server -c /etc/zabbix/zabbix_server.conf
zabbix    12065  0.0  0.1 259488  2528 ?        S    04:17   0:00 /usr/sbin/zabbix_server: configuration syncer [waiting 60 sec for processes]
zabbix    12066  0.0  0.1 259488  2532 ?        S    04:17   0:00 /usr/sbin/zabbix_server: alerter #1 started
zabbix    12067  0.0  0.1 259488  2532 ?        S    04:17   0:00 /usr/sbin/zabbix_server: alerter #2 started
zabbix    12068  0.0  0.1 259488  2532 ?        S    04:17   0:00 /usr/sbin/zabbix_server: alerter #3 started
zabbix    12069  0.0  0.1 259488  2528 ?        S    04:17   0:00 /usr/sbin/zabbix_server: housekeeper [startup idle for 30 minutes]
zabbix    12070  0.0  0.1 259560  2984 ?        S    04:17   0:00 /usr/sbin/zabbix_server: timer #1 [processed 0 triggers, 0 events in 0.000090 sec, 0 maintenances in 0.000000 sec, idle 30 sec]
zabbix    12071  0.0  0.1 259488  2908 ?        S    04:17   0:00 /usr/sbin/zabbix_server: http poller #1 [got 0 values in 0.000733 sec, idle 5 sec]
zabbix    12072  0.1  0.2 363848  5096 ?        S    04:17   0:00 /usr/sbin/zabbix_server: discoverer #1 [processed 0 rules in 0.001027 sec, idle 60 sec]
zabbix    12073  0.0  0.1 259488  2884 ?        S    04:17   0:00 /usr/sbin/zabbix_server: history syncer #1 [synced 0 items in 0.000001 sec, idle 1 sec]
zabbix    12074  0.0  0.1 259488  2884 ?        S    04:17   0:00 /usr/sbin/zabbix_server: history syncer #2 [synced 0 items in 0.000001 sec, idle 1 sec]
zabbix    12075  0.0  0.1 259488  2884 ?        S    04:17   0:00 /usr/sbin/zabbix_server: history syncer #3 [synced 0 items in 0.000001 sec, idle 1 sec]
zabbix    12076  0.0  0.1 259488  2884 ?        S    04:17   0:00 /usr/sbin/zabbix_server: history syncer #4 [synced 0 items in 0.000001 sec, idle 1 sec]
zabbix    12081  0.0  0.2 259488  3848 ?        S    04:17   0:00 /usr/sbin/zabbix_server: escalator #1 [processed 0 escalations in 0.000989 sec, idle 3 sec]
zabbix    12082  0.0  0.2 259488  3840 ?        S    04:17   0:00 /usr/sbin/zabbix_server: proxy poller #1 [exchanged data with 0 proxies in 0.000003 sec, idle 5 sec]
zabbix    12083  0.0  0.1 259488  2652 ?        S    04:17   0:00 /usr/sbin/zabbix_server: self-monitoring [processed data in 0.000007 sec, idle 1 sec]
zabbix    12084  0.0  0.1 259488  2888 ?        S    04:17   0:00 /usr/sbin/zabbix_server: task manager [processed 0 task(s) in 0.000685 sec, idle 5 sec]
zabbix    12085  0.1  0.2 366460  5268 ?        S    04:17   0:00 /usr/sbin/zabbix_server: poller #1 [got 0 values in 0.000004 sec, idle 5 sec]
zabbix    12089  0.1  0.2 366460  5268 ?        S    04:17   0:00 /usr/sbin/zabbix_server: poller #2 [got 0 values in 0.000004 sec, idle 5 sec]
zabbix    12090  0.1  0.2 366460  5268 ?        S    04:17   0:00 /usr/sbin/zabbix_server: poller #3 [got 0 values in 0.000006 sec, idle 5 sec]
zabbix    12091  0.1  0.2 366460  5268 ?        S    04:17   0:00 /usr/sbin/zabbix_server: poller #4 [got 0 values in 0.000004 sec, idle 5 sec]
zabbix    12092  0.1  0.2 366460  5268 ?        S    04:17   0:00 /usr/sbin/zabbix_server: poller #5 [got 0 values in 0.000005 sec, idle 5 sec]
zabbix    12093  0.1  0.2 366460  5268 ?        S    04:17   0:00 /usr/sbin/zabbix_server: unreachable poller #1 [got 0 values in 0.000005 sec, idle 5 sec]
zabbix    12094  0.0  0.1 259488  3620 ?        S    04:17   0:00 /usr/sbin/zabbix_server: trapper #1 [processed data in 0.000000 sec, waiting for connection]
zabbix    12095  0.0  0.1 259488  3620 ?        S    04:17   0:00 /usr/sbin/zabbix_server: trapper #2 [processed data in 0.000000 sec, waiting for connection]
zabbix    12096  0.0  0.1 259488  3620 ?        S    04:17   0:00 /usr/sbin/zabbix_server: trapper #3 [processed data in 0.000000 sec, waiting for connection]
zabbix    12100  0.0  0.1 259488  3620 ?        S    04:17   0:00 /usr/sbin/zabbix_server: trapper #4 [processed data in 0.000000 sec, waiting for connection]
zabbix    12101  0.0  0.1 259488  3620 ?        S    04:17   0:00 /usr/sbin/zabbix_server: trapper #5 [processed data in 0.000000 sec, waiting for connection]
zabbix    12102  0.0  0.1 262092  2688 ?        S    04:17   0:00 /usr/sbin/zabbix_server: icmp pinger #1 [got 0 values in 0.000005 sec, idle 5 sec]
zabbix    12105  0.0  0.1 259488  3268 ?        S    04:17   0:00 /usr/sbin/zabbix_server: alert manager #1 [sent 0, failed 0 alerts, idle 5.013129 sec during 5.013131 sec]
zabbix    12106  0.0  0.1 259488  3024 ?        S    04:17   0:00 /usr/sbin/zabbix_server: preprocessing manager #1 [queued 0, processed 0 values, idle 5.006026 sec during 5.006029 sec]
zabbix    12108  0.0  0.1 259488  2580 ?        S    04:17   0:00 /usr/sbin/zabbix_server: preprocessing worker #1 started
zabbix    12109  0.0  0.1 259488  2580 ?        S    04:17   0:00 /usr/sbin/zabbix_server: preprocessing worker #2 started
zabbix    12110  0.0  0.1 259488  2580 ?        S    04:17   0:00 /usr/sbin/zabbix_server: preprocessing worker #3 started
root      12120  0.0  0.0 112676   976 pts/0    R+   04:17   0:00 grep --color=auto zabbix

web配置zabbix

[root@linux-01 ~]# systemctl stop firewalld

在瀏覽器中打開 http://192.168.106.160/zabbix  設置zabbix

[root@linux-01 ~]# vim /etc/php.ini
date.timezone = Asia/Shanghai

[root@linux-01 ~]# systemctl restart httpd

界面 Configure DB connection
    Database host:127.0.0.1
    Database port:0
    Database name:zabbix
    User:zabbix
    Password:aming-zabbix

界面 Zabbix server details 隨意設置名字
    Name : linux-01

登陸界面  默認
    用戶名:Admin
    秘密: zabbix

Administration---Users---Admin,進去後修改密碼和語言(aminglinux)---update---Apply

忘記Admin密碼如何做

[root@linux-01 ~]# mysql -uroot -paminglinux

mysql> use zabbix;
mysql> show tables;
mysql> desc users;
mysql> update users set passwd=md5(‘aming‘) where alias=‘Admin‘;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from users;

使用新密碼登陸zabbix

客戶端安裝

[root@linux-02 ~]# wget http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm

[root@linux-02 ~]# rpm -ivh zabbix-release-3.4-2.el7.noarch.rpm 
[root@linux-02 ~]# yum install -y zabbix-agent zabbix-get zabbix-server-mysql zabbix-web zabbix-web-mysql

[root@linux-02 ~]# vim /etc/zabbix/zabbix_agentd.conf
設置服務端IP
Server=192.168.106.160
ServerActive=192.168.106.160
Hostname=linux-02

客戶端啟動

[root@linux-02 ~]# systemctl start zabbix-agent

[root@linux-02 ~]# ps aux|grep zabbix
zabbix     1392  0.0  0.0  82740  1264 ?        S    22:47   0:00 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
zabbix     1393  0.0  0.0  82740  1296 ?        S    22:47   0:00 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
zabbix     1394  0.0  0.0  82740  1852 ?        S    22:47   0:00 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
zabbix     1395  0.0  0.0  82740  1852 ?        S    22:47   0:00 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
zabbix     1396  0.0  0.0  82740  1852 ?        S    22:47   0:00 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]
zabbix     1397  0.0  0.1  82872  2200 ?        S    22:47   0:00 /usr/sbin/zabbix_agentd: active checks #1 [idle 1 sec]
root       1400  0.0  0.0 112676   984 pts/0    R+   22:47   0:00 grep --color=auto zabbix
[root@linux-02 ~]# netstat -lnpt
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:22              0.0.0.0:*               LISTEN      937/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1179/master         
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      1392/zabbix_agentd  
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      937/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1179/master         
tcp6       0      0 :::10050                :::*                    LISTEN      1392/zabbix_agentd 

2018-05-24 Linux學習