附上一段自动产生pg_profile报告及发送邮件的脚本
hostname=`hostname`
ipaddr=`ping -c 2 $hostname|awk 'NR==2 {print $5}'|cut -d '(' -f 2|cut -d ')' -f 1`
start_time=`date +'%Y-%m-%d 08:00:00'`
end_time=`date +'%Y-%m-%d 17:00:00'`
filename=`date +'%Y%m%d'`
tousers=recipient01@sample.com' 'recipient02@sample.com
subject=$hostname[$ipaddr]-Database_Performance_Report
find ~/pg_profile -type f -mtime 7|xargs -i rm -f {}
if [ ~/pg_profile/$filename.html ]; then
/usr/bin/mail -s $subject --content-type='text/html' $tousers < ~/pg_profile/$filename.html
else
/usr/lib/postgresql/10/bin/psql -h localhost -Aqtc "select profile.get_report('local',tstzrange('$start_time','$end_time'))" -o ~/pg_profile/$filename.html
/usr/bin/mail -s $subject --content-type='text/html' tousers < ~/pg_profile/$filename.html
效果如下