nginx上搭建cacti Cacti是一套基於PHP,MySQL,SNMP及RRDTool開發的網路流量監測圖形分析工具 www.2cto.com Cacti和Nagios是現在使用比較多的網路監視軟體了,對於這兩款監視軟體的區別,應該說是側重點的不同。 Cacti比較著重於直觀資料的監控,易於產生圖形,用來監控網路流量、cpu使用率、硬碟使用率等可以說很在合適不過。 而Nagios則比較注重於主機和服務的監控,並且有很強大的發送警示資訊的功能。 把兩者結合起來,既可以使警示機制高效及時,又可以很容易的查看各項資料的情況。 一、在LNMP(linux+nginx+mysql+php)上搭建cacti 1.修改selinux vim /etc/selinux/config 修改為 selinux=disabled關閉防火牆 service iptables stop chkconfig iptables off重啟將selinux生效reboot 安裝編譯工具及庫檔案 www.2cto.com for i in ` rpm -q make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch |grep 'not installed' | awk '{print $2}' `; do yum -y install $i; done安裝libmcrypt cd /usr/local/srctar zxvf libmcrypt-2.5.7.tar.gzcd libmcrypt-2.5.7./configuremake && make install安裝cmake cd /usr/local/srctar zxvf cmake-2.8.7.tar.gzcd cmake-2.8.7./configuremake && make install安裝pcre cd /usr/local/srcmkdir /usr/local/pcre #建立安裝目錄tar zxvf pcre-8.30.tar.gzcd pcre-8.30./configure --prefix=/usr/local/pcremake && make install安裝mysql groupadd mysql #添加mysql組useradd -g mysql mysql -s /bin/false #建立mysql加入到mysql組,不允許mysql登入mkdir -p /data/mysql #建立MySQL資料庫存放目錄chown -R mysql:mysql /data/mysql #設定MySQL資料庫目錄許可權mkdir -p /usr/local/mysql #建立MySQL安裝目錄cd /usr/local/srctar zxvf mysql-5.5.21.tar.gzcd mysql-5.5.21cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql-DSYSCONFDIR=/etc #配置makemake installcd /usr/local/mysqlcp ./support-files/my-huge.cnf /etc/my.cnf #拷貝設定檔vim /etc/my.cnf #在 [mysqld] 部分增加datadir = /data/mysql #添加MySQL資料庫路徑./scripts/mysql_install_db --user=mysql #產生mysql系統資料庫cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系統啟動chmod 755 /etc/init.d/mysqld #增加執行許可權chkconfig mysqld on #加入開機啟動vim /etc/rc.d/init.d/mysqld #編輯 basedir = /usr/local/mysql #MySQL程式安裝路徑datadir = /data/mysql #MySQl資料庫存放目錄service mysqld start #啟動vim /etc/profile #把mysql服務加入系統內容變數:在最後添加下面這一行 export PATH=$PATH:/usr/local/mysql/bin使環境變數立即生效 source /etc/profile下面兩行把myslq的庫檔案連結到系統預設位置,編譯類似PHP等軟體可不指定mysql的庫檔案地址。 ln -s /usr/local/mysql/lib/mysql /usr/lib/mysqlln -s /usr/local/mysql/include/mysql /usr/include/mysqlreboot #重啟mysql_secure_installation #設定Mysql密碼或者直接修改密碼/usr/local/mysql/bin/mysqladmin -u root -p password "123456" #修改密碼service mysqld restart #重啟(6)、安裝 nginx groupadd www #添加組useradd -g www www -s /bin/false #建立賬戶www並加入到組,不允許www直接登入 cd /usr/local/srctar zxvf nginx-1.0.14.tar.gzcd nginx-1.0.14./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-openssl=/usr/ --with-pcre=/usr/local/src/pcre-8.30#--with-pcre=/usr/local/src/pcre-8.30指源碼包解壓的路徑,不是安裝的路徑,否則會報錯make && make install/usr/local/nginx/sbin/nginx #啟動nginx設定nginx開啟啟動vim /etc/rc.d/init.d/nginx #編輯開機檔案添加下面內容 #!/bin/bash# nginx Startup script for the Nginx HTTP Server# it is v.0.0.2 version.# chkconfig: - 85 15# description: Nginx is a high-performance web and proxy server.# It has a lot of features, but it's not for everyone.# processname: nginx# pidfile: /var/run/nginx.pid# config: /usr/local/nginx/conf/nginx.confnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/usr/local/nginx/logs/nginx.pidRETVAL=0prog="nginx"# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ ${NETWORKING} = "no" ] && exit 0[ -x $nginxd ] || exit 0# Start nginx daemons functions.start() {if [ -e $nginx_pid ];thenecho "nginx already running...."exit 1fiecho -n $"Starting $prog: "daemon $nginxd -c ${nginx_config}RETVAL=$?echo[ $RETVAL = 0 ] && touch /var/lock/subsys/nginxreturn $RETVAL}# Stop nginx daemons functions.stop() {echo -n $"Stopping $prog: "killproc $nginxdRETVAL=$?echo[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid}reload() {echo -n $"Reloading $prog: "#kill -HUP `cat ${nginx_pid}`killproc $nginxd -HUPRETVAL=$?echo}# See how we were called.case "$1" instart)start;;stop)stop;;reload)reload;;restart)stopstart;; status)status $progRETVAL=$?;;*)echo $"Usage: $prog {start|stop|restart|reload|status|help}"exit 1esacexit $RETVALchmod 775 /etc/rc.d/init.d/nginx #賦予執行許可權chkconfig nginx on #設定開機啟動/etc/rc.d/init.d/nginx restartservice nginx restart安裝php cd /usr/local/srctar -zvxf php-5.3.10.tar.gzcd php-5.3.10mkdir -p /usr/local/php5 #建立php安裝目錄./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-gd --with-iconv --with-freetype --with-jpeg --with-png --with-zlib --with-libxml --enable-xml --enable-discard-path --enable-magic-quotes --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --with-mime-magic --enable-suhosin --enable-session --with-mcrypt --with-curlmake && make install cp php.ini-production /usr/local/php5/etc/php.ini #複製php設定檔到安裝目錄rm -rf /etc/php.ini #刪除系統內建設定檔ln -s /usr/local/php5/etc/php.ini /etc/php.ini #添加軟連結ln -s /usr/local/php5/bin/php /usr/bin/phpcp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf #拷貝模板檔案為php-fpm設定檔 vim /usr/local/php5/etc/php-fpm.conf #編輯user = www #設定php-fpm運行帳號為wwwgroup = www #設定php-fpm運行組為www pid = run/php-fpm.pid #取消注釋 設定 php-fpm開機啟動cp /usr/local/src/php-5.3.10/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm #拷貝php-fpm到啟動目錄chmod +x /etc/rc.d/init.d/php-fpm #添加許可權chkconfig php-fpm on #設定開機啟動vim /usr/local/php5/etc/php.ini #編輯設定檔 找到:;date.timezone = 修改為:date.timezone = PRC #設定時區 找到:expose_php = On 修改為:expose_php = OFF #禁止顯示php版本的資訊 找到:display_errors = On 修改為:display_errors = OFF #關閉錯誤提示配置nginx支援phpvim /usr/local/nginx/conf/nginx.conf修改/usr/local/nginx/conf/nginx.conf 設定檔,需做如下修改 user www www; #首行user去掉注釋,修改Nginx運行組為www www;必須與/usr/local/php5/etc/php-fpm.conf中的user,group配置相同,否則php運行出錯 index index.php index.html index.htm; #添加index.php # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000#location ~ \.php$ {root html;fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}/etc/init.d/nginx restart #重啟nginx cd /usr/local/nginx/html/ #進入nginx預設網站根目錄rm -rf /usr/local/nginx/html/* #刪除預設測試頁chown www.www /usr/local/nginx/html/ -R #設定目錄所有者chmod 755 /usr/local/nginx/html/ -R #設定目錄許可權 重啟nginx、mysqld、php-fpm服務 安裝配置Cacti Web服務(1)安裝rrdtool、net-snmp以及相關依賴包 for i in ` rpm -q rrdtool net-snmp rrdtool-devel net-snmp-devel net-snmp-utils net-snmp-python net-snmp-perl lm_sensors-devel file-devel rpm-devel file intltool libart_lgpl libart_lgpl-devel elfutils pango-devel* cairo-devel* mysql-devel Mod_auth_mysql php-mysql cairo dejavu-fonts-common.noarch dejavu-lgc-sans-mono-fonts.noarch dejavu-sans-mono-fonts.noarch fontpackages-filesystem.noarch libXft libXrender libthai pango pixman perl-rrdtool net-snmp net-snmp-utils tcp_wrappers-devel |grep 'not installed' | awk '{print $2}' `; do yum -y install $i; done修改snmpd設定檔, vim /etc/snmp/snmpd.conf 找到先對應的行修改,修改為下面內容 com2sec notConfigUser 127.0.0.1 public #127.0.0.1為cacti監控伺服器 #view systemview included .1.3.6.1.2.1.25.1.1 access notConfigGroup "" any noauth exact all none none view all included .1 80啟動snmpd service snmpd start chkconfig snmpd on安裝Cacti cd /usr/local/srctar zxvf cacti-0.8.7i.tar.gzcd cacti-0.8.7icp -r * /usr/local/nginx/htmlchown www.www /usr/local/nginx/html/ -R #設定目錄所有者chmod 755 /usr/local/nginx/html/ -R #設定目錄許可權配置Cacti資料庫 mysql -u root -p #輸入密碼進入MySQL控制台 create database cactidb; #建立資料庫cactidbinsert into mysql.user(Host,User,Password) values('localhost','cactiuser',password('123456')); #建立資料庫使用者cactiuser 密碼 123456grant all on cactidb.* to 'cactiuser'@'%' identified by '123456' with grant option; #授權使用者cactiuser對資料庫cactidb完全訪問flush privileges; #重新整理系統授權表,使設定生效 use cactidbsource /usr/local/nginx/html/cacti.sql #匯入cacti資料庫檔案vim /usr/local/nginx/html/include/config.php #設定資料庫串連 $database_type = "mysql";$database_default = "cactidb"; #資料庫名稱$database_hostname = "localhost"; #主機名稱,預設即可$database_username = "cactiuser"; #資料庫使用者名稱$database_password = "123456"; #資料庫密碼$database_port = "3306"; #MySQL資料庫預設串連連接埠$database_ssl = false;開啟http://Cacti-IP/ 出現安裝介面 點擊一步 每個選項下面都是[OK: FILE FOUND],表示設定檔路徑全部正確 完成之後出現登入介面 預設的使用者名稱和密碼都是admin 第一次要求輸入密碼修改,修改後點save 因為cacti主要是通過poller.php來觸發資料的採集,在crontab設定cacti每隔5分鐘採集一次資料 crontab -e */5 * * * * php /usr/local/nginx/html/poller.php &> /dev/null重啟crond /usr/bin/php /usr/local/nginx/html/poller.php #手動重新整理資料