github地址:

https://github.com/zhuqiyang/install-node_exporter-shell

批量安装node_exporter:适用于CentOS6、CentOS7两个版本。

#!/bin/bash
#######################################
#                                     #
#        install node_exporter        # 
#                                     #
#######################################

set -x
export HOST_IP=$1
export PACKAGE_NAME=$2

if [ -z "$1" ] || [ -z "$2" ]; then
cat <<EOF

	bash $0 HOST_IP PACKAGE_NAME

EOF
exit
fi

# check ssh without password
ssh -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no -o ConnectTimeout=1 $HOST_IP 'date'
if [ $? -ne 0 ]; then
	echo 'Unable to log in without password'
	exit
fi

# check 9100 port exists
export PORT_EXISTS=$(ssh $HOST_IP  'lsof -i:9100 > /dev/null' && echo $?)
if [ "$PORT_EXISTS" = "0" ]; then
	echo "9100 port exists"
	exit
fi

#  check OS version
export OS_RELEASE=`ssh $HOST_IP "uname -r"`


scp_file(){
	ssh $HOST_IP 'hostname'
	scp $PACKAGE_NAME ${HOST_IP}:/tmp/
	ssh $HOST_IP "tar -xf /tmp/$PACKAGE_NAME -C /usr/local/ && cd /usr/local/ && ln -sv node_exporter-1.0.1.linux-amd64/ node_exporter"
}

if grep '.el7.' <<< $OS_RELEASE ; then
	scp_file
	scp ./node_exporter.service ${HOST_IP}:/usr/lib/systemd/system/
	ssh $HOST_IP 'systemctl start node_exporter.service'
	ssh $HOST_IP 'systemctl enable node_exporter.service'
	ssh $HOST_IP "rm /tmp/${PACKAGE_NAME}"

elif grep '.el6.' <<< $OS_RELEASE ; then
	scp_file
	scp node_exporter ${HOST_IP}:/etc/init.d/
	ssh $HOST_IP 'chmod +x /etc/init.d/node_exporter'
	ssh $HOST_IP 'service node_exporter start'
	ssh $HOST_IP "rm /tmp/${PACKAGE_NAME}"
fi

其他版本的linux:

#!/bin/bash

export HOST_IP=$1
export PACKAGE_NAME=$2

# check ssh without password
ssh -p22 -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no $HOST_IP 'date'
if [ $? -ne 0 ]; then
        echo 'Unable to log in without password'
	echo $HOST_IP >> notok.txt
        exit
fi

# check 9100 port exists
export PORT_EXISTS=$(ssh -p22 -o ConnectTimeout=2 $HOST_IP  'lsof -i:9100 > /dev/null' && echo $?)
if [ "$PORT_EXISTS" = "0" ]; then
	echo "9100 port exists"
	echo $HOST_IP >> portnotok.txt
#	exit
fi

ssh -p22 $HOST_IP 'hostname'
scp -P22 $PACKAGE_NAME ${HOST_IP}:/tmp/
ssh -p22 $HOST_IP "tar -xf /tmp/$PACKAGE_NAME -C /usr/local/ && cd /usr/local/ && ln -sv node_exporter-1.0.1.linux-amd64/ node_exporter"
scp -P22 start.sh ${HOST_IP}:/usr/local/node_exporter/
ssh -p22 $HOST_IP "cd /usr/local/node_exporter && chmod +x start.sh && ./start.sh"
ssh -p22 $HOST_IP 'rm /tmp/node_exporter-1.0.1.linux-amd64.tar.gz'