Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 23 Oct 2014, 11:26
All times are UTC - 4
 Forum index » Advanced Topics » Cutting edge
pgprs-connect
Moderators: Flash, Ian, JohnMurga
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [1 Post]  
Author Message
Karl Godt


Joined: 20 Jun 2010
Posts: 3972
Location: Kiel,Germany

PostPosted: Tue 10 Dec 2013, 10:25    Post subject:  pgprs-connect
Subject description: counting rx/tx bytes
 

/usr/bin/pgprs-shell installs the pgprs suite with

/usr/bin/pgprs-setup
and
/usr/bin/pgprs-connect

This suite resides in /lib/modules/all-firmware/
as extracted folder or tarball ( pgprs/ or pgprs.tar.gz ) .

I try to implement some code from rc.shutdown to collect transmission and write it to a file in /var .

The code to collect is from Barry or rerwin .

Code:
#!/bin/sh



mkdir -p /var/local/sns
mkdir -p /var/local/sns/ppp0

fun_touch_files(){
for ONECOUNT in \
 sns/r sns/t pupdial/isp1/r pupdial/isp1/t pupdial/isp2/r pupdial/isp2/t ppp0/r ppp0/t sns/ppp0/r sns/ppp0/t
do mkdir -p /var/local/${ONECOUNT%/*}
touch /var/local/${ONECOUNT}x_bytes_month
done
}

fun_reset_month(){
LANG=C UPDATE_MONTH=`date +%b`
read CURRENT_MONTH</var/local/sns/current_month
[ "$CURRENT_MONTH" ] || CURRENT_MONTH=0
if [ "$UPDATE_MONTH" != "$CURRENT_MONTH" ];then
 echo "$UPDATE_MONTH" > /var/local/sns/current_month
 for ONECOUNT in sns pupdial/isp1 pupdial/isp2 ppp0 sns/ppp0;do
  [ -d /var/local/"$ONECOUNT" ] || mkdir -p /var/local/"$ONECOUNT"
  cp --backup=numbered /var/local/${ONECOUNT}/rx_bytes_month /var/local/${ONECOUNT}/rx_bytes_month-`date +%Y`-$CURRENT_MONTH
  echo -n 0 > /var/local/${ONECOUNT}/rx_bytes_month
  cp --backup=numbered /var/local/${ONECOUNT}/tx_bytes_month /var/local/${ONECOUNT}/tx_bytes_month-`date +%Y`-$CURRENT_MONTH
  echo -n 0 > /var/local/${ONECOUNT}/tx_bytes_month
 done
fi
}

fun_read_month(){
ACTIVE_INTERFACE="ppp0"
read RX_BYTES_MONTH </var/local/$ACTIVE_INTERFACE/rx_bytes_month
[ "$RX_BYTES_MONTH" ] || RX_BYTES_MONTH=0
read TX_BYTES_MONTH </var/local/$ACTIVE_INTERFACE/tx_bytes_month
[ "$TX_BYTES_MONTH" ] || TX_BYTES_MONTH=0
}

fun_read_session(){
read RX_BYTES </var/local/$ACTIVE_INTERFACE/rx_bytes_session
read TX_BYTES </var/local/$ACTIVE_INTERFACE/tx_bytes_session
}

fun_present_last_session(){
if [ "$RX_BYTES" -o "$TX_BYTES" ] ; then
echo ""
echo "Last Session :"
echo "'$RX_BYTES' Bytes down"
echo "'$TX_BYTES' Bytes up"

else
echo "No download andor upload count available for last session."
fi
}

fun_present_last_month(){
if [ "$UPDATE_MONTH" != "$CURRENT_MONTH" ];then
echo ""
echo "Last Month :"
 if
 [ -f /var/local/$ACTIVE_INTERFACE/rx_bytes_month-`date +%Y`-$CURRENT_MONTH ]
 then
 read LM_RX_BYTES </var/local/$ACTIVE_INTERFACE/rx_bytes_month-`date +%Y`-$CURRENT_MONTH
 echo "'$LM_RX_BYTES' Bytes down"
 else
 echo "No download count available for '$CURRENT_MONTH' ."
 fi
 if
 [ -f /var/local/$ACTIVE_INTERFACE/tx_bytes_month-`date +%Y`-$CURRENT_MONTH ]
 then
 read LM_TX_BYTES </var/local/$ACTIVE_INTERFACE/tx_bytes_month-`date +%Y`-$CURRENT_MONTH
 echo "'$LM_TX_BYTES' Bytes up"
 else
 echo "No upload count available for '$CURRENT_MONTH' ."
 fi
fi
}

fun_present_rxtx_bytes(){
if [ "$RX_BYTES" -a "$TX_BYTES" ] ; then
  SESS_END=`date +%s`

  echo -n "$RX_BYTES" > /var/local/$ACTIVE_INTERFACE/rx_bytes_session
  echo -n "$RX_BYTES" > /var/local/$ACTIVE_INTERFACE/rx_bytes_session-$SESS_END

  echo -n "$TX_BYTES" > /var/local/$ACTIVE_INTERFACE/tx_bytes_session
  echo -n "$TX_BYTES" > /var/local/$ACTIVE_INTERFACE/tx_bytes_session-$SESS_END

  read RX_BYTES_MONTH_la </var/local/$ACTIVE_INTERFACE/rx_bytes_month
  [ "$RX_BYTES_MONTH_la" ] || RX_BYTES_MONTH_la=0
  read RX_BYTES_MONTH_ls </var/local/sns/rx_bytes_month
  [ "$RX_BYTES_MONTH_ls" ] || RX_BYTES_MONTH_ls=0
  read RX_BYTES_MONTH_lsa </var/local/sns/$ACTIVE_INTERFACE/rx_bytes_month
  [ "$RX_BYTES_MONTH_lsa" ] || RX_BYTES_MONTH_lsa=0

  RX_BYTES_MONTH=$(((RX_BYTES_MONTH_la+RX_BYTES_MONTH_ls+RX_BYTES_MONTH_lsa)/3))

  [ "$RX_BYTES_MONTH" ] || RX_BYTES_MONTH=0

  RX_BYTES_MONTH=`expr $RX_BYTES_MONTH + $RX_BYTES`
  echo -n "$RX_BYTES_MONTH" > /var/local/$ACTIVE_INTERFACE/rx_bytes_month
  echo -n "$RX_BYTES_MONTH" > /var/local/sns/$ACTIVE_INTERFACE/rx_bytes_month
  echo -n "$RX_BYTES_MONTH" > /var/local/sns/rx_bytes_month

  read TX_BYTES_MONTH_la </var/local/$ACTIVE_INTERFACE/tx_bytes_month
  [ "$TX_BYTES_MONTH_la" ] || TX_BYTES_MONTH_la=0
  read TX_BYTES_MONTH_ls </var/local/sns/tx_bytes_month
  [ "$TX_BYTES_MONTH_ls" ] || TX_BYTES_MONTH_ls=0
  read TX_BYTES_MONTH_lsa </var/local/sns/$ACTIVE_INTERFACE/tx_bytes_month
  [ "$TX_BYTES_MONTH_lsa" ] || TX_BYTES_MONTH_lsa=0

  TX_BYTES_MONTH=$(((TX_BYTES_MONTH_la+TX_BYTES_MONTH_ls+TX_BYTES_MONTH_lsa)/3))

  [ "$TX_BYTES_MONTH" ] || TX_BYTES_MONTH=0
  TX_BYTES_MONTH=`expr $TX_BYTES_MONTH + $TX_BYTES`
  echo -n "$TX_BYTES_MONTH" > /var/local/$ACTIVE_INTERFACE/tx_bytes_month
  echo -n "$TX_BYTES_MONTH" > /var/local/sns/$ACTIVE_INTERFACE/tx_bytes_month
  echo -n "$TX_BYTES_MONTH" > /var/local/sns/tx_bytes_month

echo ""
TOTALSESSION=$( LC_ALL=C dc $(($RX_BYTES+$TX_BYTES)) 1024 \/ p );          echo -e "SESSION: $TOTALSESSION\tKiB"
TOTALMONTH=$( LC_ALL=C dc $(($RX_BYTES_MONTH+$TX_BYTES_MONTH)) 1024 \/ p );echo -e "  MONTH: $TOTALMONTH\tKiB"
TOTALSESSION=$( LC_ALL=C dc $TOTALSESSION 1024 \/ p ); echo -e "SESSION: $TOTALSESSION\tMiB"
TOTALMONTH=$( LC_ALL=C dc $TOTALMONTH 1024 \/ p );     echo -e "  MONTH: $TOTALMONTH\tMiB"
TOTALSESSION=$( LC_ALL=C dc $TOTALSESSION 1024 \/ p ); echo -e "SESSION: $TOTALSESSION\tGiB"
TOTALMONTH=$( LC_ALL=C dc $TOTALMONTH 1024 \/ p );     echo -e "  MONTH: $TOTALMONTH\tGiB"
echo ""

else
echo -e "\e[1;31m""Err .. could not read /sys/class/net/${ACTIVE_INTERFACE}/statistics/""\e[0;39m"
fi
}


fun_read_rxtx_bytes(){
ACTIVE_INTERFACE="ppp0"

mkdir -p /var/local/$ACTIVE_INTERFACE
while test "`pidof pppd`"; do
  until [ -d /sys/class/net/${ACTIVE_INTERFACE}/statistics ];do sleep 1s;done
  break
done

while test -d /sys/class/net/${ACTIVE_INTERFACE}; do
  read RX_BYTES </sys/class/net/${ACTIVE_INTERFACE}/statistics/rx_bytes
  read TX_BYTES </sys/class/net/${ACTIVE_INTERFACE}/statistics/tx_bytes
  #echo -n "$RX_BYTES" > /var/local/$ACTIVE_INTERFACE/rx_bytes_session
  #echo -n "$TX_BYTES" > /var/local/$ACTIVE_INTERFACE/tx_bytes_session
  #RX_BYTES_MONTH=`cat /var/local/$ACTIVE_INTERFACE/rx_bytes_month`
  #[ ! "$RX_BYTES_MONTH" ] && RX_BYTES_MONTH=0
  #RX_BYTES_MONTH=`expr $RX_BYTES_MONTH + $RX_BYTES`
  #echo -n "$RX_BYTES_MONTH" > /var/local/$ACTIVE_INTERFACE/rx_bytes_month
  #TX_BYTES_MONTH=`cat /var/local/$ACTIVE_INTERFACE/tx_bytes_month`
  #[ ! "$TX_BYTES_MONTH" ] && TX_BYTES_MONTH=0
  #TX_BYTES_MONTH=`expr $TX_BYTES_MONTH + $TX_BYTES`
  #echo -n "$TX_BYTES_MONTH" > /var/local/$ACTIVE_INTERFACE/tx_bytes_month

sleep 1s
done

fun_present_rxtx_bytes
}

fun_kill_pppd(){
pidof pppd && kill -s HUP `pidof pppd`
}

fun_kill_jobs(){
jobs -rp && {
  for Job in `jobs -pr`; do
  /bin/ps -p $Job && kill $Job
  done
 }
}

trap "fun_kill_pppd;sleep 4s;fun_kill_jobs;exit" HUP INT QUIT TRAP KILL TERM

fun_touch_files
fun_reset_month
fun_read_month
fun_read_session
fun_present_last_session
fun_present_last_month

echo ""
TOTALMONTHK=$( LC_ALL=C dc $(($RX_BYTES_MONTH+$TX_BYTES_MONTH)) 1024 \/ p );echo -e "  MONTH: '$TOTALMONTHK'\tKiB"
TOTALMONTHM=$( LC_ALL=C dc $TOTALMONTHK 1024 \/ p );     echo -e "  MONTH: '$TOTALMONTHM'\tMiB"
TOTALMONTHG=$( LC_ALL=C dc $TOTALMONTHM 1024 \/ p );     echo -e "  MONTH: '$TOTALMONTHG'\tGiB"
echo ""

n="c"

while  [ "$n" ]
do
/usr/sbin/pppd call gprsmm &
jobs -l
PPPD_PID=`jobs -p /usr/sbin/pppd | tail -n 1`;
echo -e '\e[0;36mPPPD_PID='$PPPD_PID'\e[0;39m'
sleep 5s

until
[ -f /sys/class/net/${ACTIVE_INTERFACE}/carrier ]
do sleep 1s
pidof pppd || break
done

echo ""

pidof pppd &&
{

while
[ 1 ]
do sleep 1s
read CARRIER </sys/class/net/${ACTIVE_INTERFACE}/carrier
[ "$CARRIER" = 1 ] && break
pidof pppd || break
done

}

pidof pppd &&
{
unset RX_BYTES TX_BYTES
fun_read_rxtx_bytes &
echo -e '\033[1;35m'"Press ENTER to disconnect and quit"'\033[0;39m'
} || {
echo -e '\033[1;36m'"Press R+ENTER to reconnect/try again"'\033[0;39m'
}
jobs -l
jobs -p fun_read_rxtx_bytes

n="n"
read n
done

fun_kill_pppd
sleep 4s
fun_kill_jobs

read -p "Press any key to quit .. " Q
exit 0


Crude with unneccessary debugging output for now because the location for the files containing the collected transmisson had changed some longer time ago .

It also has commented the writes to disk, which would result in lost counts if the machine gets powered off hard or the modem unplugged while connected.

Reads transmission every 1 second .

Uses jobs, which would probably not work in a shell that has no job control ( ie & forked or exec 'd ) .

_________________
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal Razz
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 1 of 1 [1 Post]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Advanced Topics » Cutting edge
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0701s ][ Queries: 11 (0.0090s) ][ GZIP on ]