Calculate the average per block.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calculate the average per block.
# 1  
Old 08-20-2019
Calculate the average per block.

My old school way is a one liner. And will search for average from SAR, to get the data receive rate. But, I dont think it is practical or accurate,. Because it calculates to off peak hours. I am planning to change it. My cron runs every 30 mins. When my cron runs, and my time is 14:47pm,, it will only seach in SAR file or SAR output all 2:00pm-247pm, only the block, I will take care of the data and cloumn and calculation. I just want to know if my script runs at my system stime 19:20 it will get all the data from SAR only from 7pm-720pm.. 2400 military time.

Code:
My orig one liner script
for i in `cat /home/ave2.list`;do  echo -n $i",";ssh $i "echo -n $i,|cut -c1-7 -n; f=\`/sbin/ip a|egrep '127.127|128.128' |head -1| awk '{print \$NF }' \` ; sar -n DEV|grep \$f|grep Average|awk '{print \$5 * 8 / 1000 \" Mbps\" }'"  ; done  |sed 'N;s/\n/, /' > /home/rawdata2.log

$ date
Tue Aug 20 15:44:31 UTC 2019


sample:
01:20:01 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
01:30:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
01:30:01 PM      eth0     29.56      6.58      2.46      8.42      0.00      0.00      7.04
01:30:01 PM      eth1  98130.98      0.00  66635.96      0.00      0.00      0.00      0.05
01:30:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
01:30:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
01:40:02 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
01:40:02 PM      eth0     28.26      3.85      2.38      4.28      0.00      0.00      7.00
01:40:02 PM      eth1  98787.53      0.00  65109.20      0.00      0.00      0.00      0.05
01:40:02 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
01:40:02 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
01:50:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
01:50:01 PM      eth0     29.18      5.18      2.44      6.26      0.00      0.00      7.04
01:50:01 PM      eth1  97797.48      0.00  64819.71      0.00      0.00      0.00      0.05
01:50:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
01:50:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:00:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
02:00:01 PM      eth0     30.25      8.30      2.48     11.15      0.00      0.00      7.00
02:00:01 PM      eth1  99495.59      0.00  66608.09      0.00      0.00      0.00      0.05
02:00:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:00:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00

02:00:01 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
02:10:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
02:10:01 PM      eth0     29.35      6.18      2.44      7.88      0.00      0.00      7.00
02:10:01 PM      eth1 112297.64      0.00  75682.19      0.00      0.00      0.00      0.05
02:10:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:10:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:20:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
02:20:01 PM      eth0     29.05      5.57      2.45      6.76      0.00      0.00      7.02
02:20:01 PM      eth1 105654.26      0.00  69330.72      0.00      0.00      0.00      0.05
02:20:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:20:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:30:02 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
02:30:02 PM      eth0     31.29     10.13      2.56     13.71      0.00      0.00      7.01
02:30:02 PM      eth1 104931.75      0.00  70139.40      0.00      0.00      0.00      0.05
02:30:02 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:30:02 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:40:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
02:40:01 PM      eth0     38.72     25.23      3.17     35.77      0.00      0.00      7.02
02:40:01 PM      eth1 104897.98      0.00  70268.52      0.00      0.00      0.00      0.05
02:40:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:40:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00

02:40:01 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
02:50:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
02:50:01 PM      eth0     28.72      5.14      2.38      6.33      0.00      0.00      7.00
02:50:01 PM      eth1 106437.62      0.00  71325.17      0.00      0.00      0.00      0.05
02:50:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
02:50:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:00:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
03:00:01 PM      eth0     30.09      7.76      2.52     10.06      0.00      0.00      7.02
03:00:01 PM      eth1 105988.49      0.00  71232.42      0.00      0.00      0.00      0.05
03:00:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:00:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:10:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
03:10:01 PM      eth0     31.31     10.48      2.57     14.18      0.00      0.00      7.01
03:10:01 PM      eth1 116581.94      0.00  78709.01      0.00      0.00      0.00      0.05
03:10:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:10:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:20:02 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
03:20:02 PM      eth0     29.12      5.85      2.43      7.27      0.00      0.00      7.01
03:20:02 PM      eth1 117457.21      0.00  78036.86      0.00      0.00      0.00      0.05
03:20:02 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:20:02 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00

03:20:02 PM     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
03:30:01 PM        lo      0.18      0.18      0.03      0.03      0.00      0.00      0.00
03:30:01 PM      eth0     28.17      3.81      2.37      4.22      0.00      0.00      7.02
03:30:01 PM      eth1 112443.23      0.00  74472.20      0.00      0.00      0.00      0.05
03:30:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:30:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:40:01 PM        lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
03:40:01 PM      eth0     39.85     29.56      3.17     42.19      0.00      0.00      7.00
03:40:01 PM      eth1 116666.73      0.00  77904.30      0.00      0.00      0.00      0.05
03:40:01 PM      eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
03:40:01 PM      eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:           lo      0.19      0.19      0.03      0.03      0.00      0.00      0.00
Average:         eth0     28.97      5.15      2.42      6.46      0.00      0.00      7.07
Average:         eth1  57011.34      0.00  35401.68      0.00      0.00      0.00      0.09
Average:         eth2      0.00      0.00      0.00      0.00      0.00      0.00      0.00
Average:         eth3      0.00      0.00      0.00      0.00      0.00      0.00      0.00

# 2  
Old 08-20-2019
Funny that you call that script a one liner, but I suppose you did fit it all on one line, lol.

I hate all the escaping involved in sending scripts over ssh so I used a quoted heredoc in this solution. The ip and sar commands are passed as input to a single awk using process substitution.

You were a little unclear on the time range required, here for simplicity I avoided using gawk's time functions and simply matched anything for the current hour:

Code:
while read i
do
   echo -n $i","
   echo -n $i,|cut -c1-7 -n
   ssh $i <<'EOF'
       awk -v T=$(date +%H) '
          FNR==1 { file++ }
          !f && file==1 && $0 ~ "(127.127|128.128)" { f=$NF }
          file==2 && $3==f {
             H=(($2=="PM")?12:0) + substr($1,0,2)
             if (H==T) {
                 tot+=$4
                 cnt++
             }
          }
          END {
             if(cnt) print tot / cnt * 8 / 1000 " Mbps"
             else print "0 Mbps"
          }
       ' <(/sbin/ip a) <(sar -n DEV)
EOF
done < /home/ave2.list | sed 'N;s/\n/, /' > /home/rawdata2.log


Last edited by Chubler_XL; 08-20-2019 at 05:41 PM.. Reason: Formatting
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Calculate average from a given set of keys and values

Hello, I am writing a script which expects as its input a hash with student names as the keys and marks as the values. The script then returns array of average marks for student scored 60-70, 70-80, and over 90. Output expected 50-70 1 70-90 3 over 90 0 The test script so far... (4 Replies)
Discussion started by: nans
4 Replies

2. Shell Programming and Scripting

Calculate average, azimut and distance

Gents, Please i will to get the distance and azimut from 2 coordinates: Usig excel formula i get the correct values, but i will like to do it using awk. Example A 35089.0 50345.016 9 75 1 2101774 77 70 79 483911.6 2380106.9 137.4 1 1 6 1 A 35089.0 50345.01620 75... (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

Calculate Average time of one column

Hello dears, I have a log file with records like below and want to get a average of one column based on the search of one specific keyword. 2015-02-07 08:15:28 10.102.51.100 10.112.55.101 "kevin.c" POST ... (2 Replies)
Discussion started by: Newman
2 Replies

4. Shell Programming and Scripting

Calculate average for repeated ID within a data

I have an awk script that gives the following output: Average end-to-end transmission delay 2.7 to 5.7 is 0.635392 seconds Average end-to-end transmission delay 2.1 to 5.1 is 0.66272 seconds Average end-to-end transmission delay 2.1 to 5.1 is 0.691712 seconds Average end-to-end transmission... (4 Replies)
Discussion started by: ENG_MOHD
4 Replies

5. Shell Programming and Scripting

Calculate Average AWK

I want to calculate the average line by line of some files with several lines on them, the files are identical, just want to average the 3rd columns of those files.:wall: Example file: File 1 001 0.046 0.667267 001 0.047 0.672028 001 0.048 0.656025 001 0.049 ... (2 Replies)
Discussion started by: AriasFco
2 Replies

6. Shell Programming and Scripting

AWK novice - calculate the average

Hi, I have the following data in a file for example: P1 XXXXXXX.1 YYYYYYY.1 ZZZ.1 P1 XXXXXXX.2 YYYYYYY.2 ZZZ.2 P1 XXXXXXX.3 YYYYYYY.3 ZZZ.3 P1 XXXXXXX.4 YYYYYYY.4 ZZZ.4 P1 XXXXXXX.5 YYYYYYY.5 ZZZ.5 P1 XXXXXXX.6 YYYYYYY.6 ZZZ.6 P1 XXXXXXX.7 YYYYYYY.7 ZZZ.7 P1 XXXXXXX.8 YYYYYYY.8 ZZZ.8 P2... (6 Replies)
Discussion started by: alex2005
6 Replies

7. Shell Programming and Scripting

Calculate average time using a script

Hello, I'm hoping to get some help on calculating an average time from a list of times (hour:minute:second). Here's what my list looks like right now, it will grow (I can get the full date or change the formatting of this as well): 07:55:31 09:42:00 08:09:02 09:15:23 09:27:45 09:49:26... (4 Replies)
Discussion started by: jaredhanks
4 Replies

8. Programming

calculate average

I have a file which is 2 3 4 5 6 6 so i am writing program in C to calculate mean.. #include<stdio.h> #include<string.h> #include <math.h> double CALL mean(int n , double x) main (int argc, char **argv) { char Buf,SEQ; int i; double result = 0; FILE *fp; (3 Replies)
Discussion started by: cdfd123
3 Replies

9. UNIX for Dummies Questions & Answers

Use awk to calculate average of column 3

Suppose I have 500 files in a directory and I need to Use awk to calculate average of column 3 for each of the file, how would I do that? (6 Replies)
Discussion started by: grossgermany
6 Replies

10. UNIX for Dummies Questions & Answers

calculate average of column 2

Hi I have fakebook.csv as following: F1(current date) F2(popularity) F3(name of book) F4(release date of book) 2006-06-21,6860,"Harry Potter",2006-12-31 2006-06-22,,"Harry Potter",2006-12-31 2006-06-23,7120,"Harry Potter",2006-12-31 2006-06-24,,"Harry Potter",2006-12-31... (0 Replies)
Discussion started by: onthetopo
0 Replies
Login or Register to Ask a Question