1. 程式人生 > >基於Nginx搭建的smokeping監控及配置

基於Nginx搭建的smokeping監控及配置

剛接觸Linux中的smokeping監控時,什麼也不懂,百度安裝資料都不得心應手。根據多次嘗試,總結得出的成功安裝方法,很實用。相關的命令我也寫出來了,供新手學習,大神就請跳過。在此就不多做解釋什麼是smokeping了,下面來開始安裝吧。

此方法安裝的smokeping預設位置是/usr/share/nginx/html/smokeping下,nginx使用80埠,網頁登入網址:IP/smokeping.fcgi,無需驗證使用者名稱和密碼。

1.作業系統的安裝

作業系統安裝centos6.0以上版本 64位

2.基本環境的設定

網絡卡的設定 /etc/sysconfig/network-scripts/ifcfg-eth0

DNS的設定 /etc/resolv.conf

設定時區 tzselect
依次選擇5-9-1-1所在大洲—所在國家—所使用的時區—確定所選時區。
檢驗 輸入date 檢視是否為CST時區
同步時間
ntpdate time.nist.gov
安裝epel源 rpm -Uvh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm

3.web伺服器的基本設定
安裝PHP、nginx、spawn-fcgi等基礎支援軟體
yum install -y openssh-clients telnet wget nginx php-cgi php-cli spawn-fcgi mysql-server mysql rrdtool net-snmp net-snmp-utils net-snmp-devel php-mysql php-snmp


配置PHP管理器 vi /etc/sysconfig/spawn-fcgi
在檔案改位置新增如下命令
       #SOCKET=/var/run/php-fcgi.sock
       #OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"
       SOCKET=/var/run/php-fcgi.sock
       OPTIONS="-u nginx -g nginx -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi" 

