Skip to content

Commit 6c13fcf

Browse files
authored
Merge pull request #413 from Limmen/cli_command
Packetbeats cli commands added.
2 parents 8fcdd88 + 63a340f commit 6c13fcf

File tree

1 file changed

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

1 file changed

+149
-2
lines changed

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

+149-2
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 | heartbeats | heartbeat")
690+
"| metricbeats | heartbeats | heartbeat | packetbeat | packetbeats")
691691
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
692692
"""
693693
Stops an entity
@@ -767,6 +767,10 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
767767
stop_heartbeats(ip=ip, emulation=name, ip_first_octet=id)
768768
elif entity == "heartbeat":
769769
stop_heartbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
770+
elif entity == "packetbeats":
771+
stop_packetbeats(ip=ip, emulation=name, ip_first_octet=id)
772+
elif entity == "packetbeat":
773+
stop_packetbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
770774
else:
771775
container_stopped = False
772776
for node in config.cluster_config.cluster_nodes:
@@ -1393,6 +1397,58 @@ def stop_heartbeat(ip: str, container_ip: str, emulation: str, ip_first_octet: i
13931397
bold=False)
13941398

13951399

1400+
def stop_packetbeats(ip: str, emulation: str, ip_first_octet: int) -> None:
1401+
"""
1402+
Utility function for stopping the packetbeats
1403+
1404+
:param ip: the ip of the node to stop the packetbeats
1405+
:param emulation: the emulation of the execution
1406+
:param ip_first_octet: the ID of the execution
1407+
:return: None
1408+
"""
1409+
import csle_common.constants.constants as constants
1410+
from csle_common.metastore.metastore_facade import MetastoreFacade
1411+
config = MetastoreFacade.get_config(id=1)
1412+
for node in config.cluster_config.cluster_nodes:
1413+
if node.ip == ip or ip == "":
1414+
stopped = ClusterController.stop_packetbeats(
1415+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1416+
ip_first_octet=ip_first_octet)
1417+
if stopped.outcome:
1418+
click.secho(f"Stopping packetbeats on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1419+
else:
1420+
click.secho(f"Packetbeats are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1421+
bold=False)
1422+
1423+
1424+
def stop_packetbeat(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
1425+
"""
1426+
Utility function for stopping the packetbeat
1427+
1428+
:param ip: the ip of the node to stop the packetbeat
1429+
:param container_ip: the ip of the host that traffic is running on
1430+
:param emulation: the emulation of the execution
1431+
:param ip_first_octet: the ID of the execution
1432+
:return: None
1433+
"""
1434+
import csle_common.constants.constants as constants
1435+
from csle_common.metastore.metastore_facade import MetastoreFacade
1436+
config = MetastoreFacade.get_config(id=1)
1437+
for node in config.cluster_config.cluster_nodes:
1438+
if node.ip == ip or ip == "":
1439+
stopped = ClusterController.stop_packetbeat(
1440+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1441+
ip_first_octet=ip_first_octet, container_ip=container_ip)
1442+
if stopped.outcome:
1443+
click.secho(
1444+
f"Stopping packetbeat with ip {container_ip} on port:"
1445+
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1446+
else:
1447+
click.secho(f"Packetbeat with ip {container_ip} is not "
1448+
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1449+
bold=False)
1450+
1451+
13961452
@click.argument('max_workers', default=10, type=int)
13971453
@click.argument('log_file', default="docker_statsmanager.log", type=str)
13981454
@click.argument('log_dir', default="/var/log/csle", type=str)
@@ -1587,7 +1643,8 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
15871643
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
15881644
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager "
15891645
"| trafficmanagers | trafficmanager | kafkamanager | ossecmanagers | ossecmanager "
1590-
"| ryumanager | filebeats | filebeat | metricbeats | metricbeat | heartbeat | heartbeats")
1646+
"| ryumanager | filebeats | filebeat | metricbeats | metricbeat | heartbeat | heartbeats"
1647+
"| packetbeat | packetbeats")
15911648
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
15921649
container_ip: str, no_beats: bool, initial_start: bool) -> None:
15931650
"""
@@ -1681,6 +1738,11 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
16811738
elif entity == "heartbeat":
16821739
start_heartbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id,
16831740
initial_start=initial_start)
1741+
elif entity == "packetbeats":
1742+
start_packetbeats(ip=ip, emulation=name, ip_first_octet=id, initial_start=initial_start)
1743+
elif entity == "packetbeat":
1744+
start_packetbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id,
1745+
initial_start=initial_start)
16841746
else:
16851747
container_started = False
16861748
for node in config.cluster_config.cluster_nodes:
@@ -2029,6 +2091,30 @@ def start_heartbeats(ip: str, emulation: str, ip_first_octet: int, initial_start
20292091
bold=False)
20302092

20312093

