Skip to content

Commit e3836be

Browse files
authored
Merge pull request #403 from Limmen/cli_command
Snortmanager cli functions added.
2 parents 60cf040 + f86e35e commit e3836be

File tree

1 file changed

+177
-17
lines changed
  • simulation-system/libs/csle-cli/src/csle_cli

1 file changed

+177
-17
lines changed

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

+177-17
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,8 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
684684
@click.argument('entity', default="", shell_complete=stop_shell_complete)
685685
@click.command("stop", help="prometheus | node_exporter | cadvisor | grafana | flask | container-name | "
686686
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
687-
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager")
687+
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager | snortmanagers "
688+
"| snortmanager")
688689
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
689690
"""
690691
Stops an entity
@@ -734,6 +735,10 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
734735
stop_host_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
735736
elif entity == "clientmanager":
736737
stop_client_manager(ip=ip, emulation=name, ip_first_octet=id)
738+
elif entity == "snortmanagers":
739+
stop_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
740+
elif entity == "snortmanager":
741+
stop_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
737742
else:
738743
container_stopped = False
739744
for node in config.cluster_config.cluster_nodes:
@@ -978,6 +983,57 @@ def stop_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
978983
bold=False)
979984

980985

986+
def stop_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
987+
"""
988+
Utility function for stopping the snort ids managers
989+
990+
:param ip: the ip of the node to stop the snort ids mangers
991+
:param emulation: the emulation of the execution
992+
:param ip_first_octet: the ID of the execution
993+
:return: None
994+
"""
995+
import csle_common.constants.constants as constants
996+
from csle_common.metastore.metastore_facade import MetastoreFacade
997+
config = MetastoreFacade.get_config(id=1)
998+
for node in config.cluster_config.cluster_nodes:
999+
if node.ip == ip or ip == "":
1000+
stopped = ClusterController.stop_snort_ids_managers(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1001+
emulation=emulation, ip_first_octet=ip_first_octet)
1002+
if stopped:
1003+
click.secho(f"Stopping snort ids managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1004+
else:
1005+
click.secho(f"Snort ids managers are not stopped:"
1006+
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}", bold=False)
1007+
1008+
1009+
def stop_snort_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
1010+
"""
1011+
Utility function for stopping the snort ids manager
1012+
1013+
:param ip: the ip of the node to stop the host manager
1014+
:param container_ip: the ip of the host to be stopped
1015+
:param emulation: the emulation of the execution
1016+
:param ip_first_octet: the ID of the execution
1017+
:return: None
1018+
"""
1019+
import csle_common.constants.constants as constants
1020+
from csle_common.metastore.metastore_facade import MetastoreFacade
1021+
config = MetastoreFacade.get_config(id=1)
1022+
for node in config.cluster_config.cluster_nodes:
1023+
if node.ip == ip or ip == "":
1024+
stopped = ClusterController.stop_snort_ids_manager(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1025+
emulation=emulation, ip_first_octet=ip_first_octet,
1026+
container_ip=container_ip)
1027+
if stopped:
1028+
click.secho(
1029+
f"Stopping snort ids on the host with ip {container_ip} on "
1030+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1031+
else:
1032+
click.secho(f"Snort ids on the host with ip {container_ip} is not "
1033+
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1034+
bold=False)
1035+
1036+
9811037
@click.argument('max_workers', default=10, type=int)
9821038
@click.argument('log_file', default="docker_statsmanager.log", type=str)
9831039
@click.argument('log_dir', default="/var/log/csle", type=str)
@@ -1169,7 +1225,7 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
11691225
@click.command("start", help="prometheus | node_exporter | grafana | cadvisor | flask | pgadmin | "
11701226
"container-name | emulation-name | all | statsmanager | training_job "
11711227
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
1172-
"| hostmanager | clientmanager")
1228+
"| hostmanager | clientmanager | snortmanagers | snortmanager")
11731229
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
11741230
container_ip: str, no_beats: bool) -> None:
11751231
"""
@@ -1230,6 +1286,10 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
12301286
start_host_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
12311287
elif entity == "clientmanager":
12321288
start_client_manager(ip=ip, emulation=name, ip_first_octet=id)
1289+
elif entity == "snortmanagers":
1290+
start_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
1291+
elif entity == "snortmanager":
1292+
start_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
12331293
else:
12341294
container_started = False
12351295
for node in config.cluster_config.cluster_nodes:
@@ -1499,6 +1559,57 @@ def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octe
14991559
bold=False)
15001560

15011561

1562+
def start_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int):
1563+
"""
1564+
Utility function for starting snort managers
1565+
1566+
:param ip: the ip of the node to start snort managers
1567+
:param emulation: the emulation of the execution
1568+
:param ip_first_octet: the ID of the execution
1569+
:return: None
1570+
"""
1571+
import csle_common.constants.constants as constants
1572+
from csle_common.metastore.metastore_facade import MetastoreFacade
1573+
config = MetastoreFacade.get_config(id=1)
1574+
for node in config.cluster_config.cluster_nodes:
1575+
if node.ip == ip or ip == "":
1576+
operation_outcome = ClusterController.start_snort_ids_managers(
1577+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1578+
ip_first_octet=ip_first_octet)
1579+
if operation_outcome.outcome:
1580+
click.secho(f"Starting snort ids managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1581+
else:
1582+
click.secho(f"Snort ids managers are not started:"
1583+
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}", bold=False)
1584+
1585+
1586+
def start_snort_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
1587+
"""
1588+
Utility function for starting snort ids manager
1589+
1590+
:param ip: the ip of the node to start snort ids manager
1591+
:param container_ip: the ip of the host to start
1592+
:param emulation: the emulation of the execution
1593+
:param ip_first_octet: the ID of the execution
1594+
:return: None
1595+
"""
1596+
import csle_common.constants.constants as constants
1597+
from csle_common.metastore.metastore_facade import MetastoreFacade
1598+
config = MetastoreFacade.get_config(id=1)
1599+
for node in config.cluster_config.cluster_nodes:
1600+
if node.ip == ip or ip == "":
1601+
operation_outcome = ClusterController.start_snort_ids_manager(
1602+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1603+
ip_first_octet=ip_first_octet, container_ip=container_ip)
1604+
if operation_outcome.outcome:
1605+
click.secho(f"Started snort ids manager with ip {container_ip} on "
1606+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1607+
else:
1608+
click.secho(f"Snort ids manager with ip {container_ip} is not "
1609+
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1610+
bold=False)
1611+
1612+
15021613
def run_image(image: str, name: str, create_network: bool = True, version: str = "0.0.1") -> bool:
15031614
"""
15041615
Runs a container with a given image
@@ -1786,7 +1897,7 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
17861897
@click.command("ls", help="containers | networks | images | emulations | all | environments | prometheus "
17871898
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
17881899
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
1789-
"clientmanager")
1900+
"clientmanager | snortmanagers")
17901901
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
17911902
@click.option('--all', is_flag=True, help='list all')
17921903
@click.option('--running', is_flag=True, help='list running only (default)')
@@ -1855,6 +1966,8 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
18551966
list_host_managers(ip=ip, emulation=name, ip_first_octet=id)
18561967
elif entity == "clientmanager":
18571968
list_client_manager(ip=ip, emulation=name, ip_first_octet=id)
1969+
elif entity == "snortmanagers":
1970+
list_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
18581971
else:
18591972
container = get_running_container(name=entity)
18601973
if container is not None:
@@ -1925,6 +2038,43 @@ def list_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
19252038
click.secho('+' + '-' * 50 + '+', fg='white')
19262039

19272040

2041+
def list_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
2042+
"""
2043+
Utility function for listing snort ids managers
2044+
2045+
:param ip: the ip of the node to start snort ids managers
2046+
:param emulation: the emulation of the execution
2047+
:param ip_first_octet: the ID of the execution
2048+
2049+
:return: None
2050+
"""
2051+
import csle_common.constants.constants as constants
2052+
from csle_common.metastore.metastore_facade import MetastoreFacade
2053+
config = MetastoreFacade.get_config(id=1)
2054+
for node in config.cluster_config.cluster_nodes:
2055+
if node.ip == ip or ip == "":
2056+
snort_manager_info = ClusterController.get_snort_ids_managers_info(
2057+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2058+
ip_first_octet=ip_first_octet)
2059+
2060+
for i in range(len(snort_manager_info.ips)):
2061+
status = "Running" if snort_manager_info.snortIdsManagersRunning[i] else "Stopped"
2062+
status_color = 'green' if snort_manager_info.snortIdsManagersRunning[i] else 'red'
2063+
2064+
click.secho('+' + '-' * 60 + '+', fg='white')
2065+
click.secho(f'|{"Snort ids manager IP":^40}', nl=False, fg='white')
2066+
click.secho('|', nl=False, fg='white')
2067+
click.secho(f'{snort_manager_info.ips[i]:<19}', nl=False, fg=status_color)
2068+
click.secho('|', fg='white')
2069+
2070+
click.secho('+' + '-' * 60 + '+', fg='white')
2071+
click.secho(f'|{"Status":^40}', nl=False, fg='white')
2072+
click.secho('|', nl=False, fg='white')
2073+
click.secho(f'{status:<19}', nl=False, fg=status_color)
2074+
click.secho('|', fg='white')
2075+
click.secho('+' + '-' * 60 + '+', fg='white')
2076+
2077+
19282078
def list_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
19292079
"""
19302080
Utility function for listing client managers
@@ -1940,49 +2090,59 @@ def list_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
19402090
config = MetastoreFacade.get_config(id=1)
19412091
for node in config.cluster_config.cluster_nodes:
19422092
if node.ip == ip or ip == "":
1943-
host_manager_info = ClusterController.get_client_managers_info(
2093+
client_manager_info = ClusterController.get_client_managers_info(
19442094
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
19452095
ip_first_octet=ip_first_octet)
2096+
for i in range(len(client_manager_info.ips)):
2097+
status_color = 'green' if client_manager_info.clientManagersStatuses[i].num_clients > 0 else 'red'
2098+
manager_status = 'Running' if client_manager_info.clientManagersStatuses[i].num_clients > 0 \
2099+
else 'Stopped'
19462100

1947-
click.secho('+' + '-' * 60 + '+', fg='white')
1948-
click.secho(f'|{"Host IP":^40}', nl=False, fg='white')
1949-
for i in range(len(host_manager_info.ips)):
2101+
click.secho('+' + '-' * 60 + '+', fg='white')
2102+
click.secho(f'|{"Host IP":^40}', nl=False, fg='white')
2103+
click.secho('|', nl=False, fg='white')
2104+
click.secho(f'{client_manager_info.ips[i]:<19}', nl=False, fg=status_color)
2105+
click.secho('|', fg='white')
2106+
click.secho('+' + '-' * 60 + '+', fg='white')
2107+
2108+
click.secho(f'|{"Status":^40}', nl=False, fg='white')
19502109
click.secho('|', nl=False, fg='white')
1951-
click.secho(f'{host_manager_info.ips[i]:<19}', nl=False, fg="green")
2110+
click.secho(f'{manager_status:<19}', nl=False, fg=status_color)
19522111
click.secho('|', fg='white')
19532112
click.secho('+' + '-' * 60 + '+', fg='white')
19542113

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

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

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

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

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

0 commit comments

Comments
 (0)