Last active
August 11, 2023 12:19
-
-
Save K4CZP3R/1e98dadff40624bfb5360615c1cf3067 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
NODE_EXPORTER_VERSION="1.6.1" | |
NODE_EXPORTER_USER="node_exporter" | |
BIN_DIRECTORY="/usr/local/bin" | |
# Create user if not exists | |
id -u ${NODE_EXPORTER_USER} &>/dev/null || useradd --no-create-home --shell /bin/false ${NODE_EXPORTER_USER} | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to create user ${NODE_EXPORTER_USER}" | |
exit 1 | |
fi | |
# Install wget if not exists | |
if ! command -v wget &>/dev/null; then | |
apt-get install -y wget | |
fi | |
# Install openssl if not exists | |
if ! command -v openssl &>/dev/null; then | |
apt-get install -y openssl | |
fi | |
# Download exporter and extract | |
wget https://github.com/prometheus/node_exporter/releases/download/v${NODE_EXPORTER_VERSION}/node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to download node exporter" | |
exit 1 | |
fi | |
tar -xvf node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to extract node exporter" | |
exit 1 | |
fi | |
cp node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64/node_exporter ${BIN_DIRECTORY}/ | |
chown ${NODE_EXPORTER_USER}:${NODE_EXPORTER_USER} ${BIN_DIRECTORY}/node_exporter | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to copy node exporter to ${BIN_DIRECTORY}" | |
exit 1 | |
fi | |
echo "Copied node exporter to ${BIN_DIRECTORY}" | |
rm -rf node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64.tar.gz node_exporter-${NODE_EXPORTER_VERSION}.linux-amd64 | |
echo "Removed node exporter files" | |
# Create /etc/node-exporter if not exists | |
if [ ! -d /etc/node-exporter ]; then | |
mkdir /etc/node-exporter | |
echo "Created /etc/node-exporter" | |
fi | |
# Move to /etc/node-exporter | |
cd /etc/node-exporter | |
# Generate self-signed cert for node exporter | |
openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout node_exporter.key -out node_exporter.crt -subj "/C=ZA/ST=CT/L=SA/O=VPN/CN=localhost" -addext "subjectAltName = DNS:localhost" | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to generate self-signed cert for node exporter" | |
exit 1 | |
fi | |
# Install apache2-utils if not exists | |
if ! command -v htpasswd &>/dev/null; then | |
apt-get install -y apache2-utils | |
fi | |
# Generate password using htpasswd and save to variable | |
NODE_EXPORTER_PASSWORD=$( htpasswd -nBC 10 "" | tr -d ':\n'; echo ) | |
# Check if config.yml exists | |
if [ -f /etc/node-exporter/config.yml ]; then | |
echo "config.yml already exists, removing it?" | |
read -p "Remove config.yml? [y/n]: " -n 1 -r | |
echo "" | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
rm -f /etc/node-exporter/config.yml | |
else | |
echo "Aborting" | |
exit 1 | |
fi | |
fi | |
# Create config.yml | |
cat <<EOF > /etc/node-exporter/config.yml | |
tls_server_config: | |
cert_file: /etc/node-exporter/node_exporter.crt | |
key_file: /etc/node-exporter/node_exporter.key | |
basic_auth_users: | |
prometheus: "${NODE_EXPORTER_PASSWORD}" | |
EOF | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to create config.yml" | |
exit 1 | |
fi | |
chown -R ${NODE_EXPORTER_USER}:${NODE_EXPORTER_USER} /etc/node-exporter | |
# Check if node_export.service exists | |
if [ -f /etc/systemd/system/node_exporter.service ]; then | |
echo "node_exporter.service already exists" | |
exit 1 | |
fi | |
cat > /etc/systemd/system/node_exporter.service << EOF | |
[Unit] | |
Description=Node Exporter | |
Wants=network-online.target | |
After=network-online.target | |
StartLimitIntervalSec=500 | |
StartLimitBurst=5 | |
[Service] | |
User=${NODE_EXPORTER_USER} | |
Group=${NODE_EXPORTER_USER} | |
Type=simple | |
Restart=on-failure | |
RestartSec=5s | |
WorkingDirectory=/etc/node-exporter | |
ExecStart=${BIN_DIRECTORY}/node_exporter --web.config.file=/etc/node-exporter/config.yml | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
systemctl daemon-reload | |
systemctl enable node_exporter | |
systemctl restart node_exporter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PROMETHEUS_VERSION="2.46.0" | |
PROMETHEUS_USER="prometheus" | |
BIN_DIRECTORY="/usr/local/bin" | |
# Create user if not exists | |
id -u ${PROMETHEUS_USER} &>/dev/null || useradd --no-create-home --shell /bin/false ${PROMETHEUS_USER} | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to create user ${PROMETHEUS_USER}" | |
exit 1 | |
fi | |
# Install wget if not exists | |
if ! command -v wget &>/dev/null; then | |
apt-get install -y wget | |
fi | |
# Install openssl if not exists | |
if ! command -v openssl &>/dev/null; then | |
apt-get install -y openssl | |
fi | |
# Download prometheus and extract | |
wget https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to download prometheus" | |
exit 1 | |
fi | |
tar -xvf prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to extract prometheus" | |
exit 1 | |
fi | |
cp prometheus-${PROMETHEUS_VERSION}.linux-amd64/prometheus ${BIN_DIRECTORY}/ | |
chown ${PROMETHEUS_USER}:${PROMETHEUS_USER} ${BIN_DIRECTORY}/prometheus | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to copy prometheus to ${BIN_DIRECTORY}" | |
exit 1 | |
fi | |
echo "Copied prometheus to ${BIN_DIRECTORY}" | |
rm -rf prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz prometheus-${PROMETHEUS_VERSION}.linux-amd64 | |
echo "Removed prometheus files" | |
# Create /etc/prometheus if not exists | |
if [ ! -d /etc/prometheus ]; then | |
mkdir /etc/prometheus | |
echo "Created /etc/prometheus" | |
fi | |
# Move to /etc/prometheus | |
cd /etc/prometheus | |
# # Generate self-signed cert for node exporter | |
# openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout node_exporter.key -out node_exporter.crt -subj "/C=ZA/ST=CT/L=SA/O=VPN/CN=localhost" -addext "subjectAltName = DNS:localhost" | |
# # Check if successful | |
# if [ $? -ne 0 ]; then | |
# echo "Failed to generate self-signed cert for node exporter" | |
# exit 1 | |
# fi | |
# # Install apache2-utils if not exists | |
# if ! command -v htpasswd &>/dev/null; then | |
# apt-get install -y apache2-utils | |
# fi | |
# # Generate password using htpasswd and save to variable | |
# NODE_EXPORTER_PASSWORD=$( htpasswd -nBC 10 "" | tr -d ':\n'; echo ) | |
# Check if prometheus.yml exists | |
if [ -f /etc/prometheus/prometheus.yml ]; then | |
echo "prometheus.yml already exists, removing it?" | |
read -p "Remove prometheus.yml? [y/n]: " -n 1 -r | |
echo "" | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
rm -f /etc/prometheus/prometheus.yml | |
else | |
echo "Aborting" | |
exit 1 | |
fi | |
fi | |
# Create config.yml | |
cat <<EOF > /etc/prometheus/prometheus.yml | |
global: | |
scrape_interval: 15s | |
evaluation_interval: 15s | |
scrape_configs: | |
- job_name: 'prometheus' | |
static_configs: | |
- targets: ['localhost:9090'] | |
EOF | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to create prometheus.yml" | |
exit 1 | |
fi | |
chown -R ${PROMETHEUS_USER}:${PROMETHEUS_USER} /etc/prometheus | |
# Check if prometheus.service exists | |
if [ -f /etc/systemd/system/prometheus.service ]; then | |
echo "prometheus.service already exists" | |
exit 1 | |
fi | |
cat > /etc/systemd/system/prometheus.service << EOF | |
[Unit] | |
Description=Prometheus | |
Wants=network-online.target | |
After=network-online.target | |
StartLimitIntervalSec=500 | |
StartLimitBurst=5 | |
[Service] | |
User=${PROMETHERUS_USER} | |
Group=${PROMEHTHEUS_USER} | |
Type=simple | |
Restart=on-failure | |
RestartSec=5s | |
ExecStart=${BIN_DIRECTORY}/prometheus --config.file=/etc/prometheus/prometheus.yml | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
systemctl daemon-reload | |
systemctl enable prometheus | |
systemctl restart prometheus |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# check if /etc/docker/daemon.json exists, stop if it does | |
if [ -f /etc/docker/daemon.json ]; then | |
echo "File /etc/docker/daemon.json exists, add metrics-addr and experimental manually" | |
exit 1 | |
fi | |
# Create /etc/docker/daemon.json if not exists | |
if [ ! -f /etc/docker/daemon.json ]; then | |
touch /etc/docker/daemon.json | |
fi | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to create /etc/docker/daemon.json" | |
exit 1 | |
fi | |
# Get user input, address | |
read -p "Enter metrics-address: " metrics_address | |
# Check if successful | |
if [ $? -ne 0 ]; then | |
echo "Failed to get metrics-address" | |
exit 1 | |
fi | |
# add metrics-addr and experimental to /etc/docker/daemon.json | |
cat <<EOF > /etc/docker/daemon.json | |
{ | |
"metrics-addr" : "${metrics_address}:9323", | |
"experimental" : true | |
} | |
EOF | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment