这是一份Zabbix服务器每天流量统计脚本,他有如下动作:
1.首先从mysql下边查询,并保存一个.csv文件。
2.将.csv文件加上时间戳。
3.发带有时间戳的csv文件到指定邮箱。 //当然,你要安装一个发邮件
4.删掉刚产生的csv文件。
traffic.sh
#!/bin/bash
mysql -hlocalhost -uroot -p33e9.com -B zabbix -e "select TIMESTAMPADD(day,-1,curdate()), switchname, traffic_name, FORMAT(max(bps/1000000),2) as Mbps from (select gi.itemid, h.name
as switchname, g.name as traffic_name, s.name as traffic_inout, max(u.value) as bps, from_unixtime(u.clock) as traffic_time from graphs g join graphs_items gi on g.graphid = gi.gr
aphid join items s on gi.itemid = s.itemid join hosts h on h.hostid = s.hostid join history_uint u on s.itemid = u.itemid where g.graphid in (552,557,559,561,574,578,582,584,586,5
88,590,636,639,641,554,553,566,569,571,580,654) and u.clock > UNIX_TIMESTAMP(TIMESTAMPADD(day,-1,curdate())) and u.clock < UNIX_TIMESTAMP(curdate()) group by gi.itemid) t group
by switchname, traffic_name into outfile '/var/lib/mysql-files/internet.csv' fields terminated by ','"
change_name.sh
#!/bin/bash
mv /var/lib/mysql-files/internet.csv /tmp/internet`date -d "-1 days" +%F`.csv
send_traffic.sh //要在/tmp目录建一个mailcontent.txt文件。
#!/bin/bash
mutt -s "关于线上MSTP和Internet带宽汇总`date -d "-1 days" +%F`" "好人一生平安<xxx@sina.cn>" -a /tmp/internet`date -d "-1 days" +%F`.csv </tmp/mailcontent.txt
rm_traffic.sh
#!/bin/bash
rm -rf /tmp/internet*.csv
crontab -e //最后建个任务列表就完事啦。
0 8 * * * /tmp/traffic.sh
8 8 * * * /tmp/change_name.sh
10 8 * * * /tmp/send_traffic.sh
15 8 * * * /tmp/rm_traffic.sh
yum install mutt msmtp -y //安装发送邮件软件。
whereis msmtp
touch /var/log/mmlog
vi /root/.msmtprc
account default
host smtp.sina.cn
port 25
from username@sina.cn
auth login
tls off
user username@sina.cn
password 2wsxzaq1
logfile /var/log/mmlog
wq!
chmod 600 /root/.msmtprc