Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.46 KB | None | 0 0
  1. #!/user/bin/bash
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. use DBI;
  7. use Data::Dumper;
  8.  
  9. #Script location
  10. my $speedtest_cmd #!/user/bin/bash
  11.  
  12. use warnings;
  13. use strict;
  14.  
  15. use DBI;
  16. use Data::Dumper;
  17.  
  18. #Script location
  19. my $speedtest_cmd = '/usr/bin/speedtest-cli --simple';
  20.  
  21. #Holds metrics we want to go in DB
  22. my @metrics;
  23.  
  24. #Holds results of test
  25. my @results
  26.  
  27. #DB connection params
  28. my $db_host   = '127.0.0.1';
  29. my $db_user   = 'username';
  30. my $db_passwd = 'pwd';
  31.  
  32. # DB to insert into.
  33. my $db_name  = 'speedtest';
  34. my $db_table = 'results';
  35.  
  36. foreach my $line ('$speedtest_cmd') {
  37.   if ($line =~ /^(\w+):\s([\d\.]+)\s/) {
  38.     push(@metrics, lc($1));
  39.     push(@results, lc($2));
  40.   }
  41. }
  42.  
  43. if (!@metrics || !@results) {
  44.   die "There was an error capturing output of the speedtest script.\n";
  45. }
  46.  
  47. foreach my $line ('$speedtest_cmd') {
  48.   if ($line =~ /^(\w+):\s([\d\.]+)\s/) {
  49.     push(@metrics, lc($1));
  50.     push(@results, lc($2));
  51.   }
  52. }
  53.  
  54. foreach my $line ('$speedtest_cmd') {
  55.   if ($line =~ /^(\w+):\s([\d\.]+)\s/) {
  56.     push(@metrics, lc($1));
  57.     push(@results, lc($2));
  58.   }
  59. }
  60.  
  61. if (!@metrics || !@results) {
  62.   die "There was an error capturing output of the speedtest script.\n";
  63. }
  64.  
  65. my $dbh = DBI->connect("DBI:mysql:$db_name", $db_user, $db_passwd, {RaiseError => 1});
  66.  
  67. my $sql = "INSERT INTO $db_table (" . join(', ', @metrics) . ") VALUES (?, ?, ?)";
  68. my $sth = $dbh->prepare($sql);
  69. $sth->execute(@results);
  70.  
  71. $sth->finish();
  72. $dbh->disconnect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement