Skip to content

Commit 8fc75da

Browse files
author
Marius Burkard
committed
Merge branch 'ftp_statistics' into 'stable-3.1'
Ftp statistics feature Hi, I add the ftp traffic support for pure-ftpd. - Ftp statistics display per hostname in site section (traffic in and out) - Ftp_traffic table (creation and update patch upd_0081.sql) - Cronjob - Actually pure-ftpd log rotation are configured with /etc/logrotate.d/pure-ftpd-common - Maybe replace that per Ispconfig log rotation ? - Api function for ftp traffic Thanks See merge request !266
2 parents c9b5ed4 + ddfbbd8 commit 8fc75da

File tree

10 files changed

+546
-0
lines changed

10 files changed

+546
-0
lines changed

install/sql/incremental/upd_dev_collection.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,12 @@ ALTER TABLE `web_domain` ADD `ssl_letsencrypt` enum('n','y') NOT NULL DEFAULT 'n
192192

193193
ALTER TABLE `openvz_template` CHANGE `vmguarpages` `vmguarpages` varchar(255) DEFAULT '65536:unlimited';
194194
ALTER TABLE `openvz_template` CHANGE `privvmpages` `privvmpages` varchar(255) DEFAULT '131072:139264';
195+
196+
197+
CREATE TABLE `ftp_traffic` (
198+
`hostname` varchar(255) NOT NULL,
199+
`traffic_date` date NOT NULL,
200+
`in_bytes` bigint(32) unsigned NOT NULL,
201+
`out_bytes` bigint(32) unsigned NOT NULL,
202+
PRIMARY KEY (`hostname`,`traffic_date`)
203+
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

install/sql/ispconfig3.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,20 @@ CREATE TABLE `ftp_user` (
638638

639639
-- --------------------------------------------------------
640640

641+
--
642+
-- Table structure for table `ftp_traffic`
643+
--
644+
645+
CREATE TABLE `ftp_traffic` (
646+
`hostname` varchar(255) NOT NULL,
647+
`traffic_date` date NOT NULL,
648+
`in_bytes` bigint(32) unsigned NOT NULL,
649+
`out_bytes` bigint(32) unsigned NOT NULL,
650+
PRIMARY KEY (`hostname`,`traffic_date`)
651+
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
652+
653+
-- --------------------------------------------------------
654+
641655
--
642656
-- Table structure for table `help_faq`
643657
--

interface/lib/classes/quota_lib.inc.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,67 @@ public function get_trafficquota_data($clientid = null, $lastdays = 0) {
156156

157157
return $traffic_data;
158158
}
159+
160+
public function get_ftptrafficquota_data($clientid = null, $lastdays = 0) {
161+
global $app;
162+
163+
$traffic_data = array();
164+
165+
// select vhosts (belonging to client)
166+
if($clientid != null){
167+
$sql_where = " AND sys_groupid = (SELECT default_group FROM sys_user WHERE client_id=?)";
168+
}
169+
$sites = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE active = 'y' AND (type = 'vhost' OR type = 'vhostsubdomain' OR type = 'vhostalias')".$sql_where, $clientid);
170+
171+
$hostnames = array();
172+
$traffic_data = array();
173+
174+
foreach ($sites as $site) {
175+
$hostnames[] = $site['domain'];
176+
$traffic_data[$site['domain']]['domain_id'] = $site['domain_id'];
177+
}
178+
179+
// fetch all traffic-data of selected vhosts
180+
if (!empty($hostnames)) {
181+
$tmp_year = date('Y');
182+
$tmp_month = date('m');
183+
// This Month
184+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
185+
foreach ($tmp_recs as $tmp_rec) {
186+
$traffic_data[$tmp_rec['hostname']]['this_month'] = $tmp_rec['t'];
187+
}
188+
// This Year
189+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
190+
foreach ($tmp_recs as $tmp_rec) {
191+
$traffic_data[$tmp_rec['hostname']]['this_year'] = $tmp_rec['t'];
192+
}
193+
194+
$tmp_year = date('Y', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
195+
$tmp_month = date('m', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
196+
// Last Month
197+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND MONTH(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $tmp_month, $hostnames);
198+
foreach ($tmp_recs as $tmp_rec) {
199+
$traffic_data[$tmp_rec['hostname']]['last_month'] = $tmp_rec['t'];
200+
}
201+
202+
$tmp_year = date('Y', mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
203+
// Last Year
204+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE YEAR(traffic_date) = ? AND hostname IN ? GROUP BY hostname", $tmp_year, $hostnames);
205+
foreach ($tmp_recs as $tmp_rec) {
206+
$traffic_data[$tmp_rec['hostname']]['last_year'] = $tmp_rec['t'];
207+
}
208+
209+
if (is_int($lastdays) && ($lastdays > 0)) {
210+
// Last xx Days
211+
$tmp_recs = $app->db->queryAllRecords("SELECT hostname, SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE (traffic_date >= DATE_SUB(NOW(), INTERVAL ? DAY)) AND hostname IN ? GROUP BY hostname", $lastdays, $hostnames);
212+
foreach ($tmp_recs as $tmp_rec) {
213+
$traffic_data[$tmp_rec['hostname']]['lastdays'] = $tmp_rec['t'];
214+
}
215+
}
216+
}
217+
218+
return $traffic_data;
219+
}
159220

160221
public function get_mailquota_data($clientid = null, $readable = true) {
161222
global $app;

interface/lib/classes/remote.d/sites.inc.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,21 @@ public function trafficquota_get_by_user($session_id, $client_id, $lastdays = 0)
980980
return $app->quota_lib->get_trafficquota_data($client_id, $lastdays);
981981
}
982982

983+
public function ftptrafficquota_data($session_id, $client_id, $lastdays = 0)
984+
{
985+
global $app;
986+
$app->uses('quota_lib');
987+
988+
if(!$this->checkPerm($session_id, 'trafficquota_get_by_user')) {
989+
$this->server->fault('permission_denied', 'You do not have the permissions to access this function.');
990+
return false;
991+
}
992+
if ($client_id != null)
993+
$client_id = $app->functions->intval($client_id);
994+
995+
return $app->quota_lib->get_ftptrafficquota_data($client_id, $lastdays);
996+
}
997+
983998
public function databasequota_get_by_user($session_id, $client_id)
984999
{
9851000
global $app;
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
require_once '../../lib/config.inc.php';
3+
require_once '../../lib/app.inc.php';
4+
5+
/******************************************
6+
* Begin Form configuration
7+
******************************************/
8+
9+
$list_def_file = "list/ftp_sites_stats.list.php";
10+
11+
/******************************************
12+
* End Form configuration
13+
******************************************/
14+
15+
//* Check permissions for module
16+
$app->auth->check_module_permissions('sites');
17+
18+
$app->uses('functions');
19+
20+
$app->load('listform_actions');
21+
22+
class list_action extends listform_actions {
23+
24+
private $sum_this_month = 0;
25+
private $sum_this_year = 0;
26+
private $sum_last_month = 0;
27+
private $sum_last_year = 0;
28+
29+
function prepareDataRow($rec)
30+
{
31+
global $app;
32+
33+
$rec = $app->listform->decode($rec);
34+
35+
//* Alternating datarow colors
36+
$this->DataRowColor = ($this->DataRowColor == '#FFFFFF') ? '#EEEEEE' : '#FFFFFF';
37+
$rec['bgcolor'] = $this->DataRowColor;
38+
39+
//* Set the statistics colums
40+
//** Traffic of the current month
41+
$tmp_year = date('Y');
42+
$tmp_month = date('m');
43+
$tmp_rec = $app->db->queryOneRecord("SELECT SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE hostname = ? AND YEAR(traffic_date) = ? AND MONTH(traffic_date) = ?", $rec['domain'], $tmp_year, $tmp_month);
44+
$rec['this_month_in'] = $app->functions->formatBytes($tmp_rec['ftp_in']);
45+
$rec['this_month_out'] = $app->functions->formatBytes($tmp_rec['ftp_out']);
46+
$this->sum_this_month += $tmp_rec['ftp_in']+$tmp_rec['ftp_out'];
47+
48+
//** Traffic of the current year
49+
$tmp_rec = $app->db->queryOneRecord("SELECT SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE hostname = ? AND YEAR(traffic_date) = ?", $rec['domain'], $tmp_year);
50+
$rec['this_year_in'] = $app->functions->formatBytes($tmp_rec['ftp_in']);
51+
$rec['this_year_out'] = $app->functions->formatBytes($tmp_rec['ftp_out']);
52+
$this->sum_this_year += $tmp_rec['ftp_in']+$tmp_rec['ftp_out'];
53+
54+
//** Traffic of the last month
55+
$tmp_year = date('Y', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
56+
$tmp_month = date('m', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
57+
$tmp_rec = $app->db->queryOneRecord("SELECT SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE hostname = ? AND YEAR(traffic_date) = ? AND MONTH(traffic_date) = ?", $rec['domain'], $tmp_year, $tmp_month);
58+
$rec['last_month_in'] = $app->functions->formatBytes($tmp_rec['ftp_in']);
59+
$rec['last_month_out'] = $app->functions->formatBytes($tmp_rec['ftp_out']);
60+
$this->sum_last_month += $tmp_rec['ftp_in']+$tmp_rec['ftp_out'];
61+
62+
//** Traffic of the last year
63+
$tmp_year = date('Y', mktime(0, 0, 0, date("m"), date("d"), date("Y")-1));
64+
$tmp_rec = $app->db->queryOneRecord("SELECT SUM(in_bytes) AS ftp_in, SUM(out_bytes) AS ftp_out FROM ftp_traffic WHERE hostname = ? AND YEAR(traffic_date) = ?", $rec['domain'], $tmp_year);
65+
$rec['last_year_in'] = $app->functions->formatBytes($tmp_rec['ftp_in']);
66+
$rec['last_year_out'] = $app->functions->formatBytes($tmp_rec['ftp_out']);
67+
$this->sum_last_year += $tmp_rec['ftp_in']+$tmp_rec['ftp_out'];
68+
69+
//* The variable "id" contains always the index variable
70+
$rec['id'] = $rec[$this->idx_key];
71+
72+
return $rec;
73+
}
74+
75+
function onShowEnd()
76+
{
77+
global $app;
78+
79+
$app->tpl->setVar('sum_this_month', $app->functions->formatBytes($this->sum_this_month));
80+
$app->tpl->setVar('sum_this_year', $app->functions->formatBytes($this->sum_this_year));
81+
$app->tpl->setVar('sum_last_month', $app->functions->formatBytes($this->sum_last_month));
82+
$app->tpl->setVar('sum_last_year', $app->functions->formatBytes($this->sum_last_year));
83+
$app->tpl->setVar('sum_txt', $app->listform->lng('sum_txt'));
84+
85+
$app->tpl_defaults();
86+
$app->tpl->pparse();
87+
}
88+
89+
function getQueryString($no_limit = false) {
90+
global $app;
91+
$sql_where = '';
92+
93+
//* Generate the search sql
94+
if($app->listform->listDef['auth'] != 'no') {
95+
if($_SESSION['s']['user']['typ'] == "admin") {
96+
$sql_where = '';
97+
} else {
98+
$sql_where = $app->tform->getAuthSQL('r', $app->listform->listDef['table']).' and';
99+
//$sql_where = $app->tform->getAuthSQL('r').' and';
100+
}
101+
}
102+
if($this->SQLExtWhere != '') {
103+
$sql_where .= ' '.$this->SQLExtWhere.' and';
104+
}
105+
106+
$sql_where = $app->listform->getSearchSQL($sql_where);
107+
if($app->listform->listDef['join_sql']) $sql_where .= ' AND '.$app->listform->listDef['join_sql'];
108+
$app->tpl->setVar($app->listform->searchValues);
109+
110+
$order_by_sql = $this->SQLOrderBy;
111+
112+
//* Generate SQL for paging
113+
$limit_sql = $app->listform->getPagingSQL($sql_where);
114+
$app->tpl->setVar('paging', $app->listform->pagingHTML);
115+
116+
$extselect = '';
117+
$join = '';
118+
119+
if(!empty($_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order'])){
120+
$order = str_replace(' DESC', '', $_SESSION['search'][$_SESSION['s']['module']['name'].$app->listform->listDef["name"].$app->listform->listDef['table']]['order']);
121+
list($tmp_table, $order) = explode('.', $order);
122+
if($order == 'ftp_traffic_last_month'){
123+
$tmp_year = date('Y', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
124+
$tmp_month = date('m', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
125+
$extselect .= ', SUM(ft.in_bytes+out_bytes) as calctraffic';
126+
$join .= ' INNER JOIN ftp_traffic as ft ON '.$app->listform->listDef['table'].'.domain = ft.hostname ';
127+
$sql_where .= " AND YEAR(ft.traffic_date) = '$tmp_year' AND MONTH(ft.traffic_date) = '$tmp_month'";
128+
$order_by_sql = str_replace($app->listform->listDef['table'].'.ftp_traffic_last_month', 'calctraffic', $order_by_sql);
129+
$order_by_sql = "GROUP BY domain ".$order_by_sql;
130+
} elseif($order == 'ftp_traffic_this_month'){
131+
$tmp_year = date('Y');
132+
$tmp_month = date('m');
133+
$extselect .= ', SUM(ft.in_bytes+out_bytes) as calctraffic';
134+
$join .= ' INNER JOIN ftp_traffic as ft ON '.$app->listform->listDef['table'].'.domain = ft.hostname ';
135+
$sql_where .= " AND YEAR(ft.traffic_date) = '$tmp_year' AND MONTH(ft.traffic_date) = '$tmp_month'";
136+
$order_by_sql = str_replace($app->listform->listDef['table'].'.ftp_traffic_this_month', 'calctraffic', $order_by_sql);
137+
$order_by_sql = "GROUP BY domain ".$order_by_sql;
138+
} elseif($order == 'ftp_traffic_last_year'){
139+
$tmp_year = date('Y', mktime(0, 0, 0, date("m")-1, date("d"), date("Y")));
140+
$extselect .= ', SUM(ft.in_bytes+out_bytes) as calctraffic';
141+
$join .= ' INNER JOIN ftp_traffic as ft ON '.$app->listform->listDef['table'].'.domain = ft.hostname ';
142+
$sql_where .= " AND YEAR(ft.traffic_date) = '$tmp_year'";
143+
$order_by_sql = str_replace($app->listform->listDef['table'].'.ftp_traffic_last_year', 'calctraffic', $order_by_sql);
144+
$order_by_sql = "GROUP BY domain ".$order_by_sql;
145+
} elseif($order == 'ftp_traffic_this_year'){
146+
$tmp_year = date('Y');
147+
$extselect .= ', SUM(ft.in_bytes+out_bytes) as calctraffic';
148+
$join .= ' INNER JOIN ftp_traffic as ft ON '.$app->listform->listDef['table'].'.domain = ft.hostname ';
149+
$sql_where .= " AND YEAR(ft.traffic_date) = '$tmp_year'";
150+
$order_by_sql = str_replace($app->listform->listDef['table'].'.ftp_traffic_this_year', 'calctraffic', $order_by_sql);
151+
$order_by_sql = "GROUP BY domain ".$order_by_sql;
152+
}
153+
}
154+
155+
if($this->SQLExtSelect != '') {
156+
if(substr($this->SQLExtSelect, 0, 1) != ',') $this->SQLExtSelect = ','.$this->SQLExtSelect;
157+
$extselect .= $this->SQLExtSelect;
158+
}
159+
160+
$table_selects = array();
161+
$table_selects[] = trim($app->listform->listDef['table']).'.*';
162+
$app->listform->listDef['additional_tables'] = trim($app->listform->listDef['additional_tables']);
163+
if($app->listform->listDef['additional_tables'] != ''){
164+
$additional_tables = explode(',', $app->listform->listDef['additional_tables']);
165+
foreach($additional_tables as $additional_table){
166+
$table_selects[] = trim($additional_table).'.*';
167+
}
168+
}
169+
$select = implode(', ', $table_selects);
170+
171+
$sql = 'SELECT '.$select.$extselect.' FROM '.$app->listform->listDef['table'].($app->listform->listDef['additional_tables'] != ''? ','.$app->listform->listDef['additional_tables'] : '')."$join WHERE $sql_where $order_by_sql $limit_sql";
172+
return $sql;
173+
}
174+
}
175+
176+
$list = new list_action;
177+
$list->SQLExtWhere = "(web_domain.type = 'vhost' or web_domain.type = 'vhostsubdomain')";
178+
$list->SQLOrderBy = 'ORDER BY web_domain.domain';
179+
$list->onLoad();
180+
181+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
$wb["list_head_txt"] = 'FTP traffic';
3+
$wb["domain_txt"] = 'Domain';
4+
$wb["this_month_txt"] = 'This month';
5+
$wb["last_month_txt"] = 'Last month';
6+
$wb["this_year_txt"] = 'This year';
7+
$wb["last_year_txt"] = 'Last year';
8+
$wb["sum_txt"] = 'Sum (Download + Upload)';
9+
$wb["in_out_txt"] = 'DL/UL';
10+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
$wb["list_head_txt"] = 'FTP trafic';
3+
$wb["domain_txt"] = 'Domaine';
4+
$wb["this_month_txt"] = 'Mois en cours';
5+
$wb["last_month_txt"] = 'Mois pr�c�dent';
6+
$wb["this_year_txt"] = 'Ann�e en cours';
7+
$wb["last_year_txt"] = 'Ann�e pr�c�dente';
8+
$wb["sum_txt"] = 'Total (Download + Upload)';
9+
$wb["in_out_txt"] = 'DL/UL';
10+
?>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
Datatypes:
5+
- INTEGER
6+
- DOUBLE
7+
- CURRENCY
8+
- VARCHAR
9+
- TEXT
10+
- DATE
11+
*/
12+
13+
// Name of the list
14+
$liste["name"] = "ftp_sites_stats";
15+
16+
// Database table
17+
$liste["table"] = "web_domain";
18+
19+
// Index index field of the database table
20+
$liste["table_idx"] = "domain_id";
21+
22+
// Search Field Prefix
23+
$liste["search_prefix"] = "search_";
24+
25+
// Records per page
26+
$liste["records_per_page"] = "15";
27+
28+
// Script File of the list
29+
$liste["file"] = "web_sites_stats.php";
30+
31+
// Script file of the edit form
32+
$liste["edit_file"] = "web_domain_edit.php";
33+
34+
// Script File of the delete script
35+
$liste["delete_file"] = "web_domain_del.php";
36+
37+
// Paging Template
38+
$liste["paging_tpl"] = "templates/paging.tpl.htm";
39+
40+
// Enable auth
41+
$liste["auth"] = "yes";
42+
43+
44+
/*****************************************************
45+
* Suchfelder
46+
*****************************************************/
47+
48+
$liste["item"][] = array( 'field' => "domain",
49+
'datatype' => "VARCHAR",
50+
'filters' => array( 0 => array( 'event' => 'SHOW',
51+
'type' => 'IDNTOUTF8')
52+
),
53+
'formtype' => "TEXT",
54+
'op' => "like",
55+
'prefix' => "%",
56+
'suffix' => "%",
57+
'width' => "",
58+
'value' => "");
59+
60+
?>

0 commit comments

Comments
 (0)