smokeping是对IDC网络质量,稳定性等最好的检测工具,包括常规的 ping,dig,echoping,curl等,可以 监视 www 服务器性能,监视 dns 查询性能,监视 ssh 性能等。Smokeping是rrdtool的作者Tobi Oetiker的作品,所以底层也是 rrdtool 做支持。smokeping是一个很老的开源项目了,不过考虑到现网以下两方面的需求,感觉还是有必要部署的。1、针对虚拟化平台主机重启速度较快,普通的监控平台可能敏感度不够;2、业务和网络部门经常有对网络质量和页面访问速度对比的需求。

一、smokeping的安装条件

smokeping安装前需要准备的相关包可以参看官方页面:http://oss.oetiker.ch/smokeping/doc/smokeping_install.en.html ,所需要具体如下(其中带有optional 的选项为可选包):

 1RRDtool 1.2.x or later
 2FPing (optional)
 3EchoPing (Optional)
 4Curl (Optional)
 5dig (Optional)
 6SSH (Optional)
 7Webserver
 8Perl 5.8.8 or later(对应的模块如下)
 9    FCGI
10    CGI, CGI::Fast
11    Config::Grammar
12    LWP
13    Socket6 (optional)
14    Net::Telnet (optional)
15    Net::OpenSSH (optional)
16    Net::DNS (optional)
17    Net::LDAP (optional)
18    IO::Socket::SSL (optional)
19    Authen::Radius (optional)

在centos环境下,上述包基本都可以通过增加第三方源epel 进行安装完整。我测试时安装了如下包:

1[root@361way ~]# yum install rrdtool fping wget dig curl bind-utils gcc make perl perl-Net-Telnet perl-Net-DNS perl-LDAP perl-libwww-perl perl-RadiusPerl perl-IO-Socket-SSL perl-Socket6 perl-FCGI perl-RRD-Simple  perl-CPAN rrdtool-perl perl-CGI-SpeedCGI perl-ExtUtils-MakeMaker

由于我是使有的nginx整合,如果使有apache,还需要安装如下包:

1# yum install mod_fcgid httpd httpd-devel

二、安装smokeping

1、安装smokeping

1[root@361way src]# wget http://oss.oetiker.ch/smokeping/pub/smokeping-2.6.11.tar.gz
2[root@361way src]# tar xvf smokeping-2.6.11.tar.gz
3[root@361way src]# cd smokeping-2.6.11
4[root@361way smokeping-2.6.11]# ./configure --prefix=/opt/smokeping
5[root@361way smokeping-2.6.11]# gmake install
6[root@361way smokeping-2.6.11]# make install

在进行configure配置时,如果发现perl 环境有缺少的包,会报如下错误:

 1** Aborting Configure ******************************
 2   If you know where perl can find the missing modules, set
 3   the PERL5LIB environment variable accordingly.
 4   FIRST though, make sure that 'perl' starts the perl
 5   binary you want to use for SmokePing.
 6   Now you can install local copies of the missing modules
 7   by running
 8     ./setup/build-perl-modules.sh /opt/smokeping/thirdparty
 9   The RRDs perl module is part of RRDtool. Either use the rrdtool
10   package provided by your OS or install rrdtool from source.
11   If you install from source, the RRDs module is located
12   PREFIX/lib/perl

出现上面的错误时,可以通过执行./setup/build-perl-modules.sh /opt/smokeping/thirdparty 安装所需要的包(需要能连接外网)。安装完成后,再执行后面的gmake和make install 的操作。

2、创建数据文件目录

1# cd /opt/smokeping
2# mkdir data var cache
3# chown -R www:www data  cache

nginx是用www用户执行的,所以这里相应的数据目录要有可写的权限 。

3、创建启停脚本文件

在服务启动之前,需要先配置config配置文件,配置这部分可以参看本篇第三部分。

1# wget https://blog.361way.com/wp-content/uploads/2016/05/smokeping.txt
2# mv smokeping.txt /etc/init.d/smokeping
3# chmod 755 /etc/init.d/smokeping

如需要配置开机自启动,还需要使用chconfig 命令执行如下操作:

1# chkconfig –add smokeping
2# chkconfig smokeping on

