Zabbix 监控安装使用文档 系统环境:centos 6.5_64 webUi 运行环境lnmp/lamp 都可以 实验使用的lamp Zabbix版本zabbix-2.4.7.tar.gz 一.运行环境安装#这里使用yum安装的运行环境 #yum install php php-mysql php-gd mysql mysql-server mysql-devel httpd -y #安装编译器及依赖包 # yum install gcc gcc-c++ libxml2 libxml2-devel net-snmp net-snmp-deve libcurl libcurl-devel -y #切记 关闭 iptables 关闭 selinux 二.安装及配置zabbix ①编译安装zabbix #安装包上传到/usr/local/src/ [iyunv@localhost ~]# cd /usr/local/src/ [iyunv@localhost src]# tar -zxvf zabbix-2.4.7.tar.gz [iyunv@localhost src]# cd /usr/local/src/ [iyunv@localhost zabbix-2.4.7]#./configure --prefix=/usr/local/zabbix-2.4.7/ --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 [iyunv@localhost zabbix-2.4.7]# make [iyunv@localhost zabbix-2.4.7]# make install 创建用户 为了安全考虑 zabbix 只使用普通用户运行 # groupadd zabbix # useradd -g zabbix zabbix ②初始化数据库 zabbix server 与 proxy 需要数据库,angent 不需要。尤其要注意的是 proxy 只需要导入一个 sql 文件,而 se rver 一共要导入 3 个 sql 文件。我当时在搭建 proxy 的时候导入了 3 个 sql,导致出现报错。后来才发现 proxy只需要导入一个表结构即可 #/etc/init.d/mysqld start;chkconfig mysqld on #启动mysql服务 ③配置mysql root 密码 # mysqladmin -u root password zabbix ④导入zabbix的数据库 [iyunv@localhost ~]# mysql -uroot -pzabbix zabbix < /usr/local/src/zabbix-2.4.7/database/mysql/schema.sql [iyunv@localhost ~]# mysql -uroot -pzabbix zabbix < /usr/local/src/zabbix-2.4.7/database/mysql/images.sql [iyunv@localhost ~]# mysql -uroot -pzabbix zabbix < /usr/local/src/zabbix-2.4.7/database/mysql/data.sql ⑤配置zabbix [iyunv@localhost ~]# vi /usr/local/zabbix-2.4.7/etc/zabbix_server.conf [iyunv@localhost ~]# grep -E -v "^#|^$" /usr/local/zabbix-2.4.7/etc/zabbix_server.conf LogFile=/tmp/zabbix_server.log DBName=zabbix DBUser=root DBPassword=zabbix DBPort=3306 ⑥启动zabbix server [iyunv@localhost zabbix-2.4.7]# /usr/local/zabbix-2.4.7/sbin/zabbix_server #默认端口 10051 ⑦日志查看 tail -f /tmp/zabbix_server.log #日志我们会经常检查我们的配置是否有问题经常看的 ⑧客户端安装配置 由于,我们编译安装server的时候已经安装客户端了,这里无需再次安装 这里只需要在被控端,安装即可,zabbix_server 也需要配置zabbix_agent,只需配置agent即可 #安装包上传到/usr/local/src/ [iyunv@localhost ~]# cd /usr/local/src/ [iyunv@localhost src]# tar -zxvf zabbix-2.4.7.tar.gz [iyunv@localhost src]# cd /usr/local/src/ [iyunv@localhost zabbix-2.4.7]#./configure --prefix=/usr/local/zabbix-2.4.7/ --enable-server --enable-agent [iyunv@localhost zabbix-2.4.7]# make [iyunv@localhost zabbix-2.4.7]# make install 创建用户 为了安全考虑 zabbix 只使用普通用户运行 # groupadd zabbix # useradd -g zabbix zabbix ⑨zabbix 客户端配置 [iyunv@localhost ~]# vi /usr/local/zabbix-2.4.7/etc/zabbix_agentd.conf #配置文件别搞错了 [iyunv@localhost ~]# grep -E -v "^#|^$" /usr/local/zabbix-2.4.7/etc/zabbix_agentd.conf LogFile=/tmp/zabbix_agentd.log Server=127.0.0.1 ServerActive=127.0.0.1 Hostname=zabbix-server 这里的hostname建议添加到/etc/hosts 做主机名解析 其中 Server 和 ServerActive 都指定 zabbixserver 的 IP 地址,不同的是,前者是被动后者是主动。也就是说 S erver 这个配置是用来允许 127.0.0.1 这个 ip 来我这取数据。而 serverActive 的 127.0.0.1 的意思是,客户端 主动提交数据给他。明白了吗?为什么要分主动和被动?后续再来讨论这个问题! 其他主机安装客户端记得添加 zabbix 用户。 ⑩zabbix 客户端启动 [iyunv@localhost ~]# /usr/local/zabbix-2.4.7/sbin/zabbix_agentd 三.zabbix 管理网站配置 [iyunv@localhost ~]# cd /usr/local/src/zabbix-2.4.7 [iyunv@localhost zabbix-2.4.7]# cp -rp frontends/php/ /var/www/html/ ①配置Apache 虚拟主机 [iyunv@localhost ~]# vi /etc/httpd/conf/httpd.conf NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot /var/www/html/php ServerName 172.16.1.183 </VirtualHost> ②启动Apache [iyunv@localhost ~]# /etc/init.d/httpd restart ;chkconfig httpd on
需要修改php.ini 满足安装需求,才能下一步 ③修改php参数 # vi /etc/php.ini date.timezone = Asia/Shanghai max_input_time = 300 max_execution_time = 300 post_max_size = 16M # yum install php-bcmath php-xml php-mbstring -y #安装这些依赖软件包 修改过,重启Apache 这样就OK 了,我们可以下一步安装了 这里填写,数据库信息,填写完毕,点击 “test connection ” 这里,点击下一步即可 如果这里报错,那就把这个文件下载下来,丢到对应目录、 到此,说明,我们已经安装完毕 访问,我们配置的主机,默认账号admin密码zabbix 四.Zabbix 监控 VMware主机 ①修改配置文件,启用VMware监控 [iyunv@localhost ~]# vi /usr/local/zabbix-2.4.7/etc/zabbix_server.conf #################vmware################ StartVMwareCollectors=5 #预启动的VMware数据采集线程数量,值范围:0~250 VMwareFrequency=60 #VMware的数据检测缓存大小,值范围:256K~2G VMwareCacheSize=8M #数据采集的频率,值范围:10~86400 ②添加,被监控主机 点击 “crate host” 添加主机 添加上,三个模块 填入这几个参数,root/ password就是VMware esxi 或者vcenter的账号密码 {$PASSWORD} password {$USERNAME} root 填写完毕,点击 “添加” 然后重启 zabbix -server 我们监控日志,可以看到VMware监控已经开启,等待一会,就能获得发现到的虚拟机 过了一会,就会发现,很多VMware esxi 或者vcenter上面的虚机 了 四.Zabbix 监控 Mysql ①在客户端安装zabbix_agentd chk_mysql.sh 脚本放在/etc/zabbix/目录下面并修改里面的mysql密码,根据自己情况写 #!/bin/bash # ------------------------------------------------------------------------------- # 用户名 MYSQL_USER='root' # 密码 MYSQL_PWD='redhat' # 主机地址/IP MYSQL_HOST='172.16.1.219' # 端口 MYSQL_PORT='3306' # 数据连接 MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}" # 参数是否正确 if [ $# -ne "1" ];then echo "arg error!" fi # 获取数据 case $1 in Uptime) result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3` echo $result ;; *) echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)" ;; esac
[iyunv@openstack-node1 zabbix]# vi /etc/zabbix_agentd.conf UserParameter=mysql.version,mysql -V # 获取mysql性能指标,这个是上面定义好的脚本,此脚本,会附带需要改里面的主机密码 UserParameter=mysql.status ,/etc/zabbix/chk_mysql.sh $1# 获取mysql运行状态 UserParameter=mysql.ping,mysqladmin -uroot -predhat -P3306 -h127.0.0.1 ping | grep -c alive 重启 zabbix_agentd 添加mysql模板 这里就可以看到,监控数据 五.Zabbix 监控 linux主机 上面有写安装zabbix-client端,这里我用脚本安装的,方便批量部署 脚本内容如下: #!/bin/bash # Guying 2015/12/2 15:42 #version v1 ###zabbix client install ########## ####关闭selinux 和防火墙######### /etc/init.d/iptables stop;chkconfig iptables off sed -i "s/SELINUX=.*/SELINUX=disabled/g" /etc/selinux/config /usr/sbin/setenforce 0 ################################################### yum install epel-release -y && yum install zabbix22-agent -y #######这里提示输入的是zabb-server 的ip地址###################### echo "Please input zabbix server ipaddress:" read ipaddress sed -i "s/Server=.*/Server=$ipaddress/g" /etc/zabbix_agentd.conf sed -i "s/ServerActive=.*/ServerActive=$ipaddress:10051/g" /etc/zabbix_agentd.conf #这里提示输入的主机名要和,添加监控主机名对应,否则无法通讯,建议将主机名添加到#zabbix-server 的/etc/hosts 文件里面# echo "Please input hostname:" read hostname sed -i "s/Hostname=.*/Hostname=$hostname/g" /etc/zabbix_agentd.conf /etc/init.d/zabbix-agentd restart ;chkconfig zabbix-agentd on 执行过脚本,只需在zabbix UI 上面,添加该主机即可 注意: 主机名写到zabbix-server /etc/hosts 里面,主机名和,添加主机的名字对应 六.zabbix 监控 nginx ①先运行我们的脚本,配置好zabbix client端 ②配置 client端 使用git 下载下来 我们需要改这个文件zbx_nginx_stats.py zabbix_host = '172.16.1.218' # Zabbix server IP zabbix_port = 10051 # Zabbix server port hostname = 'nginx-node4.example.com' # Name of monitored host, like it shows in zabbix web ui time_delta = 1 # grep interval in minutes # URL to nginx stat (http_stub_status_module) stat_url = 'http://172.16.1.222/nginx_stat' # Nginx log file path nginx_log_file_path = '/usr/local/nginx/logs/access.log' # Optional Basic Auth username = 'user' password = 'pass' # Temp file, with log file cursor position seek_file = '/tmp/nginx_log_stat' 修改完毕放入 /etc/zabbix/ 里面 这些需要我们修改 ③赋予,脚本可执行权限,我们zabbix客户端需要调用这个脚本 chmod +x /etc/zabbix/zbx_nginx_stats.py ④写计划任务,来运行这个脚本收集数据 # sudo crontab -e */1 * * * * /etc/zabbix/zbx_nginx_stats.py ⑤.在zabbix web界面 导入 zbx_nginx_template.xml 模块 这个不细讲了,web界面,点击configure ---模板 ----导入模板,导入即可 ⑥ 配置nginx 开启 nginx_satus功能 location /nginx_stat { stub_status on; # Turn on nginx stats access_log off; # We do not need logs for stats allow 127.0.0.1; # Security: Only allow access from IP deny all; # Deny requests from the other of the world } 配置过后,主机上添加上这个模板进行测试 参考官方文档:https://github.com/blacked/zbx_nginx_template
|