-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathiperf_server.py
78 lines (65 loc) · 2.23 KB
/
iperf_server.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Launch IPerf server in daemon mode (requires Iperf 3.1 or above).
# Can be used with cron to continuously check daemon and respawn if necessary.
# Author: Dario Vianello ([email protected])
#
# Please remember to set the correct timezone on the server running this script.
# To add the cron job to the server use:
# (crontab -l ; echo "*/1 * * * * ~/iperf_server.py") | crontab -
# Runs every minute.
###
import subprocess
import logging
import sys
import time
import os.path
IPERF_PID_FILE = "iperf.pid"
IPERF_MONITOR_LOG = "iperf_monitor.log"
IPERF_EXECUTABLE = "iperf3"
IPERF_ARGS = ["--pidfile", os.path.expanduser("~/" + IPERF_PID_FILE),
"-s", "-D"]
def check_pid(pid):
""" Check for the existence of a unix pid. """
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
def launch_iperf(executable, args):
""" Launch IPerf according to a set of given args"""
iperf_command = [executable] + args
iperf_command = subprocess.Popen(iperf_command)
# We can't check any return code, as python fires-and-forgets and
# the process enters daemon mode. We would always receive a None.
def check_iperf(pidfile):
""" Check if pidfile exists, and if the process is alive """
if os.path.isfile(pidfile):
pid = int(open(pidfile).readline().strip("\x00"))
if check_pid(pid):
return True
else:
return False
# If the file does not exits, return False
return False
# Configure logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M:%S',
filename=IPERF_MONITOR_LOG)
# MAIN
if check_iperf(IPERF_PID_FILE):
logging.debug("IPerf is running.")
else:
logging.warning("IPerf is not running. Spawning...")
launch_iperf(IPERF_EXECUTABLE, IPERF_ARGS)
# Wait for 1 second before checking if IPerf is running
# Helps on slow systems.
time.sleep(1)
if check_iperf(IPERF_PID_FILE):
logging.info("IPerf launched successfully!")
else:
logging.critical("Unable to launch IPerf correctly. Exiting...")
sys.exit(1)