-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: vezio <tyler.rimaldi@ibm.com>
- Loading branch information
vezio
committed
Aug 25, 2024
1 parent
5d8dcaa
commit 2d670df
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from iperf3_utils import * | ||
|
||
import subprocess | ||
import os | ||
import sys | ||
import signal | ||
|
||
|
||
def kill_all_iperf_servers(): | ||
try: | ||
result = subprocess.run( | ||
["ps", "aux"], text=True, capture_output=True, check=True | ||
) | ||
except subprocess.CalledProcessError as e: | ||
print(f"Error occurred while listing processes: {e}") | ||
sys.exit(1) | ||
|
||
processes = result.stdout.splitlines() | ||
|
||
for process in processes: | ||
try: | ||
if "iperf3 -s" in process and "-s" in process: | ||
parts = process.split() | ||
if len(parts) > 1: | ||
pid = int(parts[1]) | ||
# Not killing default iperf server spun up on entrypoint... | ||
if pid > 1: | ||
log.info( | ||
f"Killing iperf3 server process (PID: {pid}) in {CURR_POD_NAME} on {CURR_WORKER_NODE_NAME}" | ||
) | ||
try: | ||
os.kill(pid, signal.SIGTERM) | ||
except PermissionError: | ||
log.error( | ||
f"Permission denied: Could not kill process with PID {pid} in {CURR_POD_NAME} on {CURR_WORKER_NODE_NAME}." | ||
) | ||
sys.exit(1) | ||
except ProcessLookupError: | ||
log.error( | ||
f"Process with PID {pid} does not exist in {CURR_POD_NAME} on {CURR_WORKER_NODE_NAME}." | ||
) | ||
sys.exit(1) | ||
except Exception as e: | ||
log.error( | ||
f"Failed to kill process with PID {pid}: {e} in {CURR_POD_NAME} on {CURR_WORKER_NODE_NAME}" | ||
) | ||
sys.exit(1) | ||
else: | ||
log.info( | ||
f"Nothing left to kill in {CURR_POD_NAME} on {CURR_WORKER_NODE_NAME} (Not killing default entrypoint iperf3 server)." | ||
) | ||
else: | ||
log.error( | ||
f"Unexpected format in process line: {process} in {CURR_POD_NAME} on {CURR_WORKER_NODE_NAME}" | ||
) | ||
sys.exit(1) | ||
except ValueError: | ||
log.error( | ||
f"Could not convert PID to an integer: {process} in {CURR_POD_NAME} on {CURR_WORKER_NODE_NAME}" | ||
) | ||
sys.exit(1) | ||
except Exception as e: | ||
log.error( | ||
f"An unexpected error occurred: {e} in {CURR_POD_NAME} on {CURR_WORKER_NODE_NAME}" | ||
) | ||
sys.exit(1) | ||
|
||
|
||
if __name__ == "__main__": | ||
kill_all_iperf_servers() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters