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 border="1" 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