forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrebuild_poller_cache.php
More file actions
executable file
·201 lines (169 loc) · 6.22 KB
/
rebuild_poller_cache.php
File metadata and controls
executable file
·201 lines (169 loc) · 6.22 KB
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
#!/usr/bin/env php
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2023 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($config['base_path'] . '/lib/poller.php');
require_once($config['base_path'] . '/lib/utility.php');
if ($config['poller_id'] > 1) {
print "FATAL: This utility is designed for the main Data Collector only" . PHP_EOL;
exit(1);
}
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
$debug = false;
$host_id = 0;
$host_template_id = 0;
if (cacti_sizeof($parms)) {
foreach($parms as $parameter) {
if (strpos($parameter, '=')) {
list($arg, $value) = explode('=', $parameter);
} else {
$arg = $parameter;
$value = '';
}
switch ($arg) {
case '-d':
case '--debug':
$debug = true;
break;
case '--host-id':
$host_id = trim($value);
if (!is_numeric($host_id)) {
print 'ERROR: You must supply a valid device id to run this script!' . PHP_EOL;
exit(1);
}
break;
case '--host-template-id':
$host_template_id = trim($value);
if (!is_numeric($host_id)) {
print 'ERROR: You must supply a valid device template id to run this script!' . 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);
}
}
}
/* obtain timeout settings */
$max_execution = ini_get('max_execution_time');
/* set new timeout */
ini_set('max_execution_time', '0');
$sql_where = '';
$params = array();
if ($host_id > 0) {
$sql_where = 'WHERE dl.host_id = ?';
$params[] = $host_id;
}
if ($host_template_id > 0) {
$sql_where .= ($sql_where != '' ? ' AND ':'WHERE ') . ' h.host_template_id = ?';
$params[] = $host_template_id;
}
$pollers = array_rekey(
db_fetch_assoc('SELECT DISTINCT poller_id
FROM host
WHERE disabled = ""'),
'poller_id', 'poller_id'
);
if (cacti_sizeof($pollers)) {
print 'NOTE: Do not interrupt this script. Rebuilding the Poller Cache can take quite some time' . PHP_EOL;
foreach($pollers as $poller_id) {
/* get the hosts for this poller_id */
$hosts = array_rekey(
db_fetch_assoc_prepared("SELECT DISTINCT dl.host_id
FROM data_local AS dl
INNER JOIN host AS h
ON dl.host_id = h.id
$sql_where
AND poller_id = $poller_id
ORDER BY dl.host_id",
$params),
'host_id', 'host_id'
);
/* add special host 0 */
if ($poller_id == 1 && $host_template_id == 0 && $host_id == 0) {
$hosts[0] = 0;
}
/* issue warnings and start message if applicable */
debug("There are '" . cacti_sizeof($hosts) . "' Devices to rebuild poller items for on poller $poller_id.");
/* start rebuilding the poller cache */
if (cacti_sizeof($hosts)) {
foreach ($hosts as $host_id) {
if ($host_id > 0) {
$description = db_fetch_cell_prepared('SELECT description FROM host WHERE id = ?', array($host_id));
} else {
$description = 'Special Host Zero';
}
$data_sources = db_fetch_cell_prepared('SELECT COUNT(*) FROM data_local WHERE host_id = ?', array($host_id));
if ($data_sources > 0) {
print "NOTE: Processing Device:'$description' on Poller:'$poller_id' having $data_sources Data Sources." . PHP_EOL;
push_out_host($host_id);
} else {
print "NOTE: Removing Poller Cache Items for Device:'$description' on Poller:'$poller_id' as it has no Data Sources." . PHP_EOL;
db_execute_prepared('DELETE FROM poller_item WHERE host_id = ?', array($host_id));
}
}
}
}
}
if (!$debug) {
print PHP_EOL;
}
/* poller cache rebuilt, restore runtime parameters */
ini_set('max_execution_time', $max_execution);
/* display_version - displays version information */
function display_version() {
$version = get_cacti_cli_version();
print "Cacti Rebuild Poller Cache Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}
/* display_help - displays the usage of the function */
function display_help () {
display_version();
print PHP_EOL . 'usage: rebuild_poller_cache.php [--host-id=ID] [--debug]' . PHP_EOL . PHP_EOL;
print 'A utility to repopulate Cacti\'s poller cache for a host or a system. Note: That when performing' . PHP_EOL;
print 'for an entire Cacti system, especially a large one, this may take some time.' . PHP_EOL . PHP_EOL;
print 'Optional:' . PHP_EOL;
print ' --host-id=ID - Limit the repopulation to a single Device' . PHP_EOL;
print ' --host-template-id=ID - Limit the repopulation to a single Device Template' . PHP_EOL;
print ' --debug - Display verbose output during execution' . PHP_EOL . PHP_EOL;
}
function debug($message) {
global $debug;
if ($debug) {
print 'DEBUG: ' . trim($message) . "\n";
}
}