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
|
<?php declare(strict_types = 1);
/*
** Zabbix
** Copyright (C) 2001-2021 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 housekeeping parameters.
*/
class CHousekeeping extends CApiService {
public const ACCESS_RULES = [
'get' => ['min_user_type' => USER_TYPE_ZABBIX_USER],
'update' => ['min_user_type' => USER_TYPE_SUPER_ADMIN]
];
protected $tableName = 'config';
protected $tableAlias = 'c';
/**
* @var array
*/
private $output_fields = ['hk_events_mode', 'hk_events_trigger', 'hk_events_service', 'hk_events_internal',
'hk_events_discovery', 'hk_events_autoreg', 'hk_services_mode', 'hk_services', 'hk_audit_mode', 'hk_audit',
'hk_sessions_mode', 'hk_sessions', 'hk_history_mode', 'hk_history_global', 'hk_history', 'hk_trends_mode',
'hk_trends_global', 'hk_trends', 'db_extension', 'compression_status', 'compress_older'
];
/**
* @param array $options
*
* @throws APIException if the input is invalid.
*
* @return array
*/
public function get(array $options): array {
$api_input_rules = ['type' => API_OBJECT, 'fields' => [
'output' => ['type' => API_OUTPUT, 'in' => implode(',', $this->output_fields), 'default' => API_OUTPUT_EXTEND]
]];
if (!CApiInputValidator::validate($api_input_rules, $options, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
if ($options['output'] === API_OUTPUT_EXTEND) {
$options['output'] = $this->output_fields;
}
$db_hk = [];
$result = DBselect($this->createSelectQuery($this->tableName(), $options));
while ($row = DBfetch($result)) {
$db_hk[] = $row;
}
$db_hk = $this->unsetExtraFields($db_hk, ['configid'], []);
return $db_hk[0];
}
/**
* @param array $hk
*
* @throws APIException if the input is invalid.
*
* @return array
*/
public function update(array $hk): array {
if (self::$userData['type'] != USER_TYPE_SUPER_ADMIN) {
self::exception(ZBX_API_ERROR_PERMISSIONS,
_s('No permissions to call "%1$s.%2$s".', 'housekeeping', __FUNCTION__)
);
}
$db_hk = $this->validateUpdate($hk);
$upd_config = DB::getUpdatedValues('config', $hk, $db_hk);
if ($upd_config) {
DB::update('config', [
'values' => $upd_config,
'where' => ['configid' => $db_hk['configid']]
]);
}
self::addAuditLog(CAudit::ACTION_UPDATE, CAudit::RESOURCE_HOUSEKEEPING,
[['configid' => $db_hk['configid']] + $hk], [$db_hk['configid'] => $db_hk]
);
return array_keys($hk);
}
/**
* @param array $hk
*
* @throws APIException if the input is invalid.
*
* @return array
*/
protected function validateUpdate(array $hk): array {
$api_input_rules = ['type' => API_OBJECT, 'flags' => API_NOT_EMPTY, 'fields' => [
'hk_events_mode' => ['type' => API_INT32, 'in' => '0,1'],
'hk_events_trigger' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
'hk_events_service' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
'hk_events_internal' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
'hk_events_discovery' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
'hk_events_autoreg' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
'hk_services_mode' => ['type' => API_INT32, 'in' => '0,1'],
'hk_services' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
'hk_audit_mode' => ['type' => API_INT32, 'in' => '0,1'],
'hk_audit' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
'hk_sessions_mode' => ['type' => API_INT32, 'in' => '0,1'],
'hk_sessions' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
'hk_history_mode' => ['type' => API_INT32, 'in' => '0,1'],
'hk_history_global' => ['type' => API_INT32, 'in' => '0,1'],
'hk_history' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => '0,'.implode(':', [SEC_PER_HOUR, 25 * SEC_PER_YEAR])],
'hk_trends_mode' => ['type' => API_INT32, 'in' => '0,1'],
'hk_trends_global' => ['type' => API_INT32, 'in' => '0,1'],
'hk_trends' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => '0,'.implode(':', [SEC_PER_DAY, 25 * SEC_PER_YEAR])],
'compression_status' => ['type' => API_INT32, 'in' => '0,1'],
'compress_older' => ['type' => API_TIME_UNIT, 'flags' => API_NOT_EMPTY, 'in' => implode(':', [7 * SEC_PER_DAY, 25 * SEC_PER_YEAR])]
]];
if (!CApiInputValidator::validate($api_input_rules, $hk, '/', $error)) {
self::exception(ZBX_API_ERROR_PARAMETERS, $error);
}
$output_fields = array_diff($this->output_fields, ['db_extension']);
$output_fields[] = 'configid';
return DB::select('config', ['output' => $output_fields])[0];
}
}
|