Skip to content

Commit d1cf7d6

Browse files
authored
Merge pull request #406 from Limmen/cli_command
elkmanager CLI command added
2 parents 9eea140 + 154f2e3 commit d1cf7d6

File tree

1 file changed

+122
-11
lines changed
  • simulation-system/libs/csle-cli/src/csle_cli

1 file changed

+122
-11
lines changed

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

+122-11
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
685685
@click.command("stop", help="prometheus | node_exporter | cadvisor | grafana | flask | container-name | "
686686
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
687687
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager | snortmanagers "
688-
"| snortmanager")
688+
"| snortmanager | elkmanager")
689689
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
690690
"""
691691
Stops an entity
@@ -739,6 +739,8 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
739739
stop_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
740740
elif entity == "snortmanager":
741741
stop_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
742+
elif entity == "elkmanager":
743+
stop_elk_manager(ip=ip, emulation=name, ip_first_octet=id)
742744
else:
743745
container_stopped = False
744746
for node in config.cluster_config.cluster_nodes:
@@ -926,7 +928,7 @@ def stop_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
926928
if node.ip == ip or ip == "":
927929
stopped = ClusterController.stop_host_managers(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
928930
emulation=emulation, ip_first_octet=ip_first_octet)
929-
if stopped:
931+
if stopped.outcome:
930932
click.secho(f"Stopping host managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
931933
else:
932934
click.secho(f"Host managers are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
@@ -951,7 +953,7 @@ def stop_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet
951953
stopped = ClusterController.stop_host_manager(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
952954
emulation=emulation, ip_first_octet=ip_first_octet,
953955
container_ip=container_ip)
954-
if stopped:
956+
if stopped.outcome:
955957
click.secho(
956958
f"Stopping host with ip {container_ip} on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
957959
else:
@@ -976,7 +978,7 @@ def stop_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
976978
if node.ip == ip or ip == "":
977979
stopped = ClusterController.stop_client_manager(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
978980
emulation=emulation, ip_first_octet=ip_first_octet)
979-
if stopped:
981+
if stopped.outcome:
980982
click.secho(f"Stopping client manager on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
981983
else:
982984
click.secho(f"Client manager is not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
@@ -999,7 +1001,7 @@ def stop_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> Non
9991001
if node.ip == ip or ip == "":
10001002
stopped = ClusterController.stop_snort_ids_managers(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
10011003
emulation=emulation, ip_first_octet=ip_first_octet)
1002-
if stopped:
1004+
if stopped.outcome:
10031005
click.secho(f"Stopping snort ids managers on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
10041006
else:
10051007
click.secho(f"Snort ids managers are not stopped:"
@@ -1024,7 +1026,7 @@ def stop_snort_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_
10241026
stopped = ClusterController.stop_snort_ids_manager(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
10251027
emulation=emulation, ip_first_octet=ip_first_octet,
10261028
container_ip=container_ip)
1027-
if stopped:
1029+
if stopped.outcome:
10281030
click.secho(
10291031
f"Stopping snort ids on the host with ip {container_ip} on "
10301032
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
@@ -1034,6 +1036,29 @@ def stop_snort_ids_manager(ip: str, container_ip: str, emulation: str, ip_first_
10341036
bold=False)
10351037

10361038

1039+
def stop_elk_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
1040+
"""
1041+
Utility function for stopping the elk manage
1042+
1043+
:param ip: the ip of the node to stop the elk manger
1044+
:param emulation: the emulation of the execution
1045+
:param ip_first_octet: the ID of the execution
1046+
:return: None
1047+
"""
1048+
import csle_common.constants.constants as constants
1049+
from csle_common.metastore.metastore_facade import MetastoreFacade
1050+
config = MetastoreFacade.get_config(id=1)
1051+
for node in config.cluster_config.cluster_nodes:
1052+
if node.ip == ip or ip == "":
1053+
stopped = ClusterController.stop_elk_manager(ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT,
1054+
emulation=emulation, ip_first_octet=ip_first_octet)
1055+
if stopped.outcome:
1056+
click.secho(f"Stopping elk manager on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1057+
else:
1058+
click.secho(f"Elk manager is not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1059+
bold=False)
1060+
1061+
10371062
@click.argument('max_workers', default=10, type=int)
10381063
@click.argument('log_file', default="docker_statsmanager.log", type=str)
10391064
@click.argument('log_dir', default="/var/log/csle", type=str)
@@ -1225,7 +1250,7 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
12251250
@click.command("start", help="prometheus | node_exporter | grafana | cadvisor | flask | pgadmin | "
12261251
"container-name | emulation-name | all | statsmanager | training_job "
12271252
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
1228-
"| hostmanager | clientmanager | snortmanagers | snortmanager")
1253+
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager")
12291254
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
12301255
container_ip: str, no_beats: bool) -> None:
12311256
"""
@@ -1290,6 +1315,8 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
12901315
start_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
12911316
elif entity == "snortmanager":
12921317
start_snort_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
1318+
elif entity == "elkmanager":
1319+
start_elk_manager(ip=ip, emulation=name, ip_first_octet=id)
12931320
else:
12941321
container_started = False
12951322
for node in config.cluster_config.cluster_nodes:
@@ -1610,6 +1637,30 @@ def start_snort_ids_manager(ip: str, container_ip: str, emulation: str, ip_first
16101637
bold=False)
16111638

