File: update_heartbeat.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 (361 lines) | stat: -rw-r--r-- 11,776 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/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');

ini_set('max_execution_time', '0');

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;
$force            = false;
$data_template_id = false;
$prev_heartbeat   = false;
$new_heartbeat    = 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 '--new-heartbeat':
				$new_heartbeat = $value;
				break;
			case '--prev-heartbeat':
				$prev_heartbeat = $value;
				break;
			case '--data-template-id':
				$data_template_id = $value;
				break;
			case '--list-data-templates':
				$data_templates = db_fetch_assoc('SELECT id, name
					FROM data_template
					ORDER BY id');

				if (cacti_sizeof($data_templates)) {
					print 'ID       Data Template Name' . PHP_EOL;
					print '------   ----------------------' . PHP_EOL;

					foreach($data_templates as $dt) {
						printf('%-6s   %-25s' . PHP_EOL, $dt['id'], $dt['name']);
					}

					print PHP_EOL;
				} else {
					print 'WARNING: No Data Templates found!' . PHP_EOL;
				}

				exit(0);

				break;
			case '--list-heartbeats':
				if (cacti_sizeof($heartbeats)) {
					print 'Heartbeat   Heartbeat Name' . PHP_EOL;
					print '---------   --------------' . PHP_EOL;

					foreach($heartbeats as $index => $hb) {
						printf('%-12s%-17s' . PHP_EOL, $index, $hb);
					}

					print PHP_EOL;
				} else {
					print 'WARNING: No Heartbeats found!' . PHP_EOL;
				}

				exit(0);

				break;
			case '--list-profiles':
				$dspheartbeats = db_fetch_assoc('SELECT name, heartbeat
					FROM data_source_profiles
					ORDER BY heartbeat');

				if (cacti_sizeof($dspheartbeats)) {
					print 'Heartbeat   Data Source Profile' . PHP_EOL;
					print '---------   ----------------------' . PHP_EOL;

					foreach($dspheartbeats as $hb) {
						printf('%-12s%-25s' . PHP_EOL, $hb['heartbeat'], $hb['name']);
					}

					print PHP_EOL;
				} else {
					print 'WARNING: No Data Source Profiles found!' . PHP_EOL;
				}

				exit(0);

				break;
			case '--debug':
				$debug = true;
				break;
			case '--force':
				$force = 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);
}

$poller_interval = read_config_option('poller_interval');

if ($poller_interval * 2.0 > $new_heartbeat) {
	printf('ERROR: Your new heartbeat must be greater or equal than %.0f' . PHP_EOL, $poller_interval * 2);
	exit(1);
}

$sql_where  = '';
$sql_params = array();

if ($data_template_id !== false && $data_template_id > 0) {
	$sql_params[] = $data_template_id;
	$sql_where = ' AND dtd.data_template_id = ?';
} elseif ($data_template_id !== false && ($data_template_id <= 0 || !is_numeric($data_template_id))) {
	printf('ERROR: Your Data Template ID \'%s\' is invalid' . PHP_EOL, $data_template_id);
	exit(1);
}

if ($new_heartbeat !== false && (!is_numeric($new_heartbeat) || $new_heartbeat < -1)) {
	printf('ERROR: Your New Heartbeat \'%s\' must be greater or equal to zero.' . PHP_EOL, $new_heartbeat);
	exit(1);
}

if (!array_key_exists($new_heartbeat, $heartbeats)) {
	printf('ERROR: Your New Heartbeat \'%s\' is not a supported Heartbeat.  Use --list-heartbeats to see the list.' . PHP_EOL, $new_heartbeat);
	exit(1);
}

if ($prev_heartbeat !== false && (!is_numeric($prev_heartbeat) || $prev_heartbeat < -1)) {
	printf('ERROR: Your Previous Heartbeat \'%s\' must be greater or equal to zero' . PHP_EOL, $prev_heartbeat);
} elseif ($prev_heartbeat !== false) {
	$sql_where .= ' AND dtr.rrd_heartbeat = ?';
	$sql_params[] = $prev_heartbeat;
}

$sql_params1 = array_merge(array($config['rra_path']), $sql_params);

$rrdfiles = db_fetch_assoc_prepared("SELECT dtr.local_data_id, dtd.name_cache, dt.name,
	REPLACE(dtd.data_source_path, '<path_rra>', ?) AS rrd,
	dtr.rrd_heartbeat, GROUP_CONCAT(DISTINCT dtr.data_source_name) AS data_sources
	FROM data_template_data AS dtd
	INNER JOIN data_template AS dt
	ON dtd.data_template_id = dt.id
	INNER JOIN data_template_rrd AS dtr
	ON dtd.local_data_id = dtr.local_data_id
	WHERE dtd.local_data_id > 0
	$sql_where
	GROUP BY dtd.local_data_id",
	$sql_params1);

$total_heartbeats = array_rekey($rrdfiles, 'rrd_heartbeat', 'rrd_heartbeat');

$profile_ids = db_fetch_assoc_prepared("SELECT DISTINCT dsp.id, dsp.heartbeat, dsp.name
	FROM data_template_data AS dtd
	INNER JOIN data_template_rrd AS dtr
	ON dtd.local_data_id = dtr.local_data_id
	INNER JOIN data_source_profiles AS dsp
	ON dsp.id = dtd.data_source_profile_id
	WHERE dtd.local_data_id > 0
	$sql_where",
	$sql_params);

