2. 常见的Linux监控命令
top:显示进程信息。
htop:Linux系统中的一个互动的进程查看器。
uptime:
第一项是当前时间,up 表示系统正在运行,5:53 是系统启动的总时间,最后是系统的负载load信息。
free:查看内存。
vmstat:是Virtual Meomory Statistics(虚拟内存统计)的缩写,可对操作系统的虚拟内存、进程、CPU活动进行监控
iostat:将对系统的磁盘操作活动进行监视。它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况
df:显示磁盘分区上可以使用的磁盘空间
iftop:针对IP地址进行监控
nethogs:针对进程号来进行监控
3. 使用shell脚本来监控服务器
#!/bin/bash
MEM=`free -m|awk 'NR==2{print $NF}'`
if [ $MEM -lt 100 ];then
echo "web服务器 $ip地址 可用内存不足,当前可用内存
$MEM" | mail -s "web服务器内存不足" *****@qq.com
fi
缺点:效率低,不能实现集中报警,不可以分析历史数据
5.zabbix生产环境安装
操作系统:centos 7.6
IP地址:10.0.0.71
5.1.安装php运行环境
要求php版本 7.2以上+
安装php第三方源:
yum install epel-release -y
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install php72w-fpm php72w-gd.x86_64 php72w-bcmath.x86_64 php72w-xml.x86_64 php72w-mbstring.x86_64 php72w-ldap.x86_64 php72w-mysqlnd.x86_64 -y
安装NGINX:
yum install nginx -y
配置php-fpm和nginx:
vim /etc/php-fpm.d/www.conf
user = nginx
group = nginx
vim /etc/nginx/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/html$fastcgi_script_name;
include fastcgi_params;
}
}
}
启动NGINX和PHP-FPM
mkdir /html
nginx -t
systemctl start nginx
systemctl enable nginx
systemctl start php-fpm
systemctl enable php-fpm
准备zabbix-web的php代码
wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.8.tar.gz
tar xf zabbix-5.0.8.tar.gz
cd zabbix-5.0.8/ui/
cp -a * /html/
chown -R nginx:nginx /html
#解决首次访问 zabbix-web安装界面 error 500的错误 (查看报错解决相应的问题)
mkdir /var/lib/php/session
chown -R nginx:nginx /var/lib/php/session/
vim /etc/php.ini
max_execution_time = 300
max_input_time = 300
post_max_size = 16M
date.timezone = Asia/Shanghai
systemctl restart php-fpm.service
然后下一步进行
5.2.安装数据库
#参数链接https://www.jianshu.com/p/dd7137c4efa5
#创库授权
create database zabbix character set utf8 collate utf8_bin;
create user 'zabbix'@'localhost' identified by '123456';
grant all privileges on zabbix.* to 'zabbix'@'localhost';
5.3:安装zabbix-server
配置zabbix 5.0的源
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i 's#http://repo.zabbix.com#https://mirrors.tuna.tsinghua.edu.cn/zabbix#g' /etc/yum.repos.d/zabbix.repo
安装zabbix-server
yum install zabbix-server-mysql -y
导入zabbix初始数据`
zcat /usr/share/doc/zabbix-server-mysql-*/create.sql.gz|mysql -uzabbix -p123456 zabbix
配置zabbix-server
vim /etc/zabbix/zabbix_server.conf
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=123456
DBSocket=/tmp/mysql.sock
启动zabbix-server
systemctl start zabbix-server.service
systemctl enable zabbix-server.service
netstat -lntup|grep 10051
vim /etc/php.ini
pdo_mysql.default_socket= /tmp/mysql.sock
mysqli.default_socket = /tmp/mysql.sock
systemctl restart php-fpm
6∶监控一台服务器主机
6.1 监控zabbix-server (自己监控自己)
yum install zabbix-agent -y
systemctl start zabbix-agent.service
systemctl enable zabbix-agent.service
6.2 监控其他linux主机
添加监控前准备
主机IP地址:10.0.0.8
#安装
rpm -ivh
https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/
rhel/7/x86_64/zabbix-agent2-5.0.8-1.el7.x86_64.rpm
#配置
vim /etc/zabbix/zabbix_agent2.conf
Server=10.0.0.71
#启动
systemctl start zabbix-agent2.service
systemctl enable zabbix-agent2.service
然后重启zabbix-server:(10.0.0.71)
systemctl restart zabbix-server