| 
									
										
										
										
											2016-05-11 13:49:57 +04:30
										 |  |  | #!/bin/sh | 
					
						
							| 
									
										
										
										
											2016-05-11 13:34:13 +04:30
										 |  |  | # | 
					
						
							|  |  |  | # Copyright (c) 2016 Babak Farrokhi.  All rights reserved. | 
					
						
							|  |  |  | #        | 
					
						
							|  |  |  | # Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  | # modification, are permitted provided that the following conditions | 
					
						
							|  |  |  | # are met:       | 
					
						
							|  |  |  | #                | 
					
						
							|  |  |  | # 1. Redistributions of source code must retain the above copyright | 
					
						
							|  |  |  | #    notice, this list of conditions and the following disclaimer. | 
					
						
							| 
									
										
										
										
											2015-08-15 09:44:28 +04:30
										 |  |  | # 2. Redistributions in binary form must reproduce the above copyright | 
					
						
							|  |  |  | #    notice, this list of conditions and the following disclaimer in the | 
					
						
							|  |  |  | #    documentation and/or other materials provided with the distribution. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND | 
					
						
							|  |  |  | # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
					
						
							|  |  |  | # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 
					
						
							|  |  |  | # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 
					
						
							|  |  |  | # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 
					
						
							|  |  |  | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 
					
						
							|  |  |  | # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
					
						
							|  |  |  | # SUCH DAMAGE. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | # 12-Aug-2015 @farrokhi | 
					
						
							|  |  |  | # This script graphs ICMP RTT to remove given hosts using ping utility. | 
					
						
							|  |  |  | # It uses all IP addresses as source address on given interface. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # Configuration sample: | 
					
						
							|  |  |  | # [multiping_*] | 
					
						
							|  |  |  | # user root | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | # env.interface igb0:1 igb1:2 | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | # env.hosts "4.2.2.4, ripe.net, 192.168.0.5, 8.8.4.4" | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | # | 
					
						
							|  |  |  | # with env.interface variable, every interface should be suffixed with | 
					
						
							|  |  |  | # its fib number. In a normal environment, fib should always be 0. If | 
					
						
							|  |  |  | # you are using multiple routing tables you should use correct fib  | 
					
						
							|  |  |  | # number (e.g. igb1 on fib 4 will be igb1:4). | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | # Magic markers: | 
					
						
							|  |  |  | #%# family=auto | 
					
						
							|  |  |  | #%# capabilities=autoconf suggest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SCRIPT_NAME=$(basename $0) | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | SRC=`echo $SCRIPT_NAME | sed -n s/multiping_//p | cut -f1 -d_` | 
					
						
							|  |  |  | FIB=`echo $SCRIPT_NAME | sed -n s/multiping_//p | cut -f2 -d_` | 
					
						
							| 
									
										
										
										
											2016-08-06 12:11:04 +04:30
										 |  |  | FPING=`which fping` || ( echo "fping required but not found"; exit 1) | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | FSRC=`echo ${SRC} | sed 's/\./_/g'` | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | SETFIB="/usr/sbin/setfib ${FIB}" | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  | if [ $# -lt 1 ]; then | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 	if [ -z "${SRC}" ]; then | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 		echo "should run from symlink" | 
					
						
							|  |  |  | 		exit 1 | 
					
						
							|  |  |  | 	fi | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 	if [ -z "${hosts}" ]; then | 
					
						
							|  |  |  | 		echo "Unknown destination address" | 
					
						
							|  |  |  | 		exit 1 | 
					
						
							|  |  |  | 	fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 	TMPFILE=`mktemp -t multiping` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-11 14:16:48 +04:30
										 |  |  | 	DST=`echo ${hosts} | tr , " "` | 
					
						
							|  |  |  | 	fname=`echo ${DST} | sed 's/\./_/g'`  | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	${SETFIB} ${FPING} -A -q -s -i10 -c50 -p 25 -S ${SRC} -t1000 ${DST} 2>&1 | grep ':' | tr '%/,' ' ' | awk ' | 
					
						
							| 
									
										
										
										
											2016-05-11 14:16:48 +04:30
										 |  |  | /xmt/ {  | 
					
						
							| 
									
										
										
										
											2016-05-13 18:53:07 +04:30
										 |  |  |         name = $1; | 
					
						
							|  |  |  | 	gsub(/\./, "_", name); | 
					
						
							|  |  |  |         printf "rtt_%s.value %0.0f\n", name, $15;  | 
					
						
							|  |  |  |         printf "loss_%s.value %0.0f\n", name, $9;  | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | } | 
					
						
							|  |  |  | ' >> ${TMPFILE} | 
					
						
							| 
									
										
										
										
											2016-05-11 14:16:48 +04:30
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 	echo "multigraph rtt_${FSRC}" | 
					
						
							|  |  |  | 	grep ^rtt ${TMPFILE} | 
					
						
							|  |  |  | 	echo "multigraph loss_${FSRC}" | 
					
						
							|  |  |  | 	grep ^loss ${TMPFILE} | 
					
						
							|  |  |  | 	rm ${TMPFILE} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | elif [ "$1" == "suggest" ]; then | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 	 | 
					
						
							|  |  |  | 	# interface name format is igb0:2 - number after colon is fib number | 
					
						
							|  |  |  | 	for int in ${interface}; do | 
					
						
							|  |  |  | 		FIB=`echo ${int} | cut -f2 -d:` | 
					
						
							|  |  |  | 		NAME=`echo ${int} | cut -f1 -d:` | 
					
						
							|  |  |  | 		IPS=`ifconfig ${NAME} | grep -v tunnel | awk '/inet /{print $2}' | grep -Ev '^(10|192|127)\.'` | 
					
						
							|  |  |  | 		for DST in ${IPS}; do | 
					
						
							|  |  |  | 			echo ${DST}_${FIB} | 
					
						
							|  |  |  | 		done | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 	done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | elif [ "$1" == "autoconf" ]; then | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 	if [ -z "${hosts}" ]; then | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 		echo "no (env.hosts is not defined)" | 
					
						
							| 
									
										
										
										
											2016-05-11 13:34:13 +04:30
										 |  |  | 		exit 1 | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 	fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 	if [ -z "${interface}" ]; then | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 		echo "no (env.interface is not defined)" | 
					
						
							| 
									
										
										
										
											2016-05-11 13:34:13 +04:30
										 |  |  | 		exit 1 | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 	fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	### check env vars and return no if not set properly | 
					
						
							|  |  |  | 	echo yes | 
					
						
							|  |  |  |    	exit 0 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | elif [ "$1" == "config" ]; then | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 	if [ -z "${SRC}" ]; then | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 		exit 1 | 
					
						
							|  |  |  | 	fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	echo "multigraph rtt_${FSRC}" | 
					
						
							|  |  |  | 	echo "graph_title RTT Statistics for ${SRC}" | 
					
						
							|  |  |  | 	echo "graph_args --lower-limit 0 --base 1000" | 
					
						
							|  |  |  | 	echo "graph_vlabel milliseconds" | 
					
						
							|  |  |  | 	echo "graph_category connectivity" | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 	echo "graph_info This graph shows ICMP RTT for {$SRC} on FIB ${FIB} toward multiple destination hosts" | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	COLOR=7 | 
					
						
							|  |  |  | 	for DST in `echo ${hosts} | tr , " "`; do | 
					
						
							|  |  |  | 		fname=`echo ${DST} | sed 's/\./_/g'` | 
					
						
							|  |  |  | 		echo rtt_${fname}.type GAUGE | 
					
						
							|  |  |  | 		echo rtt_${fname}.draw LINE1 | 
					
						
							|  |  |  | 		echo rtt_${fname}.min 0 | 
					
						
							|  |  |  | 		echo rtt_${fname}.colour COLOUR${COLOR} | 
					
						
							|  |  |  | 		echo rtt_${fname}.label ${DST} | 
					
						
							|  |  |  | 		COLOR=`expr ${COLOR} + 1` | 
					
						
							|  |  |  | 	done | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	echo "multigraph loss_${FSRC}" | 
					
						
							|  |  |  | 	echo "graph_title Packet Loss Statistics for ${SRC}" | 
					
						
							| 
									
										
										
										
											2015-08-15 16:04:14 +04:30
										 |  |  | 	echo "graph_args --units-exponent 0 -r --upper-limit 100 --lower-limit 0 --base 1000" | 
					
						
							|  |  |  | 	echo "graph_scale no" | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 	echo "graph_vlabel percent" | 
					
						
							|  |  |  | 	echo "graph_category connectivity" | 
					
						
							| 
									
										
										
										
											2017-05-16 18:53:16 +04:30
										 |  |  | 	echo "graph_info This graph shows ICMP Packet Loss for source address ${SRC} on FIB ${FIB} toward multiple destination hosts" | 
					
						
							| 
									
										
										
										
											2015-08-15 09:24:04 +04:30
										 |  |  | 
 | 
					
						
							|  |  |  | 	COLOR=7 | 
					
						
							|  |  |  | 	for DST in `echo ${hosts} | tr , " "`; do | 
					
						
							|  |  |  | 		fname=`echo ${DST} | sed 's/\./_/g'` | 
					
						
							|  |  |  | 		echo loss_${fname}.type GAUGE | 
					
						
							|  |  |  | 		echo loss_${fname}.draw LINE2 | 
					
						
							|  |  |  | 		echo loss_${fname}.min 0 | 
					
						
							|  |  |  | 		echo loss_${fname}.max 100 | 
					
						
							|  |  |  | 		echo loss_${fname}.colour COLOUR${COLOR} | 
					
						
							|  |  |  | 		echo loss_${fname}.label ${DST} | 
					
						
							|  |  |  | 		COLOR=`expr ${COLOR} + 1` | 
					
						
							|  |  |  | 	done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 |