Skip to content

Commit 069d593

Browse files
authored
Merge pull request #410 from Limmen/cli_command
Filebeats cli command for start/stop added.
2 parents 4d80989 + f7abc4f commit 069d593

File tree

1 file changed

+298
-4
lines changed
  • simulation-system/libs/csle-cli/src/csle_cli

1 file changed

+298
-4
lines changed

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

+298-4
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,8 @@ def stop_shell_complete(ctx, param, incomplete) -> List[str]:
686686
"emulation-name | statsmanager | emulation_executions | pgadmin | all | nginx | postgresql "
687687
"| docker | clustermanager | hostmanagers | hostmanager | clientmanager | snortmanagers "
688688
"| snortmanager | elkmanager | trafficmanagers | trafficmanager | kafkamanager "
689-
"| ossecmanagers | ossecmanager | ryumanager")
689+
"| ossecmanagers | ossecmanager | ryumanager | filebeats | filebeat | metricbeat "
690+
"| metricbeats")
690691
def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str = "") -> None:
691692
"""
692693
Stops an entity
@@ -754,6 +755,14 @@ def stop(entity: str, name: str, id: int = -1, ip: str = "", container_ip: str =
754755
stop_ossec_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
755756
elif entity == "ryumanager":
756757
stop_ryu_manager(ip=ip, emulation=name, ip_first_octet=id)
758+
elif entity == "filebeats":
759+
stop_filebeats(ip=ip, emulation=name, ip_first_octet=id)
760+
elif entity == "filebeat":
761+
stop_filebeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
762+
elif entity == "metricbeats":
763+
stop_metricbeats(ip=ip, emulation=name, ip_first_octet=id)
764+
elif entity == "metricbeat":
765+
stop_metricbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
757766
else:
758767
container_stopped = False
759768
for node in config.cluster_config.cluster_nodes:
@@ -1224,6 +1233,110 @@ def stop_traffic_manager(ip: str, container_ip: str, emulation: str, ip_first_oc
12241233
bold=False)
12251234

12261235

1236+
def stop_filebeats(ip: str, emulation: str, ip_first_octet: int) -> None:
1237+
"""
1238+
Utility function for stopping the filebeats
1239+
1240+
:param ip: the ip of the node to stop the filebeats
1241+
:param emulation: the emulation of the execution
1242+
:param ip_first_octet: the ID of the execution
1243+
:return: None
1244+
"""
1245+
import csle_common.constants.constants as constants
1246+
from csle_common.metastore.metastore_facade import MetastoreFacade
1247+
config = MetastoreFacade.get_config(id=1)
1248+
for node in config.cluster_config.cluster_nodes:
1249+
if node.ip == ip or ip == "":
1250+
stopped = ClusterController.stop_filebeats(
1251+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1252+
ip_first_octet=ip_first_octet)
1253+
if stopped.outcome:
1254+
click.secho(f"Stopping filebeats on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1255+
else:
1256+
click.secho(f"Filebeats are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1257+
bold=False)
1258+
1259+
1260+
def stop_filebeat(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
1261+
"""
1262+
Utility function for stopping the filebeat
1263+
1264+
:param ip: the ip of the node to stop the filebeat
1265+
:param container_ip: the ip of the host that traffic is running on
1266+
:param emulation: the emulation of the execution
1267+
:param ip_first_octet: the ID of the execution
1268+
:return: None
1269+
"""
1270+
import csle_common.constants.constants as constants
1271+
from csle_common.metastore.metastore_facade import MetastoreFacade
1272+
config = MetastoreFacade.get_config(id=1)
1273+
for node in config.cluster_config.cluster_nodes:
1274+
if node.ip == ip or ip == "":
1275+
stopped = ClusterController.stop_filebeat(
1276+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1277+
ip_first_octet=ip_first_octet, container_ip=container_ip)
1278+
if stopped.outcome:
1279+
click.secho(
1280+
f"Stopping filebeat with ip {container_ip} on port:"
1281+
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1282+
else:
1283+
click.secho(f"Filebeat with ip {container_ip} is not "
1284+
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1285+
bold=False)
1286+
1287+
1288+
def stop_metricbeats(ip: str, emulation: str, ip_first_octet: int) -> None:
1289+
"""
1290+
Utility function for stopping the metricbeats
1291+
1292+
:param ip: the ip of the node to stop the metricbeats
1293+
:param emulation: the emulation of the execution
1294+
:param ip_first_octet: the ID of the execution
1295+
:return: None
1296+
"""
1297+
import csle_common.constants.constants as constants
1298+
from csle_common.metastore.metastore_facade import MetastoreFacade
1299+
config = MetastoreFacade.get_config(id=1)
1300+
for node in config.cluster_config.cluster_nodes:
1301+
if node.ip == ip or ip == "":
1302+
stopped = ClusterController.stop_metricbeats(
1303+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1304+
ip_first_octet=ip_first_octet)
1305+
if stopped.outcome:
1306+
click.secho(f"Stopping metricbeats on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1307+
else:
1308+
click.secho(f"Metricbeats are not stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1309+
bold=False)
1310+
1311+
1312+
def stop_metricbeat(ip: str, container_ip: str, emulation: str, ip_first_octet: int) -> None:
1313+
"""
1314+
Utility function for stopping the metricbeat
1315+
1316+
:param ip: the ip of the node to stop the metricbeat
1317+
:param container_ip: the ip of the host that traffic is running on
1318+
:param emulation: the emulation of the execution
1319+
:param ip_first_octet: the ID of the execution
1320+
:return: None
1321+
"""
1322+
import csle_common.constants.constants as constants
1323+
from csle_common.metastore.metastore_facade import MetastoreFacade
1324+
config = MetastoreFacade.get_config(id=1)
1325+
for node in config.cluster_config.cluster_nodes:
1326+
if node.ip == ip or ip == "":
1327+
stopped = ClusterController.stop_metricbeat(
1328+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1329+
ip_first_octet=ip_first_octet, container_ip=container_ip)
1330+
if stopped.outcome:
1331+
click.secho(
1332+
f"Stopping metricbeat with ip {container_ip} on port:"
1333+
f"{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1334+
else:
1335+
click.secho(f"Metricbeat with ip {container_ip} is not "
1336+
f"stopped:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1337+
bold=False)
1338+
1339+
12271340
@click.argument('max_workers', default=10, type=int)
12281341
@click.argument('log_file', default="docker_statsmanager.log", type=str)
12291342
@click.argument('log_dir', default="/var/log/csle", type=str)
@@ -1405,6 +1518,7 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
14051518

14061519
@click.option('--ip', default="", type=str)
14071520
@click.option('--container_ip', default="", type=str)
1521+
@click.option('--initial_start', default=False, type=bool)
14081522
@click.option('--id', default=None, type=int)
14091523
@click.option('--no_clients', is_flag=True, help='skip starting the client population')
14101524
@click.option('--no_traffic', is_flag=True, help='skip starting the traffic generators')
@@ -1417,9 +1531,9 @@ def start_shell_complete(ctx, param, incomplete) -> List[str]:
14171531
"| system_id_job | nginx | postgresql | docker | clustermanager | hostmanagers "
14181532
"| hostmanager | clientmanager | snortmanagers | snortmanager | elkmanager "
14191533
"| trafficmanagers | trafficmanager | kafkamanager | ossecmanagers | ossecmanager "
1420-
"| ryumanager")
1534+
"| ryumanager | filebeats | filebeat | metricbeats | metricbeat")
14211535
def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, no_network: bool, ip: str,
1422-
container_ip: str, no_beats: bool) -> None:
1536+
container_ip: str, no_beats: bool, initial_start: bool) -> None:
14231537
"""
14241538
Starts an entity, e.g., a container or the management system
14251539
@@ -1496,6 +1610,16 @@ def start(entity: str, no_traffic: bool, name: str, id: int, no_clients: bool, n
14961610
start_ossec_ids_manager(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id)
14971611
elif entity == "ryumanager":
14981612
start_ryu_manager(ip=ip, emulation=name, ip_first_octet=id)
1613+
elif entity == "filebeats":
1614+
start_filebeats(ip=ip, emulation=name, ip_first_octet=id, initial_start=initial_start)
1615+
elif entity == "filebeat":
1616+
start_filebeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id,
1617+
initial_start=initial_start)
1618+
elif entity == "metricbeats":
1619+
start_metricbeats(ip=ip, emulation=name, ip_first_octet=id, initial_start=initial_start)
1620+
elif entity == "metricbeat":
1621+
start_metricbeat(ip=ip, container_ip=container_ip, emulation=name, ip_first_octet=id,
1622+
initial_start=initial_start)
14991623
else:
15001624
container_started = False
15011625
for node in config.cluster_config.cluster_nodes:
@@ -1718,6 +1842,108 @@ def start_ryu_manager(ip: str, emulation: str, ip_first_octet: int):
17181842
bold=False)
17191843

