Skip to content

Commit 369f646

Browse files
authored
Merge pull request #411 from Limmen/cli_command
heartbeats cli command added
2 parents 069d593 + da94a38 commit 369f646

File tree

1 file changed

+149
-3
lines changed
  • simulation-system/libs/csle-cli/src/csle_cli

1 file changed

+149
-3
lines changed

simulation-system/libs/csle-cli/src/csle_cli/cli.py

+149-3
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
687687
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager | snortmanagers "
688688
"| snortmanager | elkmanager | trafficmanagers | trafficmanager | kafkamanager "
689689
"| ossecmanagers | ossecmanager | ryumanager | filebeats | filebeat | metricbeat "
690-
"| metricbeats")
690+
"| metricbeats | heartbeats | heartbeat")
691691
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
692692
"""
693693
Stops an entity
@@ -763,6 +763,10 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
763763
stop_metricbeats(ip=ip, emulation=name, ip_first_octet=id)
764764
elif entity == "metricbeat":
765765
stop_metricbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
766+
elif entity == "heartbeats":
767+
stop_heartbeats(ip=ip, emulation=name, ip_first_octet=id)
768+
elif entity == "heartbeat":
769+
stop_heartbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
766770
else:
767771
container_stopped = False
768772
for node in config.cluster_config.cluster_nodes:
@@ -1337,6 +1341,58 @@ def stop_metricbeat(ip: str, container_ip: str, emulation: str, ip_first_octet:
13371341
bold=False)
13381342

13391343

1344+
def stop_heartbeats(ip: str, emulation: str, ip_first_octet: int) -> None:
1345+
"""
1346+
Utility function for stopping the heartbeats
1347+
1348+
:param ip: the ip of the node to stop the heartbeats
1349+
:param emulation: the emulation of the execution
1350+
:param ip_first_octet: the ID of the execution
1351+
:return: None
1352+
"""
1353+
import csle_common.constants.constants as constants
1354+
from csle_common.metastore.metastore_facade import MetastoreFacade
1355+
config = MetastoreFacade.get_config(id=1)
1356+
for node in config.cluster_config.cluster_nodes:
1357+
if node.ip == ip or ip == "":
1358+
stopped = ClusterController.stop_heartbeats(
1359+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1360+
ip_first_octet=ip_first_octet)
1361+
if stopped.outcome:
1362+
click.secho(f"Stopping heartbeats on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1363+
else:
1364+
click.secho(f"Heartbeats are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1365+
bold=False)
1366+
1367+
1368+
def stop_heartbeat(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
1369+
"""
1370+
Utility function for stopping the heartbeat
1371+
1372+
:param ip: the ip of the node to stop the heartbeat
1373+
:param container_ip: the ip of the host that traffic is running on
1374+
:param emulation: the emulation of the execution
1375+
:param ip_first_octet: the ID of the execution
1376+
:return: None
1377+
"""
1378+
import csle_common.constants.constants as constants
1379+
from csle_common.metastore.metastore_facade import MetastoreFacade
1380+
config = MetastoreFacade.get_config(id=1)
1381+
for node in config.cluster_config.cluster_nodes:
1382+
if node.ip == ip or ip == "":
1383+
stopped = ClusterController.stop_heartbeat(
1384+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1385+
ip_first_octet=ip_first_octet, container_ip=container_ip)
1386+
if stopped.outcome:
1387+
click.secho(
1388+
f"Stopping heartbeat with ip {container_ip} on port:"
1389+
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1390+
else:
1391+
click.secho(f"Heartbeat with ip {container_ip} is not "
1392+
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1393+
bold=False)
1394+
1395+
13401396
@click.argument('max_workers', default=10, type=int)
13411397
@click.argument('log_file', default="docker_statsmanager.log", type=str)
13421398
@click.argument('log_dir', default="/var/log/csle", type=str)
@@ -1531,7 +1587,7 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
15311587
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
15321588
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager "
15331589
"| trafficmanagers | trafficmanager | kafkamanager | ossecmanagers | ossecmanager "
1534-
"| ryumanager | filebeats | filebeat | metricbeats | metricbeat")
1590+
"| ryumanager | filebeats | filebeat | metricbeats | metricbeat | heartbeat | heartbeats")
15351591
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
15361592
container_ip: str, no_beats: bool, initial_start: bool) -> None:
15371593
"""
@@ -1620,6 +1676,11 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
16201676
elif entity == "metricbeat":
16211677
start_metricbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id,
16221678
initial_start=initial_start)
1679+
elif entity == "heartbeats":
1680+
start_heartbeats(ip=ip, emulation=name, ip_first_octet=id, initial_start=initial_start)
1681+
elif entity == "heartbeat":
1682+
start_heartbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id,
1683+
initial_start=initial_start)
16231684
else:
16241685
container_started = False
16251686
for node in config.cluster_config.cluster_nodes:
@@ -1944,6 +2005,57 @@ def start_metricbeat(ip: str, container_ip: str, emulation: str, ip_first_octet:
19442005
bold=False)
19452006

19462007

