#!/bin/bash basedir="/var/base" scriptdir="/var/script" shell_dir="/shell" local_dir="/usr/local" service_name="script-exporter" config_file="$local_dir/$service_name/config.yml" exporter_url="https://zhengyu1992.cn/file/exporter/$service_name" # Create necessary directories for dir in "$basedir" "$scriptdir" "$shell_dir" "$local_dir"; do [ ! -d "$dir" ] && mkdir -p "$dir" done # Download necessary files curl -sL $exporter_url/base-monitor.sh > "$basedir/base-monitor.sh" curl -sL $exporter_url/gpu-monitor.py > "$basedir/gpu-monitor.py" curl -sL $exporter_url/base-exporter.sh > "$shell_dir/base-exporter.sh" curl -sL $exporter_url/script-exporter.sh > "$shell_dir/script-exporter.sh" curl -o /tmp/$service_name.gz $exporter_url/$service_name.gz tar zxvf /tmp/$service_name.gz -C "$local_dir" >/dev/null 2>&1 chmod +x $local_dir/$service_name/script-exporter chmod +x $shell_dir/base-exporter.sh chmod +x $shell_dir/script-exporter.sh rm -f /tmp/$service_name.gz # Add systemd service # Read /etc/os-release file source /etc/os-release if [[ "$VERSION_ID" == "7" ]]||[[ "$ID" == "alinux" ]]||[[ "$ID" == "rocky" ]]||[[ "$ID" == "anolis" ]]||[[ "$ID" == "ubuntu" ]]; then cat </etc/systemd/system/$service_name.service [Unit] Description=Script Exporter After=network.target [Service] User=root WorkingDirectory=$local_dir/script-exporter ExecStart=$local_dir/script-exporter/script-exporter -config.file $config_file Restart=on-failure RestartSec=10 [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable --now "$service_name" systemctl status "$service_name"|grep Active elif [[ "$VERSION_ID" == "6" ]]; then cat < /etc/init.d/$service_name #!/bin/sh . /etc/rc.d/init.d/functions RETVAL=0 start() { echo -n "Starting $service_name: " $local_dir/script-exporter/script-exporter -config.file $config_file > /dev/null & RETVAL=$? if [ $RETVAL -eq 0 ]; then touch /var/lock/subsys/$service_name fi echo return $RETVAL } stop() { echo -n "Stopping $service_name: " killproc $service_name RETVAL=$? if [ $RETVAL -eq 0 ]; then rm -f /var/lock/subsys/$service_name rm -f /var/run/$service_name.pid fi echo return $RETVAL } case "\$1" in start) start ;; stop) stop ;; status) status $service_name ;; restart) stop start ;; *) echo "Usage: \$0 {start|stop|status|restart}" exit 1 ;; esac exit \$RETVAL EOF chmod +x /etc/init.d/$service_name chkconfig --add "$service_name" && service "$service_name" start && service "$service_name" status else echo "Unknown or unsupported Linux distribution" fi