diff options
Diffstat (limited to 'config/sarg/sarg.inc')
-rw-r--r-- | config/sarg/sarg.inc | 126 |
1 files changed, 104 insertions, 22 deletions
diff --git a/config/sarg/sarg.inc b/config/sarg/sarg.inc index e762d9b8..5d0a91a5 100644 --- a/config/sarg/sarg.inc +++ b/config/sarg/sarg.inc @@ -31,13 +31,28 @@ POSSIBILITY OF SUCH DAMAGE. */ /* ========================================================================== */ +$pf_version=substr(trim(file_get_contents("/etc/version")),0,3); +if ($pf_version > 2.0){ + define('SARG_DIR', '/usr/pbi/sarg-' . php_uname("m")); + define('SQUID_DIR', '/usr/pbi/squid-' . php_uname("m")); + define('DANSG_DIR', '/usr/pbi/dansguardian-' . php_uname("m")); + } +else{ + define('SARG_DIR', '/usr/local'); + define('SQUID_DIR', '/usr/local'); + define('DANSG_DIR', '/usr/local'); +} + +$uname=posix_uname(); +if ($uname['machine']=='amd64') + ini_set('memory_limit', '250M'); // 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', +$sarg_proxy=array( 'squid_rc'=> SQUID_DIR . '/etc/rc.d/squid.sh', + 'squid_config'=> '/var/squid/logs/access.log', + 'squidguard_config'=> SARG_DIR . '/etc/squidGuard/squidGuard.conf', 'squidguard_block_log'=>'/var/squidGuard/log/block.log', - 'dansguardian_config'=>'/usr/local/etc/dansguardian/dansguardian.conf', + 'dansguardian_config'=> DANSG_DIR . '/etc/dansguardian/dansguardian.conf', 'dansguardian_log'=>'/var/log/dansguardian/access.log'); // END STATIC VARS @@ -56,7 +71,7 @@ function sarg_resync() { global $config; if (($_POST['Submit'] == 'Save') || !isset($_POST['Submit'])) sync_package_sarg(); - if ($_POST['Submit'] == 'Force udpate now') + if ($_POST['Submit'] == 'Force update now') run_sarg(); } @@ -80,17 +95,51 @@ function run_sarg($id=-1) { global $config, $g,$sarg_proxy; #mount filesystem writeable conf_mount_rw(); - $cmd = "/usr/local/bin/sarg"; + $cmd = SARG_DIR . "/bin/sarg"; if ($id >= 0 && is_array($config['installedpackages']['sargschedule']['config'])){ $args=$config['installedpackages']['sargschedule']['config'][$id]['args']; $action=$config['installedpackages']['sargschedule']['config'][$id]['action']; + $gzip=$config['installedpackages']['sargschedule']['config'][$id]['gzip']; + $find=$config['installedpackages']['sargschedule']['config'][$id]['find']; + $gziplevel=$config['installedpackages']['sargschedule']['config'][$id]['gziplevel']; + $daylimit=$config['installedpackages']['sargschedule']['config'][$id]['daylimit']; } else{ $args=$_POST['args']; $action=$_POST['action']; + $gzip=$_POST['gzip']; + $find=$_POST['find']; + $gziplevel=$_POST['gziplevel']; + $daylimit=""; } - log_error("Sarg: force refresh now with '".$args."' args and ".$action." action after sarg finish."); + $find=(preg_match("/(\d+)/",$find,$find_matches) ? $find_matches[1] : "60"); + log_error("Sarg: force refresh now with {$args} args, compress({$gzip}) and {$action} action after sarg finish."); + $gzip_script="#!/bin/sh\n"; + if ($gzip=="on"){ + #remove old file if exists + unlink_if_exists("/root/sarg_run_{$id}.sh"); + $gzip_script.=<<<EOF +for a in `/usr/bin/find /usr/local/sarg-reports -cmin -{$find} -type d -mindepth 1 -maxdepth 1` +do +echo \$a +/usr/bin/find \$a -name "*html" | xargs gzip {$gziplevel} +done + +EOF; + } + if (preg_match("/(\d+)/",$daylimit,$day_matches)){ + $gzip_script.=<<<EOF +for a in `/usr/bin/find /usr/local/sarg-reports -ctime +{$find} -type d -mindepth 1 -maxdepth 1` +do +echo \$a +rm -rf \$a +done + +EOF; + } + #create a new file to speedup find search + file_put_contents("/root/sarg_run_{$id}.sh",$gzip_script,LOCK_EX); mwexec($cmd. " ".$args); #check if there is a script to run after file save if (is_array($config['installedpackages']['sarg'])) @@ -99,12 +148,16 @@ function run_sarg($id=-1) { if ($action =="both" || $action=="rotate"){ log_error('executing squidguard log rotate after sarg.'); log_rotate($sarg_proxy['squidguard_block_log']); + file_put_contents($sarg_proxy['squidguard_block_log'],"",LOCK_EX); + chown($sarg_proxy['squidguard_block_log'],'proxy'); + chgrp($sarg_proxy['squidguard_block_log'],'proxy'); + mwexec(SQUID_DIR . '/sbin/squid -k reconfigure'); } - #Leve this case without break to include squid log file on squidguard option + #leave this case without break to run squid rotate too. case "squid": if ($action =="both" || $action=="rotate"){ log_error('executing squid log rotate after sarg.'); - mwexec('squid -k rotate'); + mwexec(SQUID_DIR . '/sbin/squid -k rotate'); } if ($action =="both" || $action=="restart"){ if (file_exists($sarg_proxy['squid_rc'])) @@ -119,12 +172,25 @@ function run_sarg($id=-1) { } break; } + #check compress option + if ($gzip=="on") + mwexec_bg("/bin/sh /root/sarg_run_{$id}.sh"); + #mount filesystem readonly conf_mount_ro(); } function sync_package_sarg() { global $config, $g,$sarg_proxy; + + # detect boot process + if (is_array($_POST)){ + if (!preg_match("/\w+/",$_POST['__csrf_magic'])) + return; + } + #check pkg.php sent a sync request + + $update_conf=0; #mount filesystem writeable conf_mount_rw(); @@ -150,6 +216,7 @@ function sync_package_sarg() { 'ldap_port'=> '389', 'ntlm_user_format'=>'domainname+username'); $sarguser=$config['installedpackages']['sarguser']['config'][0]; + $access_log=$sarg['proxy_server']; switch ($sarg['proxy_server']){ case 'dansguardian': $access_log= $sarg_proxy['dansguardian_log']; @@ -168,7 +235,7 @@ function sync_package_sarg() { $access_log = $config['installedpackages']['squid']['config'][0]['log_dir']. '/access.log'; break; } - if (!file_exists($access_log)){ + if (!file_exists($access_log) && $access_log !=""){ $error="Sarg config error: ".$sarg['proxy_server']." log file ($access_log) does not exists"; log_error($error); file_notice("Sarg", $error, "Sarg Settings", ""); @@ -194,7 +261,7 @@ function sync_package_sarg() { $date_format=(empty($sarg['report_date_format'])?"u":$sarg['report_date_format']); $report_type=preg_replace('/,/',' ',$sarg['report_type']); $report_charset=(empty($sarg['report_charset'])?"UTF-8":$sarg['report_charset']); - $exclude_string=(empty($sarg['exclude_string'])?"":'exclude_string"'.$sarg['exclude_string']."'"); + $exclude_string=(empty($sarg['exclude_string'])?"":'exclude_string "'.$sarg['exclude_string'].'"'); #limits $max_elapsed=(empty($sarg['max_elapsed'])?"0":$sarg['max_elapsed']); @@ -218,8 +285,8 @@ function sync_package_sarg() { $usertab="none"; } else{ - $usertab="/usr/local/etc/sarg/usertab.conf"; - file_put_contents('/usr/local/etc/sarg/usertab.conf', sarg_text_area_decode($sarguser['usertab']),LOCK_EX); + $usertab= SARG_DIR . "/etc/sarg/usertab.conf"; + file_put_contents( SARG_DIR . '/etc/sarg/usertab.conf', sarg_text_area_decode($sarguser['usertab']),LOCK_EX); } if($sarguser['ldap_enable']){ $LDAPHost=(empty($sarguser['ldap_host'])?"":"LDAPHost ".$sarguser['ldap_host']); @@ -231,20 +298,35 @@ function sync_package_sarg() { $LDAPFilterSearch=(empty($sarguser['ldap_filter_search'])?"":"LDAPFilterSearch ".$sarguser['ldap_filter_search']); } - #dirs - $dirs=array("/usr/local/www/sarg-reports"); + + #move old reports + if (is_dir("/usr/local/www/sarg-reports") && !is_dir("/usr/local/sarg-reports")) + rename("/usr/local/www/sarg-reports","/usr/local/sarg-reports"); + + #check dirs + $dirs=array("/usr/local/sarg-reports","/usr/local/www/sarg-images","/usr/local/www/sarg-images/temp"); foreach ($dirs as $dir) if (!is_dir($dir)) mkdir ($dir,0755,true); - + + #images + $simages=array("datetime.png","graph.png","sarg-squidguard-block.png","sarg.png"); + $simgdir1="/usr/local/www/sarg-images"; + $simgdir2= SARG_DIR . "/etc/sarg/images"; + foreach ($simages as $simage){ + if (!file_exists("{$simgdir1}/{$simage}")) + copy("{$simgdir2}/{$simage}","{$simgdir1}/{$simage}"); + } + + //log_error($_POST['__csrf_magic']." sarg log:". $access_log); #create sarg config files + $sarg_dir= SARG_DIR; include("/usr/local/pkg/sarg.template"); - file_put_contents("/usr/local/etc/sarg/sarg.conf", $sg, LOCK_EX); - file_put_contents('/usr/local/etc/sarg/exclude_hosts.conf', sarg_text_area_decode($sarg['exclude_hostlist']),LOCK_EX); - file_put_contents('/usr/local/etc/sarg/exclude_codes.conf', sarg_text_area_decode($sarg['exclude_codelist']),LOCK_EX); - file_put_contents('/usr/local/etc/sarg/hostalias',sarg_text_area_decode($sarg['hostalias']),LOCK_EX); - file_put_contents('/usr/local/etc/sarg/exclude_users.conf', sarg_text_area_decode($sarguser['exclude_userlist']),LOCK_EX); - + file_put_contents( SARG_DIR . "/etc/sarg/sarg.conf", $sg, LOCK_EX); + file_put_contents( SARG_DIR . '/etc/sarg/exclude_hosts.conf', sarg_text_area_decode($sarg['exclude_hostlist']),LOCK_EX); + file_put_contents( SARG_DIR . '/etc/sarg/exclude_codes', sarg_text_area_decode($sarg['exclude_codelist']),LOCK_EX); + file_put_contents( SARG_DIR . '/etc/sarg/hostalias',sarg_text_area_decode($sarg['hostalias']),LOCK_EX); + file_put_contents( SARG_DIR . '/etc/sarg/exclude_users.conf', sarg_text_area_decode($sarguser['exclude_userlist']),LOCK_EX); #check cron_tab $new_cron=array(); $cron_found=0; |