#!/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://wiki.htn.cloud/htntools/node_exporter_cent79"
CentOS8="https://wiki.htn.cloud/htntools/node_exporter_cent8"

 if cat /etc/*release | grep "CentOS Linux 7"; then
    echo "==============================================="
    echo "Installing packages Node-exporter for CentOS 7"
    echo "==============================================="
    curl -o /usr/sbin/node_exporter $CentOS7

 elif cat /etc/*release | grep "CentOS Stream 8"; then
    echo "==============================================="
    echo "Installing packages Node-exporter for CentOS 8"
    echo "==============================================="
    curl -o /usr/sbin/node_exporter $CentOS8
 fi

chmod +x /usr/sbin/node_exporter
mkdir -p /opt/script/result
echo -e "Create 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

systemctl daemon-reload; systemctl restart node-exporter.service

#systemctl restart firewalld
#firewall-cmd --add-port=9100/tcp --permanent
#firewall-cmd --reload

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