Hopefully this is OK to post, but I’ve been tinkering with the idea of a script that could pull a bunch of information from an Alta Labs device, kinda like a support tool that some other vendors use to gather information to send to support. Although it’s mostly an information and log gathering tool at this point since I’m not much of a scripter!
Figured it was put together enough to post and see if it would help anyone or anyone else felt like tinkering with it. There’s probably some redundent commands or commands that pull excess information at this point but I didn’t feel like removing anything quite yet.
Credit to richb-hanover over here for the original script that I’ve just been tinkering with: GitHub - richb-hanover/OpenWrtScripts: A set of scripts for maintaining and testing OpenWrt
There’s some instructions in the script itself on putting it together, but generally I’ve done the following:
- Open the terminal
- Navigate to the cfg diretory
cd /cfgsince this will allow it to persist through reboots and things - Run
cat > getstats.sh - Paste the contents of the script
- Press
Ctrl + Da couple of times - Run the script with
sh getstats.sh - Then it should spit out the file
/tmp/openwrtstats.txtthat can be further examined
And here’s the contents of the script itself plus an example of some of it’s output
#! /bin/sh
#
# getstats.sh - Collect diagnostic information about OpenWrt
# Write the data to a file (usually /tmp/openwrtstats.txt)
#
# Usage: sh getstats.sh [ "command 1 to be executed" "command 2" "command 3" ... ]
#
# ***** To install and run this script *****
#
# SSH into your router and execute these statements.
#
# ssh root@192.168.1.1
# cd /tmp
# cat > getstats.sh
# [paste in the contents of this file, then hit ^D]
# sh getstats.sh
# The results listed are written to the designated file
# (usually /tmp/openwrtstats.txt, unless redirected)
#
# License: GPL Copyright (c) 2013-2018 Rich Brown
#
# Based on Sebastian Moeller's original set of diagnostic info:
# https://lists.bufferbloat.net/pipermail/cerowrt-devel/2014-April/002871.html
# Based on alexmow's script to list user-installed packages
# https://forum.openwrt.org/t/script-to-list-installed-packages-for-simplifying-sysupgrade/7188/16
# File that will receive command results
out_fqn=/tmp/openwrtstats.txt
# ------- display_command() -------
# Format the command results into the output file
# Redirect both standard out and error out to that file.
display_command() {
echo "[ $1 ]" >> $out_fqn
eval "$1" >> $out_fqn 2>> $out_fqn
echo -e "\n" >> $out_fqn
}
# ------- display_user_packages() ---------
# Display a list of all packages installed after the kernel was built
display_user_packages() {
echo "[ User-installed packages ]" >> $out_fqn
install_time=`opkg status kernel | awk '$1 == "Installed-Time:" { print $2 }'`
opkg status | awk '$1 == "Package:" {package = $2} \
$1 == "Status:" { user_inst = / user/ && / installed/ } \
$1 == "Installed-Time:" && $2 != '$install_time' && user_inst { print package }' | \
sort >> $out_fqn 2>> $out_fqn
echo -e "\n" >> $out_fqn
}
#--------dmesg---------
dmesg_human () {
base=$(cut -d '.' -f1 /proc/uptime);
seconds=$(date +%s);
dmesg | sed 's/\]//;s/\[//;s/\([^.]\)\.\([^ ]*\)\(.*\)/\1\n\3/' |
while read first; do
read second;
first=`date +"%d/%m/%Y %H:%M:%S" --date="@$(($seconds - $base + $first))"`;
printf "[%s] %s\n" "$first" "$second";
done
}
#--------netstat--------
human_netstat() {
awk '
NR % 2 == 1 {
prefix = $1
for (i = 2; i <= NF; i++) header[i] = $i
}
NR % 2 == 0 {
for (i = 2; i <= NF; i++)
printf("%-25s %s\n", prefix " " header[i] ":", $i)
}
' /proc/net/netstat
}
#-------modinfo----------
modinfo_dump() {
for mod in $(lsmod | awk 'NR>1{print $1}'); do
echo "=== $mod ==="
modinfo "$mod"
done
}
#------conntrack---------
conntrack_human() {
conntrack -L -o timestamp -n | awk ' # scale raw packet/byte counts into K/M/G
function human(v) {
if (v >= 1e9) return sprintf("%.2fG", v/1e9)
if (v >= 1e6) return sprintf("%.2fM", v/1e6)
if (v >= 1e3) return sprintf("%.2fK", v/1e3)
return v
}
{
# split each key=value into kv array
for (i=1; i<=NF; i++) {
split($i, tmp, "=")
kv[tmp[1]] = tmp[2]
}
# output aligned columns
printf "%-23s %-5s %-21s -> %-21s %8s pkts %9s bytes\n",
kv["timestamp"], kv["proto"],
kv["src"]":"kv["sport"],
kv["dst"]":"kv["dport"],
human(kv["packets"]), human(kv["bytes"])
delete kv
}'
}
# ------- Main Routine -------
# Examine first argument to see if they're asking for help
if [ "$1" == "-h" ] || [ "$1" == "--help" ]
then
echo 'Usage: sh $0 "command 1 to be executed" "command 2" "command 3" ... '
echo ' '
exit
fi
# Write a heading for the file
echo "===== $0 at `date` =====" > $out_fqn
# Display four sets of commands:
# 1. Common diagnostic commands
# 2. Additional user-supplied commands (from the command line)
# 3. User-installed opkg packages
# 4. Longer/less common diagnostic output
# 1. Display the common diagnostic commands
# These are read from the list delimited by "EOF"
while read LINE; do
display_command "$LINE"
done << EOF
cat /etc/banner
date
cat /etc/openwrt_release
uname -a
uptime
top -b | head -n 20
du -sh / ; du -sh /*
cat /proc/cpuinfo
cat /proc/meminfo
cat /proc/partitions
df -h
mount
EOF
# 2. Extract arguments from the command line and display them.
while [ $# -gt 0 ]
do
display_command "$1"
shift 1
done
# 3. Display user-installed opkg packages
display_user_packages
# 4. Display the long/less frequently-needed commands
while read LINE; do
display_command "$LINE"
done << EOF
cat /etc/config/network
cat /etc/config/dhcp
cat /etc/config/firewall
dmesg_human
cat /proc/net/dev
ip -s link
netstat -tuln
human_netstat
conntrack_human
display_command "lsmod"
display_command "modinfo_dump"
display_command "uci show firewall"
display_command "iptables-save"
display_command "ip6tables-save"
display_command "ip route"
display_command "arp -n"
display_command "ubus call system board"
display_command "ubus call system info"
display_command "ubus call network.interface.lan status"
display_command "uci show network"
display_command "wg show all"
display_command "ps | grep -E 'ipsec|openvpn'"
display_command "cat /var/log/messages | tail -n 100"
EOF
# End the report
echo "===== end of $0 =====" >> $out_fqn
#cat $out_fqn
echo "Done... Diagnostic information written to $out_fqn"
echo " "
# Now press Ctl-D, then type "sh getstats.sh"