CentOS7安装Smokeping2.7.3

准备把国内的一些服务外迁,这里正好做个网络监控来监控以下,smokeping部署在aliyun Hong Kong B区

首先需要删除ECS Centos7中自带的一些阿里云监控

卸载安骑士以及监控:

wget http://update.aegis.aliyun.com/download/uninstall.sh
chmod +x uninstall.sh
./uninstall.sh

 

wget http://update.aegis.aliyun.com/download/quartz_uninstall.sh
chmod +x quartz_uninstall.sh
./quartz_uninstall.sh

可以使用

ps -aux |grep aliyun

来查看是否还有活动的aliyun 服务进程

删除残留服务:

pkill aliyun-service
rm -rf /etc/init.d/agentwatch
rm -rf /usr/sbin/aliyun*
rm -rf /usr/local/aegis*
rm -rf /etc/systemd/system/aliyun*

检查是否有cloudmonitor

cd /usr/local/cloudmonitor/
ls
/usr/local/cloudmonitor/wrapper/bin/cloudmonitor.sh stop
/usr/local/cloudmonitor/wrapper/bin/cloudmonitor.sh remove
rm -rf /usr/local/cloudmonitor

这样才算基本完成删除的aliyun的残留服务

某些文章上还会写有屏蔽云盾的IP,有时间还是得看看log来查看是不是有必要屏蔽云盾的IP

iptables -I INPUT -s 140.205.201.0/28 -j DROP
iptables -I INPUT -s 140.205.201.16/29 -j DROP
iptables -I INPUT -s 140.205.201.32/28 -j DROP
iptables -I INPUT -s 140.205.225.192/29 -j DROP
iptables -I INPUT -s 140.205.225.200/30 -j DROP
iptables -I INPUT -s 140.205.225.184/29 -j DROP
iptables -I INPUT -s 140.205.225.183/32 -j DROP
iptables -I INPUT -s 140.205.225.206/32 -j DROP
iptables -I INPUT -s 140.205.225.205/32 -j DROP
iptables -I INPUT -s 140.205.225.195/32 -j DROP
iptables -I INPUT -s 140.205.225.204/32 -j DROP

这篇文章到了这里才算是完成了服务器的初始清理工作

在安装之前我们需要关闭selinux, firewall, 同步一下时间

yum -y install ntpdate
ntpdate 0.pool.ntp.org
sestatus

或者从www.ntppool.org中选择一个

下面开始进入安装工作 ,其实在作者的官方网站把过程写的清清楚楚:

https://oss.oetiker.ch/smokeping/doc/smokeping_install.en.html
  1. 安装epel package
yum install epel-release -y

    2. 安装development tools

yum groupinstall "Development tools" -y

     3. 安装fping (smokeping 2.7.2以上需要fping4.0以上,因此需要手动编译)

cd ~
wget https://fping.org/dist/fping-4.2.tar.gz
tar -zxvf fping-4.2.tar.gz
cd fping-4.2
./configure
make
make install

安装好的fping位于/usr/local/sbin/fping

      4. 安装echoping (预防以后需要做tcp ping)

cd ~
wget https://fossies.org/linux/misc/old/echoping-6.0.2.tar.gz
tar -zxvf echoping-6.0.2.tar.gz
cd echoping-6.0.2
yum install -y popt-devel openssl openssl-devel
./configure --with-ssl --without-libidn
make
make install

安装好的echoping位于/usr/local/bin/echoping

        5 安装需要的package

cd ~
yum install rrdtool rrdtool-perl curl bind-utils gcc make vim gcc-c++ -y
yum install perl-core -y

5.5 开启SSL支持

yum install perl-LWP-Protocol-https

6. 安装apache, 注意smokeping是fcgi程序,因此apache 需要安装mod_fcgid, http的版本是2.4.6,

yum install httpd httpd-devel
yum install mod_fcgid
systemctl enable httpd

        7. 安装nginx, nginx的版本是1.16

//or reference http://nginx.org/en/linux_packages.html#RHEL-CentOS
yum install nginx -y

systemctl enable nginx

service nginx start

    8 安装smokeping

cd ~

wget https://oss.oetiker.ch/smokeping/pub/smokeping-2.7.3.tar.gz

tar -zxvf smokeping-2.7.3.tar.gz

cd smokeping-2.7.3

./configure --prefix=/opt/smokeping PERL5LIB=/usr/lib64/perl5/