2094+
def start_packetbeats(ip: str, emulation: str, ip_first_octet: int, initial_start: bool):
2095+
"""
2096+
Utility function for starting packetbeats
2097+
2098+
:param ip: the ip of the node to start packetbeats
2099+
:param emulation: the emulation of the execution
2100+
:param ip_first_octet: the ID of the execution
2101+
:return: None
2102+
"""
2103+
import csle_common.constants.constants as constants
2104+
from csle_common.metastore.metastore_facade import MetastoreFacade
2105+
config = MetastoreFacade.get_config(id=1)
2106+
for node in config.cluster_config.cluster_nodes:
2107+
if node.ip == ip or ip == "":
2108+
operation_outcome = ClusterController.start_packetbeats(
2109+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2110+
ip_first_octet=ip_first_octet, initial_start=initial_start)
2111+
if operation_outcome.outcome:
2112+
click.secho(f"Starting packetbeats on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
2113+
else:
2114+
click.secho(f"Packetbeats are not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
2115+
bold=False)
2116+
2117+
20322118
def start_heartbeat(ip: str, container_ip: str, emulation: str, ip_first_octet: int, initial_start: bool):
20332119
"""
20342120
Utility function for starting heartbeat
@@ -2056,6 +2142,33 @@ def start_heartbeat(ip: str, container_ip: str, emulation: str, ip_first_octet:
20562142
bold=False)
20572143

20582144

2145+
def start_packetbeat(ip: str, container_ip: str, emulation: str, ip_first_octet: int, initial_start: bool):
2146+
"""
2147+
Utility function for starting packetbeat
2148+
2149+
:param ip: the ip of the node to start packetbeat
2150+
:param container_ip: the ip of the host to start
2151+
:param emulation: the emulation of the execution
2152+
:param ip_first_octet: the ID of the execution
2153+
:return: None
2154+
"""
2155+
import csle_common.constants.constants as constants
2156+
from csle_common.metastore.metastore_facade import MetastoreFacade
2157+
config = MetastoreFacade.get_config(id=1)
2158+
for node in config.cluster_config.cluster_nodes:
2159+
if node.ip == ip or ip == "":
2160+
operation_outcome = ClusterController.start_packetbeat(
2161+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2162+
ip_first_octet=ip_first_octet, container_ip=container_ip, initial_start=initial_start)
2163+
if operation_outcome.outcome:
2164+
click.secho(f"Started packetbeat with ip {container_ip} on "
2165+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
2166+
else:
2167+
click.secho(f"Packetbeat with ip {container_ip} is not "
2168+
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
2169+
bold=False)
2170+
2171+
20592172
def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
20602173
"""
20612174
Utility function for starting host manager
@@ -2703,6 +2816,8 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
27032816
list_metricbeats(ip=ip, emulation=name, ip_first_octet=id)
27042817
elif entity == "heartbeats":
27052818
list_heartbeats(ip=ip, emulation=name, ip_first_octet=id)
2819+
elif entity == "packetbeats":
2820+
list_packetbeats(ip=ip, emulation=name, ip_first_octet=id)
27062821
else:
27072822
container = get_running_container(name=entity)
27082823
if container is not None:
@@ -2801,6 +2916,38 @@ def list_heartbeats(ip: str, emulation: str, ip_first_octet: int) -> None:
28012916
click.secho('+' + '-' * 60 + '+', fg='white')
28022917

28032918

2919+
def list_packetbeats(ip: str, emulation: str, ip_first_octet: int) -> None:
2920+
"""
2921+
Utility function for listing packetbeats
2922+
2923+
:param ip: the ip of the node to list packetbeats
2924+
:param emulation: the emulation of the execution
2925+
:param ip_first_octet: the ID of the execution
2926+
2927+
:return: None
2928+
"""
2929+
import csle_common.constants.constants as constants
2930+
from csle_common.metastore.metastore_facade import MetastoreFacade
2931+
config = MetastoreFacade.get_config(id=1)
2932+
for node in config.cluster_config.cluster_nodes:
2933+
if node.ip == ip or ip == "":
2934+
packetbeats_info = ClusterController.get_host_managers_info(
2935+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2936+
ip_first_octet=ip_first_octet)
2937+
click.secho('+' + '-' * 60 + '+', fg='white')
2938+
click.secho(f'|{"Host IP":^30}|{"Packetbeats running Status":^29}|', fg='white')
2939+
click.secho('+' + '-' * 60 + '+', fg='white')
2940+
for i in range(len(packetbeats_info.hostManagersStatuses)):
2941+
status = "Running" if packetbeats_info.hostManagersStatuses[i].packetbeat_running else "Stopped"
2942+
status_color = 'green' if packetbeats_info.hostManagersStatuses[i].packetbeat_running else 'red'
2943+
click.secho('|', nl=False, fg='white')
2944+
click.secho(f'{packetbeats_info.ips[i]:<30}', nl=False, fg='white')
2945+
click.secho('|', nl=False, fg='white')
2946+
click.secho(f'{status:^29}', nl=False, fg=status_color)
2947+
click.secho('|', fg='white')
2948+
click.secho('+' + '-' * 60 + '+', fg='white')
2949+
2950+
28042951
def list_metricbeats(ip: str, emulation: str, ip_first_octet: int) -> None:
28052952
"""
28062953
Utility function for listing filebeats

0 commit comments

Comments
 (0)