金融信创如何推动金融服务效率与安全的全面提升
1028
2022-09-28
nginx使用cronolog切割日志安装、部署、应用测试
我们使用的是tnginx(支持日志抽样、直接输出到log服务器等)详细安装配置见另一篇博文,
zxvf cronolog-1.6.2.tar.gz
cd cronolog-1.6.2
mkdir -p /usr/local/cronolog
./configure --prefix=/usr/local/cronolog
make
make install
ln -s /usr/local/cronolog/sbin/cronolog /usr/bin/
2,创建一个命名管道
mkfifo /data/access_log_pipe
3,配置cronolog,按需配置:
精确到分钟
nohup cat /data/access_log_pipe | /usr/local/cronolog/sbin/cronolog /data/logs/%Y%m%d/access_%Y%m%d%H%M.log &
精确到小时
nohup cat /data/access_log_pipe | /usr/local/cronolog/sbin/cronolog /data/logs/%Y%m%d/access_%Y%m%d%H.log &
注意: cronolog必须在nginx启动前启动。如果nginx先启动了,可以reload实现。
4:相关应用方式测试;
#####################################################################
4.1日志直接写入文:100/1抽样测试
测试脚本:
for i in $(seq 1000);do echo --$i---;curl {
listen 80;
server_name localhost;
access_log /data/logs/access_2014021203.log main ratio=0.01;
结果统计:
[root@computer logs]# wc -l access_2014021203.log #按照分钟切割日志
10 access_2014021203.log
####################################################################
4.2 日志写入 管道文件 抽样测试
测试脚本:
for i in $(seq 1000);do echo --$i---;curl {
listen 80;
server_name localhost;
access_log /data/access_log_pipe main ratio=0.01;
[root@computer 20140212]# wc -l access_2014021203.log
10 access_2014021203.log
4.3 测试结论;
nginx结合cronolog能够按照需求灵活的切割日志,nginx之前简单总结啦一下有三种日志切割方式,详见:
~]# more /etc/init.d/nginx.cronolog #!/bin/sh## Comments to support chkconfig ## chkconfig: 2345 77 77# Description: Startup script for nginx webserver#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDESC="nginx daemon"NAME=nginxDAEMON=/usr/local/nginx/sbin/$NAMECONFIGFILE=/usr/local/nginx/conf/$NAME.confPIDFILE=/usr/local/nginx/logs/$NAME.pidSCRIPTNAME=/etc/init.d/$NAME#日志存放位置if [ ! -d "/data/logs" ]; then mkdir -p "/data/logs" fi#生成管道文件位置if [ ! -d "/usr/local/nginx/pipe/" ]; then mkdir -p "/usr/local/nginx/pipe/" fi#cronolog相关变量LOGPATH=/data/logsPIPEPATH=/usr/local/nginx/pipe/ERROE_LOG_PIPE=/usr/local/nginx/pipe/error_log_pipeACCESS_LOG_PIPE=/usr/local/nginx/pipe/access_log_pipeCRONOLOG=/usr/local/cronolog/sbin/cronologd_mkfifo(){#判断管道文件属性、建立管道文件if [ ! -p "$ERROE_LOG_PIPE" ]; then/bin/rm "$ERROE_LOG_PIPE"mkfifo "$ERROE_LOG_PIPE"fiif [ ! -p "$ACCESS_LOG_PIPE" ]; then/bin/rm "$ACCESS_LOG_PIPE"mkfifo "$ACCESS_LOG_PIPE"fi}cronolog_start(){#ps -ef|grep wireless|grep -v grep |awk '{print $2}'|xargs kill -9 nohup cat "$ACCESS_LOG_PIPE" | $CRONOLOG $LOGPATH/%Y%m%d/Access_%Y%m%d%H.log &nohup cat "$ERROE_LOG_PIPE" | $CRONOLOG $LOGPATH/%Y%m%d/Error_%Y%m%d%H.log &}cronolog_kill(){ps -ef|grep data/logs|grep -v grep |awk '{print $2}'|xargs kill -9 ps -ef|grep nginx/pipe|grep -v grep |awk '{print $2}'|xargs kill -9 }# Gracefully exit if the package has been removed.test -x $DAEMON || exit 0d_start() {d_mkfifocronolog_start $DAEMON -c $CONFIGFILE || echo -n " already running"}d_stop() {# kill -QUIT `cat $PIDFILE` || echo -n " not running"cronolog_killkillall nginx || echo -n " not running"}d_reload() { kill -HUP `cat $PIDFILE` || echo -n " can't reload"}case "$1" in start) echo -n "Starting $DESC: $NAME" d_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" d_stop echo "." ;; reload) echo -n "Reloading $DESC configuration..." d_reload echo "reloaded." ;; status) pgrep -x $NAME > /dev/null && echo $NAME \(pid `pgrep $NAME`\) is running... || echo "$NAME is stoped." ;; test|-t) $DAEMON -t ;; restart) echo -n "Restarting $DESC: $NAME" d_stop sleep 2 d_start echo "...done!" ;; *) echo "Usage: $SERVICENAME {start|stop|restart|reload|status|check}" exit 3 ;;esacexit 0
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~