2012-09-24 15 views
0

我編寫了這個腳本來自動處理來自不同服務器的wgets,並將結果輸出到日誌文件,並通過電子郵件發送給我。我分享這一部分原因是爲了讓其他人可以爲自己使用解決方案。使用文件中的行作爲變量,提供給程序 - 自動執行wget

我把文件放在cron.weekly中,所以它每週給我發電子郵件。

Btw。我是一個絕對的新手,這是我通過從這個網站學習而寫的第一個腳本。

腳本如下: 我也從我這邊做了一些小的評論。

#!/bin/sh 
#wget -verbose -tries -timeout -dontcreatedirectories -dontsaveanything http://x output text to file 

# automating apt-get update and upgrade so it runs weekly 
apt-get update 
apt-get upgrade -y 

# this is where the logs will be stored 
cd /var/log 

# create a tmp file and if it is left behind from earlier results, empty it out 
echo -e "\n" > wget.tmp 

# these are the header texts for the results 
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
echo "WGET SCHEDULES" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 

# this will make note of the time the tests are started 
date "+START OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 

# up to 10 servers to wget from 
echo "Server #1" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
wget -v -r -t 2 -T 7 -nd -O /dev/null http://servername/file 2>> wget.tmp 
grep -i 'Length\|connect\|100%\|saved\|failed\|Error' wget.tmp >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
rm -f wget.tmp 
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 

。 繼續到 。

echo "Server #10" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
wget -v -r -t 2 -T 7 -nd -O /dev/null http://servername/file 2>> wget.tmp 
grep -i 'Length\|connect\|100%\|saved\|failed\|Error' wget.tmp >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
rm -f wget.tmp 
echo "\n" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 

# record the end time of tests 
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
date "+END OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 

#mail results to yourself 
echo "Subject: $(hostname -s| tr a-z A-Z)" Wget Results v.2.0 Sendmail "$(date +%x)"  ---- "$(date "+%A")" "$(date "+%X") ("$(date "+%Z")") ::: Week "$(date "+%V")" """ | cat - $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log | /usr/lib/sendmail -t [email protected] 

理想情況下,我想不限制自己的服務器X#,但只要有文本文件servers.list(包括由「」在文件名中,我可以將其存儲在cron。每週一次,不會被處理)。因此,對於每個服務器,寫入服務器#,並處理wget,輸出到文件,grep以記錄文件。這將是最好的,如果servers.list文件格式化

hostname filename store/to 
. 
. 
hostname filename store/to 

例子:

msn.robots.ua 100mb.test /dev/null 
justme.cov ratings.xlsx /srv/storage/latest 

的郵件看起來像

主題:服務器Wget的結果2.0版的Sendmail 12年9月9日----週日四點59分21秒(MSK):::第36周


WGET時間表

連接80 ...:|在Sun公司9月9日四點52分00秒2012

服務器#1 WGETS附表第


開始連接到xxx.xxxxxx.com | ip.ip.ip.ip 。 Length:104857600(100M)[text/plain] 102350K .......... .......... .......... ...... .... .......... 100%5.73M 0s 102400K 100%0.00 = 25s 2012-09-09 04:52:27(4.07 MB/s) - `/ dev/null'保存[104857600/104857600] 。 。在Sun公司9月9日4時59分21秒2012


WGETS附表第


END所以我的/ var/log中結束存儲文件,如WordPress,wget的 - 概要 - sep24_2012.log作爲示例文件名稱。問題在於它正在成爲一個問題,服務器發生了變化,所以我想只更新一個位置的服務器列表,讓他們自己處理它。在執行之前,它會下載新的servers.list,其餘的完成。

郵件正在工作輸出到日誌文件正在工作,它都在工作,只需要一個微小的位調整。

Ty! Ty! Ty爲閱讀。

回答

1

如果我複製你的問題吧,你要替換出的事實,你只能做10臺服務器,並要拉,而不必如果有什麼變化,以解決它在腳本文件中的數據。在此基礎上重複,這裏是我的建議:

在你server.list文件,|分隔它,我會解釋爲什麼在代碼snippit之後。所以,你的server.list文件看起來像:

for conf in $(cat server.list); do 
     server=`echo $conf | cut -d"|" -f1;` 
     filename=`echo $conf | cut -d"|" -f2;` 
     storeto=`echo $conf | cut -d"|" -f3;` 

     ... the rest of your code goes here using the above variables ... 

    done 

如果:

msn.robots.ua|100mb.test|/dev/null 
justme.cov|ratings.xlsx|/srv/storage/latest 

有了這個設置,你可以在中間像這樣把一個for循環在你的bash腳本與你的重複代碼你在你的文件中使用空格作爲分隔符,那麼每次迭代中的$ conf將只有每個空格分隔的值,而不是獲取該行並將其分開。

有可能是一個更優雅的方式使用awk做到這一點,但這會工作。

+0

優秀!它的工作原理,我做了一些閱讀後增加了一點點增量:)非常感謝!出於某種原因,我不能做+1,因爲我的代表如此之低。但你的建議做到了。 – Faruq

+0

a +1給你的答案.. = \ – Faruq

0

所以最終腳本看起來現在這個樣子

#!/bin/sh 
#wget -verbose -tries -timeout -dontcreatedirectories -dontsaveanything http://x output text to file 
# update and upgrade packages and packages list 
apt-get update 
apt-get upgrade -y 

# go to location where logs will be stored and servers list is updated from list stored on master server 
cd /var/log 
rm -f server.list 
wget http://yourserver/server.list 

# create a tmp file and if it is left behind from earlier results, empty it out 
echo -e "\n" > wget.tmp 

# these are the header texts for the results 
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
echo "WGET SCHEDULES" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 

# this will make note of the time the tests are started 
date "+START OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
echo " " >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 

### for loop to read from server.list file like this http://msn.robots.ua|100mb.test|/dev/null 
wgetcount=0 
for conf in $(cat server.list); do 
     wgetcount=$((wgetcount+1)) 
     server=`echo $conf | cut -d"|" -f1;` 
     filename=`echo $conf | cut -d"|" -f2;` 
     storeto=`echo $conf | cut -d"|" -f3;` 
     echo "Server # "$wgetcount >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
     wget -v -r -t 2 -T 7 -nd -O $storeto $server/$filename 2>> wget.tmp 
     grep -i 'Length\|connect\|100%\|saved\|failed\|Error' wget.tmp >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
     rm -f wget.tmp 
     echo " " >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
    done 

# record the end time of tests 
echo "***************" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 
date "+END OF WGETS SCHEDULE at %c" >> $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log 

#mail results to yourself 
echo "Subject: $(hostname -s| tr a-z A-Z)" Wget Results v.2.0 Sendmail "$(date +%x)"  ---- "$(date "+%A")" "$(date "+%X") ("$(date "+%Z")") ::: Week "$(date "+%V")" """ | cat - $(hostname -s)"-wget-SUMMARY-"$(date "+%b%d_%Y" | tr A-Z a-z).log | /usr/lib/sendmail -t youremailaddress 

同樣的結果,更新,更清潔和更短的代碼;)))謝謝彼得

相關問題