From 97b858b9eb4e2725ee5254a0f84d70b3d3a4d9fa Mon Sep 17 00:00:00 2001 From: Valentin Date: Fri, 7 Jun 2024 13:29:22 +0200 Subject: [PATCH] ajout du node exporter et config auto sur le host --- README.md | 5 ++-- bin/setup_backup.sh | 16 ++++--------- bin/setup_node_exporter.sh | 47 ++++++++++++++++++++++++++++++++++++++ bin/setup_ssh_keys.sh | 11 +++++++++ bin/variables.sh | 6 ++++- install.sh | 8 +++++++ 6 files changed, 78 insertions(+), 15 deletions(-) create mode 100644 bin/setup_node_exporter.sh create mode 100644 bin/setup_ssh_keys.sh diff --git a/README.md b/README.md index e0f58fb..ce7ae51 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,9 @@ Tested and optimised for [OVH VPS Starter](https://www.ovhcloud.com/fr/vps/cheap 9. Prompt for the url 10. Install and run Directus 11. Setup backup -12. Install and run the front-end -13. Setup a webhook +12. Setup node exporter +13. Install and run the front-end +14. Setup a webhook ## Post-install diff --git a/bin/setup_backup.sh b/bin/setup_backup.sh index af774d7..0a3560f 100644 --- a/bin/setup_backup.sh +++ b/bin/setup_backup.sh @@ -1,23 +1,15 @@ #!/bin/bash -echo -e "${PURPLE}${BOLD}Enter the receiving server ip : ${RESET}" -read receiving_ip -echo -e "${PURPLE}${BOLD}Enter the receiving server user : ${RESET}" -read receiving_user -echo -e "${PURPLE}${BOLD}Enter the receiving server port : ${RESET}" -read receiving_port - -ssh-keygen -ssh-copy-id -p ${receiving_port} ${receiving_user}@${receiving_ip} +./setup_ssh_keys.sh install_pkg cron cp ./assets/backup.sh ~/backup.sh chmod +x ~/backup.sh -sed -i "s/^receiving_ip=.*/receiving_ip=$receiving_ip/" ~/backup.sh -sed -i "s/^receiving_user=.*/receiving_user=$receiving_user/" ~/backup.sh -sed -i "s/^receiving_port=.*/receiving_port=$receiving_port/" ~/backup.sh +sed -i "s/^receiving_ip=.*/receiving_ip=$RECEIVING_IP/" ~/backup.sh +sed -i "s/^receiving_user=.*/receiving_user=$RECEIVING_USER/" ~/backup.sh +sed -i "s/^receiving_port=.*/receiving_port=$RECEIVING_PORT/" ~/backup.sh cronjob="0 2 * * * ~/backup.sh" diff --git a/bin/setup_node_exporter.sh b/bin/setup_node_exporter.sh new file mode 100644 index 0000000..80777c6 --- /dev/null +++ b/bin/setup_node_exporter.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +if [[ -n "$RECEIVING_IP" && -n "$RECEIVING_USER" && -n "$RECEIVING_PORT" ]]; then + if ssh -p $RECEIVING_PORT -q -o BatchMode=yes -o ConnectTimeout=5 $RECEIVING_USER@$RECEIVING_IP exit; then + install_pkg ufw + node_exporter_port="9100" + ufw allow $node_exporter_port + + node_exporter_version="1.8.1" + wget https://github.com/prometheus/node_exporter/releases/download/v${node_exporter_version}/node_exporter-${node_exporter_version}.linux-amd64.tar.gz + tar tar xvfz node_exporter-*.*-amd64.tar.gz + rm node_exporter-*.*-amd64.tar.gz + + install_pkg tmux + tmux new-session -d -s node-exporter + tmux send-keys -t node-exporter "cd node_exporter-*.*-amd64 && ./node_exporter" C-m + + get_ip + + ssh -p $RECEIVING_PORT $RECEIVING_USER@$RECEIVING_IP << EOF + if command -v yq >/dev/null 2>&1; then + cd prometheus/prometheus + + if [ -z "$(yq '.scrape_configs[] | select(.job_name == "node-exporter") | .static_configs[] | select(.targets[] == "${ip}:${node_exporter_port}")' prometheus.yml)" ]; then + yq '(.scrape_configs[] | select(.job_name == "node-exporter") | .static_configs) += [{"targets": ["${ip}:${node_exporter_port}"]}]' prometheus.yml -y > prometheus.temp.yml && mv prometheus.temp.yml prometheus.yml + else + echo "the node exporter config is already operational" + fi + + if [ -z "$(yq '.scrape_configs[] | select(.job_name == "blackbox-http") | .static_configs[] | select(.targets[] == "https://${DOMAIN_NAME}")' prometheus.yml)" ]; then + yq '(.scrape_configs[] | select(.job_name == "blackbox-http") | .static_configs) += [{"targets": ["https://${DOMAIN_NAME}"]}]' prometheus.yml -y > prometheus.temp.yml && mv prometheus.temp.yml prometheus.yml + else + echo "the html prober config is already operational" + fi + else + echo "yq is not installed on the receiving server." + fi + + EOF + else + . bin/setup_ssh_keys.sh + . bin/setup_node_exporter + fi +else + . bin/setup_ssh_keys.sh + . bin/setup_node_exporter +fi \ No newline at end of file diff --git a/bin/setup_ssh_keys.sh b/bin/setup_ssh_keys.sh new file mode 100644 index 0000000..d533bf4 --- /dev/null +++ b/bin/setup_ssh_keys.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +echo -e "${PURPLE}${BOLD}Enter the receiving server ip : ${RESET}" +read RECEIVING_IP +echo -e "${PURPLE}${BOLD}Enter the receiving server user : ${RESET}" +read RECEIVING_USER +echo -e "${PURPLE}${BOLD}Enter the receiving server port : ${RESET}" +read RECEIVING_PORT + +ssh-keygen +ssh-copy-id -p ${RECEIVING_PORT} ${RECEIVING_USER}@${RECEIVING_IP} \ No newline at end of file diff --git a/bin/variables.sh b/bin/variables.sh index b49f318..91a45f6 100644 --- a/bin/variables.sh +++ b/bin/variables.sh @@ -17,4 +17,8 @@ FRONT_DIRECTORY="" WEBSITE_TOKEN="" -CADDYFILE="/etc/caddy/Caddyfile" \ No newline at end of file +CADDYFILE="/etc/caddy/Caddyfile" + +RECEIVING_IP="" +RECEIVING_USER="" +RECEIVING_PORT="" \ No newline at end of file diff --git a/install.sh b/install.sh index 13b4efd..b3c766e 100644 --- a/install.sh +++ b/install.sh @@ -104,6 +104,14 @@ if [[ "$answer" == "y" ]]; then . bin/setup_backup.sh fi +# NODE EXPORTER +echo -e "${PURPLE}${BOLD}Setup Node Exporter ? (y/N) ${RESET}" +read answer +if [[ "$answer" == "y" ]]; then + . bin/setup_node_exporter.sh +fi + + # NUXT STATIC echo -e "${PURPLE}${BOLD}Install the front-end ? (y/N) ${RESET}" read answer