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/nginxlog

logs=`find ./ -name access.log.$time`

cat  $logs  >  /backup/nginxlog/agghour/access.log.$time.temp

grep 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.txt

cat  /backup/nginxlog/agghour/access.log.$time.temp  |sort -k 4  >  /backup/nginxlog/agghour/access.log.$time

rm -f /backup/nginxlog/agghour/access.log.$time.temp

rm -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 width=100%>
 <tr>
  <th>Time</th>
  <th>Count PV</th>
  <th>500error</th>
 </tr>
新建index1.html
 <tr>
 <\tr>
</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\t$c\t$g\t" >/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 "\n"
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脚本

Linux/Unix shell sql 之间传递变量

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/0df59cfaecde887ccfa11ec3ae43f07b.html