summaryrefslogtreecommitdiff
path: root/apps/delay.py
blob: 61be9dc770eeafffbd231b9fa7b14bfe664a992a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# 时延测试接口
import random
import threading

import requests
from apiflask.validators import OneOf
from apiflask import APIFlask,APIBlueprint,Schema
from apiflask.fields import Integer,String,List,Nested,IP,Dict
from flask import request
import pandas as pd
from requests.exceptions import Timeout
import numpy as np

bp=APIBlueprint("delay",__name__,url_prefix="/delay")

icmp_delaytable={}
tcp_delaytable={}
dns_delaytable={}

class TestNode(Schema):
    Id = Integer()
    Name=String()
    Ip=String()
    Loc=String()
    Port=Integer()

class Delay(Schema):
    Id=Integer()
    CurrDelay=String()
    # MeanDelay=Integer()
    # MaxDelay=Integer()
    Type=String()
class DelayOut(Schema):
    delay_data=List(Nested(Delay))

# 时延数据列表初始化
# icmpdelay_state={}
# tcpdelay_state={}
# dnsdelay_state={}
# df=pd.read_csv("./server.csv",encoding="utf-8")
# for index,row in df.iterrows():
#     icmpdelay_state[row['id']]={}
#     tcpdelay_state[row['id']]={}
#     dnsdelay_state[row['id']]={}


@bp.get("/<string:type>")
@bp.doc("获取每个节点的时延数据","type参数为{icmp,dns,tcp}中的一个")
@bp.input({"ip":IP(required=False)},location="query")
@bp.output(DelayOut)
def get_pernode_delay(query_data,type):
    addr=""
    if 'ip' in query_data.keys():
        addr=query_data['ip']
    ans = []
    threads=[]
    df = pd.read_csv("./server.csv", encoding="utf-8")
    for index, row in df.iterrows():
        mythread=threading.Thread(target=task,args=[ans,addr,row,type])
        mythread.start()
        threads.append(mythread)
    for t in threads:
        t.join()
    # if type=="icmp":
    #     df=pd.read_csv("./server.csv",encoding="utf-8")
    #     for index,row in df.iterrows():
    #         ans.append({
    #             'Id':row['id'],
    #             'CurrDelay':icmp_delay_query(addr,row['ip']),
    #             'Type':type})
    # if type=="tcp":
    #     df=pd.read_csv("./server.csv",encoding="utf-8")
    #     for index,row in df.iterrows():
    #         ans.append({
    #             'Id':row['id'],
    #             'CurrDelay':tcp_delay_query(addr,row['ip']),
    #             'Type':type})
    # if type=="dns":
    #     df=pd.read_csv("./server.csv",encoding="utf-8")
    #     for index,row in df.iterrows():
    #         ans.append({
    #             'Id':row['id'],
    #             'CurrDelay':dns_delay_query(addr,row['ip']),
    #             'Type':type})
    return {'delay_data':ans}

threadLock = threading.Lock()
def task(ans,addr,row,type):
    if type=="icmp":
        res=icmp_delay_query(addr,row['ip'])
        threadLock.acquire()
        ans.append({
            'Id':row['id'],
            'CurrDelay':res,
            'Type':type})
        threadLock.release()
        return
    if type=="tcp":
        res=tcp_delay_query(addr, row['ip'])
        threadLock.acquire()
        ans.append({
            'Id':row['id'],
            'CurrDelay':res,
            'Type':type})
        threadLock.release()
        return
    if type=="dns":
        res=dns_delay_query(addr, row['ip'])
        threadLock.acquire()
        ans.append({
            'Id':row['id'],
            'CurrDelay':res,
            'Type':type})
        threadLock.release()

def icmp_delay_query(target,addr):
    try:
        res=requests.get(url="http://"+addr+":2525/script/icmpdelay",params={'ip':target},timeout=5)
        print("icmp ok:" + addr + "-------" + res.text+"-------"+str(res.elapsed.total_seconds()))
        icmp_delaytable[str(addr)+str(target)]=res.text
        return res.text
    except Timeout:
        # 如果存在旧数据
        if str(addr)+str(target) in icmp_delaytable.keys():
            pass
        # 不存在则设0
        else:
            icmp_delaytable[str(addr)+str(target)]=0
        return icmp_delaytable[str(addr)+str(target)]



def tcp_delay_query(target,addr):
    try:
        res = requests.get(url="http://" + addr + ":2525/script/tcpdelay", params={'ip': target, 'port': 53}, timeout=5)
        print("tcp ok:" + addr + "-------" + res.text)
        tcp_delaytable[str(addr)+str(target)] = res.text
        return res.text
    except Timeout:
        # 如果存在旧数据
        if str(addr) + str(target) in tcp_delaytable.keys():
            pass
        # 不存在则设0
        else:
            tcp_delaytable[str(addr)+str(target)] = 0
        return tcp_delaytable[str(addr)+str(target)]



def dns_delay_query(target,addr):
    try:
        res = requests.get(url="http://" + addr + ":2525/script/dnsdelay", params={'ip': target},timeout=5)
        print("dns ok:" + addr + "-------" + res.text)
        dns_delaytable[str(addr)+str(target)] = res.text
        return dns_delaytable[str(addr)+str(target)]
    except Timeout:
        # 如果存在旧数据
        if str(addr) + str(target) in dns_delaytable.keys():
            pass
        # 不存在则设0
        else:
            dns_delaytable[str(addr)+str(target)] = 0
        return dns_delaytable[str(addr)+str(target)]