Linux下bash脚本:计算重新索引Table时间
这是曾经工作中自己编写的一个Bash脚本,且必须处理已存在的文件,即计算每个Table重新索引所花费的时间,并找出大于30分钟的重索引记录,并邮件发送至指定邮箱。格式如下(此为一小部分,文件名为Reindex.txt):
---------------------------------------------------------------------------------
Fri Sep 24 05:00:01 MYT 2010: Script /data/crondir/postgres-reindeing.sh starting
Fri Sep 24 05:06:01 MYT 2010: Reindexing of database 'DB' 'tblbmc_emergency_alert' start
Fri Sep 24 05:06:07 MYT 2010: Reindexing of database 'DB' 'tblbmc_emergency_alert' Complete
Fri Sep 24 05:06:07 MYT 2010: Reindexing of database 'DB' 'tblbmc_bat_activity_info' start
Fri Sep 24 08:00:47 MYT 2010: Reindexing of database 'DB' 'tblbmc_bat_activity_info' Complete
Fri Sep 24 08:00:47 MYT 2010: Reindexing of database 'DB' 'tblbmc_incoming_data' start
Fri Sep 24 08:02:20 MYT 2010: Reindexing of database 'DB' 'tblbmc_incoming_data' Complete
------------------------------------------------------------------------------------
我主要运用awk和sed等方法,提取出如05:06:01的时间start 和05:06:07 Complete部分,后者减去前者即得时间差值。此处贴出Bash脚本代码,其中有简单英文注释,方法比较拙劣,请勿见笑。
------------------------------------------------------------------------------------
#!/bin/bash
#delete the first line & last line with sed
sed '1d;$d' Reindex.log >Result.txt
#clear $1 with awk
awk '{$1=""}{print }' Result.txt >Result1.txt
awk '{$1=""}{print }' Result1.txt >Result2.txt
awk '{$1=""}{print }' Result2.txt >Result3.txt
#clear $3 with awk
awk '{$3=""}{print }' Result3.txt >Result4.txt
#transfer the format "h:m:s" to "s"
awk -F '[: ]+' '{print ($1*3600+$2*60+$3)}' Result4.txt>Result5.txt
#transfer all the lines to one line
paste -d " " -s Result5.txt >Result6.txt
#another way for transferring all the lines to one line
#awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}' Result5.txt >Result6.txt
#compute the cost time for each table
awk '{$1=$2-$1} {print "Reindexing of database DB --tblbmc_emergency_alert-- "++$1 " s"}' Result6.txt >ResultFinal.txt
awk '{$1=$4-$3} {print "Reindexing of database DB --tblbmc_bat_activity_info-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$6-$5} {print "Reindexing of database DB --tblbmc_incoming_data-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$8-$7} {print "Reindexing of database DB --tblbmc_bat_all_boundary-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$10-$9} {print "Reindexing of database DB --tblbmc_bat_all_technical-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$12-$11} {print "Reindexing of database DB --tblbmc_bat_allextendedparam-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$14-$13} {print "Reindexing of database DB --tblbmc_bat_allextendedparam1-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$16-$15} {print "Reindexing of database DB --tblbmc_bat_allextendedparam2-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$18-$17} {print "Reindexing of database DB --tblbmc_bat_allibuttonparam-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$20-$19} {print "Reindexing of database DB --tblbmc_bat_allidentity-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$22-$21} {print "Reindexing of database DB --tblbmc_bat_allmccparam-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$24-$23} {print "Reindexing of database DB --tblbmc_bat_alloperparam-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$26-$25} {print "Reindexing of database DB --tblbmc_bat_allphone-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$28-$27} {print "Reindexing of database DB --tblbmc_bat_batparam-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$30-$29} {print "Reindexing of database DB --tblbmc_bat_bookingid-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$32-$31} {print "Reindexing of database DB --tblbmc_bat_boundary-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$34-$33} {print "Reindexing of database DB --tblbmc_bat_cced-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$36-$35} {print "Reindexing of database DB --tblbmc_bat_clv_report-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$38-$37} {print "Reindexing of database DB --tblbmc_bat_debug_report- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$40-$39} {print "Reindexing of database DB --tblbmc_bat_elock_storage-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$42-$41} {print "Reindexing of database DB --tblbmc_bat_elock_universal-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$44-$43} {print "Reindexing of database DB --tblbmc_bat_error_report-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$46-$45} {print "Reindexing of database DB --tblbmc_bat_fms_message-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$48-$47} {print "Reindexing of database DB --tblbmc_bat_ibtnstorage-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$50-$49} {print "Reindexing of database DB --tblbmc_bat_polygon_boundary-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$52-$51} {print "Reindexing of database DB --tblbmc_bat_power_status-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$54-$53} {print "Reindexing of database DB --tblbmc_bat_scanned_ibutton-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$56-$55} {print "Reindexing of database DB --tblbmc_bat_start_journey-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$58-$57} {print "Reindexing of database DB --tblbmc_bat_stop_journey-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$60-$59} {print "Reindexing of database DB --tblbmc_bat_taxi_onjob-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$62-$61} {print "Reindexing of database DB --tblbmc_bat_taxi_trip_info-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$64-$63} {print "Reindexing of database DB --tblbmc_bat_temperature_rpt-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$66-$65} {print "Reindexing of database DB --tblbmc_bat_trigger_boundary-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$68-$67} {print "Reindexing of database DB --tblbmc_bat_tss_comm_inputboard_status-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$70-$69} {print "Reindexing of database DB--tblbmc_bat_tss_comm_mainboard_status-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$72-$71} {print "Reindexing of database DB --tblbmc_bat_user_param-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$74-$73} {print "Reindexing of database DB --tblbmc_bat_warehouse-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$76-$75} {print "Reindexing of database BDB --tblbmc_send_message_log-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$78-$77} {print "Reindexing of database DB --t_bgate_gpsinfo-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$80-$79} {print "Reindexing of database DB --t_bgate_handleinfo-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
awk '{$1=$82-$81} {print "Reindexing of database DB --t_bgate_eventinfo-- "++$1 " s"}' Result6.txt >>ResultFinal.txt
#find out the result which are >30min(1800 senconds)
awk '$6>1800' ResultFinal.txt >ResultSencond.txt
#transfer the unit "sencond" to "minute"
awk '$6=$6/60' ResultSencond.txt >ResultMin.txt
awk '$7="min"' ResultMin.txt>ResultMail.txt
#delete the temporary files
rm ./Result.txt
rm ./Result[1,2,3,4,5,6].txt
rm ./ResultFinal.txt
rm ./ResultSencond.txt
rm ./ResultMin.txt
#mail the result to the related email addresses.
mail -s "Reindex Result for you" [email protected] <ResultMail.txt