From 2a5327ac65a3cdf08c47df669b8c39ca6369a93e Mon Sep 17 00:00:00 2001 From: Marcello Coutinho Date: Wed, 4 Apr 2012 15:18:16 -0300 Subject: sarg - version 0.3 with report sortfields and date format option --- config/sarg/sarg.inc | 57 ++--- config/sarg/sarg.template | 4 +- config/sarg/sarg.xml | 16 ++ config/sarg/sarg_sorttable.js | 493 ++++++++++++++++++++++++++++++++++++++++++ pkg_config.8.xml | 2 +- pkg_config.8.xml.amd64 | 2 +- 6 files changed, 545 insertions(+), 29 deletions(-) create mode 100644 config/sarg/sarg_sorttable.js diff --git a/config/sarg/sarg.inc b/config/sarg/sarg.inc index 519619b7..eb17245b 100644 --- a/config/sarg/sarg.inc +++ b/config/sarg/sarg.inc @@ -32,6 +32,15 @@ */ /* ========================================================================== */ +// STATIC VARS +$sarg_proxy=array( 'squid_rc'=>'/usr/local/etc/rc.d/squid.sh', + 'squid_config'=>'/var/squid/logs/access.log', + 'squidguard_config'=>'/usr/local/etc/squidGuard/squidGuard.conf', + 'squidguard_block_log'=>'/var/squidGuard/log/block.log', + 'dansguardian_config'=>'/usr/local/etc/dansguardian/dansguardian.conf', + 'dansguardian_log'=>'/var/log/dansguardian/access.log'); + +// END STATIC VARS function sarg_start() { global $g, $config; @@ -68,7 +77,7 @@ function log_rotate($log_file){ rename ($log_file,$log_file.".0"); } function run_sarg($id=-1) { - global $config, $g; + global $config, $g,$sarg_proxy; #mount filesystem writeable conf_mount_rw(); $cmd = "/usr/local/bin/sarg"; @@ -86,61 +95,58 @@ function run_sarg($id=-1) { #check if there is a script to run after file save if (is_array($config['installedpackages']['sarg'])) switch ($config['installedpackages']['sarg']['config'][0]['proxy_server']){ + case "squidguard": + if ($action =="both" || $action=="rotate"){ + log_error('executing squidguard log rotate after sarg.'); + log_rotate($sarg_proxy['squidguard_block_log']); + } + #Leve this case without break to include squid log file on squidguard option case "squid": - if ($action =="both" || $action=="rotate") + if ($action =="both" || $action=="rotate"){ + log_error('executing squid log rotate after sarg.'); mwexec('squid -k rotate'); - if ($action =="both" || $action=="restart") - mwexec_bg('/usr/local/etc/rc.d/squid restart'); + } + if ($action =="both" || $action=="restart"){ + if (file_exists($sarg_proxy['squid_rc'])) + mwexec_bg($sarg_proxy['squid_rc'].' restart'); + } break; case "dansguardian": if (preg_match('/\w+/',$action) && $action !="none"){ - log_rotate('/var/log/dansguardian/access.log'); + log_rotate($sarg_proxy['dansguardian_log']); log_error('restarting dansguardian after sarg and log rotate.'); - #mwexec('/usr/local/etc/rc.d/dansguardian restart'); mwexec('/usr/bin/killall -HUP dansguardian'); } break; - case "squidguard": - if ($action =="both" || $action=="rotate") { - mwexec('squid -k rotate'); - if (file_exists('/usr/local/etc/rc.d/squidGuard_logrotate')){ - mwexec('/usr/local/etc/rc.d/squidGuard_logrotate'); - } - } - if ($action =="both" || $action=="restart") { - if (file_exists('/usr/local/etc/rc.d/squid')){ - mwexec_bg('/usr/local/etc/rc.d/squid restart'); - } - } - break; } #mount filesystem readonly conf_mount_ro(); } function sync_package_sarg() { - global $config, $g; + global $config, $g,$sarg_proxy; $update_conf=0; #mount filesystem writeable conf_mount_rw(); if (!is_array($config['installedpackages']['sarg']['config'])) $config['installedpackages']['sarg']['config'][0]=array('report_options'=>'use_graphs,remove_temp_files,main_index,use_comma', 'user_sort_field'=>'BYTES', - 'report_type'=>'topusers,topsites,sites_users,users_sites,date_time,denied,auth_failures,site_user_time_date,downloads'); + 'report_type'=>'topusers,topsites,sites_users,users_sites,date_time,denied,auth_failures,site_user_time_date,downloads', + 'report_type'=>'u'); $sarg=$config['installedpackages']['sarg']['config'][0]; switch ($sarg['proxy_server']){ case 'dansguardian': - $access_log='/var/log/dansguardian/access.log'; - $dansguardian_conf='/usr/local/etc/dansguardian/dansguardian.conf'; + $access_log= $sarg_proxy['dansguardian_log']; + $dansguardian_conf=$sarg_proxy['dansguardian_config']; $dansguardian_filter_out_date="dansguardian_filter_out_date on"; $squidguard_conf='squidguard_conf none'; break; case 'squidguard': - $squidguard_conf='squidguard_conf /usr/local/etc/squidGuard/squidGuard.conf'; + $squidguard_conf='squidguard_conf '.$sarg_proxy['squidguard_config']; $redirector_log_format='redirector_log_format #year#-#mon#-#day# #hour# #tmp#/#list#/#tmp#/#tmp#/#url#/#tmp# #ip#/#tmp# #user# #end#'; #Leve this case without break to include squid log file on squidguard option case 'squid': - $access_log='/var/squid/logs/access.log'; + $access_log= $sarg_proxy['squid_config']; if (is_array($config['installedpackages']['squid']['config'])) if (file_exists($config['installedpackages']['squid']['config'][0]['log_dir']. '/access.log')) $access_log = $config['installedpackages']['squid']['config'][0]['log_dir']. '/access.log'; @@ -164,6 +170,7 @@ function sync_package_sarg() { $use_comma=(preg_match('/use_comma/',$sarg['report_options'])?"yes":"no"); $long_url=(preg_match('/long_url/',$sarg['report_options'])?"yes":"no"); $privacy=(preg_match('/privacy/',$sarg['report_options'])?"yes":"no"); + $date_format=(empty($sarg['report_date_format'])?"u":$sarg['report_date_format']); $report_type=preg_replace('/,/',' ',$sarg['report_type']); if(!empty($sarg['include_userlist'])) $include_users="$include_users ".$sarg['include_userlist']; diff --git a/config/sarg/sarg.template b/config/sarg/sarg.template index 67a96fb0..e8d92a73 100644 --- a/config/sarg/sarg.template +++ b/config/sarg/sarg.template @@ -215,7 +215,7 @@ exclude_hosts /usr/local/etc/sarg/exclude_hosts.conf # Date format in reports: e (European=dd/mm/yy), u (American=mm/dd/yy), w (Weekly=yy.ww) # #date_format u - +date_format {$date_format} # TAG: per_user_limit file MB # Saves userid on file if download exceed n MB. # This option allow you to disable user access if user exceed a download limit. @@ -764,7 +764,7 @@ realtime_unauthenticated_records show # You may have a look at http://www.kryogenix.org/code/browser/sorttable/ # for the implementation on which sarg is based. # -# sorttable /sorttable.js +sorttable /sarg_sorttable.js # TAG: hostalias # The name of a text file containing the host names one per line and the diff --git a/config/sarg/sarg.xml b/config/sarg/sarg.xml index d406b68d..516d7838 100644 --- a/config/sarg/sarg.xml +++ b/config/sarg/sarg.xml @@ -97,6 +97,11 @@ /usr/local/www/ 0755 + + http://www.pfsense.org/packages/config/sarg/sarg_sorttable.js + /usr/local/www/ + 0755 + http://www.pfsense.org/packages/config/sarg/sarg.template /usr/local/pkg/ @@ -188,6 +193,17 @@ 10 + + + Date Format + report_date_format + + select + + + + + User Settings diff --git a/config/sarg/sarg_sorttable.js b/config/sarg/sarg_sorttable.js new file mode 100644 index 00000000..25bccb2b --- /dev/null +++ b/config/sarg/sarg_sorttable.js @@ -0,0 +1,493 @@ +/* + SortTable + version 2 + 7th April 2007 + Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/ + + Instructions: + Download this file + Add to your HTML + Add class="sortable" to any table you'd like to make sortable + Click on the headers to sort + + Thanks to many, many people for contributions and suggestions. + Licenced as X11: http://www.kryogenix.org/code/browser/licence.html + This basically means: do what you want with it. +*/ + + +var stIsIE = /*@cc_on!@*/false; + +sorttable = { + init: function() { + // quit if this function has already been called + if (arguments.callee.done) return; + // flag this function so we don't do the same thing twice + arguments.callee.done = true; + // kill the timer + if (_timer) clearInterval(_timer); + + if (!document.createElement || !document.getElementsByTagName) return; + + sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/; + + forEach(document.getElementsByTagName('table'), function(table) { + if (table.className.search(/\bsortable\b/) != -1) { + sorttable.makeSortable(table); + } + }); + + }, + + makeSortable: function(table) { + if (table.getElementsByTagName('thead').length == 0) { + // table doesn't have a tHead. Since it should have, create one and + // put the first table row in it. + the = document.createElement('thead'); + the.appendChild(table.rows[0]); + table.insertBefore(the,table.firstChild); + } + // Safari doesn't support table.tHead, sigh + if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0]; + + if (table.tHead.rows.length != 1) return; // can't cope with two header rows + + // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as + // "total" rows, for example). This is B&R, since what you're supposed + // to do is put them in a tfoot. So, if there are sortbottom rows, + // for backwards compatibility, move them to tfoot (creating it if needed). + sortbottomrows = []; + for (var i=0; i5' : ' ▴'; + this.appendChild(sortrevind); + return; + } + if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) { + // if we're already sorted by this column in reverse, just + // re-reverse the table, which is quicker + sorttable.reverse(this.sorttable_tbody); + this.className = this.className.replace('sorttable_sorted_reverse', + 'sorttable_sorted'); + this.removeChild(document.getElementById('sorttable_sortrevind')); + sortfwdind = document.createElement('span'); + sortfwdind.id = "sorttable_sortfwdind"; + sortfwdind.innerHTML = stIsIE ? ' 6' : ' ▾'; + this.appendChild(sortfwdind); + return; + } + + // remove sorttable_sorted classes + theadrow = this.parentNode; + forEach(theadrow.childNodes, function(cell) { + if (cell.nodeType == 1) { // an element + cell.className = cell.className.replace('sorttable_sorted_reverse',''); + cell.className = cell.className.replace('sorttable_sorted',''); + } + }); + sortfwdind = document.getElementById('sorttable_sortfwdind'); + if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); } + sortrevind = document.getElementById('sorttable_sortrevind'); + if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); } + + this.className += ' sorttable_sorted'; + sortfwdind = document.createElement('span'); + sortfwdind.id = "sorttable_sortfwdind"; + sortfwdind.innerHTML = stIsIE ? ' 6' : ' ▾'; + this.appendChild(sortfwdind); + + // build an array to sort. This is a Schwartzian transform thing, + // i.e., we "decorate" each row with the actual sort key, + // sort based on the sort keys, and then put the rows back in order + // which is a lot faster because you only do getInnerText once per row + row_array = []; + col = this.sorttable_columnindex; + rows = this.sorttable_tbody.rows; + for (var j=0; j 12) { + // definitely dd/mm + return sorttable.sort_ddmm; + } else if (second > 12) { + return sorttable.sort_mmdd; + } else { + // looks like a date, but we can't tell which, so assume + // that it's dd/mm (English imperialism!) and keep looking + sortfn = sorttable.sort_ddmm; + } + } + } + } + return sortfn; + }, + + getInnerText: function(node) { + // gets the text we want to use for sorting for a cell. + // strips leading and trailing whitespace. + // this is *not* a generic getInnerText function; it's special to sorttable. + // for example, you can override the cell text with a customkey attribute. + // it also gets .value for fields. + + hasInputs = (typeof node.getElementsByTagName == 'function') && + node.getElementsByTagName('input').length; + + if (node.getAttribute("sorttable_customkey") != null) { + return node.getAttribute("sorttable_customkey"); + } + else if (typeof node.textContent != 'undefined' && !hasInputs) { + return node.textContent.replace(/^\s+|\s+$/g, ''); + } + else if (typeof node.innerText != 'undefined' && !hasInputs) { + return node.innerText.replace(/^\s+|\s+$/g, ''); + } + else if (typeof node.text != 'undefined' && !hasInputs) { + return node.text.replace(/^\s+|\s+$/g, ''); + } + else { + switch (node.nodeType) { + case 3: + if (node.nodeName.toLowerCase() == 'input') { + return node.value.replace(/^\s+|\s+$/g, ''); + } + case 4: + return node.nodeValue.replace(/^\s+|\s+$/g, ''); + break; + case 1: + case 11: + var innerText = ''; + for (var i = 0; i < node.childNodes.length; i++) { + innerText += sorttable.getInnerText(node.childNodes[i]); + } + return innerText.replace(/^\s+|\s+$/g, ''); + break; + default: + return ''; + } + } + }, + + reverse: function(tbody) { + // reverse the rows in a tbody + newrows = []; + for (var i=0; i=0; i--) { + tbody.appendChild(newrows[i]); + } + delete newrows; + }, + + /* sort functions + each sort function takes two parameters, a and b + you are comparing a[0] and b[0] */ + sort_numeric: function(a,b) { + aa = parseFloat(a[0].replace(/[^0-9.-]/g,'')); + if (isNaN(aa)) aa = 0; + bb = parseFloat(b[0].replace(/[^0-9.-]/g,'')); + if (isNaN(bb)) bb = 0; + return aa-bb; + }, + sort_alpha: function(a,b) { + if (a[0]==b[0]) return 0; + if (a[0] 0 ) { + var q = list[i]; list[i] = list[i+1]; list[i+1] = q; + swap = true; + } + } // for + t--; + + if (!swap) break; + + for(var i = t; i > b; --i) { + if ( comp_func(list[i], list[i-1]) < 0 ) { + var q = list[i]; list[i] = list[i-1]; list[i-1] = q; + swap = true; + } + } // for + b++; + + } // while(swap) + } +} + +/* ****************************************************************** + Supporting functions: bundled here to avoid depending on a library + ****************************************************************** */ + +// Dean Edwards/Matthias Miller/John Resig + +/* for Mozilla/Opera9 */ +if (document.addEventListener) { + document.addEventListener("DOMContentLoaded", sorttable.init, false); +} + +/* for Internet Explorer */ +/*@cc_on @*/ +/*@if (@_win32) + document.write("