#!/usr/bin/php -q This script is only meant to run at the command line."); } /* We are not talking to the browser */ $no_http_headers = true; /* process calling arguments */ $action = ''; $ip = ''; $host_template = ''; $include_path = "../site/include/"; $delete_graphs = FALSE; $parms = $_SERVER["argv"]; array_shift($parms); if (sizeof($parms)) { foreach($parms as $parameter) { @list($arg, $value) = @explode("=", $parameter); switch ($arg) { case "--get-graphs": $action = 'get-graphs'; break; case "--drop-device": $action = 'drop-device'; break; /* case "--get-device": $action = 'get-device'; break; */ case "--get-graph-templates": $action = 'get-graph-templates'; break; case "--ip": $ip = trim($value); break; case "--host-template": $host_template = trim($value); break; case "--delete-graphs": $delete_graphs = TRUE; break; case "--include-path": $include_path = trim($value); break; case "--version": case "-V": case "-H": case "--help": die(default_die()); default: die("ERROR: Invalid Argument: ($arg)"); } } } else { die(default_die()); } $include_path = dirname(__FILE__).'/'.$include_path.'/global.php'; if (!file_exists($include_path)) { die("File " . $include_path . "/global.php not found (check include_path in freeside export config)"); } include($include_path); include_once($config["base_path"]."/lib/api_device.php"); include_once($config["base_path"]."/lib/api_automation_tools.php"); include_once($config["base_path"]."/lib/api_data_source.php"); include_once($config["base_path"]."/lib/api_graph.php"); include_once($config["base_path"]."/lib/functions.php"); /* Now take an action */ switch ($action) { case "get-graphs": displayHostGraphs(host_id($ip),TRUE); break; case "drop-device": $host_id = host_id($ip); if ($delete_graphs) { // code copied & pasted from version 0.8.8a // cacti/site/lib/host.php and cacti/site/graphs.php // unfortunately no api function for this yet $graphs = db_fetch_assoc("select graph_local.id as local_graph_id from graph_local where graph_local.host_id=" . $host_id); if (sizeof($graphs) > 0) { foreach ($graphs as $graph) { $data_sources = array_rekey(db_fetch_assoc("SELECT data_template_data.local_data_id FROM (data_template_rrd, data_template_data, graph_templates_item) WHERE graph_templates_item.task_item_id=data_template_rrd.id AND data_template_rrd.local_data_id=data_template_data.local_data_id AND graph_templates_item.local_graph_id=" . $graph["local_graph_id"] . " AND data_template_data.local_data_id > 0"), "local_data_id", "local_data_id"); if (sizeof($data_sources)) { api_data_source_remove_multi($data_sources); } api_graph_remove($graph["local_graph_id"]); } } } api_device_remove($host_id); if (host_id($ip,1)) { die("Failed to remove hostname $ip"); } exit(0); /* case "get-device": echo host_id($ip); exit(0); */ case "get-graph-templates": if (!$host_template) { die("No host template specified"); } $graphs = getGraphTemplatesByHostTemplate($host_template); if (sizeof($graphs)) { foreach (array_keys($graphs) as $gtid) { echo $gtid . "\n"; } exit(0); } die("No graph templates associated with this host template"); default: die("Specified action not found, contact a developer"); } function default_die() { return "Cacti interface for freeside. Do not use for anything else."; } function host_id($ip_address, $nodie=0) { if (!$ip_address) { die("No hostname specified"); } $devices = array(); $query = "select id from host"; $query .= " where hostname='$ip_address'"; $devices = db_fetch_assoc($query); if (sizeof($devices) > 1) { // This should never happen, just being thorough die("Multiple devices found for hostname $ip_address"); } else if (!sizeof($devices)) { if ($nodie) { return ''; } else { die("Could not find hostname $ip_address"); } } return $devices[0]['id']; } ?>