看起来很高端的项目。我感觉用Nginx比用Apache快多了。演示地址:https://ping.fly2x.cn

系统版本CentOS 7.2.

11-02更新:

更新fping版本号为最新。

参考链接

下面的过程抄袭自:
https://blog.newtouch.com/setup-config-smokeping/#%E5%8F%82%E6%95%B0%E5%9F%BA%E6%9C%AC%E9%85%8D%E7%BD%AE
这里有2.6.11版本的安装过程。在2.7.1版本中,有些地方有变动。

有变化的地方

  • 新增安装CoreListyum install perl-Module-CoreList
  • gmake install变更为make install还是用gmake install吧
  • cache目录改到了htdocs目录下面
  • Apache虚拟主机配置文件
  • Nginx配置

具体过程

1. 安装时间同步服务

yum -y install ntpdate
ntpdate times.aliyun.com

2. 安装依赖

yum groupinstall "Compatibility libraries" "Base" "Development tools" -y
yum -y install cpan perl perl-FCGI perl-CGI perl-Digest-HMAC perl-Net-Telnet perl-Net-OpenSSH perl-Net-SNMPperl-LDAP perl-Net-DNS perl-IO-Pty-Easy perl-Test-Simple perl-Sys-Syslog perl-libwww-perlperl-IO-Socket-SSL perl-Socket6 perl-CGI-SpeedyCGI perl-FCGI perl-Time-HiResperl-ExtUtils-MakeMaker rrdtool rrdtool-perl curl fping httpd httpd-devel gccmake wget libxml2-devel libpng-devel glib pango pango-devel freetypefreetype-devel fontconfig cairo cairo-devel libart_lgpl libart_lgpl-devel mod_fcgidscreen rrdtool perl-rrdtool perl-tests perl-Mozilla-CA

3. 安装fping

cd /home
wget https://fping.org/dist/fping-4.1.tar.gz
tar -zxvf fping-4.1.tar.gz -C /usr/local/src
cd /usr/local/src/fping-4.1
./configure
make && make install

4. 安装Echoping

cd /home
wget https://fossies.org/linux/misc/old/echoping-6.0.2.tar.gz
tar -zxvf echoping-6.0.2.tar.gz -C /usr/local/
cd /usr/local/echoping-6.0.2
yum install -y popt-devel openssl openssl-devel
./configure --prefix=/usr/local/echoping --with-ssl --without-libidn
make && make install

5. 安装SmokePing

1. 安装Perl模块

yum install perl-Module-Metadata perl-Perl-OSType perl-Module-Build perl-HTTP-Cookies perl-HTTP-Daemon perl-HTTP-Negotiate perl-Module-CoreList -y
perl -MCPAN -e 'install Config::Grammar'
perl -MCPAN -e 'install "LWP::Simple"'
perl -MCPAN -e 'install "Net::SNMP"'
perl -MCPAN -e 'install "Net::LDAP"'
perl -MCPAN -e 'install IO::Pty'

如果有什么模块找不到,安装失败的,先yum search perl-模块名称,再yum install 安装包名称.

2. 安装Smokeping本体

cd /home
wget https://oss.oetiker.ch/smokeping/pub/smokeping-2.7.2.tar.gz
tar -zxvf smokeping-2.7.2.tar.gz -C /usr/local/src
cd /usr/local/src/smokeping-2.7.2
export PERL5LIB=/usr/local/smokeping/thirdparty/lib/perl5/
./configure --prefix=/usr/local/smokeping
/usr/bin/gmake install

3. 创建相关目录和日志文件

cd /usr/local/smokeping
mkdir htdocs/cache data var 
touch /var/log/smokeping.log 
chown apache:apache htdocs/cache data/ var/ /var/log
  • /usr/local/smokeping/htdocs/cache/ 存放缓存文件;
  • /usr/local/smokeping/data/ 存放SmokePing的RRD数据文件;
  • /usr/local/smokeping/var/log/ 存放SmokePing的日志文件;
  • tail /var/log/httpd/error_log Apache错误日志

4. 参数配置

1. 生成配置文件

cp /usr/local/smokeping/htdocs/smokeping.fcgi.dist /usr/local/smokeping/htdocs/smokeping.fcgi 
cp /usr/local/smokeping/etc/config.dist /usr/local/smokeping/etc/config

2. 修改配置文件

*** General ***

owner    = Peter Random
contact  = [email protected]
mailhost = my.mail.host
sendmail = /usr/sbin/sendmail
# NOTE: do not put the Image Cache below cgi-bin
# since all files under cgi-bin will be executed ... this is not
# good for images.
imgcache = /usr/local/smokeping/htdocs/cache #修改cache目录位置
imgurl   = cache
datadir  = /usr/local/smokeping/data
piddir  = /usr/local/smokeping/var
cgiurl   = http://此处修改为服务器ip或者域名/smokeping.cgi
smokemail = /usr/local/smokeping/etc/smokemail.dist
tmail = /usr/local/smokeping/etc/tmail.dist
# specify this to get syslog logging
syslogfacility = local0
# each probe is now run in its own process
# disable this to revert to the old behaviour
# concurrentprobes = no

*** Alerts ***
to = [email protected]
from = [email protected]

+someloss
type = loss
# in percent
pattern = >0%,*12*,>0%,*12*,>0%
comment = loss 3 times  in a row

*** Database ***

step     = 300 #轮询周期,单位秒
pings    = 20 # 每个周期ping多少次

*** Presentation ***
charset= utf-8  #新增一行,将网页编码指定为UTF-8


#中间省略,没啥好看的

*** Targets ***
probe = FPing
menu = Top
@include targets #新建一个文件来存放要ping的主机

5. 安装中文字体

yum -y install wqy-zenhei-fonts
vim /usr/local/smokeping/lib/Smokeping/Graphs.pm

修改文件中,增加一行字体配置部分('--font', "TITLE:20:WenQuanYi Zen Hei Mono",):


    if ($mode =~ /[anc]/){
        my $val = 0;
        for my $host (@hosts){
            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' );
            my $ERROR = RRDs::error();
            return "RRDtool did not understand your input: $ERROR." if $ERROR;
            $val = $graphret->[0] if $val < $graphret->[0];
        }
        $val = 1e-6 if $val =~ /nan/i;
        $max = { $tasks[0][1] => $val * 1.5 };
    }

修改字符集,增加一行(charset= utf-8)

vim /usr/local/smokeping/etc/config

6. 增加登陆验证

用户名:smokeping
密码:自行设置

htpasswd -c /usr/local/smokeping/htdocs/htpasswd smokeping
chmod 600 /usr/local/smokeping/etc/smokeping_secrets.dist

7. 修复Web页面一个小Bug

监控点列表超出页面后,并没有滚动条可以滚动。此处修复。
vim /usr/local/smokeping/htdocs/css/smokeping-screen.css
在32行以后.sidebar里面新增一个属性overflow:scroll

.sidebar {
    background: #4c4c4c;
    position: fixed;
    top: 0;
    left: 0;
    width: 250px;
    height: 100%;
    color: #ccc;
    overflow:scroll;
}

8. 增加监控对象

原教程里面是直接编辑/usr/local/smokeping/etc/config。
我这里把这个监控目标配置文件独立了出来,编辑这个文件。
vim /usr/local/smokeping/etc/targets
此处的配置文件用户生成Web页面的菜单,以及监控目标。格式如下:

+ 一级目录,用于url显示,只能用英文
menu = 目录显示的名称,可以用中文
title = 点开目录后,右边显示的标题
++ 二级目录,用于url显示,只能用英文
menu = 二级目录显示的名称,可以用中文
title = 二级目录显示的名称,可以用中文
host = 要监控的目标的ip或者域名
# 只有一级菜单
+ Aliyun
menu = 阿里云
title = 阿里云dns
host = 223.5.5.5


# 二级菜单
+ Euro
menu = 欧洲
title = 欧洲节点
++ Italy
menu = 意大利
title = 意大利节点
host = www.domain.com
++ Britain
menu = 英国
title = 英国xx机房
host = 5.5.5.5


# 三级菜单
+ IDC
menu = IDC LookingGlass
title = 主机商的测试IP

