Skip to content

bls相关文档

用于管理服务器,它存在以下几个功能:

  • 上报信息:cpu,内存,网络,温度等信息
  • 执行shell命令
  • 定时备份指定文件夹
  • 外置接口(重点)

bls安装路径:/go/

shell
# 查看bls状态
systemctl status bls2-agent.service

# 重启bls
systemctl restart bls2-agent.service

# 查看运行日志 - 使用最近日期的文件
# 日志文件存放在 /go/storage/logs/
tail -f /go/storage/logs/xxxx.log

定时备份指点文件夹

定时备份/go/data/文件夹,默认一个小时备份检测一次,如果存在改动则备份到主控服务器。如果文件夹大小超过2MB则会取消备份。

外置接口

1.1 上报算力数据

shell
# hashrate 表示算力,其它默认即可
curl -X POST http://localhost:3001/api/v1/data \
  -H "Content-Type: application/json" \
  -d '{
    "source": "miner",
    "type": "hashrate", 
    "data": {
      "hashrate": 105.1
    }
  }'

例:上报算力脚本(nock)

shell
# 执行一次监控(测试)
./hashrate_monitor.sh

# 安装定时任务
./hashrate_monitor.sh install

# 删除定时任务
./hashrate_monitor.sh uninstall

# 查看帮助
./hashrate_monitor.sh help

# 查看定时任务
crontab -l

# 查看运行日志
tail -f /var/log/hashrate-monitor.log

# 手动删除定时任务(如果需要)
crontab -l | grep -v hashrate_monitor.sh | crontab -

运行脚本

shell
#!/bin/bash

# 挖矿算力监控脚本
# 功能:监控挖矿算力并发送到API,支持cron定时任务安装

LOG_DIR="/root/log"
API_URL="http://localhost:3001/api/v1/data"
LOG_FILE_PATH="/var/log/hashrate-monitor.log"

# 记录日志函数
log_message() {
    echo "$(date '+%Y-%m-%d %H:%M:%S'): $1" >> "$LOG_FILE_PATH"
}

# 监控功能(单次执行)- 提取算力数据(后面换项目也就是修改这个函数)
monitor_once() {
    # 查找最新的日志文件
    LOG_FILE=$(find $LOG_DIR -name "miner.log-*" -type f 2>/dev/null | sort -r | head -n 1)

    if [ -z "$LOG_FILE" ]; then
        log_message "ERROR: 未找到日志文件在目录 $LOG_DIR"
        exit 1
    fi

    # 提取最后一条speed记录中的Average值
    SPEED=$(grep "msg=Speed" "$LOG_FILE" | tail -n 1 | grep -o 'Average=[0-9.]*' | cut -d'=' -f2)

    if [ -z "$SPEED" ]; then
        log_message "ERROR: 未找到speed数据在文件 $LOG_FILE"
        exit 1
    fi

    # 保留两位小数
    HASHRATE=$(printf "%.2f" "$SPEED")

    # 发送POST请求
    RESPONSE=$(curl -s -w "%{http_code}" -X POST "$API_URL" \
        -H "Content-Type: application/json" \
        -d "{\"source\": \"miner\", \"type\": \"hashrate\", \"data\": {\"hashrate\": $HASHRATE}}")

    HTTP_CODE="${RESPONSE: -3}"

    if [ "$HTTP_CODE" = "200" ]; then
        log_message "SUCCESS: 发送成功 - 算力: ${HASHRATE} P/m"
    else
        log_message "ERROR: 发送失败,HTTP状态码: $HTTP_CODE"
        exit 1
    fi
}

# 安装cron定时任务
install_cron() {
    SCRIPT_PATH="$(realpath "$0")"
    CRON_JOB="* * * * * $SCRIPT_PATH >/dev/null 2>&1"
    
    echo "设置挖矿算力监控定时任务..."

    # 设置执行权限
    chmod +x "$SCRIPT_PATH"
    echo "已设置脚本执行权限"

    # 检查cron任务是否已存在
    if crontab -l 2>/dev/null | grep -q "$SCRIPT_PATH"; then
        echo "定时任务已存在,正在更新..."
        # 删除旧的任务
        crontab -l 2>/dev/null | grep -v "$SCRIPT_PATH" | crontab -
    fi

    # 添加新的cron任务(每分钟执行一次,因为cron不支持秒级)
    (crontab -l 2>/dev/null; echo "$CRON_JOB") | crontab -

    echo "定时任务设置完成!"
    echo "任务详情: 每分钟执行一次监控脚本"
    echo "日志文件: /var/log/hashrate-monitor.log"
    echo ""
    echo "管理命令:"
    echo "查看定时任务: crontab -l"
    echo "删除定时任务: crontab -l | grep -v '$SCRIPT_PATH' | crontab -"
    echo "查看日志: tail -f /var/log/hashrate-monitor.log"
    echo "测试运行: $SCRIPT_PATH"
}

# 卸载cron定时任务
uninstall_cron() {
    SCRIPT_PATH="$(realpath "$0")"
    
    echo "删除挖矿算力监控定时任务..."
    
    if crontab -l 2>/dev/null | grep -q "$SCRIPT_PATH"; then
        crontab -l 2>/dev/null | grep -v "$SCRIPT_PATH" | crontab -
        echo "定时任务已删除"
    else
        echo "未找到相关定时任务"
    fi
}

# 显示帮助信息
show_help() {
    echo "挖矿算力监控脚本"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  无参数      执行一次监控(默认行为)"
    echo "  install     安装cron定时任务"
    echo "  uninstall   删除cron定时任务"
    echo "  help        显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0              # 执行一次监控"
    echo "  $0 install      # 安装定时任务"
    echo "  $0 uninstall    # 删除定时任务"
}

# 主程序
case "${1:-monitor}" in
    "install")
        install_cron
        ;;
    "uninstall")
        uninstall_cron
        ;;
    "help"|"-h"|"--help")
        show_help
        ;;
    "monitor"|*)
        monitor_once
        ;;
esac

安装bls脚本

shell
#!/bin/bash

# 日志函数
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"
}

# 检查服务是否存在
is_service_exist() {
    systemctl list-unit-files | grep -q bls2-agent.service
    return $?
}

# 重启服务
restart_service() {
    log "Restarting bls2-agent service..."
    systemctl daemon-reload
    systemctl restart bls2-agent.service
    if [ $? -eq 0 ]; then
        log "bls2-agent service restarted successfully."
    else
        log "Failed to restart bls2-agent service."
        exit 1
    fi
}

# 自动启动服务
auto_start() {
    log "Setting up bls2-agent service..."

    # 创建目录
    mkdir -p /etc/systemd/system/
    if [ $? -ne 0 ]; then
        log "Failed to create directory /etc/systemd/system/."
        exit 1
    fi

    # 创建服务文件
    cat >/etc/systemd/system/bls2-agent.service <<EOF
[Unit]
Description=bls2-agent
After=network.target

[Service]
WorkingDirectory=/go
Type=simple
User=root
Group=root
ExecStart=/go/bls2-agent
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

    if [ $? -ne 0 ]; then
        log "Failed to create bls2-agent service file."
        exit 1
    fi

    # 重新加载 systemd 配置
    systemctl daemon-reload
    if [ $? -ne 0 ]; then
        log "Failed to reload systemd daemon."
        exit 1
    fi

    # 启用并启动服务
    systemctl enable bls2-agent.service
    if [ $? -ne 0 ]; then
        log "Failed to enable bls2-agent service."
        exit 1
    fi

    systemctl start bls2-agent.service
    if [ $? -ne 0 ]; then
        log "Failed to start bls2-agent service."
        exit 1
    fi

    log "bls2-agent service started successfully."
}

# 安装函数
install() {
    log "Starting installation..."

    # 安装 unzip
    apt update && apt-get install -y unzip
    if [ $? -ne 0 ]; then
        log "Failed to install unzip."
        exit 1
    fi

    # 创建 /go 目录
    mkdir -p /go
    if [ $? -ne 0 ]; then
        log "Failed to create directory /go."
        exit 1
    fi

    # 下载 bls2-agent 可执行文件
    log "Downloading bls2-agent binary..."
    wget -O /go/bls2-agent.zip http://cdn.qingbaijiang.top:61880/bls2/bls2-agent.zip
    if [ $? -ne 0 ]; then
        log "Failed to download bls2-agent binary."
        exit 1
    fi

    # 解压
    unzip -o /go/bls2-agent.zip -d /go
    if [ $? -ne 0 ]; then
        log "Failed to unzip bls2-agent binary."
        exit 1
    fi

    # 删除压缩包
    rm /go/bls2-agent.zip

    # 修改为可执行
    chmod +x /go/bls2-agent
    if [ $? -ne 0 ]; then
        log "Failed to make bls2-agent executable."
        exit 1
    fi

    log "bls2-agent binary downloaded and made executable."

    # 读取环境变量写入.env文件
    # 用户新增自有服务器使用
    echo "AGENT_USER_ID=$USER_ID" >> /go/.env 
    # 开云机器使用
    echo "AGENT_SOURCE_TYPE=$SOURCE_TYPE" >> /go/.env
    # 本地资产使用
    echo "AGENT_LOCAL_ASSET_ID=$LOCAL_ASSET_ID" >> /go/.env

    log "Environment variables written to .env file."

    # 检查服务是否已存在
    if is_service_exist; then
        log "bls2-agent service already exists. Restarting..."
        restart_service
    else
        log "bls2-agent service does not exist. Creating and starting..."
        auto_start
    fi
}

# 主函数
main() {
    install
}

# 执行主函数
main

文档版本:v1.0
最后更新:2025年08月
维护人员:系统管理团队