<commit_before>#!/bin/sh
# fnetstat by RubenKelevra 2013 - <EMAIL>
# Lizenz: AGPL 3.0 

#Warning: This script will immediately set the ssid configured in uci if its changed.

#This script expect that an temporary SSID is set on boot. The Script will read ssid_online and set it when node is online.

#Options
SLEEP=2 # wait time in seconds before rechecking
ACTIVE_CHECK=1 # do pinging selected gateway via L2-ping, if last-seen is to high
SSID_PHY0="wireless.wifi2.ssid_online"
SSID_PHY0_BOOT="wireless.wifi2.ssid"
HOSTAPD_PHY0="/var/run/hostapd-phy0.conf"
SSID_PHY1="wireless.wifi5.ssid_online" #if phy1.conf is not found, this will be not used
SSID_PHY1_BOOT="wireless.wifi5.ssid"
HOSTAPD_PHY1="/var/run/hostapd-phy1.conf" 
PING_CNT=3 #Ping-Packets for ACTIVE_CHECK: 3 is recommented, 5-10 for lossy connections
OGM_INT_MULT=2 #Tolerable missing OGMs, before pinging (if ACTIVE_CHECK=1): 1 for fast responsive, 2 for slower reactions, 3-4 for lossy connections
LED_STATUS=1

#vars
RUNTIME=60 #default runtime in seconds
MODE=1
END=0
GWQ=0
GWM=""
GWLS=0
GWL=0
OFFLINE=0
ISOFFLINE=0
RADIOONE=0
SSID_0=""
SSID_1=""
SSID_0_BOOT=""
SSID_0_ONLINE=""
SSID_0_OFFLINE=""
SSID_1_BOOT=""
SSID_1_ONLINE=""
SSID_1_OFFLINE=""
DEVICE=`cat /proc/sys/kernel/hostname`
if [ ${#DEVICE} -gt 16 ]; then #cut device-name
	DEVICE="${DEVICE:0:13}..."
fi
CHANGED=0
FORCE_CHANGE=0

#checking batctl
if [ $ACTIVE_CHECK -eq 1 ]; then
	batctl -v >/dev/null 2>&1 || { echo >&2 "batctl is required for Active-Checking, but it's not installed.  Aborting."; exit 1; }
fi

#Parameter Checking
if ! [ $# -eq 1 -o "$1" -eq "$1" ] 2>/dev/null; then
	echo "Error: Please define a runtime in seconds" && exit 2 
else
	RUNTIME=$1
	echo "Debug: Runtime will be $RUNTIME seconds, please restart me after this time"
fi

#Checking Files and Options
if [ ! -f $HOSTAPD_PHY0 ]; then 
	echo "Error: PHY0 Hostapd-File not found" && exit 2

elif [ "$(uci get $SSID_PHY0_BOOT 2>/dev/null)" == "" ]; then
	echo "Error: UCI can't find Boot-SSID for Radio0 Append them with:
uci set $SSID_PHY0_BOOT=x
uci commit" && exit 2

elif [ "$(uci get $SSID_PHY0 2>/dev/null)" == "" ]; then
	echo "Error: UCI can't find SSID for Radio0. Append them with:
uci set $SSID_PHY0=x
uci commit" && exit 2

elif [ -f $HOSTAPD_PHY1 -a "$(uci get $SSID_PHY1_BOOT 2>/dev/null)" == "" ]; then
	echo "Error: UCI can't find Boot-SSID for Radio1 Append them with:
uci set $SSID_PHY1_BOOT=x
uci commit" && exit 2

elif [ -f $HOSTAPD_PHY1 -a "$(uci get $SSID_PHY1 2>/dev/null)" == "" ]; then
	echo "Error: UCI can't find SSID for Radio1. Append them with:
uci set $SSID_PHY1=x
uci commit" && exit 2

elif ! [ -f /etc/diag.sh ]; then
	if [ $LED_STATUS -eq 1 ]; then
		echo "Error: /etc/diag.sh not found." && exit 2
	fi
fi

if [ $LED_STATUS -eq 1 ]; then
	. /etc/diag.sh
	get_status_led
fi

START=`cat /proc/uptime | cut -d"." -f1`
END=$(( $START + $RUNTIME ))
END=$(( $END - 1 ))

while [ `cat /proc/uptime | cut -d"." -f1` -lt $END ]
do
	case $MODE in
	1) #check: batman knows an gateway
		GWQ=`cat /sys/kernel/debug/batman_adv/bat0/gateways | egrep ' \([\ 0-9]+\) ' | cut -d\( -f2 | cut -d\) -f1 | sort -n | tail -n1`
		if ! [ "$GWQ" -eq "$GWQ" ] 2>/dev/null; then
			GWQ=0
		fi
		echo -n "Debug: Gateway-Quality is $GWQ"
		if [ $GWQ -lt 10 ]; then
			echo " - this is not okay, we're offline"
			OFFLINE=1
			MODE=4
		else
			echo " - this seem fine"
			OFFLINE=0
			MODE=2
			continue
		fi
		;;
	2) #check: Gateways Last-Seen
		GWM=`cat /sys/kernel/debug/batman_adv/bat0/gateways | grep "^=>" | cut -d" " -f2`
		if [ "$GWM" == "" ]; then
			GWM="00:00:00:00:00"
		fi
		GWLS=`cat /sys/kernel/debug/batman_adv/bat0/originators | grep "^$GWM" | cut -d" " -f3-5 | cut -d"s" -f1 | cut -d"." -f1`
		if ! [ "$GWLS" -eq "$GWLS" ] 2>/dev/null; then
			GWLS=65535
		fi<commit_msg>add capability of being interrupted by next instance<commit_after>#!/bin/sh
# fnetstat by RubenKelevra 2013 - <EMAIL>
# Lizenz: AGPL 3.0 

#Warning: This script will immediately set the ssid configured in uci if its changed.

#This script expect that an temporary SSID is set on boot. The Script will read ssid_online and set it when node is online.

RUNTIME=60 #default runtime in seconds

#Parameter Checking
if ! [ $# -eq 1 -o "$1" -eq "$1" ] 2>/dev/null; then
	echo "Error: Please define a runtime in seconds" && exit 2 
else
	RUNTIME=$1
	echo "Debug: Runtime will be $RUNTIME seconds, please restart me after this time"
fi

START=`cat /proc/uptime | cut -d"." -f1`
END=$(( $START + $RUNTIME ))
END=$(( $END - 1 ))

#running-pid
OWNPID=$$
PIDFILE='/tmp/ff-offline-ssid.pid'

if [ ! -f "$PIDFILE" -a -f "$PIDFILE"_"STOP"]; then
	sleep 1 #fix possible race-conditions
fi

if [ ! -f "$PIDFILE" ]; then
	echo $OWNPID > $PIDFILE
else
	#If already a pid and a stopfile exists, were exiting now.
	if ! kill -0 $(cat "$PIDFILE") > /dev/null 2>&1; then
		echo $OWNPID > $PIDFILE
		[ -f "$PIDFILE"_"STOP" ] && rm "$PIDFILE"_"STOP" #cleanup
	else
		[ -f "$PIDFILE"_"STOP" ] && logger "Warning: offline-ssid exiting because pid-file (pid running!) and stopfile already exist."; exit 1
		touch "$PIDFILE"_"STOP"
		while [ `cat /proc/uptime | cut -d"." -f1` -lt $END ]; do
			[ ! -f $PIDFILE ] && echo $OWNPID > $PIDFILE; rm "$PIDFILE"_"STOP"; break
			sleep 1
		done
	fi

#Options
SLEEP=2 # wait time in seconds before rechecking
ACTIVE_CHECK=1 # do pinging selected gateway via L2-ping, if last-seen is to high
SSID_PHY0="wireless.wifi2.ssid_online"
SSID_PHY0_BOOT="wireless.wifi2.ssid"
HOSTAPD_PHY0="/var/run/hostapd-phy0.conf"
SSID_PHY1="wireless.wifi5.ssid_online" #if phy1.conf is not found, this will be not used
SSID_PHY1_BOOT="wireless.wifi5.ssid"
HOSTAPD_PHY1="/var/run/hostapd-phy1.conf" 
PING_CNT=3 #Ping-Packets for ACTIVE_CHECK: 3 is recommented, 5-10 for lossy connections
OGM_INT_MULT=2 #Tolerable missing OGMs, before pinging (if ACTIVE_CHECK=1): 1 for fast responsive, 2 for slower reactions, 3-4 for lossy connections
LED_STATUS=1

#vars
MODE=1
END=0
GWQ=0
GWM=""
GWLS=0
GWL=0
OFFLINE=0
ISOFFLINE=0
RADIOONE=0
SSID_0=""
SSID_1=""
SSID_0_BOOT=""
SSID_0_ONLINE=""
SSID_0_OFFLINE=""
SSID_1_BOOT=""
SSID_1_ONLINE=""
SSID_1_OFFLINE=""
CHANGED=0
FORCE_CHANGE=0

#checking batctl
if [ $ACTIVE_CHECK -eq 1 ]; then
	batctl -v >/dev/null 2>&1 || { echo >&2 "batctl is required for Active-Checking, but it's not installed.  Aborting."; exit 1; }
fi



#Checking Files and Options
if [ ! -f $HOSTAPD_PHY0 ]; then 
	echo "Error: PHY0 Hostapd-File not found" && exit 2

elif [ "$(uci get $SSID_PHY0_BOOT 2>/dev/null)" == "" ]; then
	echo "Error: UCI can't find Boot-SSID for Radio0 Append them with:
uci set $SSID_PHY0_BOOT=x
uci commit" && exit 2

elif [ "$(uci get $SSID_PHY0 2>/dev/null)" == "" ]; then
	echo "Error: UCI can't find SSID for Radio0. Append them with:
uci set $SSID_PHY0=x
uci commit" && exit 2

elif [ -f $HOSTAPD_PHY1 -a "$(uci get $SSID_PHY1_BOOT 2>/dev/null)" == "" ]; then
	echo "Error: UCI can't find Boot-SSID for Radio1 Append them with:
uci set $SSID_PHY1_BOOT=x
uci commit" && exit 2

elif [ -f $HOSTAPD_PHY1 -a "$(uci get $SSID_PHY1 2>/dev/null)" == "" ]; then
	echo "Error: UCI can't find SSID for Radio1. Append them with:
uci set $SSID_PHY1=x
uci commit" && exit 2

elif ! [ -f /etc/diag.sh ]; then
	if [ $LED_STATUS -eq 1 ]; then
		echo "Error: /etc/diag.sh not found." && exit 2
	fi
fi

if [ $LED_STATUS -eq 1 ]; then
	. /etc/diag.sh
	get_status_led
fi

DEVICE=`cat /proc/sys/kernel/hostname`
if [ ${#DEVICE} -gt 16 ]; then #cut device-name
	DEVICE="${DEVICE:0:13}..."
fi

while [ `cat /proc/uptime | cut -d"." -f1` -lt $END ]; do
	[ -f "$PIDFILE"_"STOP" ] && echo "were exiting now, because were requested to."; rm $PIDFILE; exit 42
	case $MODE in
	1) #check: batman knows an gateway
		GWQ=`cat /sys/kernel/debug/batman_adv/bat0/gateways | egrep ' \([\ 0-9]+\) ' | cut -d\( -f2 | cut -d\) -f1 | sort -n | tail -n1`
		if ! [ "$GWQ" -eq "$GWQ" ] 2>/dev/null; then
			GWQ=0
		fi
		echo -n "Debug: Gateway-Quality is $GWQ"
		if [ $GWQ -lt 10 ]; then
			echo " - this is not okay, we're offline"
			OFFLINE=1
			MODE=4
		else
			echo " - this seem fine"
			OFFLINE=0
			MODE=2
			continue
		fi
		;;
	2) #check: Gateways Last-Seen
		GWM=`cat /sys/kernel/debug/batman_adv/bat0/gateways | grep "^=>" | cut -d" " -f2`
		if [ "$GWM" == "" ]; then
			GWM="00:00:00:00:00"
		fi
		GWLS=`cat /sys/kernel/debug/batman_adv/bat0/originators | grep "^$GWM" | cut -d" " -f3-5 | cut -d"s" -f1 | cut -d"." -f1`
		if ! [ "$GWLS" -eq "$GWLS" ] 2>/dev/null; then
			GWLS=65535
		fi
{'fraction_non_alphanumeric': 0.15275813295615276, 'fraction_numerical': 0.03406412069778406, 'mean_word_length': 3.667216721672167, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1772135', 'n_tokens_mistral': 3728, 'n_tokens_neox': 3421, 'n_words': 1057}