首页 > 代码库 > LNMP+zabbix分布式监控搭建及版本升级

LNMP+zabbix分布式监控搭建及版本升级

LNMP+zabbix分布式监控搭建
需要组件:gcc gcc-c++ openssl* pcre pcre-devel gd gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel autoconf automake imake expat-devel cmake libaio libaio-devel bzr bison libtool ncurses5-devel ncurses-devel net-snmp net-snmp-devel curl-devel

1. 安装及配置nginx
  # wget http://nginx.org/download/nginx-1.8.1.tar.gz
  # tar zxf nginx-1.8.1.tar.gz
  # ./configure --prefix=/usr/local/nginx \
  --with-http_ssl_module --with-http_spdy_module \
  --with-http_stub_status_module --with-pcre
  # make && make install
  # mkdir /data/logs/nginx -p
  # mkdir -p /data/site/www.apicloud.com
  # vim /usr/local/nginx/conf/nginx.conf
  server {
  listen 80;
  server_name www.apicloud.com;
  access_log /data/logs/nginx/www.xxx.com.access.log main;
  
  index index.php index.html index.html;
  root /data/site/www.xxx.com;
  
  location /
  {
  try_files $uri $uri/ /index.php?$args;
  }
  
  location ~ .*\.(php)?$
  {
  expires -1s;
  try_files $uri =404;
  fastcgi_split_path_info ^(.+\.php)(/.+)$;
  include fastcgi_params;
  fastcgi_param PATH_INFO $fastcgi_path_info;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_pass 127.0.0.1:9000;
  
  }
  }

  # /usr/local/nginx/sbin/nginx

 

2. 安装php
  # wget http://cn2.php.net/distributions/php-5.6.20.tar.gz
  # tar zxf php-5.6.20.tar.gz
  # ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-bz2 --with-curl \
  --enable-ftp --enable-sockets --disable-ipv6 --with-gd --with-jpeg-dir=/usr/local \
  --with-png-dir=/usr/local --with-freetype-dir=/usr/local --enable-gd-native-ttf \
  --with-iconv-dir=/usr/local --enable-mbstring --enable-calendar --with-gettext --enable-pdo\
  --with-libxml-dir=/usr/local --with-zlib --with-pdo-mysql=mysqlnd --with-mysqli=/usr/local/mysql/bin/mysql_config \
  --with-mysql=/usr/local/mysql --enable-dom --enable-xml --enable-fpm --with-libdir=lib64 --enable-bcmath --enable-zip --enable-exif --with-imap-ssl --with-openssl --with-mcrypt --with-fpm-user=www --  with-fpm-group=www

  # make && make install
  # cp php.ini-production /usr/local/php/etc/php.ini
  # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
  # vim /usr/local/php/etc/php.ini
  max_execution_time = 300
  memory_limit = 128M
  post_max_size = 16M
  upload_max_filesize = 2M
  max_input_time = 300
  date.timezone = PRC
  # /usr/local/php/sbin/php-fpm
  重启nginx

 

3. 安装mysql
  # wget http://120.52.73.48/jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
  # mkdir /usr/local/boost
  # mv boost_1_59_0.tar.gz /usr/local/boost/boost_1_59_0.tar.gz
  # wget http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.15.tar.gz
  # groupadd mysql
  # useradd -r -g mysql mysql
  # tar zxf mysql-5.7.15.tar.gz
  # cd mysql-5.7.15
  # cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -  DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data/mysqldb -DMYSQL_TCP_PORT=3306 -  DWITH_BOOST=/usr/local/boost/boost_1_59_0.tar.gz
  # make && make install
  # chown -R mysql:mysql /usr/local/mysql/*
  # cd /usr/local/mysql
  # bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysqldb(生成新的随机密码) mFR7jRa-9r4/
  # cp -f /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
  # cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
  # vim /etc/profile
  PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
  # export PATH
  # source /etc/profile
  # chmod o=rwx /usr/local/mysql
  # service mysqld start
  # chkconfig --level 2345 mysqld on
  # mysql -u root -p (随机密码)
  mysql> SET PASSWORD = PASSWORD(‘123456‘);
  mysql> flush privileges;

 


4. 安装zabbix
  # wget http://jaist.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/2.4.8/zabbix-2.4.8.tar.gz
  # tar zxf zabbix-3.0.3.tar.gz
  # cd zabbix-3.0.3
  # ./configure --prefix=/usr/local/zabbix/ --enable-server \
  --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --enable-proxy --enable-snmp --with-mbstring
  # make && make install
  # cp -rp frontends/php/* /data/site/www.apicloud.com/zabbix
  # cp -f /usr/local/src/zabbix-3.0.3/misc/init.d/fedora/core5/* /etc/init.d/
  # vim /etc/init.d/zabbix_server
    ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_server"
  # vim /etc/init.d/zabbix_agentd
    ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd"
  # chkconfig --add zabbix_server
  # chkconfig --add zabbix_agentd
  # chkconfig zabbix_server on
  # chkconfig zabbix_agentd on

  更改zabbix字体:
  复制C:\Windows\Fonts(楷体)到/data/site/www.apicloud.com/zabbix/fonts
  # cd /data/site/www.apicloud.com/zabbix
  # sed -i ‘s/DejaVuSans/simkai/g‘ ./include/defines.inc.php
  # ln -s /usr/local/mysql/lib/libmysqlclient.so.20 /lib64/
  # groupadd zabbix && useradd -r -g zabbix -s /etc/nologin zabbix

  初始化数据库:
  mysql> create database zabbix default charset utf8;
  # mysql -uroot -p zabbix < database/mysql/schema.sql
  # mysql -uroot -p zabbix < database/mysql/images.sql
  # mysql -uroot -p zabbix < database/mysql/data.sql
  # vim /usr/local/zabbix/etc/zabbix_server.conf
  DBUser=root
  DBPassword=123456
  # service zabbix_server start

  修改zabbix前端安装文件:
  # vim /data/site/www.apicloud.com/zabbix/include/classes/setup/CFrontendSetup.php
    添加$current = -1;
    public function checkPhpAlwaysPopulateRawPostData() {
    $current = ini_get(‘always_populate_raw_post_data‘);
    $current = -1;

  网页安装zabbix-server:
  http://ip/zabbix
  Database host 127.0.0.1
  User root
  Password 123456
  1.Download the configuration file
  2.Save it as "/data/site/www.apicloud.com/zabbix/conf/zabbix.conf.php"

  zabbix默认管理账号
  user:admin
  password:zabbix
  如果账号密码错误,需要自行设置密码:
  echo -n XXXXX | openssl md5
  mysql -uroot -p
  use zabbix;
  update users set passwd=‘XXXXXXXXXXXXXXXXXXXXXXXXXXXX‘ where userid = ‘1‘;

 

5.zabbix客户端配置
  # groupadd zabbix && useradd -r -g zabbix -s /etc/nologin zabbix
  # tar zxf zabbix-2.4.8.tar.gz
  # cd zabbix-2.4.8
  # ./configure --prefix=/usr/local/zabbix --enable-agent
  # make && make install
  # vim /usr/local/zabbix/etc/zabbix_agentd.conf
    Server=10.124.151.248 (zabbix_server IP) (被动)
    ServerActive=10.124.151.248 (zabbix_server IP) (主动)
    Hostname=agent-1 (独立主机名)
  # cp -f /usr/local/src/zabbix-2.4.8/misc/init.d/fedora/core5/zabbix_agentd /etc/init.d/
  # vim /etc/init.d/zabbix_agentd
    ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd"
  # chkconfig --add zabbix_agentd
  # chkconfig zabbix_agentd on
  # service zabbix_agentd start
  iptables -I INPUT -p tcp --dport 10050 -j ACCEPT
  service iptables save
  注:如果客户端过多可开启自动发现,自动添加到zabbix主机;

 

6.zabbix的使用

  1.主机的添加
  配置 —> 主机 —> 创建主机 —> 主机名称(Hostname) —> 可见主机名(随意) —> 群组 —> IP地址 —> 主机模板 —> 搜索模板 —> 添加 —> 添加

  2.监控项
  一种是链接模板,如果有特殊的需求可以选择手动添加监控项;
  第一种:配置 —> 主机 —> 模板 —> 添加 ;
  第二种:配置 —> 主机 —> 监控项 —> 创建监控项 —> 监控名称 —> 键值(手动在客户端配置文件里面添加,具体步骤在下面) —> 信息类型 —> 数据类型 —> 间隔秒数 —> 时间间隔(可选) —> 启用
  客户端创建键值:
  # vim /usr/local/zabbix/etc/zabbix_agentd.conf
    UnsafeUserParameters=1
    UserParameter=mongodb.node,ps -ef|grep [n]ode|wc -l
  格式:UserParameter= name.item, + command
  注:自定义监控可能出现first network error, wait for 15 seconds,可更改server和agentd的Timeout=30即可。

  3.监控报警
  zabbix监控报警流程:zabbix通过监控项监控数据 —> 触发器 —> 动作 —> 用户。
  触发器达到触发器预设值或者宕机时就产生动作,根据动作发邮件或者短信,最后发送给指定的用户。
  # yum install postfix
  # service postfix start
  # chkconfig postfix on
  邮件报警设置步骤:管理 —> 报警媒介类型 —> 管理 —> 用户 —> Admin —> 报警媒介 —> 添加 —> 配置 —> 动作 —> 创建动作 —> 操作 —> 添加

7.zabbix proxy配置(需要安装mysql)
  # groupadd zabbix && useradd -r -g zabbix -s /etc/nologin zabbix
  # tar zxf zabbix-2.4.8tar.gz
  # cd zabbix-2.4.8
  # ./configure --prefix=/usr/local/zabbix --enable-proxy --with-mysql --with-net-snmp --with-libcurl --enable-agent
  # make && make install
  mysql> create database zabbix_proxy default charset utf8;
  # mysql -uroot -p zabbix_proxy < database/mysql/schema.sql
  # service mysqld start
  # vim /usr/local/zabbix/etc/zabbix_proxy.conf
    ProxyMode=0
    Server=123.56.22.xx(zabbix_server IP)
    DBName=zabbix_proxy
    DBUser=zabbixproxy
    DBPassword=zabbixproxy
  # /usr/local/zabbix/sbin/zabbix_proxy

  服务端设置
  Server=(proxy IP)
  ServerActive=(proxy IP)
  Hostname=xxxx (独立主机名)


8.zabbix 2.4.8升级3.X.X
  1.备份zabbix前端静态文件
  2.备份zabbix安装目录
  3.备份整个mysql或者zabbix库
  4.上传源码包,并删除原安装目录,编译安装

  zabbix动作:
  名称
  XEMAIL

  默认接收人
  故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!

  默认信息
  告警主机:{HOSTNAME1}

  告警时间:{EVENT.DATE} {EVENT.TIME}

  告警等级:{TRIGGER.SEVERITY}

  告警信息: {TRIGGER.NAME}

  告警项目:{TRIGGER.KEY1}

  问题详情:{ITEM.NAME}:{ITEM.VALUE}

  当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}

  事件ID:{EVENT.ID}

  恢复主旨
  恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!

  恢复信息
  告警主机:{HOSTNAME1}

  告警时间:{EVENT.DATE} {EVENT.TIME}

  告警等级:{TRIGGER.SEVERITY}

  告警信息: {TRIGGER.NAME}

  告警项目:{TRIGGER.KEY1}

  问题详情:{ITEM.NAME}:{ITEM.VALUE}

  当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}

  事件ID:{EVENT.ID}

  注意事项:
  zabbix代理
  系统代理程式名称
  主动模式

 

LNMP+zabbix分布式监控搭建及版本升级