现代Linux系统的swap空间

以前的资料上的东西,都是以RAM很小为前提,但是现在的server,RAM动不动就是32G,64G什么的,内存要大不少

下面的内容是从fedora 28的安装文档上看到的。Fedora 28安装指南定义了有关swap 空间分配的思路。需要注意的是,fedora的分配思路可能与其他Linux发行版略有不同,但是和REHL,Centos是一摸一样的,这些建议自从Fedora19以来就没有变过

Fedora的建议:

RAM<=2GB, SWAP = 2X RAM

2GB < RAM < 8GB, SWAP = RAM

8GB < RAM < 64GB, 4G<=SWAP<=0.5XRAM

RAM> 64GB, SWAP>=4GB

但是根据多年专业的运维人员的经验来说,

RAM <= 2GB, SWAP = 2X RAM

2GB < RAM < 8GB, SWAP = RAM

SWAP > 8GB, SWAP = 8GB

nginx 带query string argument 的URL 跳转

nginx 自带的三个命令,  return, rewrite, try-files功能非常强大,基本可以完成各式各样的跳转, 比如下面的:

server {

    listen 80;

   server_name example.com;

   return 301 https://www.example.com$uri;

}

这样就完成了一个不带www的domain转向带SSL以及www前缀的domain的跳转

$uri: 按照nginx官方文档的话来说,就是current normalized URI in REQUEST

$request_uri: full original request URI (with arguments)

这里很明显就看到了$uri和$request_uri的区别,$uri 是 normalized的,换句话说,就是

  1. 去除了?以及后面的query参数
  2. encoded URL被 decoded

因此这里就出现了一个问题: exapmle.com/test.php?a=b 会跳转到https://www.example.com/test.php, query string丢失了,解决的办法很简单

return 301 https://www.example.com$uri$is_args$args
或者
return 301 https://www.example.com$request_uri

$is_args is an emprt string if there are no arguments, or a ? to signify the start of the query string.

$args then adds the arguments,

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 下面的文件夹以及文件权限,是没有任何问题的

 

简单的把SysV init转变成SystemD启动

Github上看到的,对于大部分的init script可以这么用,但是其实还是不太准确

Let’s say you have a SysV Init Script named foo

Copy the file to /etc/init.d/foo

Enable the SysV service: chkconfig --add foo

Enable the SysV service: chkconfig foo on

Start the service: service foo start. After this, systemd-sysv-generator will generate this file /run/systemd/generator.late/foo.service, copy this file to /etc/systemd/system by running: cp /run/systemd/generator.late/foo.service /etc/systemd/system/foo.service

Edit /etc/systemd/system/foo.service by running systemctl edit foo.service, add in the following line to foo.servie (this makes the service installable)

[Install] WantedBy=multi-user.target 

Enable the service: systemd enable foo.service

(Optional) You can then remove the SysV script by running

 foo off && chkconfig --del foo

Centos7设置smokeping自动启动

由于在centos7以及RHEL中,系统的启动已经由centos6的SysV变成了SystemD了,虽然原来的init的形式还是可以用的,但是最新的systemd的配置还是比较方便的.

这里我收集了centos7,debian9,ubuntu18.04以及fedora的默认自动启动配置文件

https://github.com/hippoking/public/tree/master/smokeping_init

其中ubuntu和debian还在继续使用老旧的init的方式进行自动启动,centos7和fedora28已经采用最新的systemd的方式了,但是没想到这里面有一个很大的bug,从Fedora17开始就有。。28了还没有修复

在fedora28中,典型的smokeping.service的配置是:

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

[Service]
ExecStart=/usr/sbin/smokeping --nodaemon
ExecReload=/bin/kill -HUP $MAINPID
StandardError=syslog

[Install]
WantedBy=multi-user.target

问题就出现在–nodaemon上。

如果使用了–nodaemon,smokeping就不会以守护进程启动,这样在使用systemctl查看status的时候,会发现smokeping在不断重启或者状态显示不正确

在如下两篇文章中发现了同样的问题:

https://forums.fedoraforum.org/showthread.php?286002-smokeping-2-6-on-fedora-17

https://github.com/oetiker/SmokePing/issues/44

正确的做法是设置smokeping 为守护程序,去掉–nodaemon即可

 

Linux 守护进程(Daemon)

守护进程(daemon)是一类在后台运行的特殊进程,用于执行特定的系统任务。很多守护进程在系统引导的时候启动,并且一直运行直到系统关闭。另一些只在需要的时候才启动,完成任务后就自动结束。

首先,守护进程最重要的特性是后台运行。其次,守护进程必须与其运行前的环境隔离开来。这些环境包括未关闭的文件描述符、控制终端、会话和进程组、工作目录以及文件创建掩码等。这些环境通常是守护进程从执行它的父进程(特别是shell)继承下来的。最后,守护进程的启动方式有其特殊之处。它可以在Linux系统启动时从启动脚本/etc/rc.d中启动,也可以由作业控制进程crond启动,还可以由用户终端(通常是shell)执行。

除这些,守护进程与普通进程基本上没有什么区别。因此,编写守护进样实际上是把一个普通进程按照上述的守护进程的特性改造成为守护进程

按照服务类型分为如下几个。

