Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / shell编写动态页面

shell编写动态页面,听起来好搞笑,确实 实现起来也很搞笑。举个例子我想通过web页面展示我网站每小时的pv数 以及500错,当然统计pv数和500错 这要统计前台的nginx访问日志来得到。答题思路
因为我们前台有4台nginx,我要首先合并每小时的4台nginx日志,统计出pv数和500错,然后把值传到我的web服务本机,具体脚本如下。一.nginx日志合并所在服务器上编写脚本如下
#!/bin/bash
time=`date +%Y%m%d%H -d "-1 hour"`rsync -av 192.168.0.20::logs/access.log.$time /backup/nginxlog/20/rsync -av 192.168.0.149::logs/access.log.$time  /backup/nginxlog/149/rsync -av 192.168.0.148::logs/access.log.$time  /backup/nginxlog/148/rsync -av 192.168.0.48::logs/access.log.$time  /backup/nginxlog/48/
cd /backup/nginxloglogs=`find ./ -name access.log.$time`cat  $logs  >  /backup/nginxlog/agghour/access.log.$time.tempgrep jwml? /backup/nginxlog/agghour/access.log.$time.temp|wc -l>/opt/yanchao/huoqupv.txt
awk "$9~/500/{print}" /backup/nginxlog/agghour/access.log.$time.temp|wc -l>/opt/yanchao/huoqu500.txt
awk "{++a[$1]}END{for ( i in a )print i,a[i]}" /backup/nginxlog/agghour/access.log.$time.temp|sort -k2 -rn |head -n10|awk "{print $1}">/opt/yanchao/tongji.txt
awk "{++a[$1]}END{for ( i in a )print i,a[i]}" /backup/nginxlog/agghour/access.log.$time.temp|sort -k2 -rn |head -n10|awk "{print $2}">/opt/yanchao/cishu.txtcat  /backup/nginxlog/agghour/access.log.$time.temp  |sort -k 4  >  /backup/nginxlog/agghour/access.log.$timerm -f /backup/nginxlog/agghour/access.log.$time.temprm -f /backup/nginxlog/20/* && rm -f /backup/nginxlog/149/* && rm -f /backup/nginxlog/148/*  &&  rm -f /backup/nginxlog/48/*
二.在web服务器上起个80端口
在web根目录新建index.html
<table border="1" width=100%>
 <tr>
 <th>Time</th>
 <th>Count PV</th>
 <th>500error</th>
 </tr>
新建index1.html
 <tr>
 < r>
</table>
编写脚本如下
#!/bin/bash
rm -f /var/www/html/index1.html
cp /var/www/html/index2.html /var/www/html/index1.html
rm -f /var/www/html/test.txt
c=`cat /opt/yanchao/pv.txt`
a=`date +%H:00 -d "-1 hour"`
b=`date +%H:00`
g=`cat /opt/yanchao/500.txt`
echo -e "$a-$b $c $g " >/var/www/html/test1.txt
e=($(awk "{print $1}" /var/www/html/test1.txt))
f=($(awk "{print $2}" /var/www/html/test1.txt))
d=($(awk "{print $3}" /var/www/html/test1.txt))for ((i=0;i<=${#e[@]};i++))
do
     echo  "<td>${e[i]}</td>" >>/var/www/html/test.txt
     echo  "<td>${f[i]}</td>" >>/var/www/html/test.txt
     echo  "<td>${d[i]}</td>" >>/var/www/html/test.txt
     echo -e " "
done
sed -i "/<tr>/r /var/www/html/test.txt" /var/www/html/index1.html
echo `cat /var/www/html/index.html` >> /var/www/html/linuxidc.html
echo `cat /var/www/html/index1.html` >> /var/www/html/linuxidc.html
三.新建获取数值脚本
#!/bin/bash
ssh 192.168.0.96 "cat /opt/yanchao/huoqupv.txt" >/opt/yanchao/pv.txt
ssh 192.168.0.96 "cat /opt/yanchao/huoqu500.txt" > /opt/yanchao/500.txt
ssh 192.168.0.96 "cat /opt/yanchao/tongji.txt" > /opt/yanchao/tongji.txt
ssh 192.168.0.96 "cat /opt/yanchao/cishu.txt" > /opt/yanchao/cishu.txt
添加crontab
8 * * * * (sh /opt/yanchao/huoqu.sh &)最后访问linuxidc.html推荐阅读:Linux/Unix shell 脚本中调用SQL,RMAN脚本 http://www.linuxidc.com/Linux/2012-09/70310.htmLinux/Unix shell sql 之间传递变量 http://www.linuxidc.com/Linux/2013-01/78811.htmLinux命令之sleep - 延迟指定时间 http://www.linuxidc.com/Linux/2011-04/34758.htmLinux下Shell编程——sed命令基本用法 http://www.linuxidc.com/Linux/2013-06/85526.htmLinux下Shell编程——grep命令的基本运用 http://www.linuxidc.com/Linux/2013-06/85525.htm