#!/bin/bash

set -e

if [ $(netstat -pan | grep :9100 | wc -l) != 0 ]; then
  echo "19100端口已经被占用"
  exit 1
fi

version="1.6.1"

tar -xf node_exporter-${version}.tar.gz
chown root:root -R node_exporter-${version}
mv node_exporter-${version}/node_exporter /usr/bin
rm -rf node_exporter-${version}

cat > /usr/lib/systemd/system/node_exporter.service << EOF
[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=root
ExecStart=/usr/bin/node_exporter

[Install]
WantedBy=default.target
EOF

systemctl daemon-reload
systemctl enable --now node_exporter
systemctl restart node_exporter

echo "安装node_exporter完成，可输入systemctl status node_exporter进行查看"
