#!/bin/bash #下载node_exporter安装包 version=1.6.1 local_dir="/usr/local" service_name=node_exporter exporter_url="https://zhengyu1992.cn/file/exporter/${service_name}" #exporter_url="https://ghproxy.com/https://github.com/prometheus/$service_name/releases/download/v$version" curl -o /tmp/${service_name}-$version.linux-amd64.tar.gz ${exporter_url}/${service_name}-${version}.linux-amd64.tar.gz for dir in "$local_dir"; do [ ! -d "$dir" ] && mkdir -p "$dir" done tar zxvf /tmp/${service_name}-${version}.linux-amd64.tar.gz -C /usr/local >/dev/null 2>&1 mv /usr/local/${service_name}-${version}.linux-amd64 /usr/local/${service_name} rm -f /tmp/${service_name}-${version}.linux-amd64.tar.gz ##启动$service_name # Read /etc/os-release file source /etc/os-release if [[ "$VERSION_ID" == "7" ]]||[[ "$ID" == "alinux" ]]||[[ "$ID" == "rocky" ]]||[[ "$ID" == "anolis" ]]||[[ "$ID" == "ubuntu" ]]; then echo -e '[Unit] Description=node_exporter After=network.target [Service] User=root WorkingDirectory=/usr/local/node_exporter ExecStart=/usr/local/node_exporter/node_exporter --web.listen-address=:29100 --no-collector.wifi --no-collector.hwmon --no-collector.btrfs --collector.filesystem.mount-points-exclude=^/(dev|proc|sys|run/k3s/containerd/.+|var/lib/docker/.+|var/lib/kubelet/pods/.+)($|/) --collector.netclass.ignored-devices=^(veth.*|[a-f0-9]{15})$ --collector.netdev.device-exclude=^(veth.*|[a-f0-9]{15})$ Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target' >/etc/systemd/system/node_exporter.service systemctl daemon-reload systemctl enable --now ${service_name} systemctl status "$service_name"|grep Active elif [[ "$VERSION_ID" == "6" ]]; then echo -e '#!/bin/sh . /etc/rc.d/init.d/functions RETVAL=0 start() { echo -n "Starting node_exporter: " /usr/local/node_exporter/node_exporter --web.listen-address=:29100 --no-collector.wifi --no-collector.hwmon --no-collector.btrfs --collector.filesystem.mount-points-exclude=^/(dev|proc|sys|run/k3s/containerd/.+|var/lib/docker/.+|var/lib/kubelet/pods/.+)($|/) --collector.netclass.ignored-devices=^(veth.*|[a-f0-9]{15})$ --collector.netdev.device-exclude=^(veth.*|[a-f0-9]{15})$> /dev/null & RETVAL=$? if [ $RETVAL -eq 0 ]; then touch /var/lock/subsys/node_exporter fi echo return $RETVAL } stop() { echo -n "Stopping node_exporter: " killproc node_exporter RETVAL=$? if [ $RETVAL -eq 0 ]; then rm -f /var/lock/subsys/node_exporter rm -f /var/run/node_exporter.pid fi echo return $RETVAL } case "$1" in start) start ;; stop) stop ;; status) status node_exporter ;; restart) stop start ;; *) echo "Usage: $0 {start|stop|status|restart}" exit 1 ;; esac exit $RETVAL' >/etc/init.d/node_exporter chmod +x /etc/init.d/node_exporter chkconfig --add ${service_name} service ${service_name} start && service ${service_name} status else echo "Unknown or unsupported Linux distribution" fi