small-package/luci-app-netspeedtest/root/etc/init.d/netspeedtest

92 lines
2.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh /etc/rc.common
#
# Copyright (C) 2020-2023 sirpdboy <herboy2008@gmail.com> https://github.com/sirpdboy/netspeedtest
# This is free software, licensed under the Apache License, Version 2.0 .
#
START=99
USE_PROCD=1
PROG=/usr/bin/homebox
EXTRA_COMMANDS="nstest"
TMP_T=/var/netspeedtest_nstest.tmp
BINSPEEDTEST='/usr/bin/speedtest'
LOCK=/var/lock/netspeedtest_nstest.lock
LOG=/var/log/netspeedtest.log
MAX_LOG=500
limit_log() {
local logf=$1
local max=$2
[ ! -f "$logf" ] && return
sc=${max:-$MAX_LOG}
local count=$(grep -c "" $logf)
if [ $count -gt $sc ];then
let count=count-$sc
sed -i "1,$count d" $logf
fi
}
init_log() {
local logf=$1
[ ! -f "$logf" ] && echo "" > $logf
}
echone() {
echo -ne $* >> $LOG
}
nstest() {
[ -f $LOCK ] && exit
limit_log $LOG 500
init_log $LOG
touch $LOCK
echo -ne "\n ——————————speedtest测速"—————————— >> $LOG
/usr/bin/speedtest --share > $TMP_T
echo -ne "\n 测服信息:$(cat $TMP_T | grep 'Hosted by'| cut -c10- | awk -F: '{printf $1}') 延时:$(cat $TMP_T | grep 'Hosted by' | awk -F: '{printf $2}')" >> $LOG
echo -ne "\n 下行速率:$(cat $TMP_T | grep 'Download:' |awk -F: '{printf $2}' )" >> $LOG
echo -ne " ---- 上行速率:$(cat $TMP_T | grep 'Upload:' |awk -F: '{printf $2}' )" >> $LOG
echo -ne "\n 测试结果图片链接:$(cat $TMP_T | grep 'results:' | cut -c16- ) \n" >> $LOG
echo -ne "\n 测试时间:`date +%Y-%m-%d" "%H:%M:%S` \n" >> $LOG
echo -ne " ——————————————————————————————\n" >> $LOG
echo -ne $(cat $TMP_T | grep 'results:' | cut -c16- )
rm -f $LOCK
}
tcptest(){
NDATA=`date +%Y-%m-%d' '%H:%M:%S`
echo -ne "\n $NDATA 服务器:$1 ---- 端口:$2 ---- TCP延时$3 Ms \n" >> $LOG
}
get_config() {
config_get_bool enabled $1 enabled 1
config_get_bool logger $1 logger 1
}
homebox_prepare() {
pgrep -f homebox | xargs kill -9 >/dev/null 2>&1
killall homebox
killall homebox
}
start_service() {
config_load netspeedtest
config_foreach get_config netspeedtest
[ "x$enabled" != "x1" ] && {
homebox_prepare
exit
}
procd_open_instance
procd_set_param command $PROG
[ "x$logger" == x1 ] && procd_set_param stderr 1
# procd_set_param respawn
procd_close_instance
}
service_triggers() {
procd_add_reload_trigger "netspeedtest"
}