From 34632ba857b6916ed31ce84606da2f4ec9d2835e Mon Sep 17 00:00:00 2001 From: Shubham Varshney Date: Wed, 26 Jun 2024 09:40:46 +0530 Subject: [PATCH] [PLAT-14495] Set up the node_exporter for ynp Summary: Configure node_exporter module for ynp Test Plan: Manually tried provisioing node via YNP. Verified node_exporter is running post the script execution. Reviewers: anijhawan, nbhatia Reviewed By: anijhawan Subscribers: yugaware Differential Revision: https://phorge.dev.yugabyte.com/D36115 --- .../resources/ynp/configs/config.j2 | 4 ++ .../node_agent/templates/node_agent_run.j2 | 2 +- .../provision/node_exporter/__init__.py | 0 .../provision/node_exporter/node_exporter.py | 7 +++ .../templates/node_exporter_precheck.j2 | 10 +++ .../templates/node_exporter_run.j2 | 62 +++++++++++++++++++ 6 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 managed/node-agent/resources/ynp/modules/provision/node_exporter/__init__.py create mode 100644 managed/node-agent/resources/ynp/modules/provision/node_exporter/node_exporter.py create mode 100644 managed/node-agent/resources/ynp/modules/provision/node_exporter/templates/node_exporter_precheck.j2 create mode 100644 managed/node-agent/resources/ynp/modules/provision/node_exporter/templates/node_exporter_run.j2 diff --git a/managed/node-agent/resources/ynp/configs/config.j2 b/managed/node-agent/resources/ynp/configs/config.j2 index 20976bbf13e9..8d43d22a3dea 100644 --- a/managed/node-agent/resources/ynp/configs/config.j2 +++ b/managed/node-agent/resources/ynp/configs/config.j2 @@ -60,3 +60,7 @@ locks = unlimited [InstallNodeAgent] {{ render_section(yba) -}} yb_user = {{ ynp.yb_user }} + +[ConfigureNodeExporter] +prometheus_user = prometheus +yb_home_dir = {{ ynp.yb_home_dir }} diff --git a/managed/node-agent/resources/ynp/modules/provision/node_agent/templates/node_agent_run.j2 b/managed/node-agent/resources/ynp/modules/provision/node_agent/templates/node_agent_run.j2 index caa617d13002..afd1e3bc0af7 100644 --- a/managed/node-agent/resources/ynp/modules/provision/node_agent/templates/node_agent_run.j2 +++ b/managed/node-agent/resources/ynp/modules/provision/node_agent/templates/node_agent_run.j2 @@ -7,7 +7,7 @@ airgap_flag="" {% endif %} installer_dir="{{ ynp_dir }}/../../bin" -sudo su - {{ yb_user }} -c "\"$installer_dir/node-agent-installer.sh\" -c install -u {{ url }} -t {{ api_key }} --provider_id {{ provider_id }} --instance_type {{ instance_type_name }} --zone_name {{ provider_region_zone_name }} --node_name {{ node_name }} --node_ip {{ node_ip }} --silent --skip_verify_cert $airgap_flag" +su - {{ yb_user }} -c "\"$installer_dir/node-agent-installer.sh\" -c install -u {{ url }} -t {{ api_key }} --provider_id {{ provider_id }} --instance_type {{ instance_type_name }} --zone_name {{ provider_region_zone_name }} --node_name {{ node_name }} --node_ip {{ node_ip }} --silent --skip_verify_cert $airgap_flag" # install node_agent service "$installer_dir/node-agent-installer.sh" -c install_service --user yugabyte diff --git a/managed/node-agent/resources/ynp/modules/provision/node_exporter/__init__.py b/managed/node-agent/resources/ynp/modules/provision/node_exporter/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/managed/node-agent/resources/ynp/modules/provision/node_exporter/node_exporter.py b/managed/node-agent/resources/ynp/modules/provision/node_exporter/node_exporter.py new file mode 100644 index 000000000000..741620db1ce8 --- /dev/null +++ b/managed/node-agent/resources/ynp/modules/provision/node_exporter/node_exporter.py @@ -0,0 +1,7 @@ +from ...base_module import BaseYnpModule + + +class ConfigureNodeExporter(BaseYnpModule): + + run_template = "node_exporter_run.j2" + precheck_template = "node_exporter_precheck.j2" diff --git a/managed/node-agent/resources/ynp/modules/provision/node_exporter/templates/node_exporter_precheck.j2 b/managed/node-agent/resources/ynp/modules/provision/node_exporter/templates/node_exporter_precheck.j2 new file mode 100644 index 000000000000..fedfe0c75130 --- /dev/null +++ b/managed/node-agent/resources/ynp/modules/provision/node_exporter/templates/node_exporter_precheck.j2 @@ -0,0 +1,10 @@ +service_name="node_exporter.service" + +# Get the status of node_exporter Service +status=$(systemctl show -p ActiveState "$service_name" | grep ActiveState | awk -F= '{print $2}') +if [ "$status" = "active" ]; then + echo "\"$service_name\" is active" +else + echo "\"$service_name\" is not active" + exit 1 +fi diff --git a/managed/node-agent/resources/ynp/modules/provision/node_exporter/templates/node_exporter_run.j2 b/managed/node-agent/resources/ynp/modules/provision/node_exporter/templates/node_exporter_run.j2 new file mode 100644 index 000000000000..104f7a516d2d --- /dev/null +++ b/managed/node-agent/resources/ynp/modules/provision/node_exporter/templates/node_exporter_run.j2 @@ -0,0 +1,62 @@ +thirdparty_dir="{{ ynp_dir }}/../../thirdparty" + +# Setup the directories & permissions for prometheus user +mkdir -p /opt/prometheus +mkdir -p /etc/prometheus +mkdir -p /var/log/prometheus +mkdir -p /var/run/prometheus +mkdir -p /tmp/yugabyte/metrics + +if [ -f "$thirdparty_dir/node_exporter-1.7.0.linux-amd64.tar.gz" ]; then + mv "$thirdparty_dir/node_exporter-1.7.0.linux-amd64.tar.gz" /opt/prometheus +fi + +# Check if the user "prometheus" already exists +if ! id -u prometheus >/dev/null 2>&1; then + adduser --shell /bin/bash prometheus +fi + +# Set ownership and permissions +chown -R prometheus:prometheus /opt/prometheus +chown -R prometheus:prometheus /etc/prometheus +chown -R prometheus:prometheus /var/log/prometheus +chown -R prometheus:prometheus /var/run/prometheus +chown -R yugabyte:yugabyte /tmp/yugabyte/metrics +chmod -R 755 /tmp/yugabyte/metrics + +if [ -f /opt/prometheus/node_exporter-1.7.0.linux-amd64.tar.gz ]; then + chmod +r /opt/prometheus/node_exporter-1.7.0.linux-amd64.tar.gz + + # Extract the node_exporter tarball + su - {{ prometheus_user }} -c "cd /opt/prometheus && tar zxf node_exporter-1.7.0.linux-amd64.tar.gz" +fi + +# Configure Systemd Unit +file_path="/etc/systemd/system/node_exporter.service" + +# Create the file and write the content +cat < $file_path +[Unit] +Description=node_exporter - Exporter for machine metrics. +Documentation=https://github.com/William-Yeh/ansible-prometheus +After=network.target + +[Install] +WantedBy=multi-user.target + +[Service] +Type=simple +Restart=on-failure + +User=prometheus +Group=prometheus + +ExecStart=/opt/prometheus/node_exporter-1.7.0.linux-amd64/node_exporter --web.listen-address=:9300 --collector.textfile.directory={{ yb_home_dir }}/metrics +EOL + +# Enable and start the node_exporter service +systemctl daemon-reload +systemctl enable node_exporter +systemctl start node_exporter + +echo "Node exporter setup is complete."