#!/bin/sh
author="jingjin"
Version="1.3.1"
Fileaname="node_exporter-${Version}.linux-amd64"
Filelinkcheck="false"
FileExist=""
Checksum="e77ff1b0a824a4e13f82a35d98595fe526849c09e3480d0789a56b72242d2abc"
FileUrl="https://github.com/prometheus/node_exporter/releases/download/v${Version}/${Fileaname}.tar.gz"
function prepare_check() {
echo "+++++++++++++++++++++++++++++++++++++++++++++++++开始检查系统+++++++++++++++++++++++++++++++++++++++++++++++++++"
char_num=`cat /proc/version | grep "Red Hat" -o | wc -c`
[ "${char_num}" = "0" ] && {
echo -e "[\033[31m Error \033[0m] 检测系统不是redhat、centos系统,终止程序!"
exit 1
}
cat >/tmp/node_exporter.service <<EOF
[Unit]
Description=prometheus node_expoter
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
ExecStart=/usr/local/sbin/node_exporter
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=process
[Install]
WantedBy=multi-user.target
EOF
echo "++++++++++++++++++++++++++++++++++++++++++++++++检查完毕+++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo ""
}
function get_targz() {
echo "开始下载prometheus的node_exporter到/tmp/tmp_node_exporter_store"
mkdir -p /tmp/tmp_node_exporter_store
cd /tmp/tmp_node_exporter_store || exit 1
if [ -d "/tmp/tmp_node_exporter_store" ]; then
{
# timeout 360 curl -OL ${FileUrl} && echo -e "[\033[32m Success \033[0m] tar包下载成功 "
curl --connect-timeout 3600 -m 3600 -OL ${FileUrl} && echo -e "[\033[32m Success \033[0m] tar包下载成功 "
} || {
rm -rf /tmp/tmp_node_exporter_store/
echo -e "[\033[31m ERROR \033[0m] Failed to download ${Fileaname}.tar.gz (下载${Fileaname}.tar.gz失败, 请检查网络是否正常或尝试重新执行脚本)"
echo -e "[\033[33m warning \033[0m] 可能是连接到github的网络问题!"
exit 1
}
tar -xzf /tmp/tmp_node_exporter_store/${Fileaname}.tar.gz -C /usr/local/ || {
rm -rf /tmp/tmp_node_exporter_store/node_exporter-${Version}.linux-amd64.tar.gz
echo -e "[\033[31m Error \033[0m] Failed to unzip ${Fileaname}.tar.gz(解压 ${Fileaname}.tar.gz 失败)"
exit 1
}
rm -rf /tmp/tmp_node_exporter_store
fi
}
function ln_node_exporter_check() {
if [ -f /usr/local/sbin/node_exporter ] || [ -L /usr/local/sbin/node_exporter ] ;then
test /usr/local/node_exporter-1.3.1.linux-amd64/node_exporter -ef /usr/local/sbin/node_exporter || {
echo -e "[\033[31m warning \033[0m]:/usr/local/sbin/目录下已存在node_exporter,该文件不是我们创建的软连接!请手动检查。"
FileExist="true"
exit 2
}
else
#文件不存在,我们手动创建
ln -s /usr/local/${Fileaname}/node_exporter /usr/local/sbin/node_exporter && echo -e "[\033[32m Success \033[0m] 创建/usr/local/sbin/node_exporter 软连接成功"
fi
}
function system_service_manage() {
system_super_daemon=`ps -q 1 | awk -F" " 'END {print $4}'`
if [ "${system_super_daemon}" = "systemd" ];then
echo -e "[\033[33m warning \033[0m] 准备服务脚本中 "
if [ ! -f /usr/lib/systemd/system/node_exporter.service ];then
cp /tmp/node_exporter.service /usr/lib/systemd/system/ && systemctl daemon-reload \
&& echo "+++++++++++++++++++++++++++++++++++++++重载服务列表+++++++++++++++++++++++++++++++++++++++++++++"
else
comm /tmp/node_exporter.service /usr/lib/systemd/system/node_exporter.service || {
echo -e "[\033[31m Error \033[0m] node_exporter.service已存在 /usr/lib/systemd/system/目录中,并非我们编写的,请手动排查."
}
fi
else
echo -e "[\033[31m Error \033[0m] 系统并非systemd管理,终止当前程序。"
echo -e "[\033[33m warning \033[0m] 建议手动编写init的脚本,或者手动执行命令:nohup node_expoter &"
exit 3
fi
echo "usage: systemctl status|start|stop node_exporter"
}
function main() {
prepare_check
get_targz
ln_node_exporter_check
system_service_manage
}
main