邮件大小统计

突然想了解一下我们 SMTP 服务中的入栈邮件大小,于是写了点脚本分析一下。

先上结论:

截图

日志提取

DATE=20190411
grep "message mid=" inbound.log-${DATE} > inbound.log-${DATE}.2
cat inbound.log-${DATE}.2 | awk '{print $9}' | awk -F'=' '{print $2}' > inbound.log-${DATE}.3
tar -czvf inbound.log-${DATE}.3.tgz inbound.log-${DATE}.3

scp 10.9.13.213:/opt/sendcloud/log/inbound.log-20190411.3.tgz .
scp 10.9.13.213:/opt/sendcloud/log/inbound.log-20190412.3.tgz .
scp 10.9.13.213:/opt/sendcloud/log/inbound.log-20190413.3.tgz .
find -iname inbound.log-2019041\*.3.tgz -exec tar -xzvf {} \;

统计分析

#!/usr/bin/env python3

import os

BASE_PATH = '~/Projects/Company/sendcloud/sc.inbound/'
for date in ('0411', '0412', '0413'):
    log_path = os.path.expanduser(os.path.join(BASE_PATH, 'inbound.log-2019%s.3' % date))
    result = {'~100KB': 0, '100KB~1M': 0, '1M~10M': 0, '10M~': 0}
    with open(log_path) as fp:
        for line in fp:
            content = line.strip()
            if not content.isdigit():
                print('errline: %r' % content)
                continue
            size = int(content)/1024.0
            if size <= 100:
                size_n = '~100KB'
            elif size <= 1024:
                size_n = '100KB~1M'
            elif size <= 10240:
                size_n = '1M~10M'
            else:
                size_n = '10M~'
            if size_n in result:
                result[size_n] += 1
            else:
                result[size_n] = 1
    print('%r: %r' % (date, result))

ECharts

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <script src="https://cdn.bootcss.com/echarts/4.2.1/echarts.simple.js"></script>
</head>
<body>
    <div id="main" style="width: 600px;height:400px;"></div>
    <script type="text/javascript">
        var _data = {
            '0411': {'~100K': 475505, '100K~1M': 48008, '1M~10M': 7823, '10M~': 621},
            '0412': {'~100K': 490741, '100K~1M': 35675, '1M~10M': 7004, '10M~': 492},
            '0413': {'~100K': 332244, '100K~1M': 14217, '1M~10M': 3736, '10M~': 322},
        };
        var data = [['num'], ['~100K'], ['100K~1M'], ['1M~10M'], ['10M~']];
        for (var date in _data) {
            data[0].push(date);
            data[1].push(_data[date][data[1][0]]);
            data[2].push(_data[date][data[2][0]]);
            data[3].push(_data[date][data[3][0]]);
            data[4].push(_data[date][data[4][0]]);
        }
        console.log(data);
        var option = {
            legend: {},
            tooltip : { trigger: 'item', formatter: "{d}%" },
            dataset: { source: data },
            series: [
                { type: 'pie', radius: 60, center: ['23%', '30%'] },
                { type: 'pie', radius: 60, center: ['53%', '30%'], encode: { itemName: 'num', value: '0412' } },
                { type: 'pie', radius: 60, center: ['83%', '30%'], encode: { itemName: 'num', value: '0413' } },
            ]
        };
        var myChart = echarts.init(document.getElementById('main'));
        myChart.setOption(option);
    </script>
</body>
</html>