系统守护进程:syslogd、login、crond、at等。
网络守护进程:sendmail、httpd、xinetd、等。
独立启动的守护进程:httpd、named、xinetd等。
被动守护进程(由xinetd启动):telnet、finger、ktalk等

在正常条件下,我们将程序运行产生的信息打印到控制台实时显示,如果我们想讲一个程序以守护进程的方式进行运行,就需要改变信息的输出方向,将其导向到配置文件里设置的日志文件。

  将一个进程转换为守护进程需要进行几个步骤:

 1) 调用fork( )。
2) 在父进程中,调用exit( )。
3) 调用setsid( ),给守护进程一个新的进程组和会话。
4) 通过将工作目录更改为根目录chdir( )。这是因为继承的工作目录可以位于文件系统的任何位置。守护进程倾向于在系统正常运行时间内运行,并且你不希望保持某个随机目录处于打开状态,从而阻止管理员卸载包含该目录的文件系统。
5) 关闭所有文件描述符。
6) 打开文件描述符0,1和2(标准输入,标准输出和标准错误)并将它们重定向到/dev/null。

apache2.4 运行cgi的几种方式

我这边的服务器的web server基本上都是nginx的,但是今天在配置smokeping的时候,发现还得是使用apache做为后端来跑cgi,nginx对于cgi以及fastcgi的支持不是太好,google了一整天才发现原来apache的官方文档才是最好的资料. 本文以下内容均以apache2.4 为标准

如果我们想让apache来运行cgi程序,我们首先需要配置apache来允许cgi 程序. 主要有以下几种方式:

首先我们需要注意的是,无论是以哪种方式运行,我们都需要保证在http.conf中,LoadModule命令中要有cgi或者fcgi的模块:

LoadModule cgid_module modules/mod_cgid.so

or

LoadModule fcgid_module modules/mod_fcgid.so

因为fastcgi要比cgi要快很多,因此建议这里都使用fastcgi的module

在通过yum安装完apache 以后,我们可以直接用

yum install mod_fcgid

来安装apache fastcgi module

方式一: ScriptAlias

ScriptAlias 告诉apache有一个目录被专门设定来跑cgi程序, apache也会因此默认这个目录里面都是cgi程序,进而用cgi的handler来执行

ScriptAlias 命令:

ScriptAlias "/cgi-bin/" "/usr/local/apache2/cgi-bin/"

这里需要注意的是, ScriptAlias和Alias不同, ScriptAlias被专门用cgi程序的alias, Alias则是普通意义上的一个目录的map命令

方式二: CGI outside of ScriptAlias directories

我们经常会遇到把cgi程序放在/cgi-bin/外面的情况,我们只需要两步就可以完成这个任务: 1)  用AddHandler或者SetHandler 来设置cgi-script的handler 2) 在Options命令中,必须指定ExecCGI

以下是用options的方式来设定CGI运行:

<Directory "/usr/local/apache2/htdocs/somedir">
Options +ExecCGI

AddHanlder  cgi-script .cgi
</Directory>

以上命令告诉apache 在这个目录下,以.cgi结尾的都是cgi文件

当然了,如果你没有编辑httpd.conf的权利,也是可以在.htaccess中设定的

另外你可以设定某个目录下都是cgi文件,可以使用下面的配置方式:

<Directory "/home/*/public_html/cgi-bin">
Options ExecCGI
SetHandler cgi-script
</Directory>

 

阿里云香港HKB简配benchmark

新开一台服务器来做reverse proxy,正好先测一下benchmark

———————————————————————-
CPU model : Intel(R) Xeon(R) Platinum 8163 CPU @ 2.50GHz
Number of cores : 1
CPU frequency : 2494.136 MHz
Total size of Disk : 40.0 GB (1.7 GB Used)
Total amount of Mem : 991 MB (54 MB Used)
Total amount of Swap : 0 MB (0 MB Used)
System uptime : 0 days, 0 hour 4 min
Load average : 0.01, 0.04, 0.03
OS : CentOS 7.6.1810
Arch : x86_64 (64 Bit)
Kernel : 3.10.0-957.1.3.el7.x86_64
———————————————————————-
I/O speed(1st run) : 146 MB/s
I/O speed(2nd run) : 147 MB/s
I/O speed(3rd run) : 146 MB/s
Average I/O speed : 146.3 MB/s
———————————————————————-

Platinum 8163应该是intel定制的新的CPU. 这是阿里云第四代服务器采用的CPU,Skylake架构,主频2.5GHz,计算性能问题。8163这款型号在intel官网上并没有相关信息,应该是阿里云向阿里云定制的,与之相近的Intel Xeon Platinum 8168,价格是$5890,约合¥38900元。

此类服务器提供的ECS实例族包括通用型实例g5、计算型实例c5、内存型实例r5、本地 SSD 型实例 i2、突发性能实例 t5、超级计算集群计算型实例规格族 scc、通用型神龙云服务器规格族 ebmg5等。

 

阿里云平台的ecs服务器大多使用intel 至强处理器,而且大多是定制版,包括Platinum(铂金) 8163、Gold(金牌) 6150、Gold(金牌) 6149、E5-2682v4、E5-2680v3、E5-2667v4以及E3-1240v6等CPU。