17201844

1845+
def start_filebeats(ip: str, emulation: str, ip_first_octet: int, initial_start: bool):
1846+
"""
1847+
Utility function for starting filebeats
1848+
1849+
:param ip: the ip of the node to start filebeats
1850+
:param emulation: the emulation of the execution
1851+
:param ip_first_octet: the ID of the execution
1852+
:return: None
1853+
"""
1854+
import csle_common.constants.constants as constants
1855+
from csle_common.metastore.metastore_facade import MetastoreFacade
1856+
config = MetastoreFacade.get_config(id=1)
1857+
for node in config.cluster_config.cluster_nodes:
1858+
if node.ip == ip or ip == "":
1859+
operation_outcome = ClusterController.start_filebeats(
1860+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1861+
ip_first_octet=ip_first_octet, initial_start=initial_start)
1862+
if operation_outcome.outcome:
1863+
click.secho(f"Starting filebeats on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1864+
else:
1865+
click.secho(f"Filebeats are not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1866+
bold=False)
1867+
1868+
1869+
def start_filebeat(ip: str, container_ip: str, emulation: str, ip_first_octet: int, initial_start: bool):
1870+
"""
1871+
Utility function for starting filebeat
1872+
1873+
:param ip: the ip of the node to start filebeat
1874+
:param container_ip: the ip of the host to start
1875+
:param emulation: the emulation of the execution
1876+
:param ip_first_octet: the ID of the execution
1877+
:return: None
1878+
"""
1879+
import csle_common.constants.constants as constants
1880+
from csle_common.metastore.metastore_facade import MetastoreFacade
1881+
config = MetastoreFacade.get_config(id=1)
1882+
for node in config.cluster_config.cluster_nodes:
1883+
if node.ip == ip or ip == "":
1884+
operation_outcome = ClusterController.start_filebeat(
1885+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1886+
ip_first_octet=ip_first_octet, container_ip=container_ip, initial_start=initial_start)
1887+
if operation_outcome.outcome:
1888+
click.secho(f"Started filebeat with ip {container_ip} on "
1889+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1890+
else:
1891+
click.secho(f"Filebeat with ip {container_ip} is not "
1892+
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1893+
bold=False)
1894+
1895+
1896+
def start_metricbeats(ip: str, emulation: str, ip_first_octet: int, initial_start: bool):
1897+
"""
1898+
Utility function for starting metricbeats
1899+
1900+
:param ip: the ip of the node to start metricbeats
1901+
:param emulation: the emulation of the execution
1902+
:param ip_first_octet: the ID of the execution
1903+
:return: None
1904+
"""
1905+
import csle_common.constants.constants as constants
1906+
from csle_common.metastore.metastore_facade import MetastoreFacade
1907+
config = MetastoreFacade.get_config(id=1)
1908+
for node in config.cluster_config.cluster_nodes:
1909+
if node.ip == ip or ip == "":
1910+
operation_outcome = ClusterController.start_metricbeats(
1911+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1912+
ip_first_octet=ip_first_octet, initial_start=initial_start)
1913+
if operation_outcome.outcome:
1914+
click.secho(f"Starting metricbeats on port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1915+
else:
1916+
click.secho(f"Metricbeats are not started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1917+
bold=False)
1918+
1919+
1920+
def start_metricbeat(ip: str, container_ip: str, emulation: str, ip_first_octet: int, initial_start: bool):
1921+
"""
1922+
Utility function for starting metricbeat
1923+
1924+
:param ip: the ip of the node to start metricbeat
1925+
:param container_ip: the ip of the host to start
1926+
:param emulation: the emulation of the execution
1927+
:param ip_first_octet: the ID of the execution
1928+
:return: None
1929+
"""
1930+
import csle_common.constants.constants as constants
1931+
from csle_common.metastore.metastore_facade import MetastoreFacade
1932+
config = MetastoreFacade.get_config(id=1)
1933+
for node in config.cluster_config.cluster_nodes:
1934+
if node.ip == ip or ip == "":
1935+
operation_outcome = ClusterController.start_metricbeat(
1936+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
1937+
ip_first_octet=ip_first_octet, container_ip=container_ip, initial_start=initial_start)
1938+
if operation_outcome.outcome:
1939+
click.secho(f"Started metricbeat with ip {container_ip} on "
1940+
f"port:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}")
1941+
else:
1942+
click.secho(f"Metricbeat with ip {container_ip} is not "
1943+
f"started:{constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT}",
1944+
bold=False)
1945+
1946+
17211947
def start_host_manager(ip: str, container_ip: str, emulation: str, ip_first_octet: int):
17221948
"""
17231949
Utility function for starting host manager
@@ -2278,7 +2504,7 @@ def ls_shell_complete(ctx, param, incomplete) -> List[str]:
22782504
"| node_exporter | cadvisor | pgadmin | statsmanager | flask | "
22792505
"simulations | emulation_executions | cluster | nginx | postgresql | docker | hostmanagers | "
22802506
"clientmanager | snortmanagers | elkmanager | trafficmanagers | kafkamanager | "
2281-
"ossecmanagers | ryumanager")
2507+
"ossecmanagers | ryumanager | filebeats | metricbeats")
22822508
@click.argument('entity', default='all', type=str, shell_complete=ls_shell_complete)
22832509
@click.option('--all', is_flag=True, help='list all')
22842510
@click.option('--running', is_flag=True, help='list running only (default)')
@@ -2359,6 +2585,10 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
23592585
list_ossec_ids_managers(ip=ip, emulation=name, ip_first_octet=id)
23602586
elif entity == "ryumanager":
23612587
list_ryu_manager(ip=ip, emulation=name, ip_first_octet=id)
2588+
elif entity == "filebeats":
2589+
list_filebeats(ip=ip, emulation=name, ip_first_octet=id)
2590+
elif entity == "metricbeats":
2591+
list_metricbeats(ip=ip, emulation=name, ip_first_octet=id)
23622592
else:
23632593
container = get_running_container(name=entity)
23642594
if container is not None:
@@ -2393,6 +2623,70 @@ def ls(entity: str, all: bool, running: bool, stopped: bool, ip: str, name: str,
23932623
click.secho(f"entity: {entity} is not recognized", fg="red", bold=True)
23942624

23952625

2626+
def list_filebeats(ip: str, emulation: str, ip_first_octet: int) -> None:
2627+
"""
2628+
Utility function for listing filebeats
2629+
2630+
:param ip: the ip of the node to list filebeats
2631+
:param emulation: the emulation of the execution
2632+
:param ip_first_octet: the ID of the execution
2633+
2634+
:return: None
2635+
"""
2636+
import csle_common.constants.constants as constants
2637+
from csle_common.metastore.metastore_facade import MetastoreFacade
2638+
config = MetastoreFacade.get_config(id=1)
2639+
for node in config.cluster_config.cluster_nodes:
2640+
if node.ip == ip or ip == "":
2641+
filebeats_info = ClusterController.get_host_managers_info(
2642+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2643+
ip_first_octet=ip_first_octet)
2644+
click.secho('+' + '-' * 60 + '+', fg='white')
2645+
click.secho(f'|{"Host IP":^30}|{"Filebeats running Status":^29}|', fg='white')
2646+
click.secho('+' + '-' * 60 + '+', fg='white')
2647+
for i in range(len(filebeats_info.hostManagersStatuses)):
2648+
status = "Running" if filebeats_info.hostManagersStatuses[i].filebeat_running else "Stopped"
2649+
status_color = 'green' if filebeats_info.hostManagersStatuses[i].filebeat_running else 'red'
2650+
click.secho('|', nl=False, fg='white')
2651+
click.secho(f'{filebeats_info.ips[i]:<30}', nl=False, fg='white')
2652+
click.secho('|', nl=False, fg='white')
2653+
click.secho(f'{status:^29}', nl=False, fg=status_color)
2654+
click.secho('|', fg='white')
2655+
click.secho('+' + '-' * 60 + '+', fg='white')
2656+
2657+
2658+
def list_metricbeats(ip: str, emulation: str, ip_first_octet: int) -> None:
2659+
"""
2660+
Utility function for listing filebeats
2661+
2662+
:param ip: the ip of the node to list filebeats
2663+
:param emulation: the emulation of the execution
2664+
:param ip_first_octet: the ID of the execution
2665+
2666+
:return: None
2667+
"""
2668+
import csle_common.constants.constants as constants
2669+
from csle_common.metastore.metastore_facade import MetastoreFacade
2670+
config = MetastoreFacade.get_config(id=1)
2671+
for node in config.cluster_config.cluster_nodes:
2672+
if node.ip == ip or ip == "":
2673+
metricbeats_info = ClusterController.get_host_managers_info(
2674+
ip=ip, port=constants.GRPC_SERVERS.CLUSTER_MANAGER_PORT, emulation=emulation,
2675+
ip_first_octet=ip_first_octet)
2676+
click.secho('+' + '-' * 60 + '+', fg='white')
2677+
click.secho(f'|{"Host IP":^30}|{"Metricbeats running Status":^29}|', fg='white')
2678+
click.secho('+' + '-' * 60 + '+', fg='white')
2679+
for i in range(len(metricbeats_info.hostManagersStatuses)):
2680+
status = "Running" if metricbeats_info.hostManagersStatuses[i].metricbeat_running else "Stopped"
2681+
status_color = 'green' if metricbeats_info.hostManagersStatuses[i].metricbeat_running else 'red'
2682+
click.secho('|', nl=False, fg='white')
2683+
click.secho(f'{metricbeats_info.ips[i]:<30}', nl=False, fg='white')
2684+
click.secho('|', nl=False, fg='white')
2685+
click.secho(f'{status:^29}', nl=False, fg=status_color)
2686+
click.secho('|', fg='white')
2687+
click.secho('+' + '-' * 60 + '+', fg='white')
2688+
2689+
23962690
def list_ryu_manager(ip: str, emulation: str, ip_first_octet: int) -> None:
23972691
"""
23982692
Utility function for listing ryu manager

0 commit comments

Comments
 (0)