Guest User

Untitled

a guest
Mar 24th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Path to speedtest-cli (Change this to match the path to your speedtest-cli executable)
  4. SPEEDTEST_CLI_PATH="/usr/local/bin/speedtest-cli"
  5.  
  6. # Path to the sqlite database (Change this to suit your needs)
  7. #DB_PATH="/home/lino/speedlog.db"
  8. DB_PATH="/var/www/speedtest-api/speedtest.db"
  9.  
  10. # Create test table SQL statement
  11. SQL_CREATE=" \
  12. create table tests ( \
  13. id integer PRIMARY KEY AUTOINCREMENT, \
  14. server_id integer, \
  15. sponsor text, \
  16. server_name text, \
  17. timestamp text, \
  18. distance real, \
  19. ping real, \
  20. download real, \
  21. upload real, \
  22. share text, \
  23. ip_address text \
  24. );"
  25.  
  26. # Input Field Seperator for splitting the CSV result into an array
  27. IFS=","
  28.  
  29. # Create Sqlite database and table if it does not exist
  30. if ! [ -f "$DB_PATH" ]
  31. then
  32. sqlite3 $DB_PATH "$SQL_CREATE"
  33. fi
  34.  
  35. # Do Speed Test
  36. result=$($SPEEDTEST_CLI_PATH --csv)
  37.  
  38. # Result to Array
  39. arr=($result)
  40.  
  41. # Transform values
  42. arr[6]=$(awk "BEGIN {print ${arr[6]}/1000000}")
  43. arr[7]=$(awk "BEGIN {print ${arr[7]}/1000000}")
  44.  
  45. # Array to Sqlite database
  46. sqlite3 $DB_PATH " \
  47. insert into tests ( \
  48. server_id, \
  49. sponsor, \
  50. server_name, \
  51. timestamp, \
  52. distance, \
  53. ping, \
  54. download, \
  55. upload, \
  56. share, \
  57. ip_address \
  58. ) values ( \
  59. ${arr[0]}, \
  60. '${arr[1]}', \
  61. '${arr[2]}', \
  62. '${arr[3]}', \
  63. ${arr[4]%.*}, \
  64. ${arr[5]%.*}, \
  65. ${arr[6]}, \
  66. ${arr[7]}, \
  67. '${arr[8]}', \
  68. '${arr[9]}' \
  69. );"
Add Comment
Please, Sign In to add comment