16121639

1640+
def start_elk_manager(ip: str, emulation: str, ip_first_octet: int):
1641+
"""
1642+
Utility function for starting elk manager
1643+
1644+
:param ip: the ip of the node to start elk manager
1645+
:param emulation: the emulation of the execution
1646+
:param ip_first_octet: the ID of the execution
1647+
:return: None
1648+
"""
1649+
import csle_common.constants.constants as constants
1650+
from csle_common.metastore.metastore_facade import MetastoreFacade
1651+
config = MetastoreFacade.get_config(id=1)
1652+
for node in config.cluster_config.cluster_nodes:
1653+
if node.ip == ip or ip == "":
1654+
operation_outcome = ClusterController.start_elk_manager(
1655+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1656+
ip_first_octet=ip_first_octet)
1657+
if operation_outcome.outcome:
1658+
click.secho(f"Starting elk manager on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1659+
else:
1660+
click.secho(f"Elk manager is not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1661+
bold=False)
1662+
1663+
16131664
def run_image(image: str, name: str, create_network: bool = True, version: str = "0.0.1") -> bool:
16141665
"""
16151666
Runs a container with a given image
@@ -1897,7 +1948,7 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
18971948
@click.command("ls", help="containers | networks | images | emulations | all | environments | prometheus "
18981949
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
18991950
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
1900-
"clientmanager | snortmanagers")
1951+
"clientmanager | snortmanagers | elkmanager")
19011952
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
19021953
@click.option('--all', is_flag=True, help='list all')
19031954
@click.option('--running', is_flag=True, help='list running only (default)')
@@ -1968,6 +2019,8 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
19682019
list_client_manager(ip=ip, emulation=name, ip_first_octet=id)
19692020
elif entity == "snortmanagers":
19702021
list_snort_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
2022+
elif entity == "elkmanager":
2023+
list_elk_manager(ip=ip, emulation=name, ip_first_octet=id)
19712024
else:
19722025
container = get_running_container(name=entity)
19732026
if container is not None:
@@ -2006,7 +2059,7 @@ def list_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
20062059
"""
20072060
Utility function for listing host managers
20082061
2009-
:param ip: the ip of the node to start host manager
2062+
:param ip: the ip of the node to list host manager
20102063
:param emulation: the emulation of the execution
20112064
:param ip_first_octet: the ID of the execution
20122065
@@ -2038,11 +2091,69 @@ def list_host_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
20382091
click.secho('+' + '-' * 50 + '+', fg='white')
20392092

20402093

2094+
def list_elk_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
2095+
"""
2096+
Utility function for listing elk manager
2097+
2098+
:param ip: the ip of the node to list elk manager
2099+
:param emulation: the emulation of the execution
2100+
:param ip_first_octet: the ID of the execution
2101+
2102+
:return: None
2103+
"""
2104+
import csle_common.constants.constants as constants
2105+
from csle_common.metastore.metastore_facade import MetastoreFacade
2106+
config = MetastoreFacade.get_config(id=1)
2107+
for node in config.cluster_config.cluster_nodes:
2108+
if node.ip == ip or ip == "":
2109+
elk_manager_info = ClusterController.get_elk_managers_info(
2110+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2111+
ip_first_octet=ip_first_octet)
2112+
for i in range(len(elk_manager_info.ips)):
2113+
status_color = 'green' if elk_manager_info.elkManagersRunning[i] else 'red'
2114+
manager_status = 'Running' if elk_manager_info.elkManagersRunning[i] else 'Stopped'
2115+
click.secho('+' + '-' * 60 + '+', fg='white')
2116+
click.secho(f'|{"Elk manager IP":^40}', nl=False, fg='white')
2117+
click.secho('|', nl=False, fg='white')
2118+
click.secho(f'{elk_manager_info.ips[i]:<19}', nl=False, fg=status_color)
2119+
click.secho('|', fg='white')
2120+
click.secho('+' + '-' * 60 + '+', fg='white')
2121+
click.secho(f'|{"Elk manager status":^40}', nl=False, fg='white')
2122+
click.secho('|', nl=False, fg='white')
2123+
click.secho(f'{manager_status:<19}', nl=False, fg=status_color)
2124+
click.secho('|', fg='white')
2125+
click.secho('+' + '-' * 60 + '+', fg='white')
2126+
if manager_status == "Running":
2127+
click.secho(f'|{"Elk manager elasticRunning":^40}', nl=False, fg='white')
2128+
click.secho('|', nl=False, fg='white')
2129+
elasticRunning = 'True' if elk_manager_info.elkManagersStatuses[0].elasticRunning else 'False'
2130+
click.secho(f'{elasticRunning:<19}',
2131+
nl=False, fg=status_color)
2132+
click.secho('|', fg='white')
2133+
click.secho('+' + '-' * 60 + '+', fg='white')
2134+
2135+
click.secho(f'|{"Elk manager kibanaRunning":^40}', nl=False, fg='white')
2136+
click.secho('|', nl=False, fg='white')
2137+
kibanaRunning = 'True' if elk_manager_info.elkManagersStatuses[0].kibanaRunning else 'False'
2138+
click.secho(f'{kibanaRunning:<19}',
2139+
nl=False, fg=status_color)
2140+
click.secho('|', fg='white')
2141+
click.secho('+' + '-' * 60 + '+', fg='white')
2142+
2143+
click.secho(f'|{"Elk manager logstashRunning":^40}', nl=False, fg='white')
2144+
click.secho('|', nl=False, fg='white')
2145+
logstashRunning = 'True' if elk_manager_info.elkManagersStatuses[0].logstashRunning else 'False'
2146+
click.secho(f'{logstashRunning:<19}',
2147+
nl=False, fg=status_color)
2148+
click.secho('|', fg='white')
2149+
click.secho('+' + '-' * 60 + '+', fg='white')
2150+
2151+
20412152
def list_snort_ids_managers(ip: str, emulation: str, ip_first_octet: int) -> None:
20422153
"""
20432154
Utility function for listing snort ids managers
20442155
2045-
:param ip: the ip of the node to start snort ids managers
2156+
:param ip: the ip of the node to list snort ids managers
20462157
:param emulation: the emulation of the execution
20472158
:param ip_first_octet: the ID of the execution
20482159
@@ -2079,7 +2190,7 @@ def list_client_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
20792190
"""
20802191
Utility function for listing client managers
20812192
2082-
:param ip: the ip of the node to start host manager
2193+
:param ip: the ip of the node to list client manager
20832194
:param emulation: the emulation of the execution
20842195
:param ip_first_octet: the ID of the execution
20852196

0 commit comments

Comments
 (0)