From ac1e8935839096fff188f9fbe72b0ba938523015 Mon Sep 17 00:00:00 2001 From: Patrick Cao Huu Thien Date: Mon, 28 Nov 2022 15:25:14 +0100 Subject: [PATCH] module alpine prometheus node_exporter --- main.tf | 38 ++++++++++++++++++++++++++++++++++++++ node_exporter.initd | 11 +++++++++++ variables.tf | 15 +++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 main.tf create mode 100644 node_exporter.initd create mode 100644 variables.tf diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..8601922 --- /dev/null +++ b/main.tf @@ -0,0 +1,38 @@ +resource "null_resource" "alpine_prometheus_node_exporter" { + triggers = { + source = filesha256("${path.module}/node_exporter.initd") + } + connection { + type = "ssh" + user = "root" + host = var.connection_ip + private_key = var.connection_private_key + } + + provisioner "file" { + source = "${path.module}/node_exporter.initd" + destination = "/tmp/node_exporter.initd" + } + + provisioner "remote-exec" { + inline = [ + "echo '#--------------------------------'", + "echo '# module prometheus_node_exporter'", + "echo '#--------------------------------'", + "wget -nc -q https://github.com/prometheus/node_exporter/releases/download/v${var.node_exporter_version}/node_exporter-${var.node_exporter_version}.linux-amd64.tar.gz", + "tar xzf node_exporter-${var.node_exporter_version}.linux-amd64.tar.gz node_exporter-${var.node_exporter_version}.linux-amd64/node_exporter", + "echo '# install node_exporter in /usr/sbin'", + "install -Dm755 node_exporter-${var.node_exporter_version}.linux-amd64/node_exporter /usr/sbin", + # + "echo '# install service node_exporter'", + "install -Dm755 /tmp/node_exporter.initd /etc/init.d/node_exporter", + # + "echo '# enable service node_exporter'", + "rc-update add node_exporter", + "/etc/init.d/node_exporter start", + # + "echo '# module DONE'", + ] + } + +} diff --git a/node_exporter.initd b/node_exporter.initd new file mode 100644 index 0000000..85bf99d --- /dev/null +++ b/node_exporter.initd @@ -0,0 +1,11 @@ +#!/sbin/openrc-run + +description="prometheus node exporter" +command="/usr/sbin/node_exporter" +command_args="--collector.disable-defaults --collector.cpu --collector.cpufreq --collector.diskstats --collector.filefd --collector.filesystem --collector.loadavg --collector.meminfo --collector.netclass --collector.netstat --collector.os --collector.time --collector.uname --collector.vmstat --web.disable-exporter-metrics " +pidfile="/run/${RC_SVCNAME}.pid" + +# node_exporter stay in backgroud +command_background=true +# make a pid for me + diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..6ff2fe5 --- /dev/null +++ b/variables.tf @@ -0,0 +1,15 @@ +variable "connection_ip" { + type = string + description = "ipv4" +} + +variable "connection_private_key" { + type = string + description = "private key for terraform connection" + sensitive = true +} + +variable "node_exporter_version" { + type = string + description = "like 1.5.2" +}