Skip to content

Instantly share code, notes, and snippets.

@gms8994
Created June 25, 2015 13:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gms8994/8333e227de8ed11b2e29 to your computer and use it in GitHub Desktop.
Save gms8994/8333e227de8ed11b2e29 to your computer and use it in GitHub Desktop.
Automated speedtest. Needs speedtest-cli installed.
#!/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(\@_);
}
-- 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