4、配置文件权限修改

smokeping_secrets主要是用于配置master/slave通信时的密钥文件,单主机运行smokeping时不涉及

1# cd /opt/smokeping/etc/
2# for foo in *.dist; do cp $foo `basename $foo .dist`; done
3# chmod 600 /opt/smokeping/etc/smokeping_secrets.dist

三、smokeping配置文件

smokeping配置可以分为七部分,如下:

第一部分 General 属于基本配置。值得注意的地方有imgcache和imgurl这两个参数,要注意一下,和apache配置相关联,如果配置不好的话,打开web的时候图片会显示不出来;

第二部分Alerts 是属于报警配置;

第三部分 Database RRD数据库配置。step和pings这两个参数要在启动smokeping之前配置好,如果在smokeping启动后再配置这两个参数,需要把原来的rrd文件都删除,然后再启动smokeping才可以。所以这个一定要先配置好。默认是step=300 pings=20 是每300s内执行20次ping的动作;

第四部分Presentation 是关于网络状态的一些配置和解释,基本不需要更改;

第五部分Porbes 指针配置,我们一般只用到fping所以就只要以下配置就可以了,确定路径是否正确,这里还可以设置fping包的大小,参数是packetsize;

第六分部Slaves 主从关系里面的从服务器相关配置,smokeping里面从服务器只需要安装好就可以了,不需要配置,smokeping的所有配置都是在master上面设置。这里主要设置好secrets密码文件和从服务器配置就可以了;

第七部分 Targets 目标配置。在这里可以单独配置一台服务器的监控情况,需要从slave服务器上取值的配置也是在这里配置。我已经用注释说明的很清楚了。这里要注意一下。+代表一级菜单 +下面的++是继承上面的+成为二级菜单。 而且可以有多个一级菜单和二级菜单。

config配置示例如下:

  1# cat config
  2*** General ***
  3owner    = Peter Random
  4contact  = [email protected]
  5mailhost = my.mail.host
  6sendmail = /usr/sbin/sendmail
  7# NOTE: do not put the Image Cache below cgi-bin
  8# since all files under cgi-bin will be executed ... this is not
  9# good for images.
 10imgcache = /opt/smokeping/cache
 11imgurl   = cache
 12datadir  = /opt/smokeping/data
 13piddir  = /opt/smokeping/var
 14#cgiurl   = http://192.168.0.110/smokeping/smokeping.cgi
 15cgiurl   = http://192.168.0.110/smokeping.fcgi //这个地址很重要
 16smokemail = /opt/smokeping/etc/smokemail
 17tmail = /opt/smokeping/etc/tmail
 18# specify this to get syslog logging
 19syslogfacility = local0
 20# each probe is now run in its own process
 21# disable this to revert to the old behaviour
 22# concurrentprobes = no
 23*** Alerts ***
 24to = [email protected]
 25from = [email protected]
 26+someloss
 27type = loss
 28# in percent
 29pattern = >0%,*12*,>0%,*12*,>0%
 30comment = loss 3 times  in a row
 31*** Database ***
 32#step     = 300
 33step     = 60
 34pings    = 20
 35# consfn mrhb steps total
 36AVERAGE  0.5   1  1008
 37AVERAGE  0.5  12  4320
 38    MIN  0.5  12  4320
 39    MAX  0.5  12  4320
 40AVERAGE  0.5 144   720
 41    MAX  0.5 144   720
 42    MIN  0.5 144   720
 43*** Presentation ***
 44template = /opt/smokeping/etc/basepage.html.dist
 45charset = utf-8
 46+ charts
 47menu = Charts
 48title = The most interesting destinations
 49++ stddev
 50sorter = StdDev(entries=>4)
 51title = Top Standard Deviation
 52menu = Std Deviation
 53format = Standard Deviation %f
 54++ max
 55sorter = Max(entries=>5)
 56title = Top Max Roundtrip Time
 57menu = by Max
 58format = Max Roundtrip Time %f seconds
 59++ loss
 60sorter = Loss(entries=>5)
 61title = Top Packet Loss
 62menu = Loss
 63format = Packets Lost %f
 64++ median
 65sorter = Median(entries=>5)
 66title = Top Median Roundtrip Time
 67menu = by Median
 68format = Median RTT %f seconds
 69+ overview
 70width = 600
 71height = 50
 72range = 10h
 73+ detail
 74width = 600
 75height = 200
 76unison_tolerance = 2
 77"Last 3 Hours"    3h
 78"Last 30 Hours"   30h
 79"Last 10 Days"    10d
 80"Last 400 Days"   400d
 81#+ hierarchies
 82#++ owner
 83#title = Host Owner
 84#++ location
 85#title = Location
 86*** Probes ***
 87+ FPing
 88binary = /usr/sbin/fping
 89#$packetsize = 2000
 90*** Targets ***
 91probe = FPing
 92menu = Top
 93title = Network Latency Grapher
 94remark = Welcome to the SmokePing website of xxx Company. \
 95         Here you will learn all about the latency of our network.
 96+ Test
 97menu= 361way.com smokeping测试
 98title=运维之路测试
 99#parents = owner:/Test/James location:/
100++ 361way
101menu = 361
102title =361way.com
103alerts = someloss
104host = www.361way.com
105++ 102Host
106menu = 102
107title = host102
108host = 192.168.0.102
109++ 200Host
110menu = 200
111title = host200
112host = 192.168.0.200
113++ 201Host
114menu = 201
115title = host201
116host = 192.168.0.201
117+ allHost
118menu = allhost
119title = All Host In One Graph
120host = /Test/102Host /Test/200Host /Test/201Host

四、nginx反向代理smokping cgi 页面

nginx 与 smokping整合,需要用到cgi 运行的一个插件spawn-fcgi ,该包可以通过使用epel 源里下载安装到,直接yum -y install spawn-fcgi 即可。

1、创建smokeping-fastcgi 启动脚本

1[root@361way smokeping]# cat /opt/smokeping/smokeping-fastcgi
2#!/bin/sh
3/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9007 -P /var/run/smokeping-fastcgi.pid -u www -f /opt/smokeping/htdocs/smokeping.fcgi

2、配置smokeping-fastcgi 自动启动脚本

 1[root@361way ~]# cat /etc/init.d/smokeping-fastcgi
 2#!/bin/bash
 3FCGI_SCRIPT=/opt/smokeping/smokeping-fastcgi
 4FASTCGI_USER=www
 5PIDFILE=/var/run/smokeping-fastcgi.pid
 6RETVAL=0
 7case "$1" in
 8start)
 9$FCGI_SCRIPT
10RETVAL=$?
11;;
12stop)
13PID=`cat $PIDFILE`
14kill -9 $PID $(pgrep -P $PID)
15RETVAL=$?
16;;
17restart)
18PID=`cat $PIDFILE`
19kill -9 $PID $(pgrep -P $PID)
20$FCGI_SCRIPT
21RETVAL=$?
22;;
23*)
24echo "Usage: smokeping-fastcgi {start|stop|restart}"
25exit 1
26;;
27esac
28exit $RETVAL

3、nginx配置文件

有域名的可以将其中localhost替换成相应的域名

 1server {
 2        listen       80;
 3        server_name  localhost;
 4        location / {
 5            root   /opt/smokeping/htdocs/;
 6            index  index.html index.htm index.cgi;
 7        }
 8        location ~ .*\.fcgi$ {
 9            root  /opt/smokeping/htdocs/;
10            fastcgi_pass   127.0.0.1:9007;
11            include /opt/nginx/conf/fastcgi_params;
12        }
13}

4、启动nginx与smokeping-fastcgi

1# /etc/init.d/smokeping-fastcgi start
2# /opt/nginx/sbin/nginx

在测试的时候发现一个问题:somkeping程序每一次重启时,smokeping-fastcgi 就会退出,也需要重启。所以最好给smokeping-fastcgi 开启一个守护进程,或者在smokeping 脚本中增加smokeping-fastcgi 重启的动作。

此时打开http://ip/smokeping.fcgi,即可以看到类似如下内容:

smokeping nginx
smokeping nginx

当然也可以将多台主机的信息配置到一个页面中,具体也可以参看官方demo页