Tuesday, August 19, 2014

Bash linux how to parsing log file

Разрабатывая приставку видео, мне как тестеровщику автоматизатору понадобилось парсить лог файл и в красивом виде преподносить заказчику (Excel format)

Пример лог файла приставки описан ниже:


#!/bin/bash

int choise;

if [ "$1" == "" ]; then
        echo "check loag file"
        echo "true way to execute program ./makeotchet.sh LOG_file.log number_of_tests"
        exit
else if [ "$2" == "" ]; then 
        echo "check number of tests"                                                                                                                                                                                     
        echo "true way to execute program ./makeotchet.sh LOG_file.log number_of_tests"
        exit
     fi                                                                                                                                  

fi

echo "1 - Make file to othcet"
echo "2 - Make finaly otchet"
numberoftest="$2"
logfile="$1"

function makefiletootchet
{
        echo "wifi parsing"
        #first wifi adapter
        cat $logfile | grep -A 22  First | grep Upload > wifiupload1adapter.log
        cat $logfile | grep -A 22  First | grep Download > wifidownload1adapter.log
        #second wifi adapter
        cat $logfile | grep -A 22  Second | grep Upload > wifiupload2adapter.log
        cat $logfile | grep -A 22  Second | grep Download > wifidownload2adapter.log
        echo "eth parsing"
        cat $logfile | grep -A 16  eth0 | grep Upload > ethupload.log
        cat $logfile | grep -A 16  eth0 | grep Download > ethdownload.log
        echo "mem parsing"
        cat $logfile | grep MemFree > memlog.log
        cat $logfile | grep Cached > cashedlog.log
        echo "cpu parsing"
        cat $logfile | grep idle > cpulog.log
       
        echo "cpu generate rezult"
        awk '{ print $2 }' cpulog.log | sed 's/%//g' | sed 's/\./,/g' > rez_cpu_log_user.log
        awk '{ print $4 }' cpulog.log | sed 's/%//g' | sed 's/\./,/g' > rez_cpu_log_sys.log
        awk '{ print $8 }' cpulog.log | sed 's/%//g' | sed 's/\./,/g' > rez_cpu_log_free.log
       
        echo "wifi generate rezult"
        awk '{ print $11 }' wifiupload1adapter.log > rez_wifiuplod1adapter.log
        awk '{ print $11 }' wifidownload1adapter.log > rez_wifidown1adapter.log
        awk '{ print $11 }' wifiupload2adapter.log > rez_wifiuplod2adapter.log                                                                                                 
        awk '{ print $11 }' wifidownload2adapter.log > rez_wifidown2adapter.log
       
        echo "eth generate rezult"
        awk '{ print $11 }' ethupload.log > rez_ethuplod.log
        awk '{ print $11 }' ethdownload.log > rez_ethdown.log
       
        echo "memory generate rezult"
        awk '{ print $2 }' memlog.log > rez_memlog.log
        awk '{ print $2 }' cashedlog.log > rez_cashedlog.log
       
        exit   
}

function makefinalyotchet
{
       
        stroka=1
        echo "Start parsing"
        touch finalyothcet.csv
        echo "Memory;Cashed;Wifi_up1adapter;Wifi_down1adapter;Wifi_up2adapter;Wifi_down2adapter;CPU_free;CPU_sys;CPU_user;ETH_down;ETH_upl" > finalyothcet.csv
       
        for i in $(seq 1 $numberoftest); do

          nomerstroki=$stroka'p'
          fullzapis0=`sed -n -e $nomerstroki rez_memlog.log`
          fullzapis1=`sed -n -e $nomerstroki rez_cashedlog.log`
          fullzapis2=`sed -n -e $nomerstroki rez_wifiuplod1adapter.log`
          fullzapis3=`sed -n -e $nomerstroki rez_wifidown1adapter.log`
          fullzapis4=`sed -n -e $nomerstroki rez_wifiuplod2adapter.log`                                                                                                
          fullzapis5=`sed -n -e $nomerstroki rez_wifidown2adapter.log`   
          fullzapis6=`sed -n -e $nomerstroki rez_cpu_log_free.log`
          fullzapis7=`sed -n -e $nomerstroki rez_cpu_log_sys.log`
          fullzapis8=`sed -n -e $nomerstroki rez_cpu_log_user.log`
          fullzapis9=`sed -n -e $nomerstroki rez_ethdown.log`
          fullzapis10=`sed -n -e $nomerstroki rez_ethuplod.log`
         
          skoba=";"
          fullzapisfin="$fullzapis0$skoba$fullzapis1$skoba$fullzapis2$skoba$fullzapis3$skoba$fullzapis4$skoba$fullzapis5$skoba$fullzapis6$skoba$fullzapis7$skoba$fullzapis8$skoba$fullzapis9$skoba$fullzapis10$skoba"
          echo $fullzapisfin >> finalyothcet.csv
          stroka=$((stroka+1))
         done;
         
        echo "Make finaly otchet end"
       
        rm ethupload.log
        rm ethdownload.log
        rm memlog.log
        rm cpulog.log
        rm rez_cpu_log_user.log
        rm rez_cpu_log_sys.log
        rm rez_cpu_log_free.log
        rm wifiupload1adapter.log
        rm wifidownload1adapter.log
        rm wifiupload2adapter.log
        rm wifidownload2adapter.log
        rm rez_wifiuplod1adapter.log
        rm rez_wifidown1adapter.log
        rm rez_wifiuplod2adapter.log
        rm rez_wifidown2adapter.log
        rm rez_ethuplod.log
        rm rez_ethdown.log
        rm rez_cashedlog.log
        rm rez_memlog.log
        rm cashedlog.log
       
       
        exit
}

