#!/usr/bin/php
<?php
/*
# description: Start Flow-Capture
# chkconfig: 2345 95 00
*/

$cacti_base = '/var/www/html';


include_once($cacti_base . '/include/global.php');

if (isset($_SERVER['argv'][1])) {
	switch (strtolower($_SERVER['argv'][1])) {
		case 'start':
			$devices = db_fetch_assoc("SELECT * FROM plugin_flowview_devices");
			if (!empty($devices)) {
				$path = db_fetch_cell("SELECT value FROM `settings` WHERE name = 'path_flows_dir'");
				if ($path == '')
					break;
				if (substr($path, -1) == '/') {
					$path = substr($path, 0, -1);
				}
				foreach ($devices as $device) {
					$port = $device['port'];
					$folder = $device['folder'];
					$nest = $device['nesting'];
					$v = $device['version'];
					$from = $device['allowfrom'];
					$comp = $device['compression'];
					$rotate = $device['rotation'];
					$expire = $device['expire'] * ($rotate + 1);
					if (!is_dir("$path/$folder")) {
						mkdir("$path/$folder");
					}
					if (is_dir("$path/$folder")) {
						shell_exec("/usr/bin/flow-capture -w $path/$folder 0/$from/$port -S5 -V$v -z $comp -n $rotate -e $expire -N $nest");
					}
				}
			}			
			break;
		case 'stop':
			$devices = db_fetch_assoc("SELECT * FROM plugin_flowview_devices");
			if (!empty($devices)) {
				shell_exec('killall -9 /usr/bin/flow-capture');
			}			
			break;
		default:
			break;
	}
}

