This script is only meant to run at the command line."); } /* We are not talking to the browser */ $no_http_headers = true; $dir = "/usr/share/cacti"; chdir($dir); /* work on both Cacti 0.8.6 and Cacti 0.8.7 */ if (file_exists($dir . "/include/global.php")) { include($dir . "/include/global.php"); }else{ include($dir . "/include/config.php"); } include_once($config["base_path"]."/lib/api_device.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/template.php"); include_once($config["base_path"]."/lib/utility.php"); include_once($config["base_path"]."/lib/data_query.php"); include_once($config["base_path"]."/lib/api_tree.php"); /* process calling arguments */ $parms = $_SERVER["argv"]; array_shift($parms); if (sizeof($parms)) { foreach($parms as $parameter) { @list($arg, $value) = @explode("=", $parameter); switch ($arg) { case "--add_device": $add_device_opt = 1; $device_name = trim($value); break; case "--remove_device": $remove_device_opt = 1; $device_name = trim($value); break; case "-h"; display_help(); return 1; case "--help"; display_help(); return 1; default: print "ERROR: Invalid Parameter " . $parameter . "\n\n"; display_help(); return 1; } } } if (isset($add_device_opt) && ($add_device_opt)) { $snmp_community = 'public'; $snmp_version = 1; $snmp_port = '161'; $snmp_username = ''; $snmp_password = ''; $snmp_timeout = 500; $availability_method = '1'; $ping_method = '2'; $host_template_id = '3'; add_device("", $host_template_id, $device_name, $device_name, $snmp_community, $snmp_version, $snmp_username, $snmp_password, $snmp_port, $snmp_timeout, "", $availability_method, $ping_method, "", "", "", "", "", "", "", "", ""); } else if (isset($remove_device_opt) && ($remove_device_opt)) { $device_id = db_fetch_cell("select id from host where hostname = '" . $device_name . "'"); remove_device($device_id); } else { print "ERROR: Invalid Parameter " . $parameter . "\n\n"; display_help(); return 1; } function remove_device($device_id) { $data_sources_to_act_on = array(); $graphs_to_act_on = array(); $data_sources = db_fetch_assoc("select data_local.id as local_data_id from data_local where data_local.host_id = " . $device_id); if (sizeof($data_sources) > 0) { foreach ($data_sources as $data_source) { $data_sources_to_act_on[] = $data_source["local_data_id"]; } } $graphs = db_fetch_assoc("select graph_local.id as local_graph_id from graph_local where graph_local.host_id = " . $device_id); if (sizeof($graphs) > 0) { foreach ($graphs as $graph) { $graphs_to_act_on[] = $graph["local_graph_id"]; } } api_data_source_remove_multi($data_sources_to_act_on); api_graph_remove_multi($graphs_to_act_on); api_device_remove($device_id); } function add_device($id, $host_template_id, $description, $hostname, $snmp_community, $snmp_version, $snmp_username, $snmp_password, $snmp_port, $snmp_timeout, $disabled, $availability_method, $ping_method, $ping_port, $ping_timeout, $ping_retries, $notes, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $max_oids) { $ping_port = 23; $ping_timeout = 400; $ping_retries = 1; $host_id = api_device_save($id, $host_template_id, $description, $hostname, $snmp_community, $snmp_version, $snmp_username, $snmp_password, $snmp_port, $snmp_timeout, $disabled, $availability_method, $ping_method, $ping_port, $ping_timeout, $ping_retries, $notes, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $max_oids) ; $graph_template_ids = db_fetch_assoc("select graph_template_id from host_template_graph where host_template_id=" . $host_template_id); if (sizeof($graph_template_ids) > 0) { foreach ($graph_template_ids as $graph_template_id) { $snmp_query_array = ""; $suggested_values_array = array(); create_complete_graph_from_template($graph_template_id["graph_template_id"], $host_id, $snmp_query_array, $suggested_values_array); } } push_out_host($host_id); $tree_type = "3"; # TREE_ITEM_TYPE_HOST" $tree_id = "1"; # default graph $local_graph_id = "0"; $rra_id = "0"; $host_grouping_type = "1"; $sort_children_type = "1"; #api_tree_item_save($id, $tree_id, $type, $parent_tree_item_id, $title, $local_graph_id, $rra_id, #$host_id, $host_grouping_type, $sort_children_type, $propagate_changes) api_tree_item_save("", $tree_id, $tree_type, "", "", $local_graph_id, $rra_id, $host_id, $host_grouping_type, $sort_children_type, "") ; } function display_help() { echo "Usage:\n"; echo "kusu-cacti.php [--add_device=hostname | --remove_device=hostname]\n\n"; } ?>