Mizuhara_Chizuru

awk parser

Aug 20th, 2026
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Awk 2.47 KB | Cybersecurity | 0 0
  1. BEGIN {
  2.     recv_bytes = 0;
  3.     start_time = 999999;
  4.     stop_time = 0;
  5.     total_delay = 0;
  6.    
  7.     send_pkt = 0;
  8.     recv_pkt = 0;
  9.     drop_pkt = 0;
  10. }
  11. {
  12.     event = $1;
  13.     time = $2;
  14.     trace_type = $4;
  15.     pkt_id = $6;
  16.     pkt_type = $7;    # Column 7 is the packet type (e.g., cbr, tcp, AODV)
  17.     pkt_size = $8;
  18.  
  19.     # 1. Track exactly how many packets are SENT from the Application layer
  20.     if (event == "s" && trace_type == "AGT") {
  21.         send_pkt++;
  22.         send_time[pkt_id] = time;
  23.     }
  24.  
  25.     # 2. Track exactly how many packets are RECEIVED at the Application layer
  26.     if (event == "r" && trace_type == "AGT") {
  27.         recv_bytes += pkt_size;
  28.         recv_pkt++;
  29.        
  30.         if (pkt_id in send_time) {
  31.             delay = time - send_time[pkt_id];
  32.             total_delay += delay;
  33.         }
  34.  
  35.         if (time < start_time) {
  36.             start_time = time;
  37.         }
  38.         if (time > stop_time) {
  39.             stop_time = time;
  40.         }
  41.     }
  42.  
  43.     # 3. Track packets DROPPED anywhere in the network (MAC, Router, or Queue layer)
  44.     # We only count data packets (tcp or cbr), ignoring dropped routing packets
  45.     if (event == "D" && (pkt_type == "cbr" || pkt_type == "tcp")) {
  46.         drop_pkt++;
  47.     }
  48. }
  49. END {
  50.     duration = stop_time - start_time;
  51.    
  52.     # Calculate Packet Delivery Ratio (PDR) as a percentage
  53.     if (send_pkt > 0) {
  54.         pdr = (recv_pkt / send_pkt) * 100;
  55.     } else {
  56.         pdr = 0;
  57.     }
  58.    
  59.     if (recv_pkt > 0) {
  60.         throughput = (recv_bytes * 8) / duration / 1000;
  61.         avg_delay = (total_delay / recv_pkt) * 1000;
  62.        
  63.         printf "========================================\n";
  64.         printf "          SIMULATION SCOREBOARD         \n";
  65.         printf "========================================\n";
  66.         printf "Total Packets Sent  : %d\n", send_pkt;
  67.         printf "Total Packets Recv  : %d\n", recv_pkt;
  68.         printf "Total Packets Drop  : %d\n", drop_pkt;
  69.         printf "Packet Delivery Ratio: %.2f %%\n", pdr;
  70.         printf "----------------------------------------\n";
  71.         printf "Total Data Received : %d bytes\n", recv_bytes;
  72.         printf "Simulation Duration : %.2f seconds\n", duration;
  73.         printf "Overall Throughput  : %.2f kbps\n", throughput;
  74.         printf "Average E2E Delay   : %.2f ms\n", avg_delay;
  75.         printf "========================================\n";
  76.     } else {
  77.         print "No data packets were received at the AGT layer.";
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment