#!/bin/bash
export LANG=C.UTF-8
Red='\033[0;31m'        # Red
Yellow='\033[0;33m'     # Yellow
NC='\033[0m'            # No Color

set -e

CentOS7="https://centos-repo.htn.cloud/script/node_exporter_cent7"
CentOS8="https://centos-repo.htn.cloud/script/node_exporter_cent8"
Ubuntu="https://centos-repo.htn.cloud/script/node_exporter_ubuntu"

# Xác định hệ điều hành
if grep -q "CentOS Linux 7" /etc/*release; then
    echo "==============================================="
    echo "Installing Node Exporter for CentOS 7"
    echo "==============================================="
    curl -o /usr/sbin/node_exporter $CentOS7

elif grep -q "CentOS Stream 8" /etc/*release; then
    echo "==============================================="
    echo "Installing Node Exporter for CentOS 8"
    echo "==============================================="
    curl -o /usr/sbin/node_exporter $CentOS8

elif grep -q "Ubuntu" /etc/*release; then
    echo "==============================================="
    echo "Installing Node Exporter for Ubuntu"
    echo "==============================================="
    curl -o /usr/sbin/node_exporter $Ubuntu
    chmod +x /usr/sbin/node_exporter

    # Tạo file service riêng cho Ubuntu
    cat <<EOT > /etc/systemd/system/node-exporter.service
[Unit]
Description=Prometheus Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target
EOT
fi

# Cấp quyền thực thi cho node_exporter
chmod +x /usr/sbin/node_exporter
mkdir -p /opt/script/result

# Nếu không phải Ubuntu, dùng chung file service cho CentOS
if ! grep -q "Ubuntu" /etc/*release; then
    echo -e "Creating systemctl script"
    cat <<EOT > /etc/systemd/system/node-exporter.service
[Unit]
Description=Node Exporter Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=root
WorkingDirectory=/usr/sbin
ExecStart=/usr/sbin/node_exporter \
          --collector.mountstats \
          --no-collector.hwmon --no-collector.pressure --no-collector.thermal_zone --no-collector.rapl --no-collector.infiniband \
          --no-collector.edac --no-collector.arp --no-collector.entropy --no-collector.ipvs --no-collector.schedstat --no-collector.softnet \
          --no-collector.filefd --collector.textfile.directory=/opt/script/result --collector.filesystem.ignored-mount-points=^/data(vcas|mfs)

ExecReload=/bin/kill -HUP \$MAINPID
TimeoutStopSec=10s
SendSIGKILL=no
SyslogIdentifier=prometheus_node_exporter
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target
EOT
fi

# Kích hoạt dịch vụ
systemctl daemon-reload
systemctl enable node-exporter
systemctl restart node-exporter

# Mở port 9100 trên firewalld nếu có
if command -v firewall-cmd &> /dev/null; then
    firewall-cmd --add-port=9100/tcp --permanent
    firewall-cmd --reload
fi

# Mở port 9100 trên iptables nếu có
if command -v iptables &> /dev/null; then
    iptables -C INPUT -p tcp --dport 9100 -j ACCEPT 2>/dev/null || iptables -A INPUT -p tcp --dport 9100 -j ACCEPT
    iptables-save > /etc/sysconfig/iptables
fi

# Mở port 9100 trên ufw nếu có
if command -v ufw &> /dev/null; then
    ufw allow 9100/tcp
fi

echo -e "---------${Red}"
echo -e "Done!"
echo -e "${NC}---------"

