コード例 #1
0
ファイル: node.py プロジェクト: fangqiang/upyun_tingyun
    def __init__(self, filename, pars):
        xls = XLSReader(filename)
        self.table = Table(xls.get_head(), xls.get_data())

        self.records = self.table.get_sub(lambda record: True)
        self.timeout_error = self.table.get_sub(lambda record: record[u"错误代码"] == u'任务超时(页面)')
        self.reconn = self.table.get_sub(lambda record: record[u"错误代码"] == u'与服务器的连接被重置(页面)')
        self.conn_fail = self.table.get_sub(lambda record: record[u"错误代码"] == u'无法与服务器建立连接(页面)')
        self.md5_not_match = self.table.get_sub(lambda record: record[u"错误代码"] == u'MD5值和预期不相符')
        self.slow = self.table.get_sub(lambda record: record[u"总下载时间"] > pars['slow_time'] and record[u"下载速度(B/s)"] / record[u"平均下载速度(B/s)"] < pars['download_percent']/100)
        self.target_ip_error = self.table.get_sub(lambda record: record[u"目标IP"] != self.get_expect_target_ip())

        errors = [self.timeout_error, self.reconn, self.conn_fail, self.md5_not_match]
        self.slow = self.l_sub_l(self.slow, errors)
        self.normal = self.l_sub_l(self.table.get_sub(lambda ij: True), errors + [self.slow])
