Skip to content

Commit

Permalink
Merge pull request #403 from Limmen/cli_command
Browse files Browse the repository at this point in the history
Snortmanager cli functions added.
  • Loading branch information
Limmen authored Jul 31, 2024
2 parents 60cf040 + f86e35e commit e3836be
Showing 1 changed file with 177 additions and 17 deletions.
194 changes: 177 additions & 17 deletions simulation-system/libs/csle-cli/src/csle_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,8 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
@click.argument('entity', default="", shell_complete=stop_shell_complete)
@click.command("stop", help="prometheus | node_exporter | cadvisor | grafana | flask | container-name | "
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager")
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager | snortmanagers "
"| snortmanager")
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
"""
Stops an entity
Expand Down Expand Up @@ -734,6 +735,10 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
stop_host_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
elif entity == "clientmanager":
stop_client_manager(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "snortmanagers":
stop_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "snortmanager":
stop_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
else:
container_stopped = False
for node in config.cluster_config.cluster_nodes:
Expand Down Expand Up @@ -978,6 +983,57 @@ def stop_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
bold=False)


def stop_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for stopping the snort ids managers
:param ip: the ip of the node to stop the snort ids mangers
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
stopped = ClusterController.stop_snort_ids_managers(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
emulation=emulation, ip_first_octet=ip_first_octet)
if stopped:
click.secho(f"Stopping snort ids managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Snort ids managers are not stopped:"
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}", bold=False)


def stop_snort_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for stopping the snort ids manager
:param ip: the ip of the node to stop the host manager
:param container_ip: the ip of the host to be stopped
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
stopped = ClusterController.stop_snort_ids_manager(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
emulation=emulation, ip_first_octet=ip_first_octet,
container_ip=container_ip)
if stopped:
click.secho(
f"Stopping snort ids on the host with ip {container_ip} on "
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Snort ids on the host with ip {container_ip} is not "
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


@click.argument('max_workers', default=10, type=int)
@click.argument('log_file', default="docker_statsmanager.log", type=str)
@click.argument('log_dir', default="/var/log/csle", type=str)
Expand Down Expand Up @@ -1169,7 +1225,7 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
@click.command("start", help="prometheus | node_exporter | grafana | cadvisor | flask | pgadmin | "
"container-name | emulation-name | all | statsmanager | training_job "
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
"| hostmanager | clientmanager")
"| hostmanager | clientmanager | snortmanagers | snortmanager")
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
container_ip: str, no_beats: bool) -> None:
"""
Expand Down Expand Up @@ -1230,6 +1286,10 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
start_host_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
elif entity == "clientmanager":
start_client_manager(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "snortmanagers":
start_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "snortmanager":
start_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
else:
container_started = False
for node in config.cluster_config.cluster_nodes:
Expand Down Expand Up @@ -1499,6 +1559,57 @@ def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octe
bold=False)


def start_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting snort managers
:param ip: the ip of the node to start snort managers
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
operation_outcome = ClusterController.start_snort_ids_managers(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
if operation_outcome.outcome:
click.secho(f"Starting snort ids managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Snort ids managers are not started:"
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}", bold=False)


def start_snort_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
"""
Utility function for starting snort ids manager
:param ip: the ip of the node to start snort ids manager
:param container_ip: the ip of the host to start
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
operation_outcome = ClusterController.start_snort_ids_manager(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet, container_ip=container_ip)
if operation_outcome.outcome:
click.secho(f"Started snort ids manager with ip {container_ip} on "
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
else:
click.secho(f"Snort ids manager with ip {container_ip} is not "
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
bold=False)


def run_image(image: str, name: str, create_network: bool = True, version: str = "0.0.1") -> bool:
"""
Runs a container with a given image
Expand Down Expand Up @@ -1786,7 +1897,7 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
@click.command("ls", help="containers | networks | images | emulations | all | environments | prometheus "
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
"clientmanager")
"clientmanager | snortmanagers")
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
@click.option('--all', is_flag=True, help='list all')
@click.option('--running', is_flag=True, help='list running only (default)')
Expand Down Expand Up @@ -1855,6 +1966,8 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
list_host_managers(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "clientmanager":
list_client_manager(ip=ip, emulation=name, ip_first_octet=id)
elif entity == "snortmanagers":
list_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
else:
container = get_running_container(name=entity)
if container is not None:
Expand Down Expand Up @@ -1925,6 +2038,43 @@ def list_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
click.secho('+' + '-' * 50 + '+', fg='white')


def list_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for listing snort ids managers
:param ip: the ip of the node to start snort ids managers
:param emulation: the emulation of the execution
:param ip_first_octet: the ID of the execution
:return: None
"""
import csle_common.constants.constants as constants
from csle_common.metastore.metastore_facade import MetastoreFacade
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
snort_manager_info = ClusterController.get_snort_ids_managers_info(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)

for i in range(len(snort_manager_info.ips)):
status = "Running" if snort_manager_info.snortIdsManagersRunning[i] else "Stopped"
status_color = 'green' if snort_manager_info.snortIdsManagersRunning[i] else 'red'

click.secho('+' + '-' * 60 + '+', fg='white')
click.secho(f'|{"Snort ids manager IP":^40}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{snort_manager_info.ips[i]:<19}', nl=False, fg=status_color)
click.secho('|', fg='white')

click.secho('+' + '-' * 60 + '+', fg='white')
click.secho(f'|{"Status":^40}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{status:<19}', nl=False, fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')


def list_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
"""
Utility function for listing client managers
Expand All @@ -1940,49 +2090,59 @@ def list_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
config = MetastoreFacade.get_config(id=1)
for node in config.cluster_config.cluster_nodes:
if node.ip == ip or ip == "":
host_manager_info = ClusterController.get_client_managers_info(
client_manager_info = ClusterController.get_client_managers_info(
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
ip_first_octet=ip_first_octet)
for i in range(len(client_manager_info.ips)):
status_color = 'green' if client_manager_info.clientManagersStatuses[i].num_clients > 0 else 'red'
manager_status = 'Running' if client_manager_info.clientManagersStatuses[i].num_clients > 0 \
else 'Stopped'

click.secho('+' + '-' * 60 + '+', fg='white')
click.secho(f'|{"Host IP":^40}', nl=False, fg='white')
for i in range(len(host_manager_info.ips)):
click.secho('+' + '-' * 60 + '+', fg='white')
click.secho(f'|{"Host IP":^40}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{client_manager_info.ips[i]:<19}', nl=False, fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')

click.secho(f'|{"Status":^40}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{host_manager_info.ips[i]:<19}', nl=False, fg="green")
click.secho(f'{manager_status:<19}', nl=False, fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')

click.secho(f'|{"Clients number":^40}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{host_manager_info.clientManagersStatuses[i].num_clients:<19}', nl=False, fg="green")
click.secho(f'{client_manager_info.clientManagersStatuses[i].num_clients:<19}', nl=False,
fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')

click.secho(f'|{"Client process active":^40}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{bool(host_manager_info.clientManagersStatuses[i].client_process_active):<19}',
nl=False, fg="green")
click.secho(f'{bool(client_manager_info.clientManagersStatuses[i].client_process_active):<19}',
nl=False, fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')

click.secho(f'|{"Procedure active":^40}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{bool(host_manager_info.clientManagersStatuses[i].producer_active):<19}',
nl=False, fg="green")
click.secho(f'{bool(client_manager_info.clientManagersStatuses[i].producer_active):<19}',
nl=False, fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')

click.secho(f'|{"Client time step length in seconds":^40}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{host_manager_info.clientManagersStatuses[i].clients_time_step_len_seconds:<19}',
nl=False, fg="green")
click.secho(f'{client_manager_info.clientManagersStatuses[i].clients_time_step_len_seconds:<19}',
nl=False, fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')

click.secho(f'|{"Producer time step length in seconds":^40}', nl=False, fg='white')
click.secho('|', nl=False, fg='white')
click.secho(f'{host_manager_info.clientManagersStatuses[i].producer_time_step_len_seconds:<19}',
nl=False, fg="green")
click.secho(f'{client_manager_info.clientManagersStatuses[i].producer_time_step_len_seconds:<19}',
nl=False, fg=status_color)
click.secho('|', fg='white')
click.secho('+' + '-' * 60 + '+', fg='white')

Expand Down

0 comments on commit e3836be

Please sign in to comment.