Advertisement
StenaviN

Calculate Unique data based on a series of snapshots

Apr 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.20 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. if [[ "$1" != "daily" ]] && [[ "$1" != "hourly" ]]; then
  4.   echo "Usage: ${BASH_SOURCE[0]} <daily|hourly>"
  5.   exit 1
  6. fi
  7.  
  8. awk -v type="$1" '
  9. function human(size) {
  10.  i=1
  11.  units=" KMGTPEZY";
  12.  while (size >= 1024) {
  13.    size /= 1024;
  14.    i++;
  15.  }
  16.  
  17.  unit=(i == 1 ? "" : substr(units, i, 1))
  18.  return sprintf("%.2f%sB", size, unit)
  19. }
  20.  
  21. BEGIN {
  22.  ub=0; tb=0; gtu=0; gtm=0; records=0;
  23.  date=""
  24.  system("hostname")
  25.  print "--------------------------------------------------"
  26.  printf "%*s %10s %10s %10s\n", type=="daily" ? 10 : 16, "Date", "Unique", "Meta", "Total"
  27.  print "--------------------------------------------------"
  28. }
  29.  
  30. NR == 1 {
  31.  date=$1
  32.  ub=strtonum($2)
  33.  tb=strtonum($3)
  34. }
  35.  
  36. NR != 1 {
  37.  ue=strtonum($2)
  38.  te=strtonum($3)
  39.  u=ue-ub;
  40.  if (tb != 0) m=(te-tb)/65536*24;
  41.  else m=0;
  42.  gtu+=u; gtm+=m; records+=1;
  43.  
  44.  printf "%s %10s %10s %10s\n", substr(date, 1, type=="daily" ? 10 : 16), human(u), human(m), human(u+m)
  45.  
  46.  ub=ue; tb=te;
  47.  date=$1
  48. }
  49.  
  50. END {
  51.  print "=================================================="
  52.  printf "%*s %10s %10s %10s\n", (type=="daily" ? 10 : 16), "Total:", human(gtu), human(gtm), human(gtu+gtm)
  53. }
  54. ' /root/ddfs-uniq-$1.txt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement