监控概述:报告系统的状态,监控内容包括吞吐量,反应时间,使用率,提前发现问题,精准处理问题,找出系统瓶颈在什么地方。监控数据包括:共有数据和私有数据,共有数据比如监控web,ftp,ssh 数据库等这些服务网,和tcp,udp等端口是否正常,可以使用端口扫描工具,不需要连接本机运行,不需要经过root管理员,私有数据包括cpu,内存,磁盘,网卡流量,用户信息,进程信息,需要登陆服务器。cacti 绘图好,nagios 报警优点,zabbix吸取两者优点是一个高度集成监控解决方案,可以实现企业的开源分布式监控,zabbix通过c/s模式采集监控数据,zabbix通过b/s实现web管理。
常见系统监控命令:
ps aux #查看进程
ifconfig [网卡名可选]
uptime #查看cpu负载,距上次登录时间
netstat|ss #查看端口和服务
free -m #内存
swapon -s #交换分区
traceroute #跟踪路由
df -h #挂载
iostat #磁盘读写性能
sar 1 1 #每秒,1次查看cpu,网络,磁盘
sar -n DEV 1 2 #每秒显示2次 网卡流量
工作流程是:监控服务器可以通过snmp或者agent采集数据,数据可以写入mysql,oracle等数据中,服务器使用lnmp实现web前端的管理。被监控端需要安装agent,常见网络设备一般支持snmp.
先搭建lnmp环境准备:因zabbix的页面是采用php编写的代码,上线到lnmp平台,我们可以通过web页面去管理我们的监控主机查看数据。
部署监控端:设置主机名zabbix,ip地址:192.168.1.100,关闭防火墙·和selinux 被监控客户端:主机名为web1,web2,关闭防火墙,selinux。在监控主机(192.168.1.100)部署lnmp,zabbix平台,数据库zabbix,登录数据库用户zabbix,登录密码zabbix,这里使用源码包安装,下载源码压缩包,并解压,cd切换进来,这里下载了zabbix-3.4.4.tar.gz nginx-1.17.6.tar.gz 部署环境,创建zabbix-lnmp.sh脚本,如下,并且赋予执行权限。
代码可重复执行,如登录http://192.168.1.100/index.php进入页面点击下一步报错显示红色字样,可再一次执行代码,配置port:3306,密码:zabbix,zabbix的用户默认是admin,密码是上面设置zabbix
vim zabbix-lnmp.sh
#!/bin/bash
#定义软件包的变量名
date=`date +%Y%m%d%H%M%S`
zabbix_tar_gz=zabbix-3.4.4.tar.gz
zabbix=zabbix-3.4.4
nginx_tar_gz=nginx-1.17.6.tar.gz
nginx=nginx-1.17.6
#设置防火墙,selinux
firewall-cmd --set-default-zone=trusted
sed -i '/SELINUX/s/enforcing/permissive/' /etc/selinux/config
setenforce 0
#安装lnmp平台及依赖包
echo "[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
Execstop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target" > /usr/lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl stop nginx
yum -y install gcc pcre-devel openssl-devel make
tar -xf $nginx_tar_gz
cd $nginx
./configure --with-http_ssl_module
#安装带有加密模块
[ -d /usr/local/nginx ] || make && make install
[ -d /usr/local/nginx ] && make
yum -y install php php-fpm php-mysql
#配置PHP动态解析
grep 'SCRIPT_FILENAME' /usr/local/nginx/conf/nginx.conf
if [ $? -eq 0 ]
then
sed -i '65,71 s/#//g' /usr/local/nginx/conf/nginx.conf
fi
sed -i '/SCRIPT_FILENAME/d' /usr/local/nginx/conf/nginx.conf
sed -i 's/fastcgi_params/fastcgi.conf/' /usr/local/nginx/conf/nginx.conf
sed -i '/fastcgi_buffers 8 16k/d ; ' /usr/local/nginx/conf/nginx.conf
sed -i '/fastcgi_buffer_size 32k/d ; ' /usr/local/nginx/conf/nginx.conf
sed -i '/fastcgi_connect_timeout 300/d ; ' /usr/local/nginx/conf/nginx.conf
sed -i '/fastcgi_send_timeout 300/d ; ' /usr/local/nginx/conf/nginx.conf
sed -i '/fastcgi_read_timeout 300/d ; ' /usr/local/nginx/conf/nginx.conf
sed -i '/http {/ a fastcgi_buffers 8 16k ; ' /usr/local/nginx/conf/nginx.conf
sed -i '/http {/ a fastcgi_buffer_size 32k ;' /usr/local/nginx/conf/nginx.conf
sed -i '/http {/ a fastcgi_connect_timeout 300 ; ' /usr/local/nginx/conf/nginx.conf
sed -i '/http {/ a fastcgi_send_timeout 300 ; ' /usr/local/nginx/conf/nginx.conf
sed -i '/http {/ a fastcgi_read_timeout 300 ; ' /usr/local/nginx/conf/nginx.conf
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.$date && cp objs/nginx /usr/local/nginx/sbin/ && make upgrade
yum -y install php php-fpm php-mysql
systemctl restart php-fpm
systemctl enable php-fpm
systemctl restart nginx
systemctl enable nginx
useradd nginx
/usr/local/nginx/sbin/nginx
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
chmod +x /etc/rc.local
#配置数据库默认数据库zabbix,用户密码分别是zabbix -----------------
yum -y install mariadb mariadb-server mariadb-devel
systemctl start mariadb
systemctl enable mariadb
echo "create database zabbix character set utf8;" | mysql
echo "grant all on zabbix.* to zabbix@'localhost' identified by 'zabbix'; " | mysql
cd -
tar -xf $zabbix_tar_gz
cd $zabbix/database/mysql/
mysql -uzabbix -pzabbix zabbix < schema.sql
mysql -uzabbix -pzabbix zabbix < images.sql
mysql -uzabbix -pzabbix zabbix < data.sql
#----------------------------------------------------------------
#安装zabbix依赖包
yum -y install php-gd php-xml php-ldap php-bcmath php-mbstring
yum -y install net-snmp-devel curl-devel libevent-devel
cd -
cd $zabbix/
./configure --enable-server --enable-proxy --enable-agent --with-mysql=/usr/bin/mysql_config --with-net-snmp --with-libcurl
make install
#配置zabbix注册条件
sed -i '/date.timezone = Asia\/shanghai/d ' /etc/php.ini
sed -i '/max_execution/s/300/30/' /etc/php.ini
sed -i '/max_input_time/s/300/60/' /etc/php.ini
sed -i '/post_max_size/s/32M/8M/' /etc/php.ini
sed -i '/;date.timezone =/a date.timezone = Asia/shanghai ' /etc/php.ini
sed -i '/max_execution/s/30/300/' /etc/php.ini
sed -i '/max_input_time/s/60/300/' /etc/php.ini
sed -i '/post_max_size/s/8M/32M/' /etc/php.ini
#设置zabbix登录数据库信息默认本机登录,用户,密码是zabix
sed -i '/^DBHost=localhost/d ' /usr/local/etc/zabbix_server.conf
sed -i '/^DBPassword=zabbix/d ' /usr/local/etc/zabbix_server.conf
sed -i '85i DBHost=localhost' /usr/local/etc/zabbix_server.conf
sed -i '/DBName=zabbix/s/zabbix/zabbix/' /usr/local/etc/zabbix_server.conf
sed -i '119i DBPassword=zabbix' /usr/local/etc/zabbix_server.conf
sed -i '/DBUser=zabbix/s/zabbix/zabbix/' /usr/local/etc/zabbix_server.conf
useradd zabbix
#配置zabbix-nginx服务网页安装包里边网页拷贝到nginx/html下设置权限
cd -
cd $zabbix/frontends/php/
cp -r * /usr/local/nginx/html/
chmod -R 777 /usr/local/nginx/html/
#开启服务
zabbix_server
zabbix_agentd
echo zabbix_server >> /etc/rc.local
echo zabbix_agentd >> /etc/rc.local
chmod +x /etc/rc.local
#!/bin/bash
zabbix=zabbix-3.4.4
yum -y install gcc pcre-devel autoconf
cd $zabbix/
./configure --enable-agent
make install
sed -i '/Server=127.0.0.1$/a Server=127.0.0.1,192.168.1.100' /usr/local/etc/zabbix_agentd.conf
sed -i '/Server=127.0.0.1$/d ' /usr/local/etc/zabbix_agentd.conf
sed -i '/ServerActive=127.0.0.1$/a ServerActive=192.168.1.100:10051 ' /usr/local/etc/zabbix_agentd.conf
sed -i '/ServerActive=127.0.0.1$/d ' /usr/local/etc/zabbix_agentd.conf
useradd zabbix
zabbix_agentd
echo zabbix_agentd >> /etc/rc.local
chmod +x /etc/rc.local
在被监控主机下载zabbix-3.4.4.tar.gz , 在当前目录创建agent.sh脚本,如上,赋予执行权限,自动配置安装依赖包,配置/usr/local/etc/zabbix_agentd.conf,启服务,配置完毕。浏览器登录监控端ip/index.php,完成初始化,登录用户默认为admin,密码设置了zabbix.
自定义监控key:在被监控主机上操作
#!/bin/bash
#name监控文件名key
name="count.line.passwd"
#com-line监控项命令行
com_line="sed -n '$=' /etc/passwd"
#mon_item监控文件key参数
mon_item="UserParameter=$name"
sed -i '/^UnsafeUserParameters/d' /usr/local/etc/zabbix_agentd.conf
sed -i '/UnsafeUserParameters=0/a UnsafeUserParameters=1 ' /usr/local/etc/zabbix_agentd.conf
sed -i '/^Include=\/usr\/local\/etc\/zabbix_agentd.conf.d\//d' /usr/local/etc/zabbix_agentd.conf
sed -i '265a Include=/usr/local/etc/zabbix_agentd.conf.d/' /usr/local/etc/zabbix_agentd.conf
echo "$mon_item,$com_line" > /usr/local/etc/zabbix_agentd.conf.d/$name
#echo "UserParameter=count.line.passwd,sed -n '$=' /etc/passwd" > /usr/local/etc/zabbix_agentd.conf.d/count.line.passwd
#自动式监控-------------
#sed -i '/# StartAgents/a StartAgents=0' /usr/local/etc/zabbix_agentd.conf
#sed -i '/^Server=127.0.0.1/d' /usr/local/etc/zabbix_agentd.conf
#sed -i '/Hostname=zabbix server/s/zabbix server/web1/' /usr/local/etc/zabbix_agentd.conf
#sed -i '/RefreshActiveChecks=120/s/#//' /usr/local/etc/zabbix_agentd.conf
#sed -i '/^ServerActive=192.168.1.100:10051/s/:10051//' /usr/local/etc/zabbix_agentd.conf
#------------------------
yum -y install psmisc
killall zabbix_agentd
zabbix_agentd
sleep 3
zabbix_get -s 127.0.0.1 -k $name