From 5b4cb18836158bf9169d497a64997a3b553d9fe2 Mon Sep 17 00:00:00 2001 From: Daniel Gabris Date: Thu, 11 Feb 2016 18:23:12 +0100 Subject: [PATCH 1/7] #3 added the feature to seed database with generated userdata --- Dockerfile | 2 + src/seed-test-data.php | 127 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100755 src/seed-test-data.php diff --git a/Dockerfile b/Dockerfile index 122a3ed..56f5857 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,8 +24,10 @@ COPY ./config/sql/mysql/dialup.conf /etc/freeradius/sql/mysql/dialup.conf RUN ln -s /etc/freeradius/sites-available/sql /etc/freeradius/sites-enabled/sql COPY ./src/load-test-data.sh /usr/src/build/load-test-data.sh +COPY ./src/load-test-data.sh /usr/src/build/seed-test-data.php COPY ./sql/test-data.sql /usr/src/build/sql/test-data.sql RUN chmod 755 /usr/src/build/load-test-data.sh +RUN chmod 755 /usr/src/build/seed-test-data.php RUN chmod 755 /usr/local/bin/summarise_data.php RUN ln -s /usr/local/bin/summarise_data.php /etc/cron.hourly/99.summarise_radius_data.php diff --git a/src/seed-test-data.php b/src/seed-test-data.php new file mode 100755 index 0000000..e33a0c4 --- /dev/null +++ b/src/seed-test-data.php @@ -0,0 +1,127 @@ +#!/usr/bin/php +query($query); + if (!$result) { + die($db->error); + } + + // update userinfo + $query = "INSERT into `userinfo` (`username`) values('$username')"; + echo $query."\r\n"; + $result = $db->query($query); + if (!$result) { + die($db->error); + } + + // update user_billing_detail + $query = "INSERT into `user_billing_detail` (`username`, `anniversary_day`, `action`, `status`) values('$username', '$anniversary_day', '$action', '$status')"; + echo $query."\r\n"; + $result = $db->query($query); + if (!$result) { + die($db->error); + } + + // update user_quota + $query = "INSERT into `user_quota` (`username`, `quota_date`, `quota`) values('$username', '$quota_date', '$quota')"; + echo $query."\r\n"; + $result = $db->query($query); + if (!$result) { + die($db->error); + } + + $result = $db->query("SET @disable_triggers = 1"); + if (!$result) { + die($db->error); + } + + // update radacct session dummy row + $query = "INSERT INTO `radacct` (`acctsessionid`, `acctuniqueid`, `UserName`, `GroupName`, `realm`, `NASIPAddress`, `NASPortId`, `nasporttype`, `AcctStartTime`, `AcctStopTime`, `AcctSessionTime`, `acctauthentic`, `connectinfo_start`, `connectinfo_stop`, `AcctInputOctets`, `AcctOutputOctets`, `CalledStationId`, `CallingStationId`, `acctterminatecause`, `servicetype`, `framedprotocol`, `FramedIPAddress`, `acctstartdelay`, `acctstopdelay`, `xascendsessionsvrkey`) + values('0004752B', '86716ad2a8b3d327', '$username', '', '', '114.141.96.4', '', 'ISDN', '$account_starttime', '$account_endtime', '$account_sessiontime', 'RADIUS', '155520000', '155520000', '$account_inputoctects', '$accout_outputoctects', '', 'foobar', 'Port-Error', 'Framed-User', 'PPP', '127.0.0.15', 0, 0, '')"; + echo $query."\r\n"; + $result = $db->query($query); + if (!$result) { + die($db->error); + } + + // add linear data to user_data table + $update= "INSERT into `user_data` (`username`, `datain`, `dataout`, `totaldata`, `data_hour`, `date`) values(?, ?, ?, ?, ?, ?)"; + $stmt=$db->prepare($update); + if (!$stmt) { + die($db->error); + } + $stmt->bind_param("siiiis", $username, $datain, $dataout, $totaldata, $data_hour, $date); + $stmt->execute(); + + $start = strtotime($start_date); + $end = strtotime($end_date); + $pointer = strtotime($start_date); + + $data_slice = floor($total_amount / (abs($end - $start) / 3600)); + $datain += floor($data_slice * 0.4); + $dataout += floor($data_slice * 0.6); + $totaldata = $datain + $dataout; + + echo 'total: '.$total_amount."\r\n"; + echo 'slice: '.$data_slice."\r\n"; + + while ($pointer <= $end) { + + $pointer = $pointer + 60*60; + $data_hour = date('H', $pointer); + $date = date('Y-m-d', $pointer); + echo $date.'-'.$data_hour."\r\n"; + $stmt->execute(); + + } + + $result = $db->query("SET @disable_triggers = NULL"); + if (!$result) { + die($db->error); + } + +?> From 426f3c5d687a705245b0b47f9e5e2a5e51aa23cf Mon Sep 17 00:00:00 2001 From: Daniel Gabris Date: Fri, 12 Feb 2016 04:07:16 +0100 Subject: [PATCH 2/7] #3 changes for comments --- Dockerfile | 2 +- src/seed-test-data.php | 94 +++++++++++++++++++++++++++--------------- 2 files changed, 62 insertions(+), 34 deletions(-) diff --git a/Dockerfile b/Dockerfile index 56f5857..624a19a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ COPY ./config/sql/mysql/dialup.conf /etc/freeradius/sql/mysql/dialup.conf RUN ln -s /etc/freeradius/sites-available/sql /etc/freeradius/sites-enabled/sql COPY ./src/load-test-data.sh /usr/src/build/load-test-data.sh -COPY ./src/load-test-data.sh /usr/src/build/seed-test-data.php +COPY ./src/seed-test-data.php /usr/src/build/seed-test-data.php COPY ./sql/test-data.sql /usr/src/build/sql/test-data.sql RUN chmod 755 /usr/src/build/load-test-data.sh RUN chmod 755 /usr/src/build/seed-test-data.php diff --git a/src/seed-test-data.php b/src/seed-test-data.php index e33a0c4..2530b91 100755 --- a/src/seed-test-data.php +++ b/src/seed-test-data.php @@ -4,14 +4,13 @@ date_default_timezone_set('UTC'); // initialization - $quota = 1073741824000; - $quota_date = '2015-09-17 00:00:00'; - $anniversary_day = 8; + $quota = 0; + $quota_date = ""; $action = "shape"; $status = "normal"; - $account_sessiontime = 150; - $account_inputoctects = 5571; - $accout_outputoctects = 45599; + $account_sessiontime = 1000; + $account_inputoctects = 5000; + $accout_outputoctects = 10000; $today = date("Y-m-d H:i:s"); $longopts = array( @@ -19,32 +18,54 @@ "password:", "startdate:", "enddate::", - "total:", + "totalgb:", + "nasip:", + "quotagb::", + "quotadate::", + "annivsday::" ); $options = getopt("", $longopts); - if(!isset($options["username"]) || !isset($options["password"]) || !isset($options["startdate"]) || !isset($options["total"])) - die('Please input the required parameters. ex. --username=test@testing.com --password=testing123 --startdate="2016-01-01" --enddate="2016-02-01" --total=10'."\r\n"); - var_dump($options); + // check mandatory options + if(!isset($options["username"]) || !isset($options["password"]) || !isset($options["startdate"]) || !isset($options["totalgb"]) || !isset($options["nasip"])) + die('Please input the required parameters. ex. --username=test@testing.com --password=testing123 --startdate="2016-01-01" --enddate="2016-02-01" --totalgb=10 --nasip="3.3.3.3" --quotagb=10 --quotadate="2016-01-01" --annivsday=1'.PHP_EOL); + + //var_dump($options); $username = $options["username"]; $password = $options["password"]; $start_date = date("Y-m-d H:i:s", strtotime($options["startdate"])); - $end_date = $options["enddate"] != "" ? date("Y-m-d H:i:s", strtotime($options["enddate"])) : $today; - $total_amount = $options["total"] * 1073741824; // Giga to byte + $total_amount = $options["totalgb"] * 1073741824; // Giga to byte + $nasip = $options["nasip"]; + + if(isset($options["enddate"])) + $end_date = date("Y-m-d H:i:s", strtotime($options["enddate"])); + else + $end_date = $today; + + if(isset($options["quotagb"])) + $quota = $options["quotagb"] * 1073741824; // Giga to byte + + if(isset($options["quotadate"])) + $quota_date = date("Y-m-d H:i:s", strtotime($options["quotadate"])); + + if(isset($options["annivsday"])) + $anniversary_day = $options["annivsday"]; + else + $anniversary_day = date("d", strtotime($options["startdate"])); $account_starttime = $start_date; $account_endtime = $end_date; - echo $today."\r\n"; - echo $account_starttime."\r\n"; - echo $account_endtime."\r\n"; + //echo $today.PHP_EOL; + //echo $account_starttime.PHP_EOL; + //echo $account_endtime.PHP_EOL; $db = new mysqli("mysql", "radius", "radius", "radius"); // update radcheck $query = "INSERT into `radcheck` (`username`, `attribute`, `op`, `value`) values('$username', 'Cleartext-Password', ':=', '$password')"; - echo $query."\r\n"; + echo $query.PHP_EOL; $result = $db->query($query); if (!$result) { die($db->error); @@ -52,7 +73,7 @@ // update userinfo $query = "INSERT into `userinfo` (`username`) values('$username')"; - echo $query."\r\n"; + echo $query.PHP_EOL; $result = $db->query($query); if (!$result) { die($db->error); @@ -60,29 +81,36 @@ // update user_billing_detail $query = "INSERT into `user_billing_detail` (`username`, `anniversary_day`, `action`, `status`) values('$username', '$anniversary_day', '$action', '$status')"; - echo $query."\r\n"; + echo $query.PHP_EOL; $result = $db->query($query); if (!$result) { die($db->error); } // update user_quota - $query = "INSERT into `user_quota` (`username`, `quota_date`, `quota`) values('$username', '$quota_date', '$quota')"; - echo $query."\r\n"; - $result = $db->query($query); - if (!$result) { - die($db->error); - } - - $result = $db->query("SET @disable_triggers = 1"); - if (!$result) { - die($db->error); + if($quota != 0 && $quota_date != "") { + $query = "INSERT into `user_quota` (`username`, `quota_date`, `quota`) values('$username', '$quota_date', '$quota')"; + echo $query.PHP_EOL; + $result = $db->query($query); + if (!$result) { + die($db->error); + } + + $result = $db->query("SET @disable_triggers = 1"); + if (!$result) { + die($db->error); + } } // update radacct session dummy row + $acctsessionid = bin2hex(openssl_random_pseudo_bytes(4)); + $acctuniqueid = bin2hex(openssl_random_pseudo_bytes(8)); + //echo "accsessionid: ".$acctsessionid.PHP_EOL; + //echo "acctuniqueid: ".$acctuniqueid.PHP_EOL; + $query = "INSERT INTO `radacct` (`acctsessionid`, `acctuniqueid`, `UserName`, `GroupName`, `realm`, `NASIPAddress`, `NASPortId`, `nasporttype`, `AcctStartTime`, `AcctStopTime`, `AcctSessionTime`, `acctauthentic`, `connectinfo_start`, `connectinfo_stop`, `AcctInputOctets`, `AcctOutputOctets`, `CalledStationId`, `CallingStationId`, `acctterminatecause`, `servicetype`, `framedprotocol`, `FramedIPAddress`, `acctstartdelay`, `acctstopdelay`, `xascendsessionsvrkey`) - values('0004752B', '86716ad2a8b3d327', '$username', '', '', '114.141.96.4', '', 'ISDN', '$account_starttime', '$account_endtime', '$account_sessiontime', 'RADIUS', '155520000', '155520000', '$account_inputoctects', '$accout_outputoctects', '', 'foobar', 'Port-Error', 'Framed-User', 'PPP', '127.0.0.15', 0, 0, '')"; - echo $query."\r\n"; + values('$acctsessionid', '$acctuniqueid', '$username', '', '', '$nasip', '', 'ISDN', '$account_starttime', '$account_endtime', '$account_sessiontime', 'RADIUS', '155520000', '155520000', '$account_inputoctects', '$accout_outputoctects', '', 'foobar', 'Port-Error', 'Framed-User', 'PPP', '127.0.0.15', 0, 0, '')"; + echo $query.PHP_EOL; $result = $db->query($query); if (!$result) { die($db->error); @@ -106,15 +134,15 @@ $dataout += floor($data_slice * 0.6); $totaldata = $datain + $dataout; - echo 'total: '.$total_amount."\r\n"; - echo 'slice: '.$data_slice."\r\n"; + //echo 'total: '.$total_amount.PHP_EOL; + //echo 'slice: '.$data_slice.PHP_EOL; while ($pointer <= $end) { $pointer = $pointer + 60*60; $data_hour = date('H', $pointer); $date = date('Y-m-d', $pointer); - echo $date.'-'.$data_hour."\r\n"; + echo $date.' '.$data_hour.PHP_EOL; $stmt->execute(); } From 4815bddc21e4bd3504772647fdd276b343f08f12 Mon Sep 17 00:00:00 2001 From: Daniel Gabris Date: Sat, 13 Feb 2016 09:03:31 +0100 Subject: [PATCH 3/7] #3 changes for travis test --- .travis.yml | 3 +- Dockerfile | 2 ++ src/seed-test-data.php | 37 ++++++++++++++++----- src/test-seed.php | 74 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 107 insertions(+), 9 deletions(-) create mode 100755 src/test-seed.php diff --git a/.travis.yml b/.travis.yml index a2ba2f0..25afe5f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,8 @@ before_install: script: - sleep 30 - docker exec "radius1" /bin/bash /usr/src/build/test-radius.sh - - docker exec "radius1" /bin/bash /usr/src/build/load-test-data.sh + - docker exec "radius1" /bin/bash /usr/src/build/seed-test-data.php --username=test@testing.com --password=testing123 --startdate="2016-01-01" --enddate="2016-02-01" --totalgb=10 --nasip="3.3.3.3" --quotagb=10 --quotadate="2016-01-01" --annivsday=1 + - docker exec "radius1" /bin/bash /usr/src/build/test-seed.php --username=test@testing.com --password=testing123 --totalgb=10 - docker exec "radius1" radtest test@testing.com testing123 127.0.0.1 0 testing123 - radtest test@testing.com testing123 127.0.0.1 0 password diff --git a/Dockerfile b/Dockerfile index 624a19a..3571713 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,9 +25,11 @@ RUN ln -s /etc/freeradius/sites-available/sql /etc/freeradius/sites-enabled/sql COPY ./src/load-test-data.sh /usr/src/build/load-test-data.sh COPY ./src/seed-test-data.php /usr/src/build/seed-test-data.php +COPY ./src/seed-test-data.php /usr/src/build/test-seed.php COPY ./sql/test-data.sql /usr/src/build/sql/test-data.sql RUN chmod 755 /usr/src/build/load-test-data.sh RUN chmod 755 /usr/src/build/seed-test-data.php +RUN chmod 755 /usr/src/build/test-seed.php RUN chmod 755 /usr/local/bin/summarise_data.php RUN ln -s /usr/local/bin/summarise_data.php /etc/cron.hourly/99.summarise_radius_data.php diff --git a/src/seed-test-data.php b/src/seed-test-data.php index 2530b91..ff5d08b 100755 --- a/src/seed-test-data.php +++ b/src/seed-test-data.php @@ -95,11 +95,12 @@ if (!$result) { die($db->error); } + } - $result = $db->query("SET @disable_triggers = 1"); - if (!$result) { - die($db->error); - } + // diable triggers + $result = $db->query("SET @disable_triggers = 1"); + if (!$result) { + die($db->error); } // update radacct session dummy row @@ -130,23 +131,43 @@ $pointer = strtotime($start_date); $data_slice = floor($total_amount / (abs($end - $start) / 3600)); - $datain += floor($data_slice * 0.4); - $dataout += floor($data_slice * 0.6); - $totaldata = $datain + $dataout; + $datain = floor($data_slice * 0.4); + $dataout = $data_slice - $datain; + $totaldata = $data_slice; //echo 'total: '.$total_amount.PHP_EOL; //echo 'slice: '.$data_slice.PHP_EOL; - while ($pointer <= $end) { + $restdata = $total_amount; + while ($restdata > $data_slice) { $pointer = $pointer + 60*60; $data_hour = date('H', $pointer); $date = date('Y-m-d', $pointer); echo $date.' '.$data_hour.PHP_EOL; $stmt->execute(); + if (!$stmt) { + die($db->error); + } + + $restdata -= $data_slice; + + } + // process the rest + $datain = $restdata; + $dataout = 0; + $totaldata = $restdata; + $pointer = $pointer + 60*60; + $data_hour = date('H', $pointer); + $date = date('Y-m-d', $pointer); + echo $date.' '.$data_hour.' '.$totaldata.' '.$datain.PHP_EOL; + $stmt->execute(); + if (!$stmt) { + die($db->error); } + // enable triggers $result = $db->query("SET @disable_triggers = NULL"); if (!$result) { die($db->error); diff --git a/src/test-seed.php b/src/test-seed.php new file mode 100755 index 0000000..fb64995 --- /dev/null +++ b/src/test-seed.php @@ -0,0 +1,74 @@ +#!/usr/bin/php +query($query); + if ($result->num_rows != 1) { + exit(1); + } + + // update userinfo + $query = "SELECT * FROM `userinfo` WHERE username='$username'"; + echo $query.PHP_EOL; + $result = $db->query($query); + if ($result->num_rows != 1) { + exit(1); + } + + // update user_billing_detail + $query = "SELECT * FROM `user_billing_detail` WHERE username='$username'"; + echo $query.PHP_EOL; + $result = $db->query($query); + if ($result->num_rows != 1) { + exit(1); + } + + // update user_quota + $query = "SELECT * FROM `user_quota` WHERE username='$username'"; + echo $query.PHP_EOL; + $result = $db->query($query); + if ($result->num_rows != 1) { + exit(1); + } + + $query = "SELECT * FROM `radacct` WHERE username='$username'"; + echo $query.PHP_EOL; + $result = $db->query($query); + if ($result->num_rows != 1) { + exit(1); + } + + $query = "SELECT SUM(`totaldata`) as totalsum FROM `user_data` WHERE username='$username'"; + echo $query.PHP_EOL; + $result = $db->query($query); + $row = $result->fetch_assoc(); + echo $row['totalsum']." == ".$total_amount.PHP_EOL; + if($row['totalsum'] != $total_amount) { + exit(1); + } + + echo "success!".PHP_EOL; + exit(0); + +?> From 347ecec0e27dce64d8e2d508c63d7e791fa616da Mon Sep 17 00:00:00 2001 From: Daniel Gabris Date: Sat, 13 Feb 2016 09:28:42 +0100 Subject: [PATCH 4/7] #3 changes --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 25afe5f..1dcda78 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,8 +17,8 @@ before_install: script: - sleep 30 - docker exec "radius1" /bin/bash /usr/src/build/test-radius.sh - - docker exec "radius1" /bin/bash /usr/src/build/seed-test-data.php --username=test@testing.com --password=testing123 --startdate="2016-01-01" --enddate="2016-02-01" --totalgb=10 --nasip="3.3.3.3" --quotagb=10 --quotadate="2016-01-01" --annivsday=1 - - docker exec "radius1" /bin/bash /usr/src/build/test-seed.php --username=test@testing.com --password=testing123 --totalgb=10 + - docker exec "radius1" /usr/bin/php /usr/src/build/seed-test-data.php --username=test@testing.com --password=testing123 --startdate="2016-01-01" --enddate="2016-02-01" --totalgb=10 --nasip="3.3.3.3" --quotagb=10 --quotadate="2016-01-01" --annivsday=1 + - docker exec "radius1" /usr/bin/php /usr/src/build/test-seed.php --username=test@testing.com --password=testing123 --totalgb=10 - docker exec "radius1" radtest test@testing.com testing123 127.0.0.1 0 testing123 - radtest test@testing.com testing123 127.0.0.1 0 password From 4d58e25fdfbece3f996689883f1abb096a3b94b5 Mon Sep 17 00:00:00 2001 From: Daniel Gabris Date: Sat, 13 Feb 2016 09:35:57 +0100 Subject: [PATCH 5/7] #3 changes --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 1dcda78..7751b13 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ before_install: script: - sleep 30 - docker exec "radius1" /bin/bash /usr/src/build/test-radius.sh - - docker exec "radius1" /usr/bin/php /usr/src/build/seed-test-data.php --username=test@testing.com --password=testing123 --startdate="2016-01-01" --enddate="2016-02-01" --totalgb=10 --nasip="3.3.3.3" --quotagb=10 --quotadate="2016-01-01" --annivsday=1 + - docker exec "radius1" /usr/bin/php /usr/src/build/seed-test-data.php --username=test@testing.com --password=testing123 --startdate="2016-01-01" --totalgb=10 --nasip="3.3.3.3" --quotagb=10 --quotadate="2016-01-01" --annivsday=1 - docker exec "radius1" /usr/bin/php /usr/src/build/test-seed.php --username=test@testing.com --password=testing123 --totalgb=10 - docker exec "radius1" radtest test@testing.com testing123 127.0.0.1 0 testing123 - radtest test@testing.com testing123 127.0.0.1 0 password From f74d74c36b82f1965a28832ebf2fda3165283174 Mon Sep 17 00:00:00 2001 From: Daniel Gabris Date: Sat, 13 Feb 2016 09:48:49 +0100 Subject: [PATCH 6/7] #3 changes --- src/test-seed.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/test-seed.php b/src/test-seed.php index fb64995..be7e17f 100755 --- a/src/test-seed.php +++ b/src/test-seed.php @@ -8,11 +8,13 @@ ); $options = getopt("", $longopts); - //var_dump($options); + var_dump($options); // check mandatory options - if(!isset($options["username"]) || !isset($options["password"]) || !isset($options["totalgb"])) - die('Please input the required parameters. ex. --username=test@testing.com --password=testig123 --totalgb=10'.PHP_EOL); + if(!isset($options["username"]) || !isset($options["password"]) || !isset($options["totalgb"])) { + echo 'Please input the required parameters. ex. --username=test@testing.com --password=testig123 --totalgb=10' . PHP_EOL; + exit(1); + } $username = $options["username"]; $password = $options["password"]; From c31b289f2159d591a9e11602d5e26f886c8f1791 Mon Sep 17 00:00:00 2001 From: Daniel Gabris Date: Sat, 13 Feb 2016 09:54:34 +0100 Subject: [PATCH 7/7] #3 changes --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 3571713..2077e9e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,7 +25,7 @@ RUN ln -s /etc/freeradius/sites-available/sql /etc/freeradius/sites-enabled/sql COPY ./src/load-test-data.sh /usr/src/build/load-test-data.sh COPY ./src/seed-test-data.php /usr/src/build/seed-test-data.php -COPY ./src/seed-test-data.php /usr/src/build/test-seed.php +COPY ./src/test-seed.php /usr/src/build/test-seed.php COPY ./sql/test-data.sql /usr/src/build/sql/test-data.sql RUN chmod 755 /usr/src/build/load-test-data.sh RUN chmod 755 /usr/src/build/seed-test-data.php