forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller_reindex_hosts.php
250 lines (205 loc) · 7.38 KB
/
poller_reindex_hosts.php
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/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__ . '/../include/cli_check.php');
require_once(CACTI_PATH_LIBRARY . '/api_automation_tools.php');
require_once(CACTI_PATH_LIBRARY . '/api_automation.php');
require_once(CACTI_PATH_LIBRARY . '/api_data_source.php');
require_once(CACTI_PATH_LIBRARY . '/api_graph.php');
require_once(CACTI_PATH_LIBRARY . '/api_device.php');
require_once(CACTI_PATH_LIBRARY . '/api_tree.php');
require_once(CACTI_PATH_LIBRARY . '/data_query.php');
require_once(CACTI_PATH_LIBRARY . '/poller.php');
require_once(CACTI_PATH_LIBRARY . '/snmp.php');
require_once(CACTI_PATH_LIBRARY . '/sort.php');
require_once(CACTI_PATH_LIBRARY . '/template.php');
require_once(CACTI_PATH_LIBRARY . '/utility.php');
ini_set('max_execution_time', '0');
/* 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);
$debug = false;
$host_id = '';
$query_id = 'all'; /* just to mimic the old behaviour */
$host_descr = '';
$force = false;
if (cacti_sizeof($parms)) {
foreach ($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter, 2);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-id':
case '--id':
$host_id = $value;
break;
case '-qid':
case '--qid':
$query_id = $value;
break;
case '--force':
$force = true;
break;
case '-host-descr':
case '--host-descr':
$host_descr = $value;
break;
case '-d':
case '--debug':
$debug = true;
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);
}
}
} else {
print 'ERROR: You must supply input parameters' . PHP_EOL . PHP_EOL;
display_help();
exit(1);
}
/* determine the hosts to reindex */
$params = array();
if (strtolower($host_id) == 'all') {
$sql_where = '';
} elseif (is_numeric($host_id) && $host_id > 0) {
$sql_where = 'WHERE host_id = ?';
$params[] = $host_id;
} else {
print 'ERROR: You must specify either a host_id or \'all\' to proceed.' . PHP_EOL;
display_help();
exit;
}
/* determine data queries to rerun */
if (strtolower($query_id) == 'all') {
/* do nothing */
} elseif (is_numeric($query_id) && $query_id > 0) {
$sql_where .= ($sql_where != '' ? ' AND':'WHERE') . ' hsq.snmp_query_id = ?';
$params[] = $query_id;
} else {
print 'ERROR: You must specify either a query_id or \'all\' to proceed.' . PHP_EOL;
display_help();
exit;
}
/* allow for additional filtering on host description */
$sql_where .= ($sql_where != '' ? ' AND':'WHERE') . ' IFNULL(TRIM(s.disabled),"") != "on" AND IFNULL(TRIM(h.disabled),"") != "on"';
if ($host_descr != '') {
$sql_where .= ($sql_where != '' ? ' AND':'WHERE') . ' h.description LIKE ?';
$params[] = '%' . $host_descr . '%';
}
$data_queries = db_fetch_assoc_prepared("SELECT h.description, h.hostname, hsq.host_id, hsq.snmp_query_id
FROM host_snmp_query hsq
INNER JOIN host h
ON h.id = hsq.host_id
LEFT JOIN sites s
ON s.id = h.site_id
$sql_where",
$params);
/* issue warnings and start message if applicable */
print 'WARNING: Do not interrupt this script. Reindexing can take quite some time' . PHP_EOL;
debug("There are '" . cacti_sizeof($data_queries) . "' data queries to run");
/* silently end if the registered process is still running */
if (!$force) {
if (!register_process_start('reindex', 'master', 0, 86400)) {
print "FATAL: Detected an already running process. Use --force to override" . PHP_EOL;
exit(0);
}
}
$i = 1;
$total_start = microtime(true);
if (cacti_sizeof($data_queries)) {
foreach ($data_queries as $data_query) {
if (!$debug) {
print '.';
}
$start = microtime(true);
run_data_query($data_query['host_id'], $data_query['snmp_query_id'], false, $force);
$items = db_fetch_cell_prepared('SELECT COUNT(*)
FROM host_snmp_cache
WHERE host_id = ?
AND snmp_query_id = ?',
array($data_query['host_id'], $data_query['snmp_query_id']));
$orphans = db_fetch_cell_prepared('SELECT COUNT(*)
FROM graph_local
WHERE host_id = ?
AND snmp_query_id = ?
AND snmp_index = ""',
array($data_query['host_id'], $data_query['snmp_query_id']));
$end = microtime(true);
$message = sprintf(
'Re-Index Complete: Number[%d], TotalTime[%4.2f], QueryTime[%3.2f], Device[%d], Description[%s], DQ[%d], Items[%d], Orphans[%d]',
$i,
$end - $total_start,
$end - $start,
$data_query['host_id'],
$data_query['description'],
$data_query['snmp_query_id'],
$items,
$orphans
);
debug($message);
$i++;
}
set_config_option('reindex_last_run_time', time());
unregister_process('reindex', 'master');
}
function display_version() {
$version = get_cacti_cli_version();
print "Cacti Reindex Host Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
/* display_help - displays the usage of the function */
function display_help() {
display_version();
print 'usage: poller_reindex_hosts.php --id=[host_id|all] [--qid=[ID|all]]' . PHP_EOL;
print ' [--host-descr=[description]] [--debug]' . PHP_EOL . PHP_EOL;
print '--id=host_id - The host_id to have data queries reindexed or \'all\' to reindex all hosts' . PHP_EOL;
print '--qid=query_id - Only index on a specific data query id; defaults to \'all\'' . PHP_EOL;
print '--host-descr=description - The host description to filter by (SQL filters acknowledged)' . PHP_EOL;
print '--force - Force Graph and Data Source Suggested Name Re-mapping for all items' . PHP_EOL;
print '--debug - Display verbose output during execution' . PHP_EOL;
}
function debug($message) {
global $debug;
if ($debug) {
print 'DEBUG: ' . $message . PHP_EOL;
}
}