File: add_datasource.php

package info (click to toggle)
cacti 1.2.30%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,176 kB
  • sloc: php: 123,193; javascript: 29,825; sql: 2,595; xml: 1,823; sh: 1,228; perl: 194; makefile: 65; python: 51; ruby: 9
file content (128 lines) | stat: -rwxr-xr-x 4,190 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env php
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004-2024 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/                                                   |
 +-------------------------------------------------------------------------+
*/

require(__DIR__ . '/../site/include/cli_check.php');
require_once($config['base_path'] . '/lib/api_data_source.php');
require_once($config['base_path'] . '/lib/poller.php');
require_once($config['base_path'] . '/lib/utility.php');
require_once($config['base_path'] . '/lib/template.php');

/* switch to main database for cli's */
if ($config['poller_id'] > 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 (strpos($parameter, '=')) {
		list($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!\n";
	exit(1);
}

if (!isset($data_template_id)) {
	print "ERROR: You must supply a valid data-template-id!\n";
	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]\n";

/*  display_version - displays version information */
function display_version() {
	$version = get_cacti_cli_version();
	print "Cacti Add Data Source, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}

function display_help() {
	display_version();
	print "usage: add_datasource.php --host-id=[ID] --data-template-id=[ID]\n\n";
	print "Cacti utility for adding datasources via a command line interface.\n\n";
	print "--host-id=id - The host id\n";
	print "--data-template-id=id - The numerical ID of the data template to be added\n";
}