Last active
October 10, 2018 17:05
-
-
Save dosaboy/261c52e4d820699d5834b45b5c1fe5d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash -eu | |
| # | |
| # Authors: | |
| # - [email protected] | |
| # - [email protected] | |
| # | |
| # Source: | |
| # https://gist.github.com/dosaboy/261c52e4d820699d5834b45b5c1fe5d2 | |
| # | |
| # Description: | |
| # Look for "cmd" processes and that they are listening on | |
| # the specified port and that they are bound to an ip address | |
| # that is configured on a bridge with format br<num>. This is | |
| # useful for debugging openstack nova-network. | |
| # | |
| # Requires: | |
| # - | |
| # | |
| port=${1:-53} | |
| proto=${2:-udp} | |
| cmd=${3:-dnsmasq} | |
| ip_path=sos_commands/networking/ip_-o_addr | |
| netstat_path=sos_commands/networking/netstat_-W_-neopa | |
| ps_path=sos_commands/process/ps_alxwww | |
| echo "Checking '${cmd}' processes listening on port $port proto $proto" | |
| cleanup() | |
| { | |
| ((${#ftmps[@]})) || return | |
| rm ${ftmps[@]} | |
| } | |
| trap cleanup EXIT | |
| ftmps=() | |
| [ -r "$ip_path" ] || { ip_path=`mktemp`; ip -o addr > $ip_path; ftmps+=($ip_path); } | |
| [ -r "$netstat_path" ] || { netstat_path=`mktemp`; netstat -W -neopa > $netstat_path; ftmps+=($netstat_path); } | |
| [ -r "$ps_path" ] || { ps_path=`mktemp`; ps -alxwww > $ps_path; ftmps+=($ps_path); } | |
| num_procs=`grep ${cmd} $ps_path| awk '$4=="1" {print $3}'| wc -l` | |
| readarray -t bridge_addr<<<"`sed -r 's/.+ (br[0-9]+) .+ inet ([[:digit:]\.]+)\/[[:digit:]]+ .+/\1 \2/g;t;d' $ip_path`" | |
| declare -A blookup=() | |
| for ba in "${bridge_addr[@]}"; do | |
| b=`echo $ba| awk '{print $1}'` | |
| a=`echo $ba| awk '{print $2}'` | |
| blookup[$a]=$b | |
| done | |
| checked=0 | |
| errors=0 | |
| readarray -t conns<<<"`grep ":${port} " $netstat_path| awk '{print $1, $4, $8}'| grep ":${port} " $netstat_path| awk '{print $1, $4, $8}'| sed -r "s/${proto}\s+([[:digit:]\.]+):${port}\s+([[:digit:]]+)\/${cmd}.*/\1 \2/g;t;d"`" | |
| for uc in "${conns[@]}"; do | |
| [ -n "$uc" ] || continue | |
| a=`echo $uc| awk '{print $1}'` | |
| if [ "$a" != "0.0.0.0" ]; then | |
| pid=`echo $uc| awk '{print $2}'` | |
| if [ "${blookup[$a]:-__null__}" = '__null__' ]; then | |
| if `grep -q "$a/" $ip_path`; then | |
| echo "no bridge found with address $a - this indicates that ${cmd} pid=$pid is listening to an incorrect address" | |
| else | |
| echo "no interfaces found with address $a - this indicates that ${cmd} pid=$pid is listening to an unbound address that will never receive any data" | |
| fi | |
| ((errors+=1)) | |
| fi | |
| fi | |
| ((checked+=1)) | |
| done | |
| echo "${checked}/$num_procs ${cmd} checked - $errors errors found" | |
| exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment