iperfd

Server

cat > /etc/init.d/iperf_5010

#!/bin/bash
# myapp daemon
# chkconfig: 345 20 80
# description: myapp daemon
# processname: myapp

DAEMON_PATH="/usr/bin/"
LISTEN_PORT=5010
DAEMON=iperf
DAEMONOPTS="-u -s -p $LISTEN_PORT"

NAME=iperf_$LISTEN_PORT
DESC="Iperf monitoring"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
	printf "%-50s" "Starting $NAME..."
	cd $DAEMON_PATH
	PID=`$DAEMON $DAEMONOPTS > /dev/null 2>&1 & echo $!`
	#echo "Saving PID" $PID " to " $PIDFILE
        if [ -z $PID ]; then
            printf "%s\n" "Fail"
        else
            echo $PID > $PIDFILE
            printf "%s\n" "Ok"
        fi
;;
status)
        printf "%-50s" "Checking $NAME..."
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
                printf "%s\n" "Process dead but pidfile exists"
            else
                echo "Running"
            fi
        else
            printf "%s\n" "Service not running"
        fi
;;
stop)
        printf "%-50s" "Stopping $NAME"
            PID=`cat $PIDFILE`
            cd $DAEMON_PATH
        if [ -f $PIDFILE ]; then
            kill -HUP $PID
            printf "%s\n" "Ok"
            rm -f $PIDFILE
        else
            printf "%s\n" "pidfile not found"
        fi
;;

restart)
  	$0 stop
  	$0 start
;;

*)
        echo "Usage: $0 {status|start|stop|restart}"
        exit 1
esac

Client

cat > /bin/voip_alexlinux.com.5010.sh

#!/bin/bash

#
REMOTE_HOST=alexlinux.com
PORT=5010
TIME=50

#
TMP_FILENAME="/tmp/$REMOTE_HOST.$PORT.txt"
PROM_TMP_FILENAME="/tmp/voip.$REMOTE_HOST.$PORT.prom"
PIDFILE=/tmp/iperf_$REMOTE_HOST.$PORT.pid

if [ -f "$PIDFILE" ]
then
    exit 1
fi

>$PIDFILE

iperf -c $REMOTE_HOST -p $PORT -t $TIME -u > $TMP_FILENAME

LOST=`cat $TMP_FILENAME | tail -n 1 | awk -F '(' '{print $2}' | awk -F '%' '{print $1}'`
if [[ $LOST =~ ^[0-9]+$ ]]
then
    echo "ok" > /dev/null
else
    LOST=100
fi

JITTER=`cat $TMP_FILENAME | tail -n 1 | awk '{ print substr($0, 48, 9)}' | awk '{print $2}'`
if [[ $JITTER =~ ^[0-9] ]]
then
    echo "ok" > /dev/null
else
    JITTER=0
fi

echo "iperf {dst=\"$REMOTE_HOST\",name=\"lost_percent\"} $LOST" >  $PROM_TMP_FILENAME
echo "iperf {dst=\"$REMOTE_HOST\",name=\"jitter\"} $JITTER"     >> $PROM_TMP_FILENAME

rm -f $PIDFILE

Prometheus node_exporter

cat >> /etc/crontab

* * * * * root /bin/voip_alexlinux.com.5010.sh
* * * * * root cat /tmp/*.prom > /tmp/prom/voip.prom

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>