一、監控概述
1.1、為什么要監控
監控是整個運維乃至整個產品生命周期中重要的一環,事前及時預警發現故障,事后提供詳實的數據用於追查定位問題。
- 對系統不間斷實時監控
- 實時反饋系統當前狀態
- 保證服務可靠性安全性
- 保證業務持續穩定運⾏
1.2、如何進行監控
# ⽐如我們需要監控磁盤的使⽤率
1.如何查看磁盤使⽤率 df -h
2.監控磁盤的那些指標 block、inode
3.如何獲取具體的信息 df -h|awk '/\/$/{print $(NF-1)}'
4.獲取的數值到達多少報警 80%
1.3、流⾏的監控⼯具
1.cacti、Nagios、Zabbix、
2.Lepus(天兔)數據庫監控系統
3.Open-Falcon ⼩⽶
4.Prometheus(普羅⽶修斯,Docker、K8s)
1.4、如何入手監控
1.硬件監控 路由器、交換機、防⽕牆、服務器
2.系統監控 Windows/Linux CPU、內存、磁盤、⽹絡、進程、TCP
3.服務監控 nginx、php、tomcat、redis、memcache、mysql
4.WEB監控 請求時間、響應時間、加載時間、
5.⽇志監控 ELk(收集、存儲、分析、展示) ⽇志易、阿里雲日志系統
6.安全監控 Firewalld(限制來源的ip)、WAF(Nginx+lua)、安全寶、⽜盾雲、安全狗
7.⽹絡監控 smokeping 多機房、監控寶、全國各地實時延遲
8.業務監控 活動引⼊多少流量、產⽣多少注冊量、帶來多⼤價值
1.5、Linux常用監控命令
1.5.1、CPU監控命令
w、top、htop、glances
# 安裝
yum install epel* -y
yum install glances htop -y
1.5.2、內存監控命令
free -m
1.5.3、磁盤監控命令
df iotop iostat
# 安裝
yum install iotop sysstat -y
1.5.4、網絡監控命令
ifconfig、route、glances、iftop、nethogs、netstat
# 安裝
yum install -y netstat
iftop
中間的<= =>這兩個左右箭頭,表示的是流量的⽅向。
TX:發送流量、RX:接收流量、TOTAL:總流量
# 查看TCP11中狀態
netstat -an|grep ESTABLISHED
netstat -rn #查看路由信息
netstat -lntup
1.6、Shell腳本進行簡單監控
需求: 每隔1分鍾監控⼀次內存,當你的可⽤內存低於100m,發郵件報警,要求顯示剩余內存
1.怎么獲取內存可⽤的值 free -m|awk '/^Mem/{print $NF}'
2.獲取到內存可⽤的值如何和設定的閾值進⾏⽐較
3.⽐較如果⼤於100m則不處理,如果⼩於100則報警
4.如何每隔1分鍾執⾏⼀次
[root@zabbix-server ~]# cat free.sh
#!/usr/bin/bash
HostName=$(hostname)_$(hostname -i)
Date=$(date +%F)
while true;do
Free=$(free -m|awk '/^Mem/{print $NF}')
if [ $Free -le 100 ];then
echo "$Date: $HostName Mem Is < ${Free}MB"
fi
sleep 5
done
二、Zabbix概述及安裝
2.1、Zabbix概述
2.2、Zabbix基礎架構
2.3、Zabbix Server安裝
zabbix安裝步驟:https://www.zabbix.com/download
1)配置zabbix倉庫
[root@zabbix-server ~]# rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
[root@zabbix-server ~]# yum clean all
2)安裝Zabbix server 和 agent
[root@zabbix-server ~]# yum install zabbix-server-mysql zabbix-agent -y
3)安裝zabbix前端
# 1、安裝centos-release-scl
[root@zabbix-server ~]# yum install centos-release-scl -y
# 2、配置/etc/yum.repos.d/zabbix.repo
[root@zabbix-server ~]# vim /etc/yum.repos.d/zabbix.repo
[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1 #配置zabbix-frontend生效
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
# 3、安裝zabbix前端包
[root@zabbix-server ~]# yum install zabbix-web-mysql-scl zabbix-nginx-conf-scl -y
4)安裝mysql並初始化
# 安裝mysql5.7
[root@zabbix-server ~]# cd /usr/local/src/
[root@zabbix-server src]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
[root@zabbix-server src]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm
[root@zabbix-server src]# yum -y install mysql-server
# 啟動mysql並查看初始化密碼
[root@zabbix-server src]# systemctl start mysqld
[root@zabbix-server src]# systemctl enable mysqld
[root@zabbix-server src]# netstat -lntp|grep 3306
tcp6 0 0 :::3306 :::* LISTEN 2045/mysqld
[root@zabbix-server src]# grep 'temporary password' /var/log/mysqld.log
2021-02-13T02:27:31.983845Z 1 [Note] A temporary password is generated for root@localhost: .&Ewqebl*31,
# 登錄mysql並修改密碼
[root@zabbix-server src]# mysql -uroot -p'.&Ewqebl*31,'
mysql> set password for root@localhost = password('Root123@@'); #密碼需要盡可能復雜
# 初始化mysql
mysql> create database zabbix character set utf8 collate utf8_bin;
mysql> create user zabbix@localhost identified by 'Zabbix123@@';
mysql> grant all privileges on zabbix.* to zabbix@localhost;
mysql> quit
# 導入zabbix初始數據(166張表)
[root@zabbix-server src]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
5)配置Zabbix server連接數據庫
[root@zabbix-server src]# vim /etc/zabbix/zabbix_server.conf
DBPassword=Zabbix123@@
6)zabbix前端配置
# 打開注釋並配置域名
[root@zabbix-server src]# vim /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf
listen 80;
server_name dianchou.com;
#配置php-fpm
[root@zabbix-server src]# vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
listen.acl_users = apache,nginx #添加nginx用戶
php_value[date.timezone] = Asia/Shanghai #修改時區
# 配置nginx,注釋默認配置
[root@zabbix-server src]# vim /etc/opt/rh/rh-nginx116/nginx/nginx.conf
# server {
# listen 80 default_server;
# listen [::]:80 default_server;
# server_name _;
# root /opt/rh/rh-nginx116/root/usr/share/nginx/html;
....
7)啟動zabbix server及agent
[root@zabbix-server src]# systemctl restart zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
[root@zabbix-server src]# systemctl enable zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm
8)訪問zabbix並配置
瀏覽器訪問:http://ip或http😕/域名
查看配置的信息:
[root@zabbix-server src]# cat /etc/zabbix/web/zabbix.conf.php
<?php
// Zabbix GUI configuration file.
$DB['TYPE'] = 'MYSQL';
$DB['SERVER'] = 'localhost';
$DB['PORT'] = '0';
$DB['DATABASE'] = 'zabbix';
$DB['USER'] = 'zabbix';
$DB['PASSWORD'] = 'Zabbix123@@';
// Schema name. Used for PostgreSQL.
$DB['SCHEMA'] = '';
// Used for TLS connection.
$DB['ENCRYPTION'] = false;
$DB['KEY_FILE'] = '';
$DB['CERT_FILE'] = '';
$DB['CA_FILE'] = '';
$DB['VERIFY_HOST'] = false;
$DB['CIPHER_LIST'] = '';
// Use IEEE754 compatible value range for 64-bit Numeric (float) history values.
// This option is enabled by default for new Zabbix installations.
// For upgraded installations, please read database upgrade notes before enabling this option.
$DB['DOUBLE_IEEE754'] = true;
$ZBX_SERVER = 'localhost';
$ZBX_SERVER_PORT = '10051';
$ZBX_SERVER_NAME = '';
$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
// Uncomment this block only if you are using Elasticsearch.
// Elasticsearch url (can be string if same url is used for all types).
//$HISTORY['url'] = [
// 'uint' => 'http://localhost:9200',
// 'text' => 'http://localhost:9200'
//];
// Value types stored in Elasticsearch.
//$HISTORY['types'] = ['uint', 'text'];
// Used for SAML authentication.
// Uncomment to override the default paths to SP private key, SP and IdP X.509 certificates, and to set extra settings.
//$SSO['SP_KEY'] = 'conf/certs/sp.key';
//$SSO['SP_CERT'] = 'conf/certs/sp.crt';
//$SSO['IDP_CERT'] = 'conf/certs/idp.crt';
//$SSO['SETTINGS'] = [];
9)登錄zabbix
默認的賬號及密碼: 用戶名:Admin,密碼:zabbix
10)修改默認密碼並設置語言環境
User setting =>User => Change password
2.4、Zabbix Agent安裝
准備一台服務器作為zabbix agent端
1)安裝zabbix agent
[root@web01 ~]# rpm -ivh https://mirror.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.1-1.el7.x86_64.rpm
[root@web01 ~]# rpm -ql zabbix-agent
/etc/logrotate.d/zabbix-agent
/etc/zabbix/zabbix_agentd.conf
/etc/zabbix/zabbix_agentd.d
/usr/lib/systemd/system/zabbix-agent.service
/usr/lib/tmpfiles.d/zabbix-agent.conf
/usr/sbin/zabbix_agentd
/usr/share/doc/zabbix-agent-5.0.1
/usr/share/doc/zabbix-agent-5.0.1/AUTHORS
/usr/share/doc/zabbix-agent-5.0.1/COPYING
/usr/share/doc/zabbix-agent-5.0.1/ChangeLog
/usr/share/doc/zabbix-agent-5.0.1/NEWS
/usr/share/doc/zabbix-agent-5.0.1/README
/usr/share/doc/zabbix-agent-5.0.1/userparameter_mysql.conf
/usr/share/man/man8/zabbix_agentd.8.gz
/var/log/zabbix
/var/run/zabbix
2)配置 Zabbix-Agent 指向 Zabbix-Server
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=10.0.0.101
3)啟動 Zabbix-Agent , 默認監聽在 10050 端⼝
[root@web01 ~]# systemctl start zabbix-agent
[root@web01 ~]# systemctl enable zabbix-agent
[root@web01 ~]# 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:22 0.0.0.0:* LISTEN 918/sshd
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 1054/zabbix_agentd
tcp6 0 0 :::22 :::* LISTEN 918/sshd
tcp6 0 0 :::10050 :::* LISTEN 1054/zabbix_agentd
4)配置ZabbixWeb⻚⾯,點擊配置->選擇主機->創建主機
5)點擊模板->選擇連接指示器->選擇->搜索Linux->點擊⼩按鈕添加->最后添加
6)等待幾秒,查看添加的監控主機
2.5、中文亂碼問題解決
解決方法一:
1)先搜索zabbix-web包對應字符存放的目錄
[root@zabbix-server ~]# rpm -ql zabbix-web|grep fonts
/usr/share/zabbix/assets/fonts
2)進入對應的字體目錄,發現字體是一個軟連接
[root@zabbix-server ~]# cd /usr/share/zabbix/assets/fonts
[root@zabbix-server fonts]# ll
total 0
lrwxrwxrwx 1 root root 33 Feb 13 10:22 graphfont.ttf -> /etc/alternatives/zabbix-web-font
3)進入軟連接對應的目錄,發現還是軟連接
[root@zabbix-server fonts]# cd /etc/alternatives
[root@zabbix-server alternatives]# ll
total 0
lrwxrwxrwx. 1 root root 15 Aug 9 2018 ld -> /usr/bin/ld.bfd
lrwxrwxrwx. 1 root root 34 Aug 9 2018 libnssckbi.so.x86_64 -> /usr/lib64/pkcs11/p11-kit-trust.so
lrwxrwxrwx 1 root root 26 Feb 13 10:25 mta -> /usr/sbin/sendmail.postfix
lrwxrwxrwx 1 root root 40 Feb 13 10:25 mta-aliasesman -> /usr/share/man/man5/aliases.postfix.5.gz
lrwxrwxrwx 1 root root 22 Feb 13 10:25 mta-mailq -> /usr/bin/mailq.postfix
lrwxrwxrwx 1 root root 38 Feb 13 10:25 mta-mailqman -> /usr/share/man/man1/mailq.postfix.1.gz
lrwxrwxrwx 1 root root 27 Feb 13 10:25 mta-newaliases -> /usr/bin/newaliases.postfix
lrwxrwxrwx 1 root root 43 Feb 13 10:25 mta-newaliasesman -> /usr/share/man/man1/newaliases.postfix.1.gz
lrwxrwxrwx 1 root root 23 Feb 13 10:25 mta-pam -> /etc/pam.d/smtp.postfix
lrwxrwxrwx 1 root root 22 Feb 13 10:25 mta-rmail -> /usr/bin/rmail.postfix
lrwxrwxrwx 1 root root 25 Feb 13 10:25 mta-sendmail -> /usr/lib/sendmail.postfix
lrwxrwxrwx 1 root root 41 Feb 13 10:25 mta-sendmailman -> /usr/share/man/man1/sendmail.postfix.1.gz
lrwxrwxrwx 1 root root 29 Feb 13 10:16 zabbix-server -> /usr/sbin/zabbix_server_mysql
lrwxrwxrwx 1 root root 38 Feb 13 10:22 zabbix-web-font -> /usr/share/fonts/dejavu/DejaVuSans.ttf
4)進入最終字體存放的目錄,上傳准備好的字體(可以通過windows電腦獲取),改名為DejaVuSans.ttf即可
解決方法二:
1)在zabbix-server服務器上安裝如下文件符集
[root@zabbix-server ~]# yum install wqy-microhei-fonts -y
2)替換linux上默認的字符集
[root@zabbix-server ~]# cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/fonts/dejavu/DejaVuSans.ttf
3)重啟zabbix-server,查看亂碼是否解決
[root@zabbix-server ~]# systemctl restart zabbix-server