#!/bin/bash
cd 
echo  "">>node_exporter_check.log
# 日志文件路径
LOG_FILE="/root/node_exporter_check.log"

# 检查端口9100是否在使用
check_port() {
    netstat -an | grep ':9100 ' | grep LISTEN > /dev/null 2>&1
    return $?
}

# 启动node_exporter
start_node_exporter() {
    cd node_exporter-1.6.0.linux-amd64 && nohup ./node_exporter 2>/dev/null &
    echo "$(date '+%Y-%m-%d %H:%M:%S') - node_exporter started" >> $LOG_FILE
}

# 检查并启动服务
if check_port; then
    echo "$(date '+%Y-%m-%d %H:%M:%S') - Port 9100 is already in use" >> $LOG_FILE
else
    start_node_exporter
fi
