您的位置:首页 > 运维架构 > Nginx

系统linux/redhat6.5 zabbix 2.47监控nginx1.8.0 (上)

2016-01-23 00:00 836 查看
摘要: 实现简单的搭建zabbix服务器监控nginx 。
软件准备:1.zabbix2.4.7 2.nginx1.8.0 3.mysql5.7.9

基本思路:

服务器端(虚拟机1):安装 zabbix-server zabbix_get mysql5.7.9

客户端(虚拟机2):安装zabbix-agent nginx1.8.0

服务器和客户端都是使用redhat6.5搭建的

--------------------------------------------------------------------------------------------------

实验环境:必须能上网,因为要yum和编译安装一些必要的组件

因为是实验环境,所以防火墙iptables,selinux 一定要关闭!!

服务器端和客户端的防火墙iptables,selinux 都要关闭!

重要的事情说三遍!! 如果你是大神,可以此条可以忽略。

[root@server ~]# iptables -F

[root@server ~]# chkconfig iptables off # 开机就关闭iptables

[root@server ~]# setenforce 0
setenforce: SELinux is disabled

[root@server ~]# vim /etc/selinux/config

将SELINUX=enforcing 改为 SELINUX=disabled

服务器端和客户端的防火墙iptables,selinux 都要关闭!

炮制虽繁必不敢省人工,品味虽贵必不敢减物力

----------------------------------------------------------------------------------------------------------------

服务器端的搭建(虚拟机1)

1.配置 epel,163,Zabbix三个网络yum源:

[root@server ~]#rpm -ivh http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [root@server ~]# wget -O /etc/yum.repos.d/CentOS6-Base-163.repo  #到这之前都有添加进去,后面网址也要输入进去
[root@server ~]#rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm 注意:1.如果你没有wget命令,用epel源 yum install wget就可以下载163的repo了。
2.[root@server yum.repos.d]# pwd
/etc/yum.repos.d
[root@server yum.repos.d]# ls
CentOS6-Base-163.repo  epel.repo  epel-testing.repo  local.repo
zabbix.repo
CentOS6-Base-163.repo 这个文件最好编辑一下,将$releasever改成6.原因是在redhat6.5上
$releasever 这个变量得到的值是6server 明星在下找不到这个文件夹
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/

2.安装必要的包,如果少安装一个,那就等着排错吧 (yum安装完成后,下面会有说明,如果那个包没安装成功,会报错,注意看一下,报错后,重新安装,不行的话,换个yum源)

[root@server yum.repos.d]# yum -y groupinstall "Development Tools"
[root@server yum.repos.d]#

3.创建zabbix 用户 (目的:所谓专人专用,防止出现越权行为)

[root@server yum.repos.d]#

4.在mysql中建立zabbix数据库和zabbix用户

初始化mysql5.7.9 ,自己百度哟或者看官方文档,然后启动mysql
[root@server yum.repos.d]# service mysqld start

mysql -uroot -p123456  #这是我的数据库密码:123456,不是你的!!!

5. 安装zabbix服务器端并将相关数据导入MySQL数据库中

[root@server yum.repos.d]#  yum install zabbix-server-mysql zabbix-web-mysql
zabbix_get   zabbix-agent
root@server yum.repos.d]#  cd /usr/share/doc/zabbix-server-mysql-2.4.7/create

[root@server create]#  mysql -uroot -p123456 zabbix < schema.sql

[root@server create]# mysql -uroot -p123456 zabbix < images.sql


[root@server create]# mysql -uroot -p123456 zabbix < data.sql

这三个导入的顺序不要颠倒!!不然报错

[root@server create]# mysql -uroot -p123456
mysql> use mysql
mysql> delete from user where host='localhost';
mysql> commit;
mysql> exit

6.修改zabbix配置文件中关于数据库的部分

[root@server create]# vim /etc/zabbix/zabbix_server.conf
你需要在zabbix_server.conf里面找到下面几项,并且修改成我这样的记得去掉它们前面的#号,不然会被注释掉
,不起作用的。

DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix

7.启动zabbix-server服务和httpd服务

[root@server create]# cd ~
[root@server ~]# service zabbix-server start
[root@server ~]# service httpd restart
[root@server ~]# chkconfig httpd on        #一开机就启动httpd服务
[root@server ~]# chkconfig  zabbix-server on  #同上

8.启动web界面,继续编辑(不建议使用360安全浏览器,会出问题,其他浏览器应该都可以)

1. 用ie浏览器访问http://ip/zabbix/ #这里的IP是你服务器配置的IP,例如我的服务器IP是

2.172.16.20.23.我就要在浏览器里输入http://172.16.20.23/zabbix .

3.英文看不懂没关系,下一步,下一步,总认识吧

9.哈哈出下面的问题了吧



解决方法:修改时区 vim /etc/php.ini

找到date.timezone=Asia/Shanghai #记得将date.timezone前面的#去掉!!

然后一定要重启httpd 服务:service httpd restart

不重启服务,修改是不会生效的。

安装配置客户端 (虚拟机2)

炮制虽繁必不敢省人工,品味虽贵必不敢减物力

1.配置 epel,163,Zabbix三个网络yum源:

[root@server ~]#rpm -ivh http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [root@server ~]# wget -O /etc/yum.repos.d/CentOS6-Base-163.repo  #到这之前都有添加进去,后面网址也要输入进去
[root@server ~]#rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm 注意:1.如果你没有wget命令,用epel源 yum install wget就可以下载163的repo了。
2.[root@server yum.repos.d]# pwd
/etc/yum.repos.d
[root@server yum.repos.d]# ls
CentOS6-Base-163.repo  epel.repo  epel-testing.repo  local.repo
zabbix.repo
CentOS6-Base-163.repo 这个文件最好编辑一下,将$releasever改成6.原因是在redhat6.5上
$releasever 这个变量得到的值是6server 明星在下找不到这个文件夹
baseurl=http://mirrors.163.com/centos/$releasever/os/$basearch/

