spacepaste

  1.  
  2. #!/bin/bash
  3. domain=mydomain.net
  4. spprouters=`dig _spp._udp.$domain srv +short | awk '{print $NF}' | sed 's/\.$//'`
  5. dir="$(mktemp -d)"
  6. production_dir=/var/www/widgets/htdocs/cdr
  7. mkdir -p $production_dir
  8. echo "Working folder set to: $dir"
  9. #grab data off remote servers
  10. for server in $spprouters
  11. do
  12. echo "*****Grab SQL Data from Remote Server: $server *****"
  13. # Dump data to csv. Need elevated priv to do copy command
  14. psql -U master testdb -h $server -t -c "copy (SELECT CASE WHEN DATE_TRUNC('MONTH', ABSTIME(start_ts)) = DATE_TRUNC('MONTH', CURRENT_DATE) THEN 'current' ELSE DATE_TRUNC('MONTH', ABSTIME(start_ts))::DATE::TEXT END,
  15. id,
  16. status,
  17. class,
  18. dir,
  19. volid,
  20. f_user,
  21. t_user,
  22. ABSTIME(start_ts),
  23. ABSTIME(end_ts),
  24. elapsed,
  25. callid, f_uri, t_uri, lcr_tag
  26. FROM widgets) to '/tmp/"$server".db' With CSV DELIMITER ',';"
  27. scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $server:/tmp/"$server.db" "$dir/"
  28. first_time=1
  29. #process data
  30. for tmpfile in $dir/$server.db
  31. do
  32. echo "Evaluate $tmpfile"
  33. number_of_records=$(cat $tmpfile | wc -l)
  34. echo "number of records in $tmpfile.db file is: $number_of_records"
  35. if [ "$number_of_records" -gt 0 ]; then
  36. echo "==== Processing $tmpfile ===="
  37. while IFS=, read -r date_for_filename rec_id rec_id_onwards; do
  38. #any CDR that's current will stay in the db. Don't add to inventory for deletion.
  39. if ! [ "$date_for_filename" == "current" ]
  40. then
  41. echo "$rec_id" >> $dir/$server.records_to_delete.txt;
  42. else
  43. if [ $first_time == 1 ]
  44. then
  45. #delete the existing .current.csv file if this first time trying to write to it this cycle.
  46. echo "tested first time and its true"
  47. tmpname="${server#*.}" # take file name and strip off server id
  48. rm $production_dir/$tmpname.current.csv
  49. first_time=0
  50. fi
  51. fi
  52. echo "$rec_id,$rec_id_onwards" >> $dir/$server.$date_for_filename.csv;
  53. done < $tmpfile
  54. mv $tmpfile $tmpfile.done
  55. #clean up any trailing , in records_to_delete file
  56. sed '$s/,$//' $dir/$server.records_to_delete.txt > /dev/null
  57. #copy records to delete file to respective servers. faster than running from widgets.
  58. scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null $dir/$server.records_to_delete.txt $server:/tmp/
  59. db_count_pre_prune=$(psql -U master testdb -h $server -t -c "SELECT COUNT(*) FROM widgets")
  60. echo "$db_count_pre_prune records in db before pruning db"
  61. # trigger the deletion on remote machine.
  62. deletion_status=$(psql -U master testdb -h $server -c "CREATE TEMP TABLE tmp_cdr (id int); COPY tmp_cdr FROM '/tmp/$server.records_to_delete.txt'; DELETE FROM widgets USING tmp_cdr WHERE widgets.id = tmp_cdr.id;")
  63. echo "$deletion_status is results of deletion attempt"
  64. db_count_post_prune=$(psql -U master testdb -h $server -t -c "SELECT COUNT(*) FROM widgets")
  65. echo "$db_count_post_prune $server " >> "$dir/audit.txt"
  66. else
  67. echo "No records to process on $server"
  68. fi
  69. done
  70. done
  71. #validate remaining records in db.
  72. total_current_csv_records="$(wc -l $dir/*current.csv |awk '{print $1}' | tail -1)"
  73. echo "$total_current_csv_records = number of records is *current.csv files"
  74. total_in_db=$(cat $dir/audit.txt | awk '{print $1}' | awk '{ sum+=$1 } END { print sum }')
  75. echo "$total_in_db = number of records remaining in db"
  76. if ! [ "$total_in_db" == "$total_current_csv_records" ]
  77. then
  78. echo "$(date) - whoops! Expecting $total_current_csv_records records (combined) in servers. But actual count is $total_in_db." >> $production_dir/cdr-prune.exceptions
  79. fi
  80. cd $dir
  81. for csvfile in *.csv
  82. do
  83. echo "$csvfile is the csvfile"
  84. tmpname="${csvfile#*.}" # take file name and strip off server id
  85. cat $csvfile >> $production_dir/$tmpname
  86. done
  87. #Clean up
  88. rm -r $dir
  89.