0

i am just not that good, to get it work...

i wrote a little script that runs a speedtest every 15 minutes, and logs the output in daily log files:

#!/bin/bash

DATE=$(date +%d.%m.%Y_%H:%M)
DAY=$(date +%d.%m.%Y)
WANIP=$(dig +short myip.opendns.com @resolver1.opendns.com)
echo "Timestamp: "$DATE >> /home/rene/speedtests/$DAY.log
echo "WanIP: "$WANIP >> /home/rene/speedtests/$DAY.log

/home/rene/speedtest-cli --server 3199 --share --simple >> /home/rene/speedtests/$DAY.log
echo "----------" >> /home/rene/speedtests/$DAY.log

The log looks as following:

[rene@manjaro-home ~]$ cat speedtests/11.10.2016.log 
Timestamp: 11.10.2016_00:00
WanIP: 77.119.x.x
Ping: 63.773 ms
Download: 32.90 Mbit/s
Upload: 29.05 Mbit/s
Share results: http://www.speedtest.net/result/5703959727.png
----------
Timestamp: 11.10.2016_00:15
WanIP: 77.119.x.x
Ping: 70.06 ms
Download: 30.85 Mbit/s
Upload: 26.31 Mbit/s
Share results: http://www.speedtest.net/result/5703983637.png
----------
Timestamp: 11.10.2016_00:30
WanIP: 77.119.x.x
Ping: 65.722 ms
Download: 31.16 Mbit/s
Upload: 26.80 Mbit/s
Share results: http://www.speedtest.net/result/5704006209.png
----------

Want to lodge a complaint against my Provider, cause i should have 150/50, and since a month i got this bad bandwidth =(

now i need a script that converts this log file like this:

Time,Ping,Download,Upload,Link
11.10.2016_00:00,63.773,32.90,29.05,http://www.speedtest.net/result/5703959727.png
11.10.2016_00:15,70.06,30.85,26.31,http://www.speedtest.net/result/5703983637.png
11.10.2016_00:30,65.722,31.16,26.80,http://www.speedtest.net/result/5704006209.png

i already tried some stuff, but i am just to dump to do it with awk ;-D

[rene@manjaro-home ~]$ cat daily-csv.sh 
#!/bin/bash
DAY=$(date +%d.%m.%Y)

#echo '"Timestamp","Ping","Download","Upload","Link"' > /home/rene/speedtests/$DAY.csv
for line in `cat /home/rene/speedtests/$DAY.log`; do
            timestamp=$(echo $line | awk -F"Timestamp: " '{$0=$1}2');
echo $timestamp
done





#for F in /home/rene/speedtests/$DAY.log
#do
#    {
#        read Timestamp
#        read WanIP
#        read Ping
#        read Download
#        read Upload
#   read Link
#   read Placeholder
#    } < $F
#    echo "$Timestamp,$Ping,$Download,$Upload,$Link" >> speedtests/$DAY.csv
#    
#TIME=${Timestamp#* }
#PING=${Ping#* }
#DOWN=${Download#* }
#UP=${Upload#* }
#LINK=${Link#*: }
#
#echo $TIME,$PING,$DOWN,$UP,$LINK
#
#
#done

The outcommented part only gives the first log.

So ... could anybody help me out? :)

0

well for the first, i was working with the script that you uploaded but does not work in my environment (Solaris 10), so this is the answer to your ask:

    #!/bin/bash

    #######################################################
    # Script to convert with SED and AWK a logfile to CSV #
    #######################################################


    ######################
    ##    Empty files   ##
    ######################

    cat /dev/null>a.txt
    cat /dev/null>b.txt
    cat /dev/null>c.txt
    cat /dev/null>d.txt
    cat /dev/null>e.txt

    cat /dev/null>one.txt
    cat /dev/null>two.txt
    cat /dev/null>three.txt
    cat /dev/null>four.txt
    cat /dev/null>five.csv


    #########################################
    ##    Separate all aoutputs in files   ##
    #########################################


    for line in `cat speedtests/11.10.2016.log | grep "Timestamp: "`;
    do
    echo $line |awk -F"Timestamp: " '{print $1}' | sed '/^$/d';
    done>a.txt
    cat a.txt;



    for line in `cat speedtests/11.10.2016.log | grep "Ping"`;
    do
    echo $line |awk -F"Ping:" '{print $1}' | sed '/ms/d' |sed '/^$/d';
    done>b.txt;
    cat b.txt;



    for line in `cat speedtests/11.10.2016.log | grep "Download: "`;
    do
    echo $line |awk -F"Download: " '{print $1}' | sed '/Mbit/d' | sed '/^$/d';
    done>c.txt
    cat c.txt


    for line in `cat speedtests/11.10.2016.log | grep "Upload: "`;
    do
    echo $line |awk -F"Upload: " '{print $1}' | sed '/Mbit/d' | sed '/^$/d';
    done>d.txt
    cat d.txt


    for line in `cat speedtests/11.10.2016.log | grep "http"`;
    do
    echo $line |awk -F":" '{print $2}'  |sed '/^$/d' | sed  '1s/./https:\//' | sed 's/\//\\\\\//g' 
    done>e.txt
    cat e.txt


    #################################################################
    ##    Create outputs from previous outputs and adding a coma   ##
    #################################################################

    while read var
    do
    sed "s/$/,$var/" a.txt > one.txt
    done<b.txt
    cat one.txt;

    while read var
    do
    sed "s/$/,$var/" one.txt > two.txt
    done<c.txt
    cat two.txt;

    while read var
    do
    sed "s/$/,$var/" two.txt > three.txt
    done<d.txt
    cat three.txt;


    while read var
    do
    sed "s/$/,$var/" three.txt > four.txt
    done<e.txt
    cat four.txt;

    ##########################################
    ##    Add header only if you requided   ##
    ##########################################

    echo "------Time---------Ping--Down---Up--------Link-------------------------------------" >five.csv

    cat four.txt >>five.csv

    ###############################################
    ##    This is the final output as you whish  ##
    ###############################################

    cat five.csv;

Please feedback

Regards Luis

  • Hello, whats the error in your environment? – René Oct 14 '16 at 6:56
  • maybe i could help you there if you also want to use the script :-) your script is working fine, thanks :-) i modded it, so that the first Line is Time,Ping,Down,Up,Link so i could use the csv with gnuplot to create analystic-graphs. i also changed some stuff cause the Output was like: [code]Timestamp:,66.698,20.84,26.37,speedtest.net/result/5706751025.png 11.10.2016_23:45,66.698,20.84,26.37,speedtest.net/result/5706751025.png[/code] But its working now, thanks :) – René Oct 14 '16 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.