3月 15

日志 压缩 备份 定时删除脚本

[编写整理:simonzhang 2010-03-15 2012-04-08修改]

  在linux下有大量日志需要压缩备份,并定期清理长期保存的备份日志。对于多处日志存放使用数组进行循环处理。脚本修改好后,设置定时任务即可。

#!/bin/sh
##############################################
# AUTHOR: simonzhang
# back log
# Ver : 1.1 For Production
# description: 
# 2010-03-12  
##############################################
####### set log patch
log_path=("/usr/local/nginx/logs/" "/usr/local/tomcat6/logs/" )
####### set backup log patch
bak_log_path=("/usr/local/nginx/logs/back" "/usr/local/tomcat6/logs/back")

####### set backup  3 day ago log
backupdays=3

#######clear 180 day ago compress log
deletedays=180

#######  start
for (( i=0 ; i<${#log_path[@]} ; i++ ))
do
        cd ${log_path[i]}
    if [ ! -f ${bak_log_path[i]} ] ; then
            /bin/mkdir -p ${bak_log_path[i]}
    fi
        /usr/bin/find  * -ctime +$backupdays -maxdepth 0 -not -name *.pid -not -name error* -exec zip -m {}.zip  {} \;
        /bin/mv *.zip ${bak_log_path[$i]}
        cd ${bak_log_path[$i]}
        echo /usr/bin/find  * -ctime +$deletedays -maxdepth 0 -exec rm {} \;
done
############  end
3月 15

网站压力测试微型工具技术备忘

[记录整理:张子萌 2010-03-15]

经常对服务器单个模块做非专业性压力测试,所以学习了两个小巧大的工具,做技术备忘。
如果需要做比较详细数据请使用LoadRunner、QALoad、JMeter等专业工具。

一款是apahe自带的ab工具。一款是webbench。

1.apache自带ab工具

此工具为apache自带可以显示较多测试信息,
http://apache.freelamp.com/httpd/httpd-2.2.15.tar.gz
以下编译只为获得ab工具,不作为apache安装使用,编译时添加安装路径,是为方便
不小心安装后删除方便。
# cd httpd-2.2.15
# ./configure –prefix=/usr/local/apache_ab
# make
# cd support/.libs/
在目录下已经可以看到ab程序了,ab程序主要参数解释如下

格式: ./ab [options] [http://]hostname[:port]/path
Options are:
-n requests Number of requests to perform
# 在默认会话中发送请求的个数,默认时为50000
-c concurrency Number of multiple requests to make
# 一次请求所产生的次数
-t timelimit Seconds to max. wait for responses
# 测试所进行的最大秒数。默认时,没有时间c和n参数限制请求完毕为止。
-b windowsize Size of TCP send/receive buffer, in bytes
# 发送和接受包大小
-w Print out results in HTML tables
# 以HTML表的格式输出结果
-v verbosity How much troubleshooting info to print
# 打印头部信息,参数为输出级别1-4
-i Use HEAD instead of GET
# 使用head请求不是get请求
-H attribute Add Arbitrary header line, eg. ‘Accept-Encoding: gzip’
Inserted after all normal header lines. (repeatable)
-A attribute Add Basic WWW Authentication, the attributes
are a colon separated username and password.
# web使用庄户密码
-P attribute Add Basic Proxy Authentication, the attributes
are a colon separated username and password.
# 对代理使用账户密码
-X proxy:port Proxyserver and port number to use
# 使用代理服务器

开始测试主要使用参数也就两个,以下对部分结果做解释
# ./ab -c 1000 -n 1000 http://172.192.1.100/
# url结尾如不是文件(如:index.html),一定要增加“/”
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 172.192.1.100 (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests

Server Software: nginx
Server Hostname: 172.192.1.100
Server Port: 80

Document Path: /
Document Length: 2731 bytes
# 传输单个文档大小

Concurrency Level: 1000
# 并发数
Time taken for tests: 3.320 seconds
# 本次测试持续的时间
Complete requests: 1000
# 本次测试完成请求数量
Failed requests: 0
# 本此测试失败数量
Write errors: 0
Total transferred: 3039360 bytes
HTML transferred: 2749800 bytes
Requests per second: 301.18 [#/sec] (mean)
# 每秒钟处理数量的平均值
Time per request: 3320.263 [ms] (mean)
# 处理请求的平均响应时间
Time per request: 3.320 [ms] (mean, across all concurrent requests)
# 每个请求实际运行时间的平均值
Transfer rate: 893.94 [Kbytes/sec] received
# 平均每秒网络流量

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 200 701.6 26 3000
Processing: 1 275 373.3 254 3280
Waiting: 1 275 373.2 254 3280
Total: 1 475 735.5 302 3316
# 网络响应时间,主要为connect时间。
# 以下为请求处理时间的分布

Percentage of the requests served within a certain time (ms)
50% 302
66% 348
75% 362
80% 369
90% 410
95% 3008
98% 3016
99% 3311
100% 3316 (longest request)

2.webbench工具
获取地址:
http://blog.s135.com/soft/linux/webbench/webbench-1.5.tar.gz
获取版本进行编译
# tar zxvf webbench-1.5.tar.gz
# cd webbench-1.5
# make
在本目录下可以看到webbench软件,webbench程序主要参数如下
格式:webbench [option]… URL
-f|–force Don’t wait for reply from server.
-r|–reload &
nbsp; Send reload request – Pragma: no-cache.
-t|–time Run benchmark for seconds. Default 30.
-p|–proxy Use proxy server for request.
-c|–clients Run HTTP clients at once. Default one.
-9|–http09 Use HTTP/0.9 style requests.
-1|–http10 Use HTTP/1.0 protocol.
-2|–http11 Use HTTP/1.1 protocol.
–get Use GET request method.
–head Use HEAD request method.
–options Use OPTIONS request method.
–trace Use TRACE request method.
-?|-h|–help This information.
-V|–version Display program version.

使用100个并发持续5秒钟
#./webbench -c 100 -t 5 http://www.google.com/
Webbench – Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://www.google.com/
100 clients, running 5 sec.

Speed=36 pages/min, 4289 bytes/sec.
Requests: 3 susceed, 0 failed.

3.两款工具对比

ab工具显示测试数据比webbench详细。但是ab可测试并发量小于webbench,ab并发使用
不要超过1024(主要受到apache配置和系统资源限制),webbench并发使用数量不要超过10000,
如果超出此方为可能会引起测试机短暂假死现象,且不能获得返回数据。此两宽小型软件已经
够用,对于网上常见的Siege,http_load等就不看了。

3月 12

nginx 0.8 安装 配置

【整理人:张子萌 最近一次修改2010-12】

nginx的官方网站:http://nginx.org/

1.下载nginx源码
nginx-0.8.34.tar.gz

如果希望在Nginx中使用正则表达式使配置更灵活,需要安装PCRE。
查看是否安装pcre
# rpm -qa pcre*

如果希望在是用ssl加密部分请安装openssl。

如果希望压缩传送需要安装gzip

如果使用nginx使用的缓存,需要清理指定url需要下载并同时编译。
http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz

推荐使用升级工具安装,升级工具使用可以参照:
http://simon-zzm.blog.163.com/blog/static/88809522201025102237958/

2. 编译安装nginx
首先创建账户,指定用户与组的id均为700
# groupadd -g 700 nginx
# useradd -g700 -u700 nginx
解压、安装nginx,对于不需要的模块可以不编译。
最后一行为清理指定url模块,可以选择性编译
# tar zxvf ngx_cache_purge-1.0.tar.gz
# tar zxvf nginx-0.8.34.tar.gz
# cd nginx-0.8.34
# ./configure –prefix=/usr/local/nginx
–user=nginx
–group=nginx
–with-pcre
–with-select_module
–with-poll_module
–with-http_stub_status_module
–with-http_ssl_module
–with-http_realip_module
–with-http_gzip_static_module
–add-module=/usr/local/src/ngx_cache_purge-1.0
# make && make install

3. nginx配置文件

主要配置文件:/usr/local/nginx/conf/nginx.conf
# 程序运行用户
user nginx nginx;
# 启动进程数
worker_processes 8;

#给每个进程分配cpu,例:将8个进程分配到8个cpu,也可以写多个,或将一个进程分配到多个cpu

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

# 全局错误日志和程序PID文件
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

pid logs/nginx.pid;

# 工作模式及连接数上限
# 工作模式有:select(标准模式),poll(标准模式),kqueue(高效模式,适用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
# poll(高效模式。适用Linux 2.6+,SuSE 8.2,Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
events {
use poll;
worker_connections 1024;
}

# 设定http服务器,反向代理功能提供负载均衡支持
http {
#设定mime类型
include mime.types;
default_type application/octet-stream;

# 隐藏nginx版本,防止攻击
server_tokens off;
# 设定日志格式
#log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
# ‘$status $body_bytes_sent “$http_referer” ‘
# ‘”$http_user_agent” “$http_x_forwarded_for”‘;
#设定access log
#access_log logs/access.log main;
#linux下强大的静态文件发送功能,一定要开启
sendfile on;
tcp_nopush on;

keepalive_timeout 60;
tcp_nodelay on;

# 定义一个叫“mysvr”的记录区,总容量为 10M
# 和下面location中的limit_conn一起限制单个IP的并发连接数为10
# limit_zone只能是用在http中,limit_conn可以使用在http、sever、local中
limit_zone mysvr $binary_remote_addr 10m;

#设定负载均衡的服务器列表
upstream mysvr {
ip_hash #如果不需要可以删除直接采用轮训方法负载
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.0.1:80 weight=5;
server 192.168.0.2:80 weight=1;
server 192.168.0.3:80 weight=6;
server 192.168.0.5:80;
server 192.168.0.6:80;

#down 表示单前的server暂时不参与负载
#weight 默认为1.weight越大,负载的权重就越大。
#max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
#fail_timeout:max_fails次失败后,暂停的时间。
#backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
#ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题
}
# 设定缓存临时目录,临时目录要与proxy_cache_path中目录一样
proxy_temp_path /usr/local/nginx_test/proxy_temp;
# proxy_cache_path中参数如下
# levels指定该缓存空间有两层hash目录,第一层目录是1个字母,第二层为2个字母
# keys_zone为这个空间起个名字,比如 cache_one,在server模块中proxy_cache使用
# 200m 内存缓存空间大小为200MB
# inactive 1天没有被访问的内容自动清除;
# max_size使用硬盘缓存大小30G
# clean_time指定一分钟清理一次缓存。
proxy_cache_path /usr/local/nginx_test/proxy_temp/ levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

#设定虚拟主机,默认为监听80端口
server {
listen 80;
server_name 192.168.0.7 mysvr.c
om www.mysvr.com;
#charset gb2312;
#charset utf8;
#charset koi8-r;
#设定本虚拟主机的访问日志
#access_log logs/host.access.log main;

#对 “/” 启用负载均衡
location / {
proxy_pass http://mysvr;
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;
client_max_body_size 10m; #允许客户端请求的最大单文件字节数
client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数
proxy_connect_timeout 90; #与后端连接超时时间
proxy_send_timeout 90; #后端服务器回传超时时间
proxy_read_timeout 90; #连成功后,后端服务器响应超时时间。
proxy_buffer_size 4k; #代理服务器保存用户头信息的缓存大小
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k; #高负荷缓村大小。大小计算方法proxy_buffers*2
proxy_temp_file_write_size 64k; #设定缓存文件大小,大于这个值经直接从upstream获得。
limit_conn mysvr 10; #每个IP只能有10个并发
limit_rate 100k; #每个并发只能有100K的速度
index index.html index.htm; #选则文件后缀解析都优先顺序
expires 30d; #客户端缓存时间 30天

}

#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus

3月 11

优化linux资源处理限制

系统报出错误”Too many open files”,说明系统资源调用需要进行调整。

ulimit 通过一些参数选项来管理不同种类的系统资源。

ulimit 命令的格式为:ulimit [options] [limit]

具体的 options 含义以及简单示例可以参考以下表格。

选项 [options] 含义 例子
-H 设置硬资源限制,一旦设置不能增加。 ulimit – Hs 64;限制硬资源,线程栈大小为 64K。
-S 设置软资源限制,设置后可以增加,但是不能超过硬资源设置。 ulimit – Sn 32;限制软资源,32 个文件描述符。
-a 显示当前所有的 limit 信息。 ulimit – a;显示当前所有的 limit 信息。
-c 最大的 core 文件的大小, 以 blocks 为单位。 ulimit – c unlimited; 对生成的 core 文件的大小不进行限制。
-d 进程最大的数据段的大小,以 Kbytes 为单位。 ulimit -d unlimited;对进程的数据段大小不进行限制。
-f 进程可以创建文件的最大值,以 blocks 为单位。 ulimit – f 2048;限制进程可以创建的最大文件大小为 2048 blocks。
-l 最大可加锁内存大小,以 Kbytes 为单位。 ulimit – l 32;限制最大可加锁内存大小为 32 Kbytes。
-m 最大内存大小,以 Kbytes 为单位。 ulimit – m unlimited;对最大内存不进行限制。
-n 可以打开最大文件描述符的数量。 ulimit – n 128;限制最大可以使用 128 个文件描述符。
-p 管道缓冲区的大小,以 Kbyts 为单位。 ulimit – p 512;限制管道缓冲区的大小为 512 Kbytes。
-s 线程栈大小,以 Kbytes 为单位。 ulimit – s 512;限制线程栈的大小为 512 Kbytes。
-t 最大的 CPU 占用时间,以秒为单位。 ulimit – t unlimited;对最大的 CPU 占用时间不进行限制。
-u 用户最大可用的进程数。 ulimit – u 64;限制用户最多可以使用 64 个进程。
-v 进程最大可用的虚拟内存,以 Kbytes 为单位。 ulimit – v 200000;限制最大可用的虚拟内存为 200000 Kbytes。

可以使用ulimit命令修改系统资源处理限制,但是修改只对当前操作起作用,当用户注销后参数回还原为默认值。可以通过修改系统配置文件使参数生效。

使用管理员权限打开/etc/security/limits.conf

在文件中添加

* soft nofile 8192
* hard nofile 20480

添加后保存文件,再次登录使用ulimit -a命令查看是否修改成功。

修改内容分为四列:

第一列为分配用户,可以为用户名、用户组名(@group) ,统配符 * 表示缺省规则

第二列为执行模式,hard 强制实行由管理员设置由Linux Kernel执行,用户无权改变。
soft 非强制执行,用户可以改变在之前存在的,但是用户注销后参数还原为默认值。

第三列为资源种类:

core – core 文件的大小 (KB)
data – 最大的data大小 (KB)
fsize – maximum filesize (KB)
memlock – max locked-in-memory address space (KB)
nofile – max number of open files
rss – max resident set size (KB)
stack – max stack size (KB)
cpu – max CPU time (MIN)
nproc – max number of processes
as – address space limit
maxlogins – max number of logins for this user.

第四列为数值。

参考文档:

http://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/index.html

3月 10

linux 下用 cronolog 切割 apache日誌

cronolog安装可见http://simon-zzm.blog.163.com/blog/static/88809522201022112040647/

apache安装位置:/usr/local/apache/

编辑配置文件httpd.conf

LogFormat “%v %h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” mylog
LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” combined
LogFormat “%h %l %u %t “%r” %>s %b” common


LogFormat “%h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i” %I %O” combinedio

CustomLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log” combined

如果使用虚拟机可以使用以下配置

httpd-vhosts.conf

ErrorLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log”
CustomLog “|/usr/local/sbin/cronolog /usr/local/apache/logs/access.%Y%m%d-%H.log” common