/usr/bin/gmake install

 

注意,如果我们不加上 ./configure –prefix=/opt/smokeping PERL5LIB=/usr/lib64/perl5/,会提示下面的:

 

** Ready to install Smokeping ******************************

Settings:

PERL5LIB = not set
PERL = /usr/bin/perl

The Smokeping Makefiles use GNU make functionality.
Continue installation with

/usr/bin/gmake install

 

如果我们加上PERL5LIB=/usr/lib64/perl5的话,会显示如下:

** Ready to install Smokeping ******************************

Settings:

PERL5LIB = /usr/lib64/perl5/
PERL = /usr/bin/perl

The Smokeping Makefiles use GNU make functionality.
Continue installation with

/usr/bin/gmake install

9 初始化smokeping, 我们需要建立三个文件夹,data用来存放rrd文件,var用来存放smokeping的pid, log用来存放smokeping.log

cd /opt/smokeping

mkdir data var log

chmod 777 data var log

touch log/smokeping.log

rename config file and set permission

cd /opt/smokeping/etc/

for foo in *.dist; do cp $foo `basename $foo .dist`; done

chmod 600 /opt/smokeping/etc/smokeping_secrets.dist

配置config文件

编辑/opt/smokeping/etc/config, 配置从github上download

重启smokeping或者重载配置:

/opt/smokeping/bin/smokeping --reload
/opt/smokeping/bin/smokeping --restart

10 设置apache

把/opt/smokeping/htdoc下面的文件copy到/var/www/html去

同时在/var/www/html目录下

mkdir cache

chmod 777 cache

把smokeping.fcgi.dist改名为smokeping.fcgi放到cgi-bin下面

11 编辑apache 配置文件

设置Listen 80 为

Listen 8080

设置 /var/www/html

AllowOverride All

设置/var/www/cgi-bin

AllowOverride All

同时配置一下mod_fcgid的参数,否则可能会因为需要监测的hosts的数量太多,引起fcgid错误,比如说如下的错误:

[Mon jan 30 15:31:44.937317 2019] [fcgid:warn] [pid 17946] [client 127.0.0.1:53506] mod_fcgid: HTTP request length 135754 (so far) exceeds MaxRequestLen (131072)

编辑

vi /etc/httpd/conf.d/fcgid.conf

再最下面添加:

FcgidMaxRequestLen 4000000

12 配置nginx

配置文件:

server{
listen 80;
server_name www.xxxxxx.com;
index index.html index.htm index.php default.html default.htm default.php;
access_log off;
error_log /var/log/nginx/www.xxxxxx.com.error.log;
location / {
proxy_pass http://127.0.0.1:8080;
#Proxy Settings
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 128k;
proxy_buffers 6 32k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
client_max_body_size 50m;
}
}

13 进入/var/www/html, 设置.htaccess

RewriteEngine On
RewriteRule ^$ /cgi-bin/smokeping.fcgi [NC,L]

14. 写入smokeping自启动文件

编辑/etc/systemd/system/smokeping.service

[Unit]
Description=Latency Logging and Graphing System
After=syslog.target network.target

[Service]
ExecStart=/opt/smokeping/bin/smokeping
ExecReload=/bin/kill -HUP $MAINPID
StandardError=syslog
Type=forking
PIDFile=/opt/smokeping/var/smokeping.pid


[Install]
WantedBy=multi-user.target


设置完整个以后

别忘了重启daemon

systemctl daemon-reload

下面主要是slave的安装设置:

15) 对于slave来说, 基本过程和上面一样,但是apache 和nginx 是不需要安装的, 也就是跳过步骤6和7

然后如下:

cd ~

wget https://oss.oetiker.ch/smokeping/pub/smokeping-2.7.3.tar.gz

tar -zxvf smokeping-2.7.3.tar.gz

cd smokeping-2.7.3

./configure --prefix=/opt/smokeping PERL5LIB=/usr/lib64/perl5/

/usr/bin/gmake install

cd /opt/smokeping

mkdir data var cache

chmod 777 data var cache

设置slave的密码文件,路径为/opt/smokeping/etc/secret.txt

vi /opt/smokeping/etc/secret.txt

为了方便后面的教程,假设slave的密码是gcphongkong,我们这里在secret.txt里面只需要写入gcphongkong,其他的都不需要

chmod 600 /opt/smokeping/etc/secret.txt