2008+
def start_heartbeats(ip: str, emulation: str, ip_first_octet: int, initial_start: bool):
2009+
"""
2010+
Utility function for starting heartbeats
2011+
2012+
:param ip: the ip of the node to start heartbeats
2013+
:param emulation: the emulation of the execution
2014+
:param ip_first_octet: the ID of the execution
2015+
:return: None
2016+
"""
2017+
import csle_common.constants.constants as constants
2018+
from csle_common.metastore.metastore_facade import MetastoreFacade
2019+
config = MetastoreFacade.get_config(id=1)
2020+
for node in config.cluster_config.cluster_nodes:
2021+
if node.ip == ip or ip == "":
2022+
operation_outcome = ClusterController.start_heartbeats(
2023+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2024+
ip_first_octet=ip_first_octet, initial_start=initial_start)
2025+
if operation_outcome.outcome:
2026+
click.secho(f"Starting heartbeats on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
2027+
else:
2028+
click.secho(f"Heartbeats are not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
2029+
bold=False)
2030+
2031+
2032+
def start_heartbeat(ip: str, container_ip: str, emulation: str, ip_first_octet: int, initial_start: bool):
2033+
"""
2034+
Utility function for starting heartbeat
2035+
2036+
:param ip: the ip of the node to start heartbeat
2037+
:param container_ip: the ip of the host to start
2038+
:param emulation: the emulation of the execution
2039+
:param ip_first_octet: the ID of the execution
2040+
:return: None
2041+
"""
2042+
import csle_common.constants.constants as constants
2043+
from csle_common.metastore.metastore_facade import MetastoreFacade
2044+
config = MetastoreFacade.get_config(id=1)
2045+
for node in config.cluster_config.cluster_nodes:
2046+
if node.ip == ip or ip == "":
2047+
operation_outcome = ClusterController.start_heartbeat(
2048+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2049+
ip_first_octet=ip_first_octet, container_ip=container_ip, initial_start=initial_start)
2050+
if operation_outcome.outcome:
2051+
click.secho(f"Started heartbeat with ip {container_ip} on "
2052+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
2053+
else:
2054+
click.secho(f"Heartbeat with ip {container_ip} is not "
2055+
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
2056+
bold=False)
2057+
2058+
19472059
def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
19482060
"""
19492061
Utility function for starting host manager
@@ -2504,7 +2616,7 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
25042616
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
25052617
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
25062618
"clientmanager | snortmanagers | elkmanager | trafficmanagers | kafkamanager | "
2507-
"ossecmanagers | ryumanager | filebeats | metricbeats")
2619+
"ossecmanagers | ryumanager | filebeats | metricbeats | heartbeats")
25082620
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
25092621
@click.option('--all', is_flag=True, help='list all')
25102622
@click.option('--running', is_flag=True, help='list running only (default)')
@@ -2589,6 +2701,8 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
25892701
list_filebeats(ip=ip, emulation=name, ip_first_octet=id)
25902702
elif entity == "metricbeats":
25912703
list_metricbeats(ip=ip, emulation=name, ip_first_octet=id)
2704+
elif entity == "heartbeats":
2705+
list_heartbeats(ip=ip, emulation=name, ip_first_octet=id)
25922706
else:
25932707
container = get_running_container(name=entity)
25942708
if container is not None:
@@ -2655,6 +2769,38 @@ def list_filebeats(ip: str, emulation: str, ip_first_octet: int) -> None:
26552769
click.secho('+' + '-' * 60 + '+', fg='white')
26562770

26572771

2772+
def list_heartbeats(ip: str, emulation: str, ip_first_octet: int) -> None:
2773+
"""
2774+
Utility function for listing heartbeats
2775+
2776+
:param ip: the ip of the node to list heartbeats
2777+
:param emulation: the emulation of the execution
2778+
:param ip_first_octet: the ID of the execution
2779+
2780+
:return: None
2781+
"""
2782+
import csle_common.constants.constants as constants
2783+
from csle_common.metastore.metastore_facade import MetastoreFacade
2784+
config = MetastoreFacade.get_config(id=1)
2785+
for node in config.cluster_config.cluster_nodes:
2786+
if node.ip == ip or ip == "":
2787+
heartbeats_info = ClusterController.get_host_managers_info(
2788+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2789+
ip_first_octet=ip_first_octet)
2790+
click.secho('+' + '-' * 60 + '+', fg='white')
2791+
click.secho(f'|{"Host IP":^30}|{"Heartbeats running Status":^29}|', fg='white')
2792+
click.secho('+' + '-' * 60 + '+', fg='white')
2793+
for i in range(len(heartbeats_info.hostManagersStatuses)):
2794+
status = "Running" if heartbeats_info.hostManagersStatuses[i].heartbeat_running else "Stopped"
2795+
status_color = 'green' if heartbeats_info.hostManagersStatuses[i].heartbeat_running else 'red'
2796+
click.secho('|', nl=False, fg='white')
2797+
click.secho(f'{heartbeats_info.ips[i]:<30}', nl=False, fg='white')
2798+
click.secho('|', nl=False, fg='white')
2799+
click.secho(f'{status:^29}', nl=False, fg=status_color)
2800+
click.secho('|', fg='white')
2801+
click.secho('+' + '-' * 60 + '+', fg='white')
2802+
2803+
26582804
def list_metricbeats(ip: str, emulation: str, ip_first_octet: int) -> None:
26592805
"""
26602806
Utility function for listing filebeats

0 commit comments

Comments
 (0)