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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2023 Zabbix SIA
**
** 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.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
/**
* Class containing methods for operations with configuration.
*/
class CConfiguration extends CApiService {
public const ACCESS_RULES = [
'export' => ['min_user_type' => USER_TYPE_ZABBIX_USER],
'import' => ['min_user_type' => USER_TYPE_ZABBIX_USER],
'importcompare' => ['min_user_type' => USER_TYPE_ZABBIX_USER]
];
/**
* @param array $params
*
* @return string
*/
private function exportCompare(array $params) {
$this->validateExport($params, true);
return $this->exportForce($params);
}
/**
* @param array $params
*
* @return string
*/
public function export(array $params) {
$this->validateExport($params);
return $this->exportForce($params);
}
/**
* Validate input parameters for export() and exportCompare() methods.
*
* @param array $params
* @param bool $with_unlinked_parent_templates
*
* @throws APIException if the input is invalid.
*/
private function validateExport(array &$params, bool $with_unlinked_parent_templates = false): void {
$api_input_rules = ['type' => API_OBJECT, 'fields' => [
'format' => ['type' => API_STRING_UTF8, 'flags' => API_REQUIRED, 'in' => implode(',', [CExportWriterFactory::YAML, CExportWriterFactory::XML, CExportWriterFactory::JSON, CExportWriterFactory::RAW])],
'prettyprint' => ['type' => API_BOOLEAN, 'default' => false],
'options' => ['type' => API_OBJECT, 'flags' => API_REQUIRED, 'fields' => [
'groups' => ['type' => API_IDS],
'hosts' => ['type' => API_IDS],
'images' => ['type' => API_IDS],
'maps' => ['type' => API_IDS],
'mediaTypes' => ['type' => API_IDS],
'templates' => ['type' => API_IDS]
]]
]];
if ($with_unlinked_parent_templates) {
$api_input_rules['fields'] += ['unlink_parent_templates' => ['type' => API_OBJECTS, 'flags' => API_ALLOW_NULL, 'default' => [], 'fields' => [
'templateid' => ['type' => API_ID],
'unlink_templateids' => ['type' => API_IDS]
]]];
}
if (!CApiInputValidator::validate($api_input_rules, $params, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
if ($params['format'] === CExportWriterFactory::XML) {
$lib_xml = (new CFrontendSetup())->checkPhpLibxml();
if ($lib_xml['result'] == CFrontendSetup::CHECK_FATAL) {
self::exception(ZBX_API_ERROR_INTERNAL, $lib_xml['error']);
}
$xml_writer = (new CFrontendSetup())->checkPhpXmlWriter();
if ($xml_writer['result'] == CFrontendSetup::CHECK_FATAL) {
self::exception(ZBX_API_ERROR_INTERNAL, $xml_writer['error']);
}
}
}
/**
* @param array $params
*
* @return string
*/
private function exportForce(array $params) {
$params['unlink_parent_templates'] = array_key_exists('unlink_parent_templates', $params)
? $params['unlink_parent_templates']
: [];
$export = new CConfigurationExport($params['options'], $params['unlink_parent_templates']);
$export->setBuilder(new CConfigurationExportBuilder());
$writer = CExportWriterFactory::getWriter($params['format']);
$writer->formatOutput($params['prettyprint']);
$export->setWriter($writer);
$export_data = $export->export();
if ($export_data === false) {
self::exception(ZBX_API_ERROR_PERMISSIONS, _('No permissions to referred object or it does not exist!'));
}
return $export_data;
}
/**
* Validate input parameters for import() and importcompare() methods.
*
* @param type $params
*
* @throws APIException if the input is invalid.
*/
protected function validateImport($params): void {
$api_input_rules = ['type' => API_OBJECT, 'fields' => [
'format' => ['type' => API_STRING_UTF8, 'flags' => API_REQUIRED, 'in' => implode(',', [CImportReaderFactory::YAML, CImportReaderFactory::XML, CImportReaderFactory::JSON])],
'source' => ['type' => API_STRING_UTF8, 'flags' => API_REQUIRED],
'rules' => ['type' => API_OBJECT, 'flags' => API_REQUIRED, 'fields' => [
'discoveryRules' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false],
'deleteMissing' => ['type' => API_BOOLEAN, 'default' => false]
]],
'graphs' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false],
'deleteMissing' => ['type' => API_BOOLEAN, 'default' => false]
]],
'groups' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false]
]],
'hosts' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false]
]],
'httptests' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false],
'deleteMissing' => ['type' => API_BOOLEAN, 'default' => false]
]],
'images' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false]
]],
'items' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false],
'deleteMissing' => ['type' => API_BOOLEAN, 'default' => false]
]],
'maps' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false]
]],
'mediaTypes' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false]
]],
'templateLinkage' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'deleteMissing' => ['type' => API_BOOLEAN, 'default' => false]
]],
'templates' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false]
]],
'templateDashboards' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false],
'deleteMissing' => ['type' => API_BOOLEAN, 'default' => false]
]],
'triggers' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false],
'deleteMissing' => ['type' => API_BOOLEAN, 'default' => false]
]],
'valueMaps' => ['type' => API_OBJECT, 'fields' => [
'createMissing' => ['type' => API_BOOLEAN, 'default' => false],
'updateExisting' => ['type' => API_BOOLEAN, 'default' => false],
'deleteMissing' => ['type' => API_BOOLEAN, 'default' => false]
]]
]]
]];
if (!CApiInputValidator::validate($api_input_rules, $params, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
if (array_key_exists('maps', $params['rules']) && !self::checkAccess(CRoleHelper::ACTIONS_EDIT_MAPS)
&& ($params['rules']['maps']['createMissing'] || $params['rules']['maps']['updateExisting'])) {
self::exception(ZBX_API_ERROR_PARAMETERS, _s('Incorrect value for field "%1$s": %2$s.', 'rules',
_('no permissions to create and edit maps')
));
}
if ($params['format'] === CImportReaderFactory::XML) {
$lib_xml = (new CFrontendSetup())->checkPhpLibxml();
if ($lib_xml['result'] == CFrontendSetup::CHECK_FATAL) {
self::exception(ZBX_API_ERROR_INTERNAL, $lib_xml['error']);
}
$xml_reader = (new CFrontendSetup())->checkPhpXmlReader();
if ($xml_reader['result'] == CFrontendSetup::CHECK_FATAL) {
self::exception(ZBX_API_ERROR_INTERNAL, $xml_reader['error']);
}
}
}
/**
* @param array $params
*
* @return bool
*/
public function import($params) {
$this->validateImport($params);
$import_reader = CImportReaderFactory::getReader($params['format']);
$data = $import_reader->read($params['source']);
$import_validator_factory = new CImportValidatorFactory($params['format']);
$import_converter_factory = new CImportConverterFactory();
$validator = new CXmlValidator($import_validator_factory, $params['format']);
$data = $validator
->setStrict(true)
->validate($data, '/');
foreach (['1.0', '2.0', '3.0', '3.2', '3.4', '4.0', '4.2', '4.4', '5.0', '5.2', '5.4'] as $version) {
if ($data['zabbix_export']['version'] !== $version) {
continue;
}
$data = $import_converter_factory
->getObject($version)
->convert($data);
$data = $validator
// Must not use XML_INDEXED_ARRAY key validation for the converted data.
->setStrict(false)
->validate($data, '/');
}
// Get schema for converters.
$schema = $import_validator_factory
->getObject(ZABBIX_EXPORT_VERSION)
->getSchema();
// Convert human readable import constants to values Zabbix API can work with.
$data = (new CConstantImportConverter($schema))->convert($data);
// Add default values in place of missed tags.
$data = (new CDefaultImportConverter($schema))->convert($data);
// Normalize array keys and strings.
$data = (new CImportDataNormalizer($schema))->normalize($data);
$adapter = new CImportDataAdapter();
$adapter->load($data);
$configuration_import = new CConfigurationImport(
$params['rules'],
new CImportReferencer(),
new CImportedObjectContainer()
);
return $configuration_import->import($adapter);
}
/**
* Preview changes that would be done to templates.
*
* @param array $params Same params, as for import.
*
* @return array
*
* @throws APIException
* @throws Exception
*/
public function importcompare(array $params): array {
$this->validateImport($params);
$import_reader = CImportReaderFactory::getReader($params['format']);
$data = $import_reader->read($params['source']);
$import_validator_factory = new CImportValidatorFactory($params['format']);
$import_converter_factory = new CImportConverterFactory();
$validator = new CXmlValidator($import_validator_factory, $params['format']);
$data = $validator
->setStrict(true)
->setPreview(true)
->validate($data, '/');
foreach (['1.0', '2.0', '3.0', '3.2', '3.4', '4.0', '4.2', '4.4', '5.0', '5.2', '5.4'] as $version) {
if ($data['zabbix_export']['version'] !== $version) {
continue;
}
$data = $import_converter_factory
->getObject($version)
->convert($data);
$data = $validator
// Must not use XML_INDEXED_ARRAY key validation for the converted data.
->setStrict(false)
->setPreview(true)
->validate($data, '/');
}
// Get schema for converters.
$schema = $import_validator_factory
->getObject(ZABBIX_EXPORT_VERSION)
->getSchema();
// Normalize array keys and strings.
$data = (new CImportDataNormalizer($schema))->normalize($data);
$adapter = new CImportDataAdapter();
$adapter->load($data);
$import = $adapter->getData();
$imported_entities = [];
$entities = [
'groups' => 'name',
'templates' => 'template'
];
foreach ($entities as $entity => $name_field) {
if (array_key_exists($entity, $import)) {
$imported_entities[$entity]['uuid'] = array_column($import[$entity], 'uuid');
$imported_entities[$entity][$name_field] = array_column($import[$entity], $name_field);
}
}
$imported_ids = [];
$db_templates = [];
foreach ($imported_entities as $entity => $data) {
switch ($entity) {
case 'groups':
$imported_ids['groups'] = API::HostGroup()->get([
'output' => ['groupid'],
'filter' => [
'uuid' => $data['uuid'],
'name' => $data['name']
],
'preservekeys' => true,
'searchByAny' => true
]);
$imported_ids['groups'] = array_keys($imported_ids['groups']);
break;
case 'templates':
$options = [
'output' => ['templateid', 'uuid'],
'filter' => [
'uuid' => $data['uuid'],
'host' => $data['template']
],
'preservekeys' => true,
'searchByAny' => true
];
if ($params['rules']['templateLinkage']['deleteMissing']) {
$options['selectParentTemplates'] = ['templateid', 'name'];
}
$db_templates = API::Template()->get($options);
$imported_ids['templates'] = array_keys($db_templates);
break;
default:
break;
}
}
$unlink_templates_data = [];
if ($params['rules']['templateLinkage']['deleteMissing']) {
$import_tmp_parent_tmp_names = [];
foreach ($import['templates'] as $template) {
if (array_key_exists('templates', $template)) {
$parent_tmp = array_column($template['templates'], 'name');
$import_tmp_parent_tmp_names[$template['name']] = $parent_tmp;
$import_tmp_parent_tmp_names[$template['uuid']] = $parent_tmp;
}
else {
$import_tmp_parent_tmp_names[$template['name']] = [];
$import_tmp_parent_tmp_names[$template['uuid']] = [];
}
}
foreach ($db_templates as $db_template) {
$db_parent_tmp_names = array_column($db_template['parentTemplates'], 'name', 'templateid');
if ($db_parent_tmp_names) {
$unlink_templateids = array_key_exists($db_template['uuid'], $import_tmp_parent_tmp_names)
? array_diff($db_parent_tmp_names, $import_tmp_parent_tmp_names[$db_template['uuid']])
: array_diff($db_parent_tmp_names, $import_tmp_parent_tmp_names[$db_template['name']]);
if ($unlink_templateids) {
$unlink_templates_data[$db_template['templateid']] = [
'templateid' => $db_template['templateid'],
'unlink_templateids' => array_keys($unlink_templateids)
];
}
}
}
}
// Get current state of templates in same format, as import to compare this data.
$export = API::Configuration()->exportCompare([
'format' => CExportWriterFactory::RAW,
'prettyprint' => false,
'options' => $imported_ids,
'unlink_parent_templates' => $unlink_templates_data
]);
// Normalize array keys and strings.
$export = (new CImportDataNormalizer($schema))->normalize($export);
$export = $export['zabbix_export'];
$importcompare = new CConfigurationImportcompare($params['rules']);
return $importcompare->importcompare($export, $import);
}
}
|