#!/bin/sh # Version of the script SCRIPT_VERSION=1.3 # Output directory for the result of the script OUTPUT_DIR="`dirname $0`" # Execution's environment of the script SCRIPT_ENV=unknown # PLATFORM: IBTS, IFEMTO, ISTATION, IFEVO PLATFORM=unknown # Serial number of the product: 39020003 SERIAL=unknown # ETH0 MAC: 70:76:ff:01:77:71 => 7771 SHORT_MAC=unknown # LED paths SYSLEDPATH_4_0_IBTS="/sys/class/leds/yellow:debug" SYSLEDPATH_4_0_IFEMTO="/sys/class/leds/led1:red:power" SYSLEDPATH="/sys/class/leds/status" LED_PATH="/do/not/exist" # Script arguments LEDS_MANAGEMENT=true EXEC_COMMANDS=true # # Print the version of the script # show_version() { echo "Script version: ${SCRIPT_VERSION}" } # # Show help # show_help() { show_version echo -e "Usage: $0 [-h|--help] [-l|--ignore-leds] [-o|--output-dir ] \t-h|--help: this help \t-v|--version: version of the script \t-l|--ignore-leds: this script does not manage the leds (default: autodetection) \t-o|--output-dir : force the output to use for extraction (default: current directory) \t-c|--disable-commands: disable diagnosis commands (default: executed) " } # # Compare 2 versions to know if inferior or superior # Res: 0: $1 = $2 # 1: $1 > $2 # 2: $1 < $2 # compare_versions () { if [[ $1 == $2 ]] then return 0 fi local IFS=. local i ver1=($1) ver2=($2) # fill empty fields in ver1 with zeros for ((i=${#ver1[@]}; i<${#ver2[@]}; i++)) do ver1[i]=0 done for ((i=0; i<${#ver1[@]}; i++)) do if [[ -z ${ver2[i]} ]] then # fill empty fields in ver2 with zeros ver2[i]=0 fi if ((10#${ver1[i]} > 10#${ver2[i]})) then return 1 fi if ((10#${ver1[i]} < 10#${ver2[i]})) then return 2 fi done return 0 } # # Copy a file or directory if exists # Arguments: # $1: file/directory to copy # $2: destination directory (create if necessary) # copy_if_exists(){ local from="$1" local to="$2" if [ -e "${from}" ] ; then [ ! -d "${to}" ] && mkdir -p "${to}" cp -a "${from}" "${to}" fi } # # Check KerOS/SPN version and stop if: # - KerOS < 4.0 # - SPN < 2.1 # check_fw_version(){ # check if /etc/version exists if [ -a /etc/version ]; then FW_VERSION=$(cat /etc/version) # Detect type of firmware: KerOS or SPN if [[ "${FW_VERSION}" == *-spn-* ]]; then # Check if SPN version is not inferior to 2.1, stop if it is the case compare_versions ${FW_VERSION:0:3} "2.1" if [ $? -eq 2 ]; then echo "Your SPN firmware version ($FW_VERSION) is inferior to v2.1. Please use the adequate script." >> ${OUTPUT_DIR}/WRONG_FIRMWARE_VERSION.txt exit fi else # Check if KerOS version is not inferior to 4.0, stop if it is the case compare_versions ${FW_VERSION:0:3} "4.0" if [ $? -eq 2 ]; then echo "Your KerOS firmware version ($FW_VERSION) is inferior to v4. Please use the adequate script." >> ${OUTPUT_DIR}/WRONG_FIRMWARE_VERSION.txt exit fi fi else echo "Could not find /etc/version" >> ${OUTPUT_DIR}/WRONG_FIRMWARE_VERSION.txt exit fi } # # Check platform we are running on # detect_platform() { PLATFORM="$(fslotpreg getplatform)" if [ $? -ne 0 ]; then PLATFORM="NOT_SUPPORTED" fi echo "Platform detected: ${PLATFORM}" } # # Get product serial number information: SERIAL and SHORT_MAC # fill_serials() { local reg=$(fslotpreg read GP1) if [ $? -eq 0 ]; then SERIAL=$(printf "%X\n" $reg) else SERIAL="unknown" fi SHORT_MAC=$(cat /sys/class/net/eth0/address | awk -F: '{print $5$6}') } # # Detect the type of environment where the script is launched. # Possible environments: # - udev # - shell # detect_env() { # Detection is only based on the presence or not of the variable DEVNAME if [ ! -z ${DEVNAME} ] ; then SCRIPT_ENV=udev exec > /dev/console 2>&1 # Output the script execution in debug console else SCRIPT_ENV=shell LEDS_MANAGEMENT=false OUTPUT_DIR=$(pwd) fi echo "Script environment: ${SCRIPT_ENV}" } # # Detect type of leds depending of platform/firmware version # detect_leds() { if [ -e "${SYSLEDPATH}" ]; then LED_PATH="${SYSLEDPATH}" elif [ -e "${SYSLEDPATH_4_0_IBTS}" ]; then LED_PATH="${SYSLEDPATH_4_0_IBTS}" elif [ -e "${SYSLEDPATH_4_0_IFEMTO}" ]; then LED_PATH="${SYSLEDPATH_4_0_IFEMTO}" fi } # # Set default led behavior # $1: trigger # # if trigger = timer : $2 = delay_on, $3 = delay_off # set_led() { trigger="${1}" on="$2" off="$3" if [ -e "${LED_PATH}" ]; then echo $trigger > "${LED_PATH}/trigger" case "$trigger" in "timer") echo ${on:=500} > "${LED_PATH}/delay_on" echo ${off:=500} > "${LED_PATH}/delay_off" ;; "none") echo ${on:=0} > "${LED_PATH}/brightness" ;; *) # Nothing to do ;; esac fi } get_nmea_frames() { gps_tty="$1" if ! [ -e "$gps_tty" ]; then echo "$gps_tty doesn't exist" return 1 fi if [ -n "$(lsof | grep $(readlink $gps_tty))" ]; then echo "$gps_tty already used by $(lsof | grep $(readlink $gps_tty))" return 2 else # Pseudo-term keep only FIRST frames so we keep last of these old frames and get few new frames # It will take 3 seconds timeout -t 3 cat $gps_tty > /tmp/nmea.log cat /tmp/nmea.log fi } # # Execute some diagnosis commands (can take a long time) # execute_commands() { echo "Executing diagnosis commands (can take a long time)" echo -e "\tSystems commands" ( echo -e "\n\n\n************** fslotpreg getplatform *****************" fslotpreg getplatform echo -e "\n\n\n************** fslotpreg fslotpreg read GP1 *****************" fslotpreg read GP1 echo -e "\n\n\n************** date *****************" date echo -e "\n\n\n************** uptime *****************" uptime echo -e "\n\n\n************** ps ww *****************" ps ww echo -e "\n\n\n************** opkg status *****************" opkg status echo -e "\n\n\n************** monit status *****************" monit status echo -e "\n\n\n************** mount *****************" mount ) >> ${tmpdir}/cmd_results.txt echo -e "\tNetwork commands" ( echo -e "\n\n\n************** ifconfig -a *****************" ifconfig -a echo -e "\n\n\n************** gsmdiag.py *****************" gsmdiag.py ; cat /tmp/gsmdiag.txt echo -e "\n\n\n************** route *****************" route -n echo -e "\n\n\n************** iptables -L *****************" iptables -L echo -e "\n\n\n************** connmanctl technologies *****************" connmanctl technologies echo -e "\n\n\n************** services *****************" connmanctl services if [ -e /dev/cdc-wdm1 ]; then echo -e "\n\n\n************** qmicli get mode *****************" qmicli -d /dev/cdc-wdm1 --nas-get-system-selection-preference fi ) >> ${tmpdir}/network_result.txt get_nmea_frames /dev/nmea3 > ${tmpdir}/nmea.log [ $? -ne 0 ] && get_nmea_frames /dev/nmea2 >> ${tmpdir}/nmea.log # Overlay modifications echo -e "\tOverlay commands" overlay diff -l /.rootfs.ro -u /user/.rootfs_upper/ >> ${tmpdir}/overlay_status.txt << EOF yes EOF echo "End of diagnosis commands" } # # Get all the logs, configuration files and commands and save it # in the resquested output directory. # Arguments: # $1: output directory # get_logs() { local output_dir="$1" local name="Logs_${SERIAL}_${SHORT_MAC}_$(date "+%Y%m%d-%H%M%S")" local tmpdir="/user/${name}" # tmp files echo "Extracting tmp files" copy_if_exists /tmp/sys_startup_status.json ${tmpdir}/tmp/ copy_if_exists /tmp/board_info.json ${tmpdir}/tmp/ copy_if_exists /tmp/calib_loraloc.json ${tmpdir}/tmp/ copy_if_exists /tmp/calib_rf.json ${tmpdir}/tmp/ # network configuration echo "Extracting network configuration" copy_if_exists /etc/network/ ${tmpdir}/etc/ copy_if_exists /etc/firewall.d/ ${tmpdir}/etc/ # CPF (lorad + lorafwd) configuration echo "Extracting CPF configuration (if any)" for lorad_dir in /user/etc/lorad /etc/lorad do if [ -d ${lorad_dir} ] ; then echo -e "\tExtracting lorad configuration" copy_if_exists /etc/default/lorad ${tmpdir}/etc/default/ copy_if_exists ${lorad_dir} ${tmpdir}${lorad_dir%/*} fi done for lorafwd_dir in /user/etc/lorafwd /etc/lorafwd do if [ -d ${lorafwd_dir} ] ; then echo -e "\tExtracting lorafwd configuration" copy_if_exists /etc/default/lorafwd ${tmpdir}/etc/default/ copy_if_exists ${lorafwd_dir} ${tmpdir}${lorafwd_dir%/*} fi done # WMC configuration and logs echo "Extracting WMC configuration and logs (if any)" if [ -d /user/bscc ] ; then echo -e "\tExtracting BSCC configuration and logs" copy_if_exists /user/bscc/var.xml ${tmpdir}/user/bscc/ copy_if_exists /user/bscc/traces/ ${tmpdir}/user/bscc/ fi if [ -d /user/snmp ] ; then echo -e "\tExtracting SNMP configuration and logs" copy_if_exists /user/snmp/snmpd.conf ${tmpdir}/user/snmp/ copy_if_exists /user/snmp/traces/ ${tmpdir}/user/snmp/ fi # miscellaneous configuration and logs echo "Extracting miscellaneous configuration and logs" copy_if_exists /etc/rcU.d/ ${tmpdir}/etc/ copy_if_exists /.update/update.log ${tmpdir}/ copy_if_exists /user/.update ${tmpdir}/user/ copy_if_exists /user/.upgrade.log ${tmpdir}/user/ copy_if_exists /user/.sysupgrade.log ${tmpdir}/user/ copy_if_exists /etc/version ${tmpdir}/etc/ copy_if_exists /etc/sysupgrade.conf ${tmpdir}/etc/ copy_if_exists /etc/sysupgrade.d/ ${tmpdir}/etc/ copy_if_exists /etc/.squash_layer_version ${tmpdir}/etc/ echo ${SCRIPT_VERSION} > ${tmpdir}/script_version if ${EXEC_COMMANDS} ; then execute_commands fi # Finish by system logs to prevent space errors echo "Extracting system logs" copy_if_exists /var/log/ ${tmpdir}/var/ echo "Saving all data in ${output_dir}/${name}.tar.gz" tar -C /user -czf ${output_dir}/${name}.tar.gz ${name} echo "Done" sync rm -rf $tmpdir } ##################### # MAIN ##################### # Detect the environment of execution of the script detect_env # Manage scripts arguments while [[ $# -gt 0 ]] do key="$1" case $key in -l|--ignore-leds) LEDS_MANAGEMENT=false shift ;; -o|--output-dir) OUTPUT_DIR="$2" shift 2 ;; -c|--disable-commands) EXEC_COMMANDS=false shift ;; -h|--help) show_help shift exit 1 ;; -v|--version) show_version shift exit 1 ;; *) shift ;; esac done # Check if firmware version is compatible check_fw_version # Detect type of platform detect_platform # Fill SERIAL and SHORT_MAC variables fill_serials # Set led On if ${LEDS_MANAGEMENT} ; then detect_leds set_led none 1 fi # Get logs get_logs ${OUTPUT_DIR} # End of script, manage user's notifications if ${LEDS_MANAGEMENT} ; then # Prepare reboot after USB unplug cat > /tmp/usbkey_remove << EOF # Switch off Led echo "none" > ${LED_PATH}/trigger EOF # All is right, make Led blinking fast to notify User set_led timer 100 100 fi