コード例 #2
0
ファイル: node.py プロジェクト: fangqiang/upyun_tingyun
class Node(object):
    cdn_file = 'upyun/datafile/lists_cdn_new'

    def __init__(self, filename, pars):
        xls = XLSReader(filename)
        self.table = Table(xls.get_head(), xls.get_data())

        self.records = self.table.get_sub(lambda record: True)
        self.timeout_error = self.table.get_sub(lambda record: record[u"错误代码"] == u'任务超时(页面)')
        self.reconn = self.table.get_sub(lambda record: record[u"错误代码"] == u'与服务器的连接被重置(页面)')
        self.conn_fail = self.table.get_sub(lambda record: record[u"错误代码"] == u'无法与服务器建立连接(页面)')
        self.md5_not_match = self.table.get_sub(lambda record: record[u"错误代码"] == u'MD5值和预期不相符')
        self.slow = self.table.get_sub(lambda record: record[u"总下载时间"] > pars['slow_time'] and record[u"下载速度(B/s)"] / record[u"平均下载速度(B/s)"] < pars['download_percent']/100)
        self.target_ip_error = self.table.get_sub(lambda record: record[u"目标IP"] != self.get_expect_target_ip())

        errors = [self.timeout_error, self.reconn, self.conn_fail, self.md5_not_match]
        self.slow = self.l_sub_l(self.slow, errors)
        self.normal = self.l_sub_l(self.table.get_sub(lambda ij: True), errors + [self.slow])

    @staticmethod
    def l_sub_l(target_set, sub_sets):
        negative = []
        for sub_set in sub_sets:
            negative += [i[u'时间'] for i in sub_set]

        return [i for i in target_set if i[u'时间'] not in negative]

    def get_percent(self, record, key, records):
        val = record[key]
        (cnt0, cnt1, cnt2) = (0, 0, 0)
        for i in self.records:
            cnt0 += 1 if i[key] == val else 0
        for i in records:
            cnt1 += 1 if i[key] == val else 0
        for i in self.normal:
            cnt2 += 1 if i[key] == val else 0

        per = self.save2f(cnt1 / float(cnt0))
        per1 = self.save2f(cnt2 / float(cnt0))

        s = str(per) + "%, " + str(per1) + "%" + "(%s/%s)" % (str(cnt1), str(cnt0))
        return s

    def is_dest_ip_right(self, ip):

        ip_f = IP_Filter()
        ips = ip_f.get_ip_from_file(self.cdn_file)

        if ip in ips:
            return "ok"
        else:
            return "error"

    def report_err(self, records):
        l = []
        head = [u'源ip', u"源ip(分析)", u'源dns', u"源dns(分析)", u'目标IP', u"目标IP(分析)",
                # u"期望ip",
                u"dns解析", u"错误类型", u"下载速度(B/s)"]

        for i in records:
            tmp = [i[u'监测点IP'],
                   self.get_percent(i, u'监测点IP', records),
                   i[u'DNS服务器'],
                   self.get_percent(i, u'DNS服务器', records),
                   i[u'目标IP'],
                   self.get_percent(i, u'目标IP', records),
                   # self.get_expect_target_ip(),
                   self.is_dest_ip_right(i[u"目标IP"]),
                   i[u"错误代码"],
                   "(%d%s) %.2f/%.2f " % (self.save2f(i[u'下载速度(B/s)'] / i[u'平均下载速度(B/s)']), '%',
                                          i[u'下载速度(B/s)'] / 1024, i[u'平均下载速度(B/s)'] / 1024)
                   ]

            l.append(tmp)
        return head, l

    def report_slow(self):
        l = []
        head = [u'源ip', u"源ip(分析)", u'源dns', u"源dns(分析)", u'目标IP', u"目标IP(分析)",
                u"dns解析", u"总下载时间", u"下载速度(B/s)"]

        for i in self.slow:
            tmp = [i[u'监测点IP'],
                   self.get_percent(i, u'监测点IP', self.slow),
                   i[u'DNS服务器'],
                   self.get_percent(i, u'DNS服务器', self.slow),
                   i[u'目标IP'],
                   # self.get_expect_target_ip(),
                   self.get_percent(i, u'目标IP', self.slow),
                   self.is_dest_ip_right(i[u"目标IP"]),
                   "%.2f" % i[u"总下载时间"],
                   "(%d%s) %.2f/%.2f " % (self.save2f(i[u'下载速度(B/s)'] / i[u'平均下载速度(B/s)']), '%',
                                          i[u'下载速度(B/s)'] / 1024, i[u'平均下载速度(B/s)'] / 1024)
                   ]
            l.append(tmp)
        return head, l

    def report_all(self):

        head = [u"监测点", u'总检测(次)', u"timeout(次)", u'MD5值错误(次)', u"连接重置(次)", u'连接失败(次)',
                u"期望目标IP", u"目标IP不符合期望(次)", u"响应慢",
                u"正常"]

        length = len(self.records)

        if length == 0:
            return head, []

        records = [[self.records[0][u"城市"] + "(" + self.records[0][u"运营商"] + ")",
                    length,
                    "%d (%d%s)" % (len(self.timeout_error), self.save2f(len(self.timeout_error) / float(length)), "%"),
                    "%d (%d%s)" % (len(self.md5_not_match), self.save2f(len(self.md5_not_match) / float(length)), "%"),
                    "%d (%d%s)" % (len(self.reconn), self.save2f(len(self.reconn) / float(length)), "%"),
                    "%d (%d%s)" % (len(self.conn_fail), self.save2f(len(self.conn_fail) / float(length)), "%"),
                    self.get_expect_target_ip(),
                    "%d (%d%s)" % (
                        len(self.target_ip_error), self.save2f(len(self.target_ip_error) / float(length)), "%"),
                    len(self.slow),
                    len(self.normal)
                    ]]
        return head, records

    def report_normal(self):
        if not self.normal:
            return [], []

        l = []
        for row in self.normal:
            assert isinstance(row, Row)
            l.append(row.values())

        head = row.keys()
        return head, l

    @staticmethod
    def save2f(f):
        return int(float('%.2f' % f) * 100)

    def get_expect_target_ip(self):

        l = []
        for i in self.records:
            l.append(i[u"目标IP"])

        myset = set(l)
        d = {}
        for item in myset:
            d[l.count(item)] = item

        return d[max(d.keys())]