|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | +import os |
| 17 | +import sys |
| 18 | +import time |
| 19 | + |
| 20 | +from loguru import logger |
| 21 | + |
| 22 | +from nemo_curator.core.utils import get_free_port |
| 23 | +from nemo_curator.metrics.constants import ( |
| 24 | + DEFAULT_GRAFANA_WEB_PORT, |
| 25 | + DEFAULT_NEMO_CURATOR_METRICS_PATH, |
| 26 | + DEFAULT_PROMETHEUS_WEB_PORT, |
| 27 | +) |
| 28 | +from nemo_curator.metrics.utils import ( |
| 29 | + download_and_extract_prometheus, |
| 30 | + download_grafana, |
| 31 | + is_grafana_running, |
| 32 | + is_prometheus_running, |
| 33 | + launch_grafana, |
| 34 | + run_prometheus, |
| 35 | + write_grafana_configs, |
| 36 | +) |
| 37 | + |
| 38 | +# ---------------------------- |
| 39 | +# Helpers |
| 40 | +# ---------------------------- |
| 41 | + |
| 42 | + |
| 43 | +def start_prometheus_grafana( |
| 44 | + prometheus_web_port: int = DEFAULT_PROMETHEUS_WEB_PORT, |
| 45 | + grafana_web_port: int = DEFAULT_GRAFANA_WEB_PORT, |
| 46 | +) -> None: |
| 47 | + # Check if the prometheus or grafana is running. If yes we assume that they were setup using this script and skip the setup. |
| 48 | + if is_prometheus_running() or is_grafana_running(): |
| 49 | + logger.info("Prometheus or Grafana is already running. Skipping the setup.") |
| 50 | + return |
| 51 | + |
| 52 | + # Touch our director for nemo curator incase it doesn't exist |
| 53 | + os.makedirs(DEFAULT_NEMO_CURATOR_METRICS_PATH, exist_ok=True) |
| 54 | + |
| 55 | + prometheus_web_port = get_free_port(prometheus_web_port) |
| 56 | + grafana_web_port = get_free_port(grafana_web_port) |
| 57 | + |
| 58 | + prometheus_dir = download_and_extract_prometheus() |
| 59 | + |
| 60 | + # Run prometheus |
| 61 | + try: |
| 62 | + run_prometheus(prometheus_dir, prometheus_web_port) |
| 63 | + except Exception as error: |
| 64 | + error_msg = f"Failed to start Prometheus: {error}" |
| 65 | + logger.error(error_msg) |
| 66 | + raise |
| 67 | + |
| 68 | + # ----------------------------- |
| 69 | + # Grafana setup and launch |
| 70 | + # ----------------------------- |
| 71 | + try: |
| 72 | + grafana_dir = download_grafana() |
| 73 | + grafana_ini_path = write_grafana_configs(grafana_web_port, prometheus_web_port) |
| 74 | + launch_grafana(grafana_dir, grafana_ini_path) |
| 75 | + |
| 76 | + # Wait a bit to ensure Grafana starts |
| 77 | + time.sleep(2) |
| 78 | + except Exception as error: |
| 79 | + error_msg = f"Failed to setup or start Grafana: {error}" |
| 80 | + logger.error(error_msg) |
| 81 | + raise |
| 82 | + |
| 83 | + logger.info("If you are running using Xenna, please remember to export XENNA_RAY_METRICS_PORT=8080") |
| 84 | + logger.info("You can access the grafana dashboard at http://localhost:3000, username: admin, password: admin") |
| 85 | + logger.info("You can access the prometheus dashboard at http://localhost:9090") |
| 86 | + logger.info("Currently, we only provide a xenna dashboard,") |
| 87 | + logger.info("but you can add more dashboards by adding json files") |
| 88 | + logger.info(f"in {DEFAULT_NEMO_CURATOR_METRICS_PATH}/grafana/dashboards") |
| 89 | + logger.info("from /tmp/ray/session_latest/metrics/grafana/dashboards") |
| 90 | + logger.info("To kill prometheus and grafana, run: pkill -f 'prometheus .*' ; pkill -f 'grafana server'") |
| 91 | + logger.info("Prometheus stores tha persistant data inside data, if you want to delete the data, run: rm -rf data") |
| 92 | + return |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + # Add an argument parser to get the prometheus and grafana ports |
| 97 | + parser = argparse.ArgumentParser() |
| 98 | + parser.add_argument( |
| 99 | + "--prometheus_web_port", type=int, default=DEFAULT_PROMETHEUS_WEB_PORT, help="The port to run prometheus on" |
| 100 | + ) |
| 101 | + parser.add_argument( |
| 102 | + "--grafana_web_port", type=int, default=DEFAULT_GRAFANA_WEB_PORT, help="The port to run grafana on" |
| 103 | + ) |
| 104 | + parser.add_argument("--yes", action="store_true", help="Skip the confirmation prompt") |
| 105 | + args = parser.parse_args() |
| 106 | + |
| 107 | + if not args.yes: |
| 108 | + print("This will download and start prometheus and grafana on the following ports:") |
| 109 | + print(f"Prometheus: {args.prometheus_web_port}") |
| 110 | + print(f"Grafana: {args.grafana_web_port}") |
| 111 | + print("Are you sure you want to continue? (y/n)") |
| 112 | + if input() != "y": |
| 113 | + print("Exiting...") |
| 114 | + sys.exit(0) |
| 115 | + |
| 116 | + start_prometheus_grafana(args.prometheus_web_port, args.grafana_web_port) |
0 commit comments