#!/usr/bin/php
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004-2025 The Cacti Group                                 |
 |                                                                         |
 | This program is free software; you can redistribute it and/or           |
 | modify it under the terms of the GNU General Public License             |
 | as published by the Free Software Foundation; either version 2          |
 | of the License, or (at your option) any later version.                  |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU General Public License for more details.                            |
 +-------------------------------------------------------------------------+
 | Cacti: The Complete RRDtool-based Graphing Solution                     |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Cacti Group. See  |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://www.cacti.net/                                                   |
 +-------------------------------------------------------------------------+
*/

$cacti_base = '/var/www/html/cacti';
if (!file_exists($cacti_base) || !is_dir($cacti_base)) {
	$cacti_base = '/usr/local/share/cacti';
} 

/**
 * determine the cacti base directory
 */
$cacti_base = get_cacti_base();

if ($cacti_base === false) {
	print 'FATAL: Cacti install base directory can not be determined' . PHP_EOL;
	exit(1);
}

include_once($cacti_base . '/include/cli_check.php');
include_once($cacti_base . '/lib/poller.php');
include_once($cacti_base . '/plugins/flowview/setup.php');
include_once($cacti_base . '/plugins/flowview/functions.php');

global $global, $cacti_base;

print 'NOTE: Starting Flow Collection' . PHP_EOL;

flowview_connect();

$devices = flowview_db_fetch_assoc('SELECT * FROM plugin_flowview_devices');
$legacy  = flowview_db_fetch_cell('SELECT COUNT(*) FROM plugin_flowview_devices WHERE cmethod = 1');

if (!empty($devices)) {
	foreach ($devices as $device) {
		$php_binary = read_config_option('path_php_binary');
		print "NOTE: Launching cacti-flow-capture as '" . $cacti_base . '/plugins/flowview/flow_collector.php --listener-id=' . $device['id'] . "'" . PHP_EOL;

		exec_background($php_binary, ' -q ' . $cacti_base . '/plugins/flowview/flow_collector.php --listener-id=' . $device['id']);

	}
} else {
	print 'NOTE: No Flow Capture Listeners configured' . PHP_EOL;
}

// do not stop this process. Without this, service control will fail
do {
	sleep (10);
}
while (true);

function get_cacti_base() {
	$cacti_bases = array(
		'/var/www/html/cacti',
		'/usr/local/share/cacti/site',
		'/usr/local/share/cacti',
		'/usr/share/cacti',
		'/var/www/html',
		'/opt/cacti'
	);

	/* Lets check for the poller in the standard locations */
	foreach($cacti_bases as $cacti_base) {
		if ((file_exists($cacti_base) || is_dir($cacti_base)) && file_exists("$cacti_base/poller.php")) {
			return $cacti_base;
		}
	} 

	/* Lets check if our flowview service directory is in the cacti directory */
	$dir = implode('/', array_slice(explode('/', dirname(__FILE__)), 0, -3));
	if (file_exists("$dir/poller.php")) {
		return $dir;
	}

	return false;
}

