File: apply_automation_rules.php

package info (click to toggle)
cacti 1.2.30%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: 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 (194 lines) | stat: -rwxr-xr-x 6,526 bytes parent folder | download | duplicates (2)
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
#!/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_automation_tools.php');
require_once($config['base_path'] . '/lib/api_automation.php');
require_once($config['base_path'] . '/lib/api_data_source.php');
require_once($config['base_path'] . '/lib/api_graph.php');
require_once($config['base_path'] . '/lib/api_device.php');
require_once($config['base_path'] . '/lib/api_tree.php');
require_once($config['base_path'] . '/lib/data_query.php');
require_once($config['base_path'] . '/lib/functions.php');
require_once($config['base_path'] . '/lib/poller.php');
require_once($config['base_path'] . '/lib/reports.php');
require_once($config['base_path'] . '/lib/template.php');
require_once($config['base_path'] . '/lib/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);

global $debug;

$debug       = false;
$hostname    = '';
$description = '';
$ids         = array();

if (cacti_sizeof($parms)) {
	foreach($parms as $parameter) {
		if (strpos($parameter, '=')) {
			list($arg, $value) = explode('=', $parameter, 2);
		} else {
			$arg = $parameter;
			$value = '';
		}

		switch ($arg) {
			case '-d':
			case '--debug':
				$debug = true;
				break;
			case '--hostname':
				$hostname = $value;
				break;
			case '--description':
				$description = $value;
				break;
			case '--ids':
				$ids = explode(' ', $value);
				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);
		}
	}
}

// Check for matching like/regex
if (!cacti_sizeof($ids) && $hostname == '' && $description == '') {
	print 'FATAL: You must specify either ids, a hostname or host description pattern' . PHP_EOL;
	exit(1);
}

$sql_where = '';

// Check device id range
if (cacti_sizeof($ids)) {
	foreach($ids as $id) {
		if (!is_numeric($id) || $id <= 0) {
			print 'FATAL: Device id ' . $id . ' is not a valid device.  Can not continue.' . PHP_EOL;
			exit(1);
		}
	}

	$sql_where = 'WHERE h.id IN (' . implode(',', $ids) . ')';
}

// Check hostname
if ($hostname != '') {
	$sql_where .= ($sql_where != '' ? ' AND ':'WHERE ') . '(';
	$regex = false;

	if (validate_is_regex($hostname)) {
		$regex = true;
		$sql_where .= 'h.hostname RLIKE ' . db_qstr($hostname);
	}

	$hostname = '%' . $hostname . '%';
	$sql_where .= ($regex == true ? ' OR ':'') . 'h.hostname LIKE ' . db_qstr($hostname) . ')';
}

$dregex = false;
if ($description != '') {
	$sql_where .= ($sql_where != '' ? ' AND ':'WHERE ') . '(';
	$regex = false;

	if (validate_is_regex($description)) {
		$regex = true;
		$sql_where .= 'h.description RLIKE ' . db_qstr($description);
	}

	$description = '%' . $description . '%';
	$sql_where .= ($regex == true ? ' OR ':'') . 'h.description LIKE ' . db_qstr($description) . ')';
}

$devices = array_rekey(
	db_fetch_assoc('SELECT id FROM host AS h ' . $sql_where),
	'id', 'id'
);

if (cacti_sizeof($devices)) {
	if ($debug) {
		print 'DEBUG: Found ' . cacti_sizeof($devices) . ' devices to run automation on' . PHP_EOL;
	}

	foreach($devices as $device_id) {
		if ($debug) {
			print 'DEBUG: Running automation for Device ID ' . $device_id . PHP_EOL;
		}

		cacti_log('NOTE: Running CLI based Batch Automation on Device[' . $device_id . ']', false, 'AUTOM8');

		automation_update_device($device_id);
	}

	if ($debug) {
		print 'DEBUG: automation run complete!' . PHP_EOL;
	}
} elseif ($debug) {
	print 'DEBUG: No devices found for this automation pass.' . PHP_EOL;
}

/*  display_version - displays version information */
function display_version() {
	$version = get_cacti_cli_version();
	print "Cacti Apply Automation Rules Utility, Version $version, " . COPYRIGHT_YEARS . PHP_EOL;
}

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

	print PHP_EOL;
	print 'usage: apply_automation_rules.php --ids="id1 id2 ..." || --description=S || --hostname=S [--debug]' . PHP_EOL . PHP_EOL;
	print 'A utility to execute Cacti automation rules for a devices or devices.  Any of the following' . PHP_EOL;
	print 'three options can be used, but at least one must be specified.' . PHP_EOL . PHP_EOL;
	print 'Required:' . PHP_EOL;
	print '    --ids="id1 id2 ..." - A space delimited list of device ids.' . PHP_EOL;
	print '    --hostname="S"      - Either a SQL where clause or REGEX of the devices hostname.' . PHP_EOL;
	print '    --description="S"   - Either a SQL where clause or REGEX of the devices description.' . PHP_EOL . PHP_EOL;
	print 'Optional:' . PHP_EOL;
	print '    --debug             - Provide verbose output during automation' . PHP_EOL . PHP_EOL;
}