| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# iperf-server.py |
| 4 |
# |
| 5 |
# Author: Toke Høiland-Jørgensen ([email protected]) |
| 6 |
# Date: 26 november 2012 |
| 7 |
# Copyright (c) 2012-2016, Toke Høiland-Jørgensen |
| 8 |
# |
| 9 |
# This program is free software: you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation, either version 3 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU General Public License |
| 20 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
from __future__ import absolute_import, division, print_function, unicode_literals |
| 23 |
|
| 24 |
import copy |
| 25 |
import csv |
| 26 |
import json |
| 27 |
import subprocess |
| 28 |
import threading |
| 29 |
import time |
| 30 |
|
| 31 |
iperf_cols = [ |
| 32 |
'timestamp', |
| 33 |
'server_host', |
| 34 |
'server_port', |
| 35 |
'client_host', |
| 36 |
'transfer_id', |
| 37 |
'start_time', |
| 38 |
'time_interval', |
| 39 |
'total_length', |
| 40 |
'speed', |
| 41 |
'jitter', |
| 42 |
'errors', |
| 43 |
'datagrams', |
| 44 |
'error_percent', |
| 45 |
'out_of_order', |
| 46 |
] |
| 47 |
|
| 48 |
TIMEOUT = 10.0 |
| 49 |
|
| 50 |
|
| 51 |
class IperfReader(csv.DictReader): |
| 52 |
|
| 53 |
def __init__(self, csvfile): |
| 54 |
csv.DictReader.__init__(self, csvfile, fieldnames=iperf_cols) |
| 55 |
|
| 56 |
|
| 57 |
def line_iterator(fp): |
| 58 |
line = fp.readline() |
| 59 |
while line: |
| 60 |
yield line |
| 61 |
line = fp.readline() |
| 62 |
|
| 63 |
|
| 64 |
class TestData(object): |
| 65 |
|
| 66 |
def __init__(self, r_id): |
| 67 |
self._id = r_id |
| 68 |
self._last_update = time.time() |
| 69 |
self._records = [] |
| 70 |
|
| 71 |
def add_record(self, record): |
| 72 |
self._records.append(record) |
| 73 |
self._last_update = time.time() |
| 74 |
|
| 75 |
def expired(self): |
| 76 |
return time.time() - self._last_update > TIMEOUT |
| 77 |
|
| 78 |
def records(self): |
| 79 |
return copy.deepcopy(self._records) |
| 80 |
|
| 81 |
|
| 82 |
class Monitor(threading.Thread): |
| 83 |
|
| 84 |
def __init__(self, *args, **kwargs): |
| 85 |
threading.Thread.__init__(self, *args, **kwargs) |
| 86 |
self._test_data = {} |
| 87 |
self._lock = threading.RLock() |
| 88 |
|
| 89 |
def run(self): |
| 90 |
|
| 91 |
prog = subprocess.Popen(['iperf', '-s', '-u', '-i', '0.5', '-y', 'c'], |
| 92 |
stdin=subprocess.PIPE, |
| 93 |
stdout=subprocess.PIPE, |
| 94 |
stderr=None, |
| 95 |
universal_newlines=True) |
| 96 |
prog.stdin.close() |
| 97 |
reader = IperfReader(line_iterator(prog.stdout)) |
| 98 |
|
| 99 |
for record in reader: |
| 100 |
self.handle_record(record) |
| 101 |
|
| 102 |
def handle_record(self, record): |
| 103 |
parts = record['time_interval'].split("-") |
| 104 |
length = float(parts[1]) - float(parts[0]) |
| 105 |
|
| 106 |
# too long interval, this is a summary record |
| 107 |
if length > 2: |
| 108 |
return |
| 109 |
|
| 110 |
r_id = record['transfer_id'] |
| 111 |
|
| 112 |
self._lock.acquire() |
| 113 |
if r_id not in self._test_data: |
| 114 |
self._test_data[r_id] = TestData(r_id) |
| 115 |
self._test_data[r_id].add_record(record) |
| 116 |
self._lock.release() |
| 117 |
|
| 118 |
self.collect_garbage() |
| 119 |
|
| 120 |
def collect_garbage(self): |
| 121 |
self._lock.acquire() |
| 122 |
for k, v in list(self._test_data.items()): |
| 123 |
if v.expired(): |
| 124 |
del self._test_data[k] |
| 125 |
self._lock.release() |
| 126 |
|
| 127 |
def get_records(self, r_id): |
| 128 |
self._lock.acquire() |
| 129 |
try: |
| 130 |
return self._test_data[r_id].records() |
| 131 |
finally: |
| 132 |
self._lock.release() |
| 133 |
|
| 134 |
|
| 135 |
monitor = Monitor() |
| 136 |
monitor.start() |
| 137 |
|
| 138 |
while True: |
| 139 |
try: |
| 140 |
request_id = input().strip() |
| 141 |
print(json.dumps(monitor.get_records(request_id))) |
| 142 |
except KeyError: |
| 143 |
print("No records for id %s." % request_id) |