From c1b7196f415109629c0538b5e5b2312dfe781ba5 Mon Sep 17 00:00:00 2001 From: Deon George Date: Thu, 4 Aug 2011 12:53:33 +1000 Subject: [PATCH] Added traffic collection of Exetel Ethernet --- modules/adsl/classes/model/adsl/supplier.php | 7 + .../classes/controller/task/service.php | 21 +- .../classes/service/traffic/adsl/exetelpe.php | 180 ++++++++++++++++++ 3 files changed, 199 insertions(+), 9 deletions(-) create mode 100644 modules/service/classes/service/traffic/adsl/exetelpe.php diff --git a/modules/adsl/classes/model/adsl/supplier.php b/modules/adsl/classes/model/adsl/supplier.php index d1eb51cd..82b85af1 100644 --- a/modules/adsl/classes/model/adsl/supplier.php +++ b/modules/adsl/classes/model/adsl/supplier.php @@ -37,5 +37,12 @@ class Model_ADSL_Supplier extends ORMOSB { return $services; } + + /** + * Return a list of active suppliers + */ + public function active() { + return $this->where('status','=',1)->find_all(); + } } ?> diff --git a/modules/service/classes/controller/task/service.php b/modules/service/classes/controller/task/service.php index 5537a96c..67826832 100644 --- a/modules/service/classes/controller/task/service.php +++ b/modules/service/classes/controller/task/service.php @@ -11,13 +11,18 @@ * @license http://dev.osbill.net/license.html */ class Controller_Task_Service extends Controller_Template { + private function _traffic_suppliers($active=FALSE) { + $suppliers = ORM::factory('adsl_supplier'); + + return $active ? $suppliers->active() : $suppliers->find_all(); + } + /** * List all services by their default checkout method */ public function action_gettraffic() { - // @todo This things to collect traffic on needs to be configurable - foreach (array('ExetelVisp','ExetelHSPA','PeopleAgent','iiNetADSL') as $source) { - $traffic = Service_Traffic_ADSL::instance($source); + foreach ($this->_traffic_suppliers(TRUE) as $aso) { + $traffic = Service_Traffic_ADSL::instance($aso->name); $traffic->update_traffic(); } @@ -27,9 +32,8 @@ class Controller_Task_Service extends Controller_Template { * Charges for excess traffic usage */ public function action_chargetraffic() { - // @todo This things to collect traffic on needs to be configurable - foreach (array('ExetelVisp','ExetelHSPA','PeopleAgent','iiNetADSL') as $source) { - $traffic = Service_Traffic_ADSL::instance($source); + foreach ($this->_traffic_suppliers(TRUE) as $aso) { + $traffic = Service_Traffic_ADSL::instance($aso->name); $traffic->charge_excess_traffic(); } @@ -39,9 +43,8 @@ class Controller_Task_Service extends Controller_Template { * Send alerts to users when they exceed their traffic allowance */ public function action_alerttraffic() { - // @todo This things to collect traffic on needs to be configurable - foreach (array('ExetelVisp','ExetelHSPA','PeopleAgent','iiNetADSL') as $source) { - $traffic = Service_Traffic_ADSL::instance($source); + foreach ($this->_traffic_suppliers(TRUE) as $aso) { + $traffic = Service_Traffic_ADSL::instance($aso->name); $traffic->alert_traffic(); } diff --git a/modules/service/classes/service/traffic/adsl/exetelpe.php b/modules/service/classes/service/traffic/adsl/exetelpe.php new file mode 100644 index 00000000..7f1b37b0 --- /dev/null +++ b/modules/service/classes/service/traffic/adsl/exetelpe.php @@ -0,0 +1,180 @@ +fetchresult = TRUE; + + // If we have already collected the date data, return it. + if (! empty(Service_Traffic_ADSL_ExetelPE::$return[$date])) + return Service_Traffic_ADSL_ExetelPE::$return[$date]; + + include_once 'includes/kohana/modules/simplehtmldom/classes/simple_html_dom.php'; + + // Find our services that need to be collected this way. + $update = array(); + + foreach ($this->so->services() as $so) { + if ($so->service_adsl->service_stats_collect AND $so->service_adsl->service_stats_lastupdate < $date) { + // Start Session + $request = Request::factory($this->so->stats_url) + ->method('POST') + ->post($this->login_user_field,$so->service_adsl->service_username) + ->post($this->login_pass_field,$so->service_adsl->service_password) + ->post('doLogin',1) + ->post('submit','Login'); + + $request->get_client()->options($this->curlopts+array( + CURLOPT_COOKIEJAR=>sprintf('/tmp/usage.cookies.%s.txt',$so->service_adsl->service_number), + )); + + $response = $request->execute(); + $data = $response->body(); + + if (! $data) { + // @todo Log into a log file + printf('Bad fetch for %s [%s]',$so->service_adsl->service_number,$this->so->stats_lastupdate); + #$html = new simple_html_dom(); + #$html->load($data); + #$html->save(sprintf('/afs/local/tmp/usage.%s.%s.login.html',$so->service_adsl->service_number,'login')); + continue; + } + + for ($servicedate=date('Y-m-d',strtotime($this->so->stats_lastupdate.'+1 day')); + $servicedate <= $this->today; + $servicedate=date('Y-m-d',strtotime('+1 month',strtotime(date('Y-m',strtotime($servicedate)).'-01')))) { + + $lastday = date('Y-m-d',strtotime('-1 second',strtotime('+1 month',strtotime(date('Y-m',strtotime($servicedate)).'-01')))); + if (strtotime($lastday) > time()) + $lastday = date('Y-m-d',strtotime('yesterday')); + + $html = new simple_html_dom(); + $notdebug = TRUE; + if ($notdebug) { + $request = Request::factory($this->so->stats_url.'usage_customize_query.php') + ->method('POST') + ->post('year_search_key',date('Y',strtotime($servicedate))) + ->post('month_search_key',date('m',strtotime($servicedate))) + ->post('start_day_search_key',date('d',strtotime($servicedate))) + ->post('end_day_search_key',date('d',strtotime($lastday))) + ->post('do_usage_search',1); + + $request->get_client()->options($this->curlopts+array( + CURLOPT_COOKIEFILE=>sprintf('/tmp/usage.cookies.%s.txt',$so->service_adsl->service_number), + )); + + $response = $request->execute(); + $result = $response->body(); + + $html->load($result); + #$html->save(sprintf('/afs/local/tmp/usage.%s.%s.html',$so->service_adsl->service_number,$servicedate)); + } else { + $html->load_file(sprintf('/afs/local/tmp/usage.%s.%s.html',$so->service_adsl->service_number,$servicedate)); + } + + $header = array(); + $data = array(); + + foreach ($html->find('td') as $index => $fieldset) { + if (! preg_match('/Usage Detail/',$fieldset->plaintext)) + continue; + + foreach ($fieldset->find('table',0)->find('tr',0)->find('td',0)->find('table',0)->find('tr') as $key => $values) { + foreach ($values->children() as $a => $b) { + #print_r(array('a'=>$a,'b'=>$b->plaintext)); + + # Header + if ($key == 0) { + switch ($b->plaintext) { + case 'Date': $header[$a] = 'date'; break; + case 'OffPeak Upload': $header[$a] = 'up_offpeak'; break; + case 'Peak Upload': $header[$a] = 'up_peak'; break; + case 'Upload': $header[$a] = 'up_peak'; break; + case 'OffPeak Download': $header[$a] = 'down_offpeak'; break; + case 'Peak Download': $header[$a] = 'down_peak'; break; + case 'Download': $header[$a] = 'down_peak'; break; + case 'Duration': break; + default: + printf('Unkown header :%s',$b->plaintext); + $this->fetchresult = FALSE; + continue; + } + #echo "INDEX: $key:$a\n"; + #echo "TAG: ".$b->tag."\n"; + #echo "HEADER: ".$b->plaintext."\n"; + + # Data + } else { + if (isset($header[$a])) + $data[$header[$a]] = $b->plaintext; + #echo "INDEX: $key:$a\n"; + #echo "TAG: ".$b->tag."\n"; + #echo "VALUES: ".$b->plaintext."\n"; + } + } + + if (isset($data['date']) && preg_match('/[0-9]{4}$/',$data['date'])) { + $sdate = date('Y-m-d',strtotime(str_replace('/','-',$data['date']))); + unset($data['date']); + + if (isset($update[$so->service_adsl->service_number][$sdate])) + foreach ($data as $key => $value) + $update[$so->service_adsl->service_number][$sdate][$key] += $value; + else + $update[$so->service_adsl->service_number][$sdate] = $data; + + $update[$so->service_adsl->service_number][$sdate]['service'] = $so->service_adsl->service_number; + $update[$so->service_adsl->service_number][$sdate]['date'] = $sdate; + } + } + } + } + + // If we got here and have data, we had a good fetch, update the stats date + $so->service_adsl->service_stats_lastupdate = $lastday; + $so->service_adsl->save(); + } + } + + // If there are no updates, return an empty array + if (! $update) + return array(); + + // Reformat the data into date order. + foreach ($update as $service => $sdata) + foreach ($sdata as $sdate => $details) + Service_Traffic_ADSL_ExetelPE::$return[$sdate][$service] = $details; + + // If the date we want is empty, return an array + if (empty(Service_Traffic_ADSL_ExetelPE::$return[$date])) + return array(); + + // Return the date we asked for + return Service_Traffic_ADSL_ExetelPE::$return[$date]; + } +} +?>