#!/usr/bin/env php 1) { db_switch_remote_to_main(); } // process calling arguments $parms = $_SERVER['argv']; array_shift($parms); unset($host_id); unset($graph_template_id); unset($data_template_id); foreach ($parms as $parameter) { if (str_contains($parameter, '=')) { [$arg, $value] = explode('=', $parameter, 2); } else { $arg = $parameter; $value = ''; } switch ($arg) { case '--host-id': $host_id = trim($value); if (!is_numeric($host_id)) { print 'ERROR: You must supply a valid host-id to run this script!' . PHP_EOL; exit(1); } break; case '--data-template-id': $data_template_id = $value; if (!is_numeric($data_template_id)) { print 'ERROR: You must supply a numeric data-template-id!' . PHP_EOL; exit(1); } break; case '--version': case '-V': case '-v': display_version(); exit(0); case '--help': case '-H': case '-h': display_help(); exit(0); default: print 'ERROR: Invalid Parameter ' . $parameter . PHP_EOL . PHP_EOL; display_help(); exit(1); } } if (!isset($host_id)) { print 'ERROR: You must supply a valid host-id!' . PHP_EOL; exit(1); } if (!isset($data_template_id)) { print 'ERROR: You must supply a valid data-template-id!' . PHP_EOL; exit(1); } // Following code was copied from data_sources.php->function form_save->save_component_data_source_new $save['id'] = '0'; $save['data_template_id'] = $data_template_id; $save['host_id'] = $host_id; $local_data_id = sql_save($save, 'data_local'); change_data_template($local_data_id, $data_template_id); // update the title cache update_data_source_title_cache($local_data_id); // update host data if (!empty($host_id)) { push_out_host($host_id, $local_data_id); } print "DS Added - DS[$local_data_id]" . PHP_EOL; /** * display_version - displays version information * * @return void */ function display_version() : void { $version = get_cacti_cli_version(); print "Cacti Add Data Source, Version $version, " . COPYRIGHT_YEARS . PHP_EOL; } /** * display_help - displays help information * * @return void */ function display_help() : void { display_version(); print PHP_EOL; print 'usage: add_datasource.php --host-id=[ID] --data-template-id=[ID]' . PHP_EOL . PHP_EOL; print 'Cacti utility for adding datasources via a command line interface.' . PHP_EOL . PHP_EOL; print '--host-id=id - The host id' . PHP_EOL; print '--data-template-id=id - The numerical ID of the data template to be added' . PHP_EOL; }