if (cacti_sizeof($profile_ids) > 1) {
	printf('There are %s Data Source Proiles Impacted by this change.' . PHP_EOL, cacti_sizeof($profile_ids));
} elseif (cacti_sizeof($profile_ids) == 1) {
	printf('There is %s Data Source Proile Impacted by this change.' . PHP_EOL, cacti_sizeof($profile_ids));
} else {
	printf('ERROR: Not RRDfiles found that match your --prev-heartbeat or --data-template-id setting' . PHP_EOL);
	exit(1);
}

printf('Current Data Source Profile Heartbeats found are: %s' . PHP_EOL, implode(', ', $total_heartbeats));
printf('There will be %s RRDfiles updated as a result of this command.' . PHP_EOL, cacti_sizeof($rrdfiles));

if (!$force) {
	$exit = false;

	foreach($profile_ids as $pid) {
		if ($pid['heartbeat'] != $new_heartbeat) {
			printf('ERROR: Data Source Profile \'%s\' has a heartbeat of %s which does' . PHP_EOL, $pid['name'], $pid['heartbeat']);
			printf('       not match the new heartbeat of %s.  Use the --force option to update' . PHP_EOL, $new_heartbeat);
			printf('       the Data Source Profile\'s heartbeat as well as the Data Sources.' . PHP_EOL . PHP_EOL);
			$exit = true;
		}
	}

	if ($exit) {
		exit(1);
	}
} else {
	printf('This is a forced run, impacted Data Source Profiles will have their Heartbeats updated as well' . PHP_EOL);
}

$i = 0;
if (cacti_sizeof($rrdfiles)) {
	foreach($rrdfiles as $f) {
		if (file_exists($f['rrd'])) {
			$command = sprintf("rrdtool tune %s ", $f['rrd']);

			$data_sources = explode(',', $f['data_sources']);

			foreach($data_sources as $ds) {
				$command .= " --heartbeat $ds:$new_heartbeat";
			}

			$output      = array();
			$return_code = 0;

			if (1 == 2) {
				printf("Updating Heartbeat for Data Source:%s, Data Template:%s, RRD:%s from 600 to 900" . PHP_EOL, $f['name_cache'], $f['name'], $f['rrd']);
				printf("The RRDtool command is '$command'" . PHP_EOL);
			}

			$result = exec($command, $output, $return_code);

			if ($return_code != 0) {
				printf("Warning Error Occurred: " . implode(', ', $output) . PHP_EOL);
			} else {
				db_execute_prepared('UPDATE data_template_rrd
					SET rrd_heartbeat = ?
					WHERE local_data_id = ?',
					array($new_heartbeat, $f['local_data_id']));
			}
		} else {
			printf('WARNING: RRDfile \'%s\' does not exist!' . PHP_EOL);
		}

		$i++;

		if ($i % 100 == 0) {
			printf("Processed %s RRDfiles" . PHP_EOL, $i);
		}
	}

	printf("Processed a Total of %s RRDfiles" . PHP_EOL, $i);

	if ($data_template_id > 0) {
		db_execute_prepared('UPDATE data_template_rrd
			SET rrd_heartbeat = ?
			WHERE local_data_id = 0
			AND data_template_id = ?',
			array($new_heartbeat, $data_template_id));
	}

	if ($force) {
		foreach($profile_ids as $pid) {
			db_execute_prepared('UPDATE data_source_profiles
				SET heartbeat = ?
				WHERE id = ?',
				array($new_heartbeat, $pid['id']));
		}

		if ($data_template_id > 0) {
			db_execute_prepared('UPDATE data_template_rrd
				SET rrd_heartbeat = ?
				WHERE local_data_id = 0
				AND data_template_id = ?',
				array($new_heartbeat, $data_template_id));
		} else {
			db_execute_prepared('UPDATE data_template_rrd
				SET rrd_heartbeat = ?
				WHERE local_data_id = 0',
				array($new_heartbeat));
		}
	}
}

/**
 * display_version - displays version information
 *
 * @return (void)
 */
function display_version() {
	$version = get_cacti_cli_version();
	print "Cacti Update RRDfile Heartbeat Utility, Version $version, " . COPYRIGHT_YEARS . "\n";
}

/**
 * display_help - displays the usage of the function
 *
 * @return (void)
 */
function display_help () {
	display_version();

	print "\nusage: update_heartbeat.php --new-heartbeat=N [--data-template-id=id] [--prev-heartbeat=N] [--force] [--debug|-d]\n\n";
	print "A utility to update RRDfile heartbeats and the Cacti database to match.\n\n";
	print "Required:\n";
	print "    --new-heartbeat=N     - A Heartbeat in seconds.  It must align with available Heartbeats in Cacti\n";
	print "                            Currently Heartbeats include from 20-172800 seconds.  The value must also\n";
	print "                            be at least two times the current Cacti poller interval.\n";
	print "Optional:\n";
	print "    --data-template-id=N  - Only update Cacti Data Source Heartbeats that are associated with a Data Template id.\n";
	print "    --prev-heartbeat=N    - Only update Cacti Data Sources that currently have the Heartbeat specified.\n";
	print "    --force               - If the hearbeat selected does not match the Data Source Profile, update the\n";
	print "                            Data Source Profile to match the command.  Otherwise, the script will exit.\n";
	print "    --debug               - Display verbose output during execution\n\n";
	print "List Options:\n";
	print "    --list-data-templates - List all Data Templates\n";
	print "    --list-heartbeats     - List all supported Heartbeats\n";
	print "    --list-profiles       - List all Data Source Profiles and their Heartbeats\n\n";
}

function debug($message) {
	global $debug;

	if ($debug) {
		print "DEBUG: " . trim($message) . "\n";
	}
}