select choise in "1" "2";
 do

    echo you picked $choise
    if [ $choise == 1 ]; then
       echo "Your choise 1"
       makefiletootchet;
   
               
    else if [ $choise == 2 ]; then
        echo "Your choise 2"
        makefinalyotchet;
                             
        fi
   
    fi
    break;
 done

На выходе имею файл Excel -который парсит загрузку памяти, Wifi, Ethernet, CPU:
Memory;Cashed;Wifi_up1adapter;Wifi_down1adapter;Wifi_up2adapter;Wifi_down2adapter;CPU_free;CPU_sys;CPU_user;ETH_down;ETH_upl
49280;22236;23313;83,3;8,3;8,3;85040;50735
...
...

________________________EXAMPLE OF LOG FILE___________________________________
***** Start test 0 *****
MemFree:           49280 kB
Buffers:           17704 kB
CPU test begin
CPU:  8.3% usr  8.3% sys  0.0% nic 83.3% idle  0.0% io  0.0% irq  0.0% sirq
[7m  PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND [0m
17868 17861 root     S     3128  1.6   0  0.0 grep CPU
lan test
[network Thu Jan  1 00:00:50 PST 1970] iface = eth0; start time is 28847 ; timeout is 12
[network Thu Jan  1 00:00:51 PST 1970] ip_addr suuccessful; current time is 28851
[network Thu Jan  1 00:00:51 PST 1970] ping
PING 10.54.4.156 (10.54.4.156): 56 data bytes

--- 10.54.4.156 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 4.192/4.192/4.192 ms
[network Thu Jan  1 00:00:51 PST 1970] ping successful; current time is 28851
[network Thu Jan  1 00:00:51 PST 1970] iface_up: time = 4
[network Thu Jan  1 00:00:51 PST 1970] waiting for 17983 process...
[network Thu Jan  1 00:00:51 PST 1970] doing iperf to 10.54.4.156 ...
[network Thu Jan  1 00:00:55 PST 1970] Upload rate: 50735 kbps
[network Thu Jan  1 00:00:55 PST 1970] Download rate: 85040 kbps
[network Thu Jan  1 00:00:55 PST 1970] process finished
[network Thu Jan  1 00:00:55 PST 1970] iperf: unexpected_end = 28851
[network Thu Jan  1 00:00:55 PST 1970] iperf: current_time = 28855
[network Thu Jan  1 00:00:55 PST 1970] iperf: time = 4
[network] ethernet: Passed, 8 s (ifup: 4, iperf: 4)
[network] exit 0
LAN test begin
CPU:  0.0% usr  9.0% sys  0.0% nic 90.9% idle  0.0% io  0.0% irq  0.0% sirq
[7m  PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND [0m
18027 17861 root     S     3128  1.6   0  0.0 grep CPU
wifi test milan
[network Thu Jan  1 00:00:56 PST 1970] Searching for Wi-Fi USB device...
[network Thu Jan  1 00:00:56 PST 1970] Wi-Fi USB device found: 1
[network Thu Jan  1 00:01:01 PST 1970] Reenabling Ethernet (ip=192.168.1.1,gw=192.168.1.10)
[network Thu Jan  1 00:01:03 PST 1970] iface = wlan0; start time is 28856 ; timeout is 12
[network Thu Jan  1 00:01:03 PST 1970] ip_addr suuccessful; current time is 28863
[network Thu Jan  1 00:01:03 PST 1970] ping
PING 10.54.4.156 (10.54.4.156): 56 data bytes

--- 10.54.4.156 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 12.128/12.128/12.128 ms
[network Thu Jan  1 00:01:03 PST 1970] ping successful; current time is 28863
[network Thu Jan  1 00:01:03 PST 1970] iface_up: time = 7
[network Thu Jan  1 00:01:03 PST 1970] doing iperf to 10.54.4.156 ...
[network Thu Jan  1 00:01:03 PST 1970] waiting for 18172 process...
[network Thu Jan  1 00:01:07 PST 1970] Upload rate: 22236 kbps
[network Thu Jan  1 00:01:08 PST 1970] Download rate: 23313 kbps
[network Thu Jan  1 00:01:08 PST 1970] process finished
[network] wifi: ERROR: Slow download rate (23313)
[network Thu Jan  1 00:01:08 PST 1970] Shutting down wlan0 network interface
[network] exit 1
second test wifi begin
CPU:  0.0% usr  9.0% sys  0.0% nic 90.9% idle  0.0% io  0.0% irq  0.0% sirq
[7m  PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND [0m
18221 17861 root     S     3128  1.6   0  0.0 grep CPU
wifi test ap-lo-07
[network Thu Jan  1 00:01:09 PST 1970] Searching for Wi-Fi USB device...
[network Thu Jan  1 00:01:09 PST 1970] Wi-Fi USB device found: 1
[network Thu Jan  1 00:01:15 PST 1970] Reenabling Ethernet (ip=192.168.1.1,gw=192.168.1.10)
[network Thu Jan  1 00:01:17 PST 1970] iface = wlan0; start time is 28869 ; timeout is 12
[network Thu Jan  1 00:01:17 PST 1970] ip_addr suuccessful; current time is 28877
[network Thu Jan  1 00:01:17 PST 1970] ping
PING 10.54.4.156 (10.54.4.156): 56 data bytes

--- 10.54.4.156 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
[network Thu Jan  1 00:01:18 PST 1970] ping attempt failed; current time is 28878
PING 10.54.4.156 (10.54.4.156): 56 data bytes

--- 10.54.4.156 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
[network Thu Jan  1 00:01:19 PST 1970] ping attempt failed; current time is 28879
PING 10.54.4.156 (10.54.4.156): 56 data bytes

--- 10.54.4.156 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
[network Thu Jan  1 00:01:20 PST 1970] ping attempt failed; current time is 28880
[network Thu Jan  1 00:01:26 PST 1970] Reenabling Ethernet (ip=192.168.1.1,gw=192.168.1.10)
[network Thu Jan  1 00:01:27 PST 1970] iface = wlan0; start time is 28869 ; timeout is 12
[network Thu Jan  1 00:01:27 PST 1970] ip_addr suuccessful; current time is 28887
[network Thu Jan  1 00:01:27 PST 1970] ping
PING 10.54.4.156 (10.54.4.156): 56 data bytes

--- 10.54.4.156 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss
[network Thu Jan  1 00:01:28 PST 1970] ping attempt failed; current time is 28888
[network] wifi: ERROR: iface_up_timeout (12) exceeded (ping).
[network Thu Jan  1 00:01:28 PST 1970] Shutting down wlan0 network interface
[network] exit 1
finaly test wifi begin
CPU:  8.3% usr  8.3% sys  0.0% nic 83.3% idle  0.0% io  0.0% irq  0.0% sirq
[7m  PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND [0m
18496 17861 root     S     3128  1.6   0  0.0 grep CPU
bad blok test
nandtype test
pcbasn set test
csn set test
pcbasn get test
csn get test
mac get test
edid test
7890
Format PCBASN-ok!
Writnig CSN
PCBASN save on the device!
usb test
led blue test
123456789012345
12345
Test nand type-ok!
[network Thu Jan  1 00:01:30 PST 1970] doing make_cksum /tmp/run/edid.txt ...
cat /tmp/run/start_firmware.log | grep Bad eraseblock
Test bad block-ok!
[network Thu Jan  1 00:01:30 PST 1970] 26196460391216 = 26196460391216
[network] : Passed
[network] exit 0
Start test board led time: 2000 msec frequency: 1 HZ.
00:26:AA:A3:6A:2A
led eth test
video test begin
Start test ETH led time: 2000 msec
CPU: 33.3% usr 66.6% sys  0.0% nic  0.0% idle  0.0% io  0.0% irq  0.0% sirq
[7m  PID  PPID USER     STAT   VSZ %MEM CPU %CPU COMMAND [0m
18649 17861 root     S     3128  1.6   0  0.0 grep CPU
hdmi test
Sleep 5 sec, otchet generating
Time for copy data to USB1:0m1.061s
[playback Thu Jan  1 00:01:34 PST 1970] Waiting for 10.54.4.156:/srv/nfs4/1MB.ts playback to start...
Copy to USB-ok!
[playback Thu Jan  1 00:01:34 PST 1970] srt:
Test main board led fail, time is out!
[playback Thu Jan  1 00:01:35 PST 1970] srt: <param name="playback_url" value="nfs://10.54.4.156:/srv/nfs4/1MB.ts"/>
[playback Thu Jan  1 00:01:35 PST 1970] Video start successfully!
Time for copy data from USB1:0m0.190s
ir test
Copy from USB-ok!
Start test IR time: 2 sec
Out: 0,  0xea15e608,  0
Test eth led fail, time is out!
Out: 0,  0xea15e608,  0
Out: 0,  0xea15e608,  0
Out: 0,  0xea15e608,  0
Test IR  fail, time is out!

No comments:

Post a Comment