Created
June 25, 2015 13:40
-
-
Save gms8994/8333e227de8ed11b2e29 to your computer and use it in GitHub Desktop.
Automated speedtest. Needs speedtest-cli installed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use Data::Dumper; | |
use DBI; | |
my $dbh = DBI->connect("", "", "", { AutoCommit => 1 }); | |
my $data = `/usr/local/bin/speedtest-cli --simple`; | |
my @lines = split /\n/, $data; | |
my %values; | |
foreach my $line (@lines) { | |
my @values = split / /, $line; | |
if ($values[0] =~ /ping/i) { | |
$values{ping} = $values[1]; | |
} elsif ($values[0] =~ /download/i) { | |
$values{download} = to_bps($values[1], $values[2]); | |
} elsif ($values[0] =~ /upload/i) { | |
$values{upload} = to_bps($values[1], $values[2]); | |
} | |
} | |
$dbh->do("INSERT INTO speedtest (location, ping, upload, download, created) VALUES ('home', $values{ping}, $values{upload}, $values{download}, CURRENT_TIMESTAMP)"); | |
sub to_bps { | |
my ($value, $persec) = @_; | |
if ($persec =~ /^Mb/) { | |
$value *= 1024 * 1024 * 1024; | |
} | |
return $value; | |
} | |
sub dd { | |
die Dumper(\@_); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- DROP TABLE "public"."speedtest"; | |
CREATE TABLE "public"."speedtest" ( | |
"id" integer NOT NULL DEFAULT nextval('speedtest_id_seq'::regclass), | |
"location" character varying(50), | |
"ping" double precision, | |
"upload" double precision, | |
"download" double precision, | |
"created" timestamp without time zone, | |
CONSTRAINT "speedtest_pkey" PRIMARY KEY (id) | |
) WITHOUT OIDS; | |
-- Indexes | |
CREATE UNIQUE INDEX speedtest_pkey ON speedtest USING btree (id); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment