function remove_files($file_array) { global $config, $debug, $archived, $purged; include_once $config['library_path'] . '/api_graph.php'; include_once $config['library_path'] . '/api_data_source.php'; maint_debug('RRDClean is now running on ' . sizeof($file_array) . ' items'); /* determine the location of the RRA files */ if (isset($config['rra_path'])) { $rra_path = $config['rra_path']; } else { $rra_path = $config['base_path'] . '/rra'; } /* let's prepare the archive directory */ $rrd_archive = read_config_option('rrd_archive', TRUE); if ($rrd_archive == '') { $rrd_archive = $rra_path . '/archive'; } rrdclean_create_path($rrd_archive); /* now scan the files */ foreach ($file_array as $file) { $source_file = $rra_path . '/' . $file['name']; switch ($file['action']) { case '1': if (unlink($source_file)) { maint_debug('Deleted: ' . $file['name']); } else { cacti_log($file['name'] . " Error: unable to delete from {$rra_path}!", true, 'MAINT'); } $purged++; break; case '3': $target_file = $rrd_archive . '/' . $file['name']; $target_dir = dirname($target_file); if (!is_dir($target_dir)) { rrdclean_create_path($target_dir); } if (rename($source_file, $target_file)) { maint_debug('Moved: ' . $file['name'] . ' to: ' . $rrd_archive); } else { cacti_log($file['name'] . " Error: unable to move to {$rrd_archive}!", true, 'MAINT'); } $archived++; break; } /* drop from data_source_purge_action table */ db_execute_prepared('DELETE FROM `data_source_purge_action` WHERE name = ?', array($file['name'])); maint_debug('Delete from data_source_purge_action: ' . $file['name']); //fetch all local_graph_id's according to this data source $lgis = db_fetch_assoc_prepared('SELECT DISTINCT gl.id FROM graph_local AS gl INNER JOIN graph_templates_item AS gti ON gl.id = gti.local_graph_id INNER JOIN data_template_rrd AS dtr ON dtr.id=gti.task_item_id INNER JOIN data_local AS dl ON dtr.local_data_id=dl.id WHERE (local_data_id=?)', array($file['local_data_id'])); if (sizeof($lgis)) { /* anything found? */ cacti_log('Processing ' . sizeof($lgis) . ' Graphs for data source id: ' . $file['local_data_id'], true, 'MAINT'); /* get them all */ foreach ($lgis as $item) { $remove_lgis[] = $item['id']; cacti_log('remove local_graph_id=' . $item['id'], true, 'MAINT'); } /* and remove them in a single run */ if (!empty($remove_lgis)) { api_graph_remove_multi($remove_lgis); } } /* remove related data source if any */ if ($file['local_data_id'] > 0) { cacti_log('removing data source: ' . $file['local_data_id'], true, 'MAINT'); api_data_source_remove($file['local_data_id']); } } cacti_log('RRDClean has finished a purge pass of ' . sizeof($file_array) . ' items', true, 'MAINT'); }
function remove_files($file_array, $debug) { global $config; include_once $config["library_path"] . "/api_graph.php"; include_once $config["library_path"] . "/api_data_source.php"; cacti_log("RRDClean is now running on " . sizeof($file_array) . " items", true, "RRDCLEAN"); /* determine the location of the RRA files */ if (isset($config["rra_path"])) { $rra_path = $config["rra_path"]; } else { $rra_path = $config["base_path"] . "/rra"; } /* let's prepare the directories */ $rrd_backup = read_config_option("rrd_backup", TRUE); $rrd_archive = read_config_option("rrd_archive", TRUE); if ($rrd_backup == "") { $rrd_backup = $rra_path . "/backup"; } if ($rrd_archive == "") { $rrd_archive = $rra_path . "/archive"; } rrdclean_create_path($rrd_backup); rrdclean_create_path($rrd_archive); /* now scan the files */ foreach ($file_array as $file) { $source_file = $rra_path . "/" . $file["name"]; switch ($file['action']) { case "1": if (unlink($source_file)) { cacti_log("Deleted: " . $file["name"], true, "RRDCLEAN"); } else { cacti_log($file["name"] . " Error: unable to delete from {$rra_path}!", true, "RRDCLEAN"); } break; case "2": $target_file = $rrd_backup . "/" . $file["name"]; $target_dir = dirname($target_file); if (!is_dir($target_dir)) { rrdclean_create_path($target_dir); } if (copy($source_file, $target_file)) { cacti_log("Copied: " . $file["name"] . " to: " . $rrd_backup, true, "RRDCLEAN"); } else { cacti_log($file["name"] . " Error: unable to save to {$rrd_backup}!", true, "RRDCLEAN"); } break; case "3": $target_file = $rrd_archive . "/" . $file["name"]; $target_dir = dirname($target_file); if (!is_dir($target_dir)) { rrdclean_create_path($target_dir); } if (rename($source_file, $target_file)) { cacti_log("Moved: " . $file["name"] . " to: " . $rrd_archive, true, "RRDCLEAN"); } else { cacti_log($file["name"] . " Error: unable to move to {$rrd_archive}!", true, "RRDCLEAN"); } break; } /* drop from plugin_rrdclean_action table */ $sql = "DELETE FROM `plugin_rrdclean_action` WHERE name = '" . $file["name"] . "'"; db_execute($sql); if (read_config_option("log_verbosity", TRUE) == POLLER_VERBOSITY_DEBUG) { cacti_log("delete from plugin_rrdclean_action: " . $file["name"], true, "RRDCLEAN"); } //fetch all local_graph_id's according to this data source $lgis = db_fetch_assoc("SELECT DISTINCT " . "graph_local.id " . "FROM " . "graph_local " . "INNER JOIN " . "( " . "( data_template_rrd " . "INNER JOIN graph_templates_item " . "ON data_template_rrd.id=graph_templates_item.task_item_id " . ") " . "INNER JOIN " . "data_local " . "ON data_template_rrd.local_data_id=data_local.id " . ") " . "ON graph_local.id = graph_templates_item.local_graph_id " . "WHERE ( " . "local_data_id=" . $file["local_data_id"] . ")"); if (sizeof($lgis)) { /* anything found? */ cacti_log("Processing " . sizeof($lgis) . " Graphs for data source id: " . $file["local_data_id"], true, "RRDCLEAN"); /* get them all */ foreach ($lgis as $item) { $remove_lgis[] = $item['id']; cacti_log("remove local_graph_id=" . $item['id'], true, "RRDCLEAN"); } /* and remove them in a single run */ if (!empty($remove_lgis)) { api_graph_remove_multi($remove_lgis); } } /* remove related data source if any */ if ($file["local_data_id"] > 0) { cacti_log("removing data source: " . $file["local_data_id"], true, "RRDCLEAN"); api_data_source_remove($file["local_data_id"]); } } cacti_log("RRDClean has finished " . sizeof($file_array) . " items", true, "RRDCLEAN"); }