首页 > 代码库 > 10.zabbix
10.zabbix
10.zabbix
监控的目的 | 基础的不用说了,负载,cpu,内存,磁盘 再高级点,各个服务,比如nginx, mysql, php 再深层次,那就是针对你们业务的指标,比如访问日志错误数(5xx),mysql慢查询,tcp连接数等。 |
1、准备lnmp环境 | #create nginx+php huanjing yum -y install gcc gcc-c++ ncurses-devel perl openssl openssl-devel pcre pcre-devel zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libmcrypt-devel mhash mcrypt #setup nginx ###rz nginx-1.6.3-1.x86_64.rpm到/server/tools目录 && cd /server/tools rpm -ivh nginx-1.6.3-1.x86_64.rpm #直接手动安装 useradd www -u888 -s /sbin/nologin -M mkdir -p /application/nginx/conf/extra cd /application/nginx/conf/extra cat >../nginx.conf<<EOF worker_processes 1; events { worker_connections 1024; } error_log logs/error.log error; http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/*.conf; log_format main ‘\$remote_addr - \$remote_user [\$time_local] "\$request" ‘ ‘\$status \$body_bytes_sent "\$http_referer" ‘ ‘"\$http_user_agent" "\$http_x_forwarded_for"‘; access_log logs/access.log main; } EOF cat >www.conf<<EOF server { listen 80; server_name www.etiantian.org etiantian.org; location / { root html/www; index index.html index.htm; } } EOF cat >blog.conf<<EOF server { listen 80; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } } EOF mkdir -p ../../html/www ../../html/blog echo www >../../html/www/index.html echo blog >../../html/blog/index.html /application/nginx/sbin/nginx /application/nginx/sbin/nginx -s reload lsof -i :80 #setup php and huanjing ###rz libiconv-1.14.tar.gz 和 php-5.5.32.tar 到/server/tools/ 准备进行编译安装 cd /server/tools/ tar zxf libiconv-1.14.tar.gz cd libiconv-1.14 ./configure --prefix=/usr/local/libiconv make make install cd ../ tar zxf php-5.5.32.tar.gz cd php-5.5.32 ./configure \ --prefix=/application/php5.5.32 \ --with-mysql=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with gettext=mysqld \ --with-iconv-dir=/usr/local/libiconv \ --with-freetype-dir \ --with-jpeg-dir \ --with-png-dir \ --with-zlib \ --with-libxml-dir=/usr \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --with-curl \ --enable-mbregex \ --enable-fpm \ --enable-mbstring \ --with-mcrypt \ --with-gd \ --enable-gd-native-ttf \ --with-openssl \ --with-mhash \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-soap \ --enable-short-tags \ --enable-static \ --with-xsl \ --with-fpm-user=www \ --with-fpm-group=www \ --enable-ftp \ --enable-opcache=no ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64/ touch ext/phar/phar.phar make make install ln -s /application/php5.5.32/ /application/php cp php.ini-production /application/php/lib/php.ini cd /application/php/etc/ cp php-fpm.conf.default php-fpm.conf /application/php/sbin/php-fpm lsof -i :9000 ps -ef |grep php-fpm #setup mysql ###rz mysql-5.5.56-linux-glibc2.5-x86_64.tar.gz 到/server/tools/ 准备进行编译安装 cd /server/tools tar -xf mysql-5.5.56-linux-glibc2.5-x86_64.tar.gz \mv mysql-5.5.56-linux-glibc2.5-x86_64 /application/mysql-5.5.56 ln -s /application/mysql-5.5.56 /application/mysql useradd mysql -u890 -s /sbin/nologin -M id mysql chown -R mysql.mysql /application/mysql ll -d /application/mysql mkdir -p /application/mysql/data cd /application/mysql /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql sed -i ‘s#/usr/local/#/application/#g‘ /application/mysql/bin/mysqld_safe \cp support-files/my-small.cnf /etc/my.cnf /application/mysql/bin/mysqld_safe --user=mysql & PATH="/application/mysql/bin/:$PATH" echo "$PATH" >>/etc/profile which mysql sed -i ‘s#/usr/local/#/application/#g‘ support-files/mysql.server \cp support-files/mysql.server /etc/init.d/mysqld chmod +x /etc/init.d/mysqld /etc/init.d/mysqld stop lsof -i :3306 /etc/init.d/mysqld start lsof -i :3306 chkconfig --add mysqld mysql show databases; quit #mysqladmin -uroot password oldboy123 #mysql -uroot -poldboy123 -e "create database wordpress;" #mysql -uroot -poldboy123 -e "grant all on wordpress.* to wordpress@‘172.16.1.%‘ identified by ‘123456‘;" #mysql -uroot -poldboy123 -e "flush privileges;" ###改进方案:快速上线lnmp环境,使用自己打包的nginx和环境包 |
2、编译安装zabbix (zabbix-3.0.9) | yum -y install fping net-snmp-devel unixODBC-devel openssl-devel OpenIPMI-devel java-1.7.0-openjdk-devel.x86_64 cd /server/tools/ ###rz zabbix-3.0.9.tar.gz到当前目录 tar xf zabbix-3.0.9.tar.gz cd zabbix-3.0.9 ./configure --prefix=/application/zabbix-3.0.9 --enable-server --enable-agent --enable-java --enable-ipv6 --with-mysql=/application/mysql/bin/mysql_config --with-net-snmp --with-libcurl --with-libxml2 --with-openipmi --with-unixodbc --with-openss make install ln -s /application/zabbix-3.0.9/ /application/zabbix |
3、添加zabbix用户 | useradd zabbix -s /sbin/nologin |
4、mysql数据库操作 | mysql create database zabbix character set utf8 collate utf8_bin; grant all on zabbix.* to zabbix@‘127.0.0.1‘ identified by ‘zabbix‘; flush privileges; mysql -uroot zabbix </server/tools/zabbix-3.0.9/database/mysql/schema.sql mysql -uroot zabbix </server/tools/zabbix-3.0.9/database/mysql/images.sql mysql -uroot zabbix </server/tools/zabbix-3.0.9/database/mysql/data.sql |
5、web代码 | mkdir /application/nginx/html/zabbix cp -a /server/tools/zabbix-3.0.9/frontends/php/* /application/nginx/html/zabbix/ chown -R www.www /application/nginx/html/zabbix/ |
6、配置zabbix | #youhua php sed -i ‘s#post_max_size = 8M#post_max_size = 16M#g;s#max_execution_time = 30#max_execution_time = 300#g;s#max_input_time = 60#max_input_time = 600#g;910a date.timezone = Asia/Shanghai‘ /application/php/lib/php.ini pkill php-fpm /application/php/sbin/php-fpm #youhua nginx cat >/application/nginx/conf/nginx.conf<<EOF 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; root html; location / { root html; index index.php index.html index.htm; } location ~ .*\.(php|php5)?\$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; } access_log logs/zabbix.log; } } EOF /application/nginx/sbin/nginx -t /application/nginx/sbin/nginx -s reload #youhua zabbix fuwu sed -i -e ‘77a DBHost=127.0.0.1‘ -e ‘111a DBPassword=zabbix‘ /application/zabbix/etc/zabbix_server.conf cp /server/tools/zabbix-3.0.9/misc/init.d/fedora/core/zabbix_{server,agentd} /etc/init.d/ sed -i ‘s#BASEDIR=/usr/local#BASEDIR=/application/zabbix#g‘ /et c/init.d/zabbix_{server,agentd} /etc/init.d/zabbix_server start ###这两步可以放到前面编译安装php的时候一起添加好模块 cd /server/tools/php-5.5.32/ext/gettext /application/php/bin/phpize #需要扩容php模块的时候需要执行此句 ./configure --with-php-config=/application/php/bin/php-config make && make install echo " extension = gettext.so">> /application/php/lib/php.ini /application/php/bin/php -m|grep gettext #检测是否安装成功 cd /server/tools/php-5.5.32/ext/mysqli /application/php/bin/phpize #需要扩容php模块的时候需要执行此句 ./configure --with-php-config=/application/php/bin/php-config make && make install echo " extension = mysqli.so">> /application/php/lib/php.ini /application/php/bin/php -m|grep mysqli pkill php lsof -i :9000 /application/php/sbin/php-fpm lsof -i :9000 #youhua fonts(ziti) yum -y install wqy-microhei-fonts \cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /application/nginx/html/zabbix/fonts/DejaVuSans.ttf ###以上全部是服务器的操作! |
7、zabbix客户端安装使用 | #/etc/init.d/zabbix_agentd start #服务端的agent(客户端)启动 #新机器的安装方法 仅安装agent监测端即可! ###rz相关软件包到当前目录 yum localinstall -y zabbix-agent-3.0.9-1.el6.x86_64.rpm sed -i ‘s#Server=127.0.0.1#Server=172.16.1.63#g‘ /etc/zabbix/zabbix_agentd.conf /etc/init.d/zabbix-agent start |
8、邮件告警 | ix-agent star echo "set from=15361848205@189.cn smtp=smtp.189.cn smtp-auth-user=15361848205 smtp-auth-password=123456abc smtp-auth=login " >>/etc/mail.rc cat >/server/scripts/sendmailtest.sh<<EOF char="send from 20has-pc by 20has " mail -s "my dear,\$char \$(date +%F_%T)" 15361848205@189.cn </etc/passwd EOF sh /server/scripts/sendmailtest.sh #模拟邮件测试而已 #mail -s "my dear,2017年7月9日 00:20:26" 714582797@qq.com </etc/passwd #mail -s 邮件主题 收件人 邮件正文 |
总结 | 处理了两次天坑: 1、张导的视频在编译安装zabbix的时候是直接yum install java-devel ,在我的机器这样提示找不到安装包,经过和鸡血哥聊天指导 直接yum指定java包解决 解决办法:yum -y install java-1.7.0-openjdk-devel.x86_64 2、第一次安装zabbix报错是因为张导的视频设置的nginx用户,我的nginx包设置的是www的用户,所以无权限写入 解决方法:chown -R www.www /application/nginx/html/zabbix/ |
10.zabbix