1#!/usr/bin/env php
2<?php
3/*
4 +-------------------------------------------------------------------------+
5 | Copyright (C) 2004-2021 The Cacti Group                                 |
6 |                                                                         |
7 | This program is free software; you can redistribute it and/or           |
8 | modify it under the terms of the GNU General Public License             |
9 | as published by the Free Software Foundation; either version 2          |
10 | of the License, or (at your option) any later version.                  |
11 |                                                                         |
12 | This program is distributed in the hope that it will be useful,         |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
15 | GNU General Public License for more details.                            |
16 +-------------------------------------------------------------------------+
17 | Cacti: The Complete RRDtool-based Graphing Solution                     |
18 +-------------------------------------------------------------------------+
19 | This code is designed, written, and maintained by the Cacti Group. See  |
20 | about.php and/or the AUTHORS file for specific developer information.   |
21 +-------------------------------------------------------------------------+
22 | http://www.cacti.net/                                                   |
23 +-------------------------------------------------------------------------+
24*/
25
26require(__DIR__ . '/../include/cli_check.php');
27require_once($config['base_path'] . '/lib/api_automation_tools.php');
28require_once($config['base_path'] . '/lib/api_automation.php');
29require_once($config['base_path'] . '/lib/api_data_source.php');
30require_once($config['base_path'] . '/lib/api_graph.php');
31require_once($config['base_path'] . '/lib/api_device.php');
32require_once($config['base_path'] . '/lib/data_query.php');
33require_once($config['base_path'] . '/lib/poller.php');
34require_once($config['base_path'] . '/lib/snmp.php');
35require_once($config['base_path'] . '/lib/sort.php');
36require_once($config['base_path'] . '/lib/template.php');
37require_once($config['base_path'] . '/lib/utility.php');
38
39/* process calling arguments */
40$parms = $_SERVER['argv'];
41array_shift($parms);
42
43if (cacti_sizeof($parms)) {
44	$displayHosts 		= false;
45	$displayDataQueries = false;
46	$quietMode			= false;
47
48	unset($host_id);
49	unset($data_query_id);
50	unset($reindex_method);
51
52	foreach($parms as $parameter) {
53		if (strpos($parameter, '=')) {
54			list($arg, $value) = explode('=', $parameter);
55		} else {
56			$arg = $parameter;
57			$value = '';
58		}
59
60		switch ($arg) {
61			case '-d':
62				$debug = true;
63
64				break;
65			case '--host-id':
66				$host_id = trim($value);
67				if (!is_numeric($host_id)) {
68					print "ERROR: You must supply a valid host-id to run this script!\n";
69					exit(1);
70				}
71
72				break;
73			case '--data-query-id':
74				$data_query_id = $value;
75				if (!is_numeric($data_query_id)) {
76					print "ERROR: You must supply a numeric data-query-id for all hosts!\n";
77					exit(1);
78				}
79
80				break;
81			case '--reindex-method':
82				if (is_numeric($value) &&
83					($value >= DATA_QUERY_AUTOINDEX_NONE) &&
84					($value <= DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION)) {
85					$reindex_method = $value;
86				} else {
87					switch (strtolower($value)) {
88						case 'none':
89							$reindex_method = DATA_QUERY_AUTOINDEX_NONE;
90							break;
91						case 'uptime':
92							$reindex_method = DATA_QUERY_AUTOINDEX_BACKWARDS_UPTIME;
93							break;
94						case 'index':
95							$reindex_method = DATA_QUERY_AUTOINDEX_INDEX_NUM_CHANGE;
96							break;
97						case 'fields':
98							$reindex_method = DATA_QUERY_AUTOINDEX_FIELD_VERIFICATION;
99							break;
100						default:
101							print "ERROR: You must supply a valid reindex method for all hosts!\n";
102							exit(1);
103					}
104				}
105				break;
106			case '--version':
107			case '-V':
108			case '-v':
109				display_version();
110				exit(0);
111			case '--help':
112			case '-H':
113			case '-h':
114				display_help();
115				exit(0);
116			case '--list-hosts':
117				$displayHosts = true;
118				break;
119			case '--list-data-queries':
120				$displayDataQueries = true;
121				break;
122			case '--quiet':
123				$quietMode = true;
124				break;
125			default:
126				print "ERROR: Invalid Argument: ($arg)\n\n";
127				display_help();
128				exit(1);
129		}
130	}
131
132	/* list options, recognizing $quietMode */
133	if ($displayHosts) {
134		$hosts = getHosts();
135		displayHosts($hosts, $quietMode);
136		exit;
137	}
138	if ($displayDataQueries) {
139		$data_queries = getSNMPQueries();
140		displaySNMPQueries($data_queries, $quietMode);
141		exit;
142	}
143
144	/*
145	 * verify required parameters
146	 * for update / insert options
147	 */
148	if (!isset($host_id)) {
149		print "ERROR: You must supply a valid host-id for all hosts!\n";
150		exit(1);
151	}
152
153	if (!isset($data_query_id)) {
154		print "ERROR: You must supply a valid data-query-id for all hosts!\n";
155		exit(1);
156	}
157
158	if (!isset($reindex_method)) {
159		print "ERROR: You must supply a valid reindex-method for all hosts!\n";
160		exit(1);
161	}
162
163
164	/*
165	 * verify valid host id and get a name for it
166	 */
167	$host_name = db_fetch_cell('SELECT hostname FROM host WHERE id = ' . $host_id);
168	if (!isset($host_name)) {
169		print "ERROR: Unknown Host Id ($host_id)\n";
170		exit(1);
171	}
172
173	/*
174	 * verify valid data query and get a name for it
175	 */
176	$data_query_name = db_fetch_cell('SELECT name FROM snmp_query WHERE id = ' . $data_query_id);
177	if (!isset($data_query_name)) {
178		print "ERROR: Unknown Data Query Id ($data_query_id)\n";
179		exit(1);
180	}
181
182	/*
183	 * Now, add the data query and run it once to get the cache filled
184	 */
185	$exists_already = db_fetch_cell("SELECT host_id FROM host_snmp_query WHERE host_id=$host_id AND snmp_query_id=$data_query_id AND reindex_method=$reindex_method");
186	if ((isset($exists_already)) &&
187		($exists_already > 0)) {
188		print "ERROR: Data Query is already associated for host: ($host_id: $host_name) data query ($data_query_id: $data_query_name) reindex method ($reindex_method: " . $reindex_types[$reindex_method] . ")\n";
189		exit(1);
190	} else {
191		db_execute('REPLACE INTO host_snmp_query
192			(host_id,snmp_query_id,reindex_method)
193			VALUES (' .
194				$host_id        . ',' .
195				$data_query_id  . ',' .
196				$reindex_method . ')');
197
198		/* recache snmp data */
199		run_data_query($host_id, $data_query_id);
200	}
201
202	if (is_error_message()) {
203		print "ERROR: Failed to add this data query for host ($host_id: $host_name) data query ($data_query_id: $data_query_name) reindex method ($reindex_method: " . $reindex_types[$reindex_method] . ")\n";
204		exit(1);
205	} else {
206		print "Success - Host ($host_id: $host_name) data query ($data_query_id: $data_query_name) reindex method ($reindex_method: " . $reindex_types[$reindex_method] . ")\n";
207		exit;
208	}
209} else {
210	display_help();
211	exit;
212}
213
214/*  display_version - displays version information */
215function display_version() {
216	$version = get_cacti_cli_version();
217	print "Cacti Add Data Query Utility, Version $version, " . COPYRIGHT_YEARS . "\n";
218}
219
220function display_help() {
221	display_version();
222
223	print "\nusage: add_data_query.php --host-id=[ID] --data-query-id=[dq_id] --reindex-method=[method] [--quiet]\n\n";
224	print "Required Options:\n";
225	print "    --host-id         the numerical ID of the host\n";
226	print "    --data-query-id   the numerical ID of the data_query to be added\n";
227	print "    --reindex-method  the reindex method to be used for that data query\n";
228	print "                      0|None   = no reindexing\n";
229	print "                      1|Uptime = Uptime goes Backwards\n";
230	print "                      2|Index  = Index Count Changed\n";
231	print "                      3|Fields = Verify all Fields\n\n";
232	print "List Options:\n";
233	print "    --list-hosts\n";
234	print "    --list-data-queries\n";
235	print "    --quiet - batch mode value return\n\n";
236	print "If the data query was already associated, it will be reindexed.\n\n";
237}
238