Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[terraform] install prometheus node-exporter - ec2 user data #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions modules/terraform-aws-alternat/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ data "cloudinit_config" "config" {

gzip = true
base64_encode = true
part {
content_type = "text/x-shellscript"
content = file("${path.module}/../../scripts/node-exporter.sh")
}
part {
content_type = "text/x-shellscript"
content = templatefile("${path.module}/alternat.conf.tftpl", {
Expand Down
77 changes: 77 additions & 0 deletions scripts/node-exporter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# ABOUT: this script installs the prometheus-node-exporter
# as a systemd service on the system

export SERVICE_USER="node_exporter"
export CONFIG_DIR="/etc/node-exporter"
export VERSION="1.6.1"

# download and install binary

mkdir /tmp/download
cd /tmp/download
wget "https://github.com/prometheus/node_exporter/releases/download/v${VERSION}/node_exporter-${VERSION}.linux-amd64.tar.gz"
tar xzf node_exporter-${VERSION}.linux-amd64.tar.gz
cp node_exporter-${VERSION}.linux-amd64/node_exporter /usr/local/sbin/

# create service user and group

adduser --system --no-create-home --group ${SERVICE_USER}

# create config

mkdir ${CONFIG_DIR}

cat >${CONFIG_DIR}/node_exporter_env <<EOL
OPTIONS="--collector.textfile.directory /var/lib/node_exporter/textfile_collector --collector.tcpstat --collector.systemd --collector.filesystem.ignored-mount-points=^/(sys|proc|dev|run)($|/)"
EOL

chown -R ${SERVICE_USER}:${SERVICE_USER} ${CONFIG_DIR}
chmod 0644 ${CONFIG_DIR}/node_exporter_env

# create data dir

mkdir -p /var/lib/node_exporter/textfile_collector
chown -R ${SERVICE_USER}:${SERVICE_USER} /var/lib/node_exporter

# create service dependency - socket

cat >/etc/systemd/system/node_exporter.socket <<EOL
[Unit]
Description=Node Exporter

[Socket]
ListenStream=9100

[Install]
WantedBy=sockets.target
EOL

chown root:root /etc/systemd/system/node_exporter.socket
chmod 0644 /etc/systemd/system/node_exporter.socket

# create systemd service unit

cat >/etc/systemd/system/node_exporter.service <<EOL
[Unit]
Description=Node Exporter
Requires=node_exporter.socket

[Service]
User=node_exporter
EnvironmentFile=/etc/node-exporter/node_exporter_env
ExecStart=/usr/local/sbin/node_exporter --web.systemd-socket $OPTIONS

[Install]
WantedBy=multi-user.target
EOL

chown root:root /etc/systemd/system/node_exporter.service
chmod 0644 /etc/systemd/system/node_exporter.service

# enable and start systemd service

systemctl daemon-reload
systemctl enable node_exporter.service
systemctl start node_exporter.service