blob: 9eedac6f30bdd737f956522a2dad1a2d7414a699 [file] [log] [blame]
#!/bin/sh
. /etc/utils.sh
. /etc/chrome_utils.sh
DELAY="$1" # if empty, run only once
printstat()
{
local result="$1"
shift
"$@" | sort -rn | head -n 5 | (
while read val pid comm arg0 arg1 rest; do
[ "$comm" = "python" ] && comm=${arg1##/*/} # use the .py script name
[ "$comm" = "browser_shell" ] && \
comm=$(format_chrome_comm "$pid" "$arg1 $rest")
result="$result $comm($val)"
done;
echo "$result"
)
}
freemem()
{
free | (
read header
read h1 total junk
read h2a h2b real_used real_free junk
echo "$1 total=$total used=$real_used free=$real_free"
)
}
buddyinfo()
{
while read junk1 junk2 junk3 label data; do
echo "$1 $label: $data"
done </proc/buddyinfo
}
dfspace()
{
df -k | {
out=
read header
while read dev blocks used avail percent fs junk; do
if [ "$used" != 0 ]; then
out="$out$fs:$used "
fi
done
echo "$1 $out"
}
}
carriers()
{
ip link show | {
out=
while IFS=': ' read n ifc flags smtu mtu sdisc disc smaster master sstate state junk; do
if [ "$smtu $sdisc $smaster" = "mtu qdisc state" ]; then
out="$out$ifc($master) "
elif [ "$smtu $sdisc $sstate" = "mtu qdisc state" ]; then
out="$out$ifc($state) "
fi
done
echo "$1 $out"
}
}
fdcount()
{
local testopt="$2"
ps ax -o pid=,comm= |
while read pid comm junk; do
for d in /proc/$pid/fd/*; do
[ "$testopt" "$d" ] && echo "$comm"
done
done |
sort |
uniq -c |
sort -nr |
head -n10 | {
out=
while read count comm junk; do
out="$out$comm($count) "
done
echo "$1 $out"
}
}
ip_only()
{
while read ip junk; do
contains "$ip" ":" || echo "$ip" "$junk"
done
}
ip6_only()
{
while read ip junk; do
contains "$ip" ":" && echo "$ip" "$junk"
done
}
nameservers()
{
grep '^nameserver' /etc/resolv.conf | while read ns ip junk; do
echo "$ip"
done
}
defaultroutes()
{
ip "$@" route list | while read a b IP c IF d; do
if [ "$a $b" = "default via" ]; then
if startswith "$IP" "fe80:"; then
# IPV6 link-local address, return IP and interface
echo "$IP,$IF"
else
echo "$IP"
fi
fi
done
}
do_ping()
{
VER=$1
IP=$2
shift 2
ping$VER -n -c1 -w5 "$@" $IP 2>&1 | {
while read nbytes bytes from ip serial ttl time ms; do
if [ "$bytes $from $ms" = "bytes from ms" ]; then
echo "${time#time=}ms/${ttl#ttl=}ttl"
exit
fi
done
echo "ERR"
}
}
pings()
{
out=
for ip in \
127.0.0.1 \
$(defaultroutes) \
$(nameservers | ip_only) \
gstatic.com
do
out="$out$ip($(do_ping "" $ip)) "
done
echo "$1 $out"
}
pings6()
{
out=
for ip_if in \
::1 \
$(defaultroutes -6) \
$(nameservers | ip6_only) \
gstatic.com
do
out=$out$(echo "$ip_if" | while IFS=, read ip if; do
if [ -n "$if" ]; then
echo "$ip($(do_ping "6" $ip -I $if)) "
else
echo "$ip($(do_ping "6" $ip)) "
fi
done)
done
echo "$1 $out"
}
# integration tests can set different files to read from
extrapingfile()
{
VER="$1"
if [ "$VER" = "6" ]; then
filename=${EXTRAPING6FILE:-"/tmp/extra_ping6_servers"}
else
filename=${EXTRAPINGFILE:-"/tmp/extra_ping_servers"}
fi
echo $filename
}
extrapings()
{
LOG="$1"
VER="$2"
filename=$(extrapingfile "$VER")
if [ -f "$filename" ]; then
out=
for ip in $(cat "$filename" | sed -e 's/,/ /g'); do
out="$out$ip($(do_ping "$VER" $ip)) "
done
echo "$LOG $out"
fi
}
get_list_length()
{
filename="$1"
len=0
for x in $(cat "$filename" 2>/dev/null | sed -e 's/,/ /g'); do
len=$((len + 1))
done
echo $len
}
select_by_index()
{
filename="$1"
index="$2"
i=0
for x in $(cat "$filename" 2>/dev/null | sed -e 's/,/ /g'); do
if [ "$i" -eq "$index" ]; then
echo "$x"
return
fi
i=$((i + 1))
done
}
# Traceroute to one destination in the extra ping servers
extratraceroute()
{
LOG="$1"
VER="$2"
TOTAL="$3"
filename=$(extrapingfile "$VER")
if [ "$VER" = "6" ]; then
ipver="-6"
else
ipver="-4"
fi
len=$(get_list_length "$filename")
if [ "$len" -gt 0 ]; then
index=$((TOTAL % len))
site=$(select_by_index "$filename" "$index")
echo -n "$LOG $site"
traceroute -n -N 1 -q 1 -w 1 "$ipver" "$site" | tail -n +2 | while read num ip tim unit; do
echo -n " $ip($tim$unit)"
done
echo
fi
}
gstatics()
{
gs=$(gstatic)
rv=$?
if [ -n "$gs" ]; then
echo "$gs" | while read ip stat; do
echo -n "$ip($stat) "
done
fi
echo
return $rv
}
wifistats()
{
local phy="$1"
local queuefile="/sys/kernel/debug/ieee80211/$phy/ath9k/queues"
if [ -e "$queuefile" ]; then
while read line; do
echo "$phy: ath9k-queue: $line"
done <$queuefile
fi
}
if [ -n "$DELAY" ]; then
# Stagger startup after a power outage
sleep $(randint 1 "$DELAY")
fi
# Setting LONGPING in the environment allows test cases to be constructed
rand=$(randint 1 3600)
longping=${LONGPING:-$rand}
numtraceroutes=0
while :; do
# time used by each process, followed by cumulative time (which includes
# children, including already terminated ones). Note that the 'cputime'
# format doesn't work with --cumulative, so we need to use bsdtime instead.
printstat "cputop:" ps --no-headers -e -o bsdtime,pid,comm,cmd
printstat "ccputop:" ps --no-headers --cumulative -e -o bsdtime,pid,comm,cmd
printstat "memtop:" ps --no-headers -e -o rss,pid,comm,cmd
echo "loadavg: $(cat /proc/loadavg)"
freemem "mem:"
buddyinfo "buddyinfo:"
dfspace "df:"
carriers "carriers:"
fdcount "fdcount:" -e
fdcount "sockcount:" -S
for phy in /sys/kernel/debug/ieee80211/phy*; do
wifistats "$(basename "$phy")"
done
# these can cause network activity, so /tmp/disable_pings is a way to
# stop it if it's getting annoying or causing trouble.
if [ ! -e /tmp/disable_pings ]; then
pings "ping:" "$gs_retval"
pings6 "ping6:" "$gs_retval"
if [ -z "$DELAY" ] || [ "$longping" -gt 3600 ]; then
if is-network-box || is-storage-box; then
extrapings "ping:" ""
extrapings "ping6:" "6"
echo "gstatic: $(gstatics)"
extratraceroute "traceroute:" "" "$numtraceroutes"
extratraceroute "traceroute6:" "6" "$numtraceroutes"
numtraceroutes=$((numtraceroutes + 1))
fi
longping=0
fi
if is-network-box || is-storage-box; then
echo "dns: $(dnsck 8.8.8.8 8.8.4.4 2001:4860:4860::8888 2001:4860:4860::8844)"
else
echo "dns: $(dnsck)"
fi
fi
if runnable castcheck; then
echo "castcheck: $(castcheck)"
fi
if runnable dialcheck; then
echo "dialcheck: $(dialcheck)"
fi
# Trigger nexus heap stats, if available
if [ -e /proc/bcmdriver/debug ]; then
echo core >/proc/bcmdriver/debug
fi
if [ -n "$DELAY" ]; then
sleep "$DELAY" || exit 1
longping=$((longping + DELAY))
else
break
fi
done