重啟php管理器及設定開機啟動服務 
/etc/init.d/spawn-fcgi restart 
chkconfig spawn-fcgi on 
建立/var/lib/php下session 目錄
mkdir -p /var/lib/php/session
chown -R nginx.nginx /var/lib/php/session
修改PHP檔案配置 vi /etc/php.ini
(1)修改PHP時區為Asia/ShangHai,在檔案如下位置新增該句命令
;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;
[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
;date.timezone =
date.timezone = Asia/ShangHai
(2)修改PHP呼叫記憶體限制,在檔案如下位置修改如下
;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;
; Maximum execution time of each script, in seconds
; http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time
max_execution_time = 30
; Maximum amount of memory a script may consume (128MB)
; http://www.php.net/manual/en/ini.core.php#ini.memory-limit
#memory_limit = 128M
memory_limit = 512M 
為nginx新增fcgi[不安裝無法開啟smokeping],支援fcgi支援需要一些元件支援,所有元件均下載到tmp目錄下
安裝元件FCGI
cd /tmp
yum -y install gcc wget perl-devel perl-CPAN
wget http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/FCGI-0.74.tar.gz
tar zxvf FCGI-0.74.tar.gz
cd FCGI-0.74
perl Makefile.PL
make && make install

安裝元件FCGI-ProcManager

cd /tmp
wget http://search.cpan.org/CPAN/authors/id/B/BO/BOBTFISH/FCGI-ProcManager-0.24.tar.gz
tar zxvf FCGI-ProcManager-0.24.tar.gz
cd FCGI-ProcManager-0.24
perl Makefile.PL
make && make install

安裝元件Getopt-Long
cd /tmp
wget http://search.cpan.org/CPAN/authors/id/J/JV/JV/Getopt-Long-2.42.tar.gz
tar zxvf Getopt-Long-2.42.tar.gz
cd Getopt-Long-2.42
perl Makefile.PL
make && make install
安裝元件IO-String
cd /tmp
wget http://search.cpan.org/CPAN/authors/id/G/GA/GAAS/IO-String-1.08.tar.gz
tar zxvf IO-String-1.08.tar.gz
cd IO-String-1.08
perl Makefile.PL
make && make install

安裝元件IO
cd /tmp
wget http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz
tar zxvf IO-1.25.tar.gz
cd IO-1.25
perl Makefile.PL
make && make install

 安裝元件IO-All
cd /tmp
wget http://search.cpan.org/CPAN/authors/id/I/IN/INGY/IO-All-0.66.tar.gz
tar zxvf IO-All-0.66.tar.gz
cd IO-All-0.66
perl Makefile.PL
make && make install
 安裝元件ExtUtils-Constant
cd /tmp
wget http://search.cpan.org/CPAN/authors/id/N/NW/NWCLARK/ExtUtils-Constant-0.23.tar.gz
tar zxvf ExtUtils-Constant-0.23.tar.gz
cd ExtUtils-Constant-0.23
perl Makefile.PL
make && make install

安裝元件Socket
cd /tmp
wget http://search.cpan.org/CPAN/authors/id/P/PE/PEVANS/Socket-2.014.tar.gz
tar zxvf Socket-2.014.tar.gz
cd Socket-2.014
perl Makefile.PL
make && make install
安裝nginx-fcgi
cd /tmp
wget http://122.228.213.147/nginx-fcgi
cp nginx-fcgi /etc/
chmod +x /etc/nginx-fcgi
cd /etc/nginx
mkdir log
cd log/
touch nginx-fcgi.log
chown nginx:nginx  nginx-fcgi.log

 註冊fcgi
cd /tmp
wget http://122.228.213.147/fcgi
cp ./fcgi /etc/init.d/
chmod +x /etc/init.d/fcgi
chkconfig --add fcgi

設定fcgi開機啟動及重啟fcgi服務
chkconfig fcgi on
service fcgi restart

4.smokeping安裝 
安裝smokeping的依賴包
yum install -y rrdtool fping  curl perl-Net-Telnet perl-Net-DNS perl-LDAP perl-CGI-SpeedyCGI perl-libwww-perl  perl-IO-Socket-SSL  rrdtool-perl gcc gcc-c++ zlib-devel pcre-devel perl-devel rrdtool-devel ntp perl-Perl-OSType perl-IPC-Cmd 
安裝smokeping
cd /tmp
wget http://oss.oetiker.ch/smokeping/pub/smokeping-2.6.9.tar.gz
tar -zxvf smokeping-2.6.9.tar.gz
cd smokeping-2.6.9
mkdir /usr/share/nginx/html/smokeping
./configure --prefix=/usr/share/nginx/html/smokeping
#出現提示缺少部分元件
安裝FCGI、CGI、CGI::Fast、Config::Grammar、Digest::HMAC_MD5、Net::Telnet、Net::OpenSSH、Net::SNMP、Net::LDAP、Net::DNS、IO::Pty、LWP
根據提示執行相關語句
./setup/build-perl-modules.sh /usr/share/nginx/html/smokeping/thirdparty
./configure --prefix=/usr/share/nginx/html/smokeping
/usr/bin/gmake install
初始化環境
mkdir /usr/share/nginx/html/smokeping/{cache,data,var}
chmod 400 /usr/share/nginx/html/smokeping/etc/smokeping_secrets.dist
cp /usr/share/nginx/html/smokeping/htdocs/smokeping.fcgi.dist  /usr/share/nginx/html/smokeping/htdocs/smokeping.fcgi
(出現報錯後把smokeping重新安裝一遍,最後執行根據提示執行相關語句/usr/bin/gmake install)
cp /usr/share/nginx/html/smokeping/etc/config.dist  /usr/share/nginx/html/smokeping/etc/config
chown -R nginx:nginx /usr/share/nginx/html/smokeping 
新增smokeping為系統服務,在/etc/init.d建立smokeping檔案
wget http://122.228.213.147/smokeping.sh
mv smokeping.sh smokeping
cp ./smokeping /etc/init.d/
chmod +x /etc/init.d/smokeping
chkconfig --add smokeping
chkconfig smokeping on
service smokeping restart
有警告正常情況。
編輯ngingx配置檔案 vi /etc/nginx/conf.d/default.conf
在檔案中新增對於smokeping的支援(紅色部分為新增、修改內容)
location / {
        root   /usr/share/nginx/html/smokeping;
        index  index.html index.htm;
        }
        location  ~\.(pl|fcgi)$
        {
          root /usr/share/nginx/html/smokeping/htdocs/;
          include fastcgi_params;
          fastcgi_pass unix:/tmp/nginx-fcgi.sock;
          fastcgi_index index.pl;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        }

設定nginx開機啟動及重啟nginx服務
chkconfig nginx on 
service nginx restart 

新增smokeping中文支援
頁面支援中文 vi /usr/share/nginx/html/smokeping/etc/config
cd /usr/share/nginx/html/smokeping/etc    
vi config
在配置檔案的*** Presentation ***下新增(紅色部分為新增內容)
*** Presentation ***
charset = utf-8
template = /usr/local/smokeping/etc/basepage.html.dist
vi basepage.html.dist
如頁面左側層裡文字依然是亂碼  vi /usr/share/nginx/html/smokeping/etc/basepage.html.dist
可在basepage.html.dist檔案頭部位置中新增一句。
<HEAD>
<META charset="utf-8" />
<META HTTP-EQUIV="Cache-Control" content="no-cache">

新增rrd圖片支援中文
安裝中文字型檔  yum -y install wqy-zenhei-fonts.noarch
修改配置檔案  vi /usr/share/nginx/html/smokeping/lib/Smokeping/Graphs.pm
my ($graphret,$xs,$ys) = RRDs::graph
("dummy",
'--start', $tasks[0][1],
'--end', $tasks[0][2],
'--font TITLE:20:"WenQuanYi Zen Hei Mono"',
"DEF:maxping=$cfg->{General}{datadir}${host}.rrd:median:AVERAGE",
'PRINT:maxping:MAX:%le' );

安裝完成後,重啟smokeping服務  /etc/init.d/smokeping restart

瀏覽器輸入:IP/smokeping.fcgi 即可。