这里如果不设置600,会被报错

slave 需要提供一个hostname,为了方便,这个name可以不是这个服务器的真实的hostname,可以在启动文件中添加slave name,这里我们设置为gcphk

16) 启动文件也因此需要做一些修改, 编辑/etc/systemd/system/smokeping.service, 重点关注 –slave-name 这个配置

[Unit]
Description=Latency Logging and Graphing System
After=syslog.target network.target

[Service]
ExecStart=/opt/smokeping/bin/smokeping --master-url=http://xxx.xxx.xxx/cgi-bin/smokeping.fcgi --cache-dir=/opt/smokeping/cache/ --slave-name=gcphk --shared-secret=/opt/smokeping/etc/secret.txt --pid-dir=/opt/smokeping/var --logfile=/opt/smokeping/smokeping.log
ExecReload=/bin/kill -HUP $MAINPID
StandardError=syslog
Type=forking
PIDFile=/opt/smokeping/var/smokeping.pid


[Install]
WantedBy=multi-user.target

设置完systemd以后,我们需要重启daemon

systemctl daemon-reload

这样我们就配置完成了slave,配置了slave 先不要启动,必须得先在master上配置完以后再启动slave,这样才能读取配置

17) slave的hostname的解释

slave 的hostname 需要和master 的secret.txt相对应

这里的hostname 可以不是服务器真正的hostname,只要master认为是就可以了,具体设置可以看16,如果不在启动配置上添加”–slave-name”参数,那么就需要服务器真正的hostname与master上的文件相对应

18)

在Master上的设置:

在/opt/smokeping/etc/config 中添加slave 部分:

*** Slaves ***
secrets=/opt/smokeping/etc/smokeping_secrets

+gcphk
display_name = GCP_HK
location = HK
color = ff0000

在底部target这里,加上

*** Targets ***

slaves = gcphk

这个配置简单易懂,然后 smokeping_secrets里面配置secrets,格式是host:secrets

如果按照图上的slave来说,就应该是

gcphk:gcphongkong

同时smokeping_secrets的权限应该设置为600,否则会被slave 报错, 同时apache也要有权限read smokeping_secrets, 因此可以这样

chown apache:apache smokeping_secrets
chown apache:apache config

chmod 600 smokeping_secrets

然后reload smokeping和httpd

systemctl reload smokeping

systemctl reload httpd

然后再slave 启动smokeping

systemctl start smokeping

systemctl enable smokeping

 

FAQ:

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

这是因为slave 是通过apache或者nginx写入数据到/opt/smokeping/data 的,如果slave的数据不显示,则是apache或者nginx对data 文件夹以及里面的文件没有写入权限,因此可以这样:

chown apache:apche /opt/smokeping/data -R

chown nginx:nginx /opt/smokeping/data -R

在centos下通过编译安装的smokeping,都是以root运行的,因此修改data 下面的文件夹以及文件权限,是没有任何问题的

 

Mysql 无故crash

一入十二月,家里事多.

前几天看统计,无意间发现一个站点没有流量了,缸开始还以为是宕机了,吓我一跳,因为这个服务器上还有几个VM 跑着很重要的数据

查看了半天数据才发现原来是mysql 挂掉了

mysql 在这个VM上稳定了那么长时间,怎么会挂掉??查看了半天的err log,结果上面什么都没有

刚开始以为是innodb 的buffer 出现了问题,仔细查了查也没有问题。。。后来无意间才发现是磁盘满了。。。因为在这台VM上,我单独给mysql 添加了一块SSD硬盘,但是因为数据库读写过于频繁,mysql-bin的日志把整个硬盘都占满了。。。。

解决办法:

肯定就是删除mysql-bin.00XX文件了,但是网上都说不要直接删除,会对mysql-bin.index有损害. 但是目前mysql无法启动,也无法使用purge 来删除mysql-bin

其实解决问题很简单,首先删除mysql-bin.00001 和mysql-bin.00002, 然后打开mysql-bin.index把相应的文件名删掉就可以了,其实就是相当于手动的编辑mysql-bin.index, 和purge 的过程一样

删除一两个mysql-bin以后,就可以正常启动mysql,然后直接flush logs, reset master 就可以了,这样就会删除全部的mysql-bin,然后重新建立mysql-bin.index

最后编辑一些/etc/my.cnf, 把bin-log的失效日期改为3天就可以了