2.安装必要的包,和安装zabbix-agent客户端

[root@server yum.repos.d]# yum -y groupinstall "Development Tools"
[root@server yum.repos.d]#

3.修改agent的配置文件

[root@agent ~]# vi /etc/zabbix/zabbix_agentd.conf
找到#Server=  将它改成 Server=ip     #服务器的IP(虚拟机1的IP)
记得将ServerActive=127.0.0.1 注释掉!!!也就是变成###ServerActive=127.0.0.1

4.启动zabbix-agent服务

[root@agent ~]#service zabbix-agent start

5.编译安装nginx-1.8.0

5.1搭建编译环境

[root@agent ~]#yum -y install gcc gcc-c++ autoconf automake make
[root@agent ~]#

5.2下载nginx-1.8.0 ,并解压

[root@agent ~]#yum install wget
[root@agent ~]#wget    #网址也要输入,
[root@agent ~]#tar -zxvf  nginx-1.8.0.tar.gz
[root@agent ~]#  cd  nginx-1.8.0
[root@agent nginx-1.8.0]# ./configure –-with-http_stub_status_module #模块不默认安装,一点要安装这个模块
[root@agent nginx-1.8.0]#  make && make install
[root@agent nginx-1.8.0]#  cd ../
[root@agent ~]#

5.3 启动nginx,观察现象

[root@agent ~]# /usr/local/nginx/sbin/nginx

nginx启动正常启动后,在浏览器中输出http://本机IP/ 会出现如下画面



5.4 设置开机启动nginx,如果高版本的nginx支持chkconfig nginx on

(或者 chkconfig nginx on )更好了,下面的脚本可以忽略。(可以先试试这个两个命令能否让nginx开机启动)

[root@agent ~]# vim /etc/init.d/nginx

添加如下代码

5.6 修改访问权限

[root@agent ~]#chmod a+x /etc/init.d/nginx  #a+x参数表示 ==> all user can execute 所有用户可执行
[root@agent ~]#echo /etc/init.d/nginx start >> /etc/rc.local  #利用重定向将/etc/init.d/nginx start 追加到 /etc/rc.local 文件的末尾处

5.7 开启nginx的stub_status模块

查看已安装的 Nginx 是否包含 stub_status 模块

[root@agent ~]# vim /usr/local/nginx/conf/nginx.conf

server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location /{root   html;index  index.html index.htm;}location ~ /status    #红字为修改部分{stub_status on;access_log off;#加入访问限制allow IP;            #IP=服务器的IP,虚拟机1的IPallow  127.0.0.1;deny all;}}

重启nginx

service nginx restart
nginx status信息

[root@agent ~]# curl http://127.0.0.1/ngx_status Active connections: 10
server accepts handled requests
9 9 8
Reading: 0 Writing: 0 Waiting: 0

5.8 zabbix客户端配置status

/usr/bin/status.sh
#!/bin/bash
# Functions to return nginx stats
function active {
/usr/bin/curl "http://127.0.0.1:80/status" 2>/dev/null| grep 'Active' | awk '{print $NF}' }
function reading {
/usr/bin/curl "http://127.0.0.1:80/status" 2>/dev/null| grep 'Reading' | awk '{print $2}' }
function writing {
/usr/bin/curl "http://127.0.0.1:80/status" 2>/dev/null| grep 'Writing' | awk '{print $4}'}
function waiting {
/usr/bin/curl "http://127.0.0.1:80/status" 2>/dev/null| grep 'Waiting' | awk '{print $6}' }
function accepts {
/usr/bin/curl "http://127.0.0.1:80/status" 2>/dev/null| awk NR==3 | awk '{print $1}' }
function handled {
/usr/bin/curl "http://127.0.0.1:80/status" 2>/dev/null| awk NR==3 | awk '{print $2}'}
function requests {
/usr/bin/curl "http://127.0.0.1:80/status" 2>/dev/null| awk NR==3 | awk '{print $3}' }
# Run the requested function
$1
EOF

5.9 修改权限

位置:cd /usr/bin/
修改status.sh 的权限 和属主:属组
chmod o+x status.sh
chown zabbix.zabbix status.sh
[root@nginx bin]# ls -ll |grep status.sh

-rwxr-xr-x 1 zabbix zabbix 1225 Jan 22 08:13 status.sh

5.10

修改nginx服务器上zabbix客户端的zabbix_agentd.conf配置文件,然后重启zabbix agentd客户端。

vim /etc/zabbix/zabbix_agentd.conf  然后修改和下面一样
LogFile=/tmp/zabbix_agentd.log
Server=IP       #zabbix server端的IP地址
ServerActive=IP #zabbix server端的IP地址
Hostname=IP2    #本地的IP地址
UnsafeUserParameters=1
##下面的是新添加进去的key(键值)
UserParameter=nginx.accepts, /usr/bin/status.sh accepts
UserParameter=nginx.handled, /usr/bin/status.sh handled
UserParameter=nginx.requests, /usr/bin/status.sh requests
UserParameter=nginx.connections.active, /usr/bin/status.sh active
UserParameter=nginx.connections.reading, /usr/bin/status.sh reading
UserParameter=nginx.connections.writing, /usr/bin/status.sh writing
UserParameter=nginx.connections.waiting,/usr/bin/status.sh waiting


系统linux/redhat6.5 zabbix 2.47监控nginx1.8.0 (下)

http://my.oschina.net/u/2618987/blog/608262
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息