++ Vultr
+++ Vultr-los
menu = 洛杉矶
title = Vultr 洛杉矶
host = losangels.vultr.com
+++ Vultr-tokyo
menu = 东京
title = Vultr 东京
host = tokyo.lg.vultr.com

++ DigitalOcean
+++ Do-london
menu = 伦敦
title = DO 伦敦
host = london.lg.digitalocean.com

+++ Do-tokyo
menu = 东京
title = DO东京
host = tokyo.lg.digitalocean.com

# 将已有的节点重新分类
+ AllTokyo
menu = 所有东京机房的主机
host = IDC\Vultr-tokoy IDC\Do-tokyo
配置在Web页面中的作用

6.Web服务器配置

1. Apache

为SmokePing新增一个Apache虚拟主机配置
vim /etc/httpd/conf.d/smokeping.conf

<VirtualHost *:80>
    ServerName ip或者域名
    Alias /cache "/usr/local/smokeping/htdocs/cache/"
    Alias / "/usr/local/smokeping/htdocs/"
    <Directory  "/usr/local/smokeping/htdocs/">
        AllowOverride None
        Options All
        AddHandler cgi-script .fcgi .cgi
        AllowOverride AuthConfig
        Order allow,deny
        Allow from all
        AuthName "Smokeping"
        AuthType Basic
        AuthUserFile /usr/local/smokeping/htdocs/htpasswd
        Require valid-user
        DirectoryIndex smokeping.fcgi
    </Directory>
    <Directory  "/usr/local/smokeping/htdocs/cache/">
        AllowOverride None
        Options All
        AllowOverride All
        Order allow,deny
        Allow from all
        AuthName "Smokeping"
        AuthType Basic
        AuthUserFile /usr/local/smokeping/htdocs/htpasswd
        Require valid-user
    </Directory>
</VirtualHost>

重启Apache服务器service httpd restart
设置cache目录权限chmod 755 -R /usr/local/smokeping/htdocs/cache
给cache目录增加权限管理文件.htaccess。vim /usr/local/smokeping/htdocs/cache/.htaccess

<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy all
</Files>

2. Nginx

Nginx配置
抄袭自:http://www.361way.com/nginx-smokeping/5096.html
还是以上面安装smokeping的位置为例:/usr/local/smokeping

1. 安装spawn-fcgi, EPEL源里面有此软件

yum -y install spawn-fcgi

2. 创建一个spawn运行smokeping cgi的脚本

cd /usr/local/smokeping
touch smokeping-fastcgi
chmod +x smokeping-fastcgi
nano smokeping-fastcgi

输入以下内容

#!/bin/sh
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9007 -P /var/run/smokeping-fastcgi.pid -u nginx -f /usr/local/smokeping/htdocs/smokeping.fcgi

3. 运行该脚本

[root@cloud smokeping]# ./smokeping-nginx-cgi 
spawn-fcgi: child spawned successfully: PID: 31352

重启smokeping后,也要重启这个脚本。
首先查看脚本的pid,结束它,然后再运行。

[root@localhost data]# netstat -anop | grep 9007
tcp        0      0 127.0.0.1:9007          0.0.0.0:*               LISTEN      9781/perl            off (0.00/0/0)
[root@localhost data]# kill 9781

4. 为Nginx添加配置文件。此处为yum安装nginx的配置。使用各种一件包的请自行修改include后面的路径。

 server {
    listen       80;
    server_name  smokeping.example.cn;
    location / {
        root   /usr/local/smokeping/htdocs/;
        index  smokeping.fcgi;
    }
    location ~ .*\.fcgi$ {
        root  /usr/local/smokeping/htdocs/;
        fastcgi_pass   127.0.0.1:9007;
        include /etc/nginx/conf/fastcgi_params;
    }
}

5. 检查Nginx配置,并重载配置文件。

nginx -t
nginx -s reload

7. 多个机器ping(Slave 服务器)

1. 配置主服务器 master

在主服务器的配置文件里面找到slave相关配置。
编辑配置文件/usr/local/smokeping/etc/config.

*** Slaves ***
secrets=/opt/smokeping/etc/smokeping_secrets.dist
+ myslave1
display_name=美国电信
color=ff0000

+ myslave2
display_name=法国电信
color=10663A
然后编辑slave密码文件/opt/smokeping/etc/smokeping_secrets.dist。
每行一条,格式为"slave的名称:slave的密码"。名称和配置文件里面的名称一致。

myslave1:slave1spassword
myslave2:slave2spassword
在要监控的菜单项目下面添加slave=slave的名称,可以在一二三级菜单添加,代表监控此菜单以下的所有项目。

+ Other
menu = ChinaNet & ChinaUnicom & ChinaMobile
title = Test smokeping
slaves=myslave1
++ dianxin
menu = 电信网络监控
title = 电信网络监控列表
slaves=myslave1
host = /Other/dianxin/dianxin-bj /Other/dianxin/dianxin-hlj /Other/dianxin/dianxin-tj  /Other/dianxin/dianxin-sc  /Other/dianxin/dianxin-sh /Other/dianxin/dianxin-gz
+++ dianxin-bj
menu = 北京电信
title = 北京电信
slaves=myslave2
alerts = someloss
host = 202.96.199.133

2. 配置从服务器slave

按照上面提示安装好smokeping后,先在一个文件里面填写上面设定的密码。
比如/usr/local/smokeping/etc/secrets.txt。
然后运行以下命令:

bin/smokeping --master-url=http://ping.fly2x.cn/smokeping.fcgi --slave-name="myslave1" --cache-dir=/usr/local/smokeping/data/ --shared-secret=/usr/local/smokeping/etc/secrets.txt

如果有问题则运行,查看是否有错误。

bin/smokeping --master-url=http://ping.fly2x.cn/smokeping.fcgi --slave-name="myslave1" --cache-dir=/usr/local/smokeping/data/ --shared-secret=/usr/local/smokeping/etc/secrets.txt --debug

8. Q&A

1. Q:FPing: Executing /usr/sbin/fping ... failed at FPing.pm line 146.

A: 尝试运行一下屏幕提示的命令,如果提示fping不存在,则需要创建一个软链接(快捷方式)。例如用which fping查看fping实际地址是/usr/local/sbin/fping,创建一个软链接ln -s /usr/local/sbin/fping /usr/sbin/fpingln命令第二个参数是文件的地址,第三个参数是要创建的软链接的地址。

2. Q:warning I don't know the slave xxx

A: master的配置文件没有写对应slave的配置,写入即可。配置文件/usr/local/smokeping/etc/config.

3. Q:No secret found for slave xxx

A: master的密码文件没有对应的slave,写入即可。密码文件/opt/smokeping/etc/smokeping_secrets.dist.

4. Q: master says 502, 500, 5xx~之类的错误

A: 如果是nginx的话,重启smokeping程序后还要重启cgi脚本。

5. Q: WARNING Master said 413 Request Entity Too Large

A: 在Nginx配置文件server里面添加client_max_body_size 50m;

6. Q: Slave端正常,但是不显示slave的数据;新增监控点不显示数据;

A: 将/usr/local/smokeping/data目录,以及子目录和文件的所有者改为nginx或者www,具体那个取决于你的nginx或者apache的用户。

chown -R /usr/local/smokeping/data nginx:nginx
或
chown -R /usr/local/smokeping/data www:www

7. Q: ERROR: the shared secret file (/usr/local/smokeping/etc/secrets.txt) is world-readable or writable at /usr/local/smokeping/lib/Smokeping.pm line 4237.

A: Slave的密码文件权限不正确,修改一下。

chmod 600 /usr/local/smokeping/etc/secrets.txt

8. Q: WARNING Master said 501 Protocol scheme 'https' is not supported (LWP::Protocol::https not installed)

A: 安装Perl的https模块。

yum install perl-LWP-Protocol-https

9.Q: Server said WARNING: No targets found for slave 'us'

Sent data to Server. Server said WARNING: No targets found for slave 'us'
ERROR: we did not get config from the master. Maybe we are not configured as a slave for any of the targets on the master ?

A:没有给Slave配置要ping的目标。

文章目录