Repository navigation
Expand file tree
/
Copy pathredfish_bmcs.php
More file actions
228 lines (187 loc) · 9.1 KB
/
Copy pathredfish_bmcs.php
File metadata and controls
228 lines (187 loc) · 9.1 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
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
<?php
/*
* SPDX-FileCopyrightText: 2026 Thomas Vincent <thomasvincent@gmail.com>
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of plugin_redfish, a plugin for Cacti.
* https://github.com/somethingwithproof/plugin_redfish
*
* 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.
*/
/*
* Redfish BMCs (Redfish Admin realm). Parses on PHP 8.0;
* redfish_page_start() refuses an unsupported runtime before any plugin
* class loads. No request here contacts a BMC.
*/
chdir('../../');
include('./include/auth.php');
include_once('./plugins/redfish/setup.php');
if (!redfish_page_start()) {
exit;
}
redfish_require_post('redfish_bmcs.php', array('save', 'delete', 'retry', 'import', 'provision', 'provision_confirm', 'templates'));
$redfish_bmcs = redfish_bmcs();
$redfish_scope = redfish_device_scope();
$redfish_page = new \Somethingwithproof\Redfish\BmcsPage($redfish_bmcs, redfish_credentials(), $redfish_scope);
$redfish_user = redfish_user_id();
/**
* The BMC named by the request, or null when it does not exist or sits on a
* Cacti device the user may not see. Every action on an existing BMC goes
* through here before it reads or writes anything else.
*/
function redfish_requested_bmc(\Somethingwithproof\Redfish\BmcRepository $bmcs, \Somethingwithproof\Redfish\DeviceScope $scope, int $id): ?\Somethingwithproof\Redfish\Bmc {
$bmc = $id > 0 ? $bmcs->find($id) : null;
if ($bmc !== null && !$scope->allowsBmc($bmc)) {
raise_message('redfish_denied', __('You do not have access to the Cacti device of this BMC.', 'redfish'), MESSAGE_LEVEL_ERROR);
return null;
}
return $bmc;
}
switch ((string) get_nfilter_request_var('action')) {
case 'save':
$redfish_id = (int) get_filter_request_var('id');
$redfish_input = array();
foreach (\Somethingwithproof\Redfish\BmcInput::FIELDS as $redfish_field) {
$redfish_input[$redfish_field] = (string) get_nfilter_request_var($redfish_field);
}
$redfish_existing = redfish_requested_bmc($redfish_bmcs, $redfish_scope, $redfish_id);
if ($redfish_id > 0 && $redfish_existing === null) {
header('Location: redfish_bmcs.php');
exit;
}
$redfish_parsed = \Somethingwithproof\Redfish\BmcInput::parse($redfish_input, $redfish_id);
$redfish_errors = $redfish_parsed->errors;
if ($redfish_parsed->bmc !== null) {
$redfish_errors = array_merge($redfish_errors, $redfish_page->databaseErrors($redfish_parsed->bmc));
}
if ($redfish_errors !== array() || $redfish_parsed->bmc === null) {
top_header();
$redfish_page->renderEdit($redfish_existing, $redfish_input, $redfish_errors);
bottom_footer();
exit;
}
$redfish_saved = $redfish_bmcs->save($redfish_parsed->bmc, $redfish_user);
raise_message('redfish_saved', $redfish_saved > 0 ? __('The BMC was saved.', 'redfish') : __('The BMC could not be saved.', 'redfish'), $redfish_saved > 0 ? MESSAGE_LEVEL_INFO : MESSAGE_LEVEL_ERROR);
header('Location: redfish_bmcs.php');
exit;
case 'delete':
$redfish_bmc = redfish_requested_bmc($redfish_bmcs, $redfish_scope, (int) get_filter_request_var('id'));
if ($redfish_bmc !== null && $redfish_bmcs->delete($redfish_bmc->id, $redfish_user)) {
raise_message('redfish_deleted', __('The BMC and its cached data were deleted. Its Cacti device was left in place.', 'redfish'), MESSAGE_LEVEL_INFO);
}
header('Location: redfish_bmcs.php');
exit;
case 'retry':
$redfish_bmc = redfish_requested_bmc($redfish_bmcs, $redfish_scope, (int) get_filter_request_var('id'));
if ($redfish_bmc !== null) {
(new \Somethingwithproof\Redfish\HealthRepository(false))->clearHolds($redfish_bmc->id);
raise_message('redfish_retry', __('Backoff and held failures were cleared; the collector reads the BMC on its next run.', 'redfish'), MESSAGE_LEVEL_INFO);
}
header('Location: redfish_bmcs.php');
exit;
case 'provision':
case 'provision_confirm':
$redfish_bmc = redfish_requested_bmc($redfish_bmcs, $redfish_scope, (int) get_filter_request_var('id'));
if ($redfish_bmc === null) {
header('Location: redfish_bmcs.php');
exit;
}
$redfish_provisioner = redfish_provisioner();
$redfish_objects = redfish_cacti_objects();
// Adding a device, data queries, or graphs is device management in
// Cacti, so it needs that realm as well as Redfish Admin (ADR 0015).
// A mapping to a device deleted since cannot take graphs.
$redfish_refusal = $redfish_provisioner->refusal();
if (!is_realm_allowed(3)) {
$redfish_refusal = __('Adding devices and graphs needs Cacti\'s device management permission.', 'redfish');
} elseif ($redfish_bmc->hostId > 0 && $redfish_objects->device($redfish_bmc->hostId) === null) {
$redfish_refusal = __('The device of this BMC was deleted. Choose another device, or none, on the BMC form first.', 'redfish');
}
if (get_nfilter_request_var('action') === 'provision' || $redfish_refusal !== null) {
top_header();
$redfish_page->renderProvision($redfish_bmc, $redfish_refusal === null ? $redfish_provisioner->plan($redfish_bmc, (new \Somethingwithproof\Redfish\NodeCacheRepository(false))->memberCounts($redfish_bmc->id)) : array(), $redfish_refusal);
bottom_footer();
exit;
}
if ($redfish_bmc->hostId > 0) {
$redfish_graphs = $redfish_provisioner->attach($redfish_bmc->id, $redfish_bmc->hostId, $redfish_user);
raise_message('redfish_provisioned', __('The data queries were indexed again and %d graphs were added.', $redfish_graphs, 'redfish'), MESSAGE_LEVEL_INFO);
} else {
$redfish_error = null;
$redfish_host = $redfish_provisioner->create($redfish_bmc, $redfish_user, $redfish_error);
raise_message('redfish_provisioned', $redfish_host > 0 ? __('The device was created with its Redfish graphs.', 'redfish') : (string) $redfish_error, $redfish_host > 0 ? MESSAGE_LEVEL_INFO : MESSAGE_LEVEL_ERROR);
}
header('Location: redfish_bmcs.php');
exit;
case 'templates':
// Import updates templates already in Cacti, so it needs Cacti's
// Import Templates permission as well as Redfish Admin.
if (!is_realm_allowed(17)) {
raise_message('redfish_templates', __('Installing the Redfish templates needs Cacti\'s Import Templates permission.', 'redfish'), MESSAGE_LEVEL_ERROR);
} else {
$redfish_installed = redfish_template_installer()->install();
raise_message('redfish_templates', $redfish_installed ? __('The Redfish templates are installed.', 'redfish') : __('The Redfish templates could not be installed. The health page shows why.', 'redfish'), $redfish_installed ? MESSAGE_LEVEL_INFO : MESSAGE_LEVEL_ERROR);
}
header('Location: redfish_bmcs.php');
exit;
case 'import':
$redfish_csv = '';
if (isset($_FILES['import_file']['tmp_name'], $_FILES['import_file']['size']) && is_string($_FILES['import_file']['tmp_name']) && is_uploaded_file($_FILES['import_file']['tmp_name']) && (int) $_FILES['import_file']['size'] <= \Somethingwithproof\Redfish\BmcImport::MAX_BYTES) {
$redfish_csv = (string) file_get_contents($_FILES['import_file']['tmp_name']);
}
$redfish_import = \Somethingwithproof\Redfish\BmcImport::parse(
$redfish_csv,
$redfish_bmcs->credentialIds($redfish_scope),
function (string $address, int $port) use ($redfish_bmcs, $redfish_scope): ?string {
return $redfish_bmcs->addressTakenBy($address, $port, 0, $redfish_scope);
},
function (string $name) use ($redfish_bmcs): bool {
return $redfish_bmcs->nameTaken($name, 0);
},
function (int $host) use ($redfish_scope): bool {
return $redfish_scope->allowsNew($host);
},
function (int $host) use ($redfish_bmcs): ?string {
return $redfish_bmcs->findByHost($host)?->name;
}
);
unset($redfish_csv);
if ($redfish_import->errors !== array() || !$redfish_bmcs->import($redfish_import->bmcs, $redfish_user)) {
top_header();
$redfish_page->renderImport($redfish_import->errors !== array() ? $redfish_import->errors : array(__('The import could not be saved; nothing was imported.', 'redfish')));
bottom_footer();
exit;
}
raise_message('redfish_imported', __('%d BMCs were imported.', count($redfish_import->bmcs), 'redfish'), MESSAGE_LEVEL_INFO);
header('Location: redfish_bmcs.php');
exit;
case 'export':
header('Content-Type: application/json; charset=utf-8');
header('Content-Disposition: attachment; filename="redfish-bmcs.json"');
header('X-Content-Type-Options: nosniff');
print $redfish_page->exportJson();
exit;
case 'edit':
$redfish_id = (int) get_filter_request_var('id');
$redfish_bmc = redfish_requested_bmc($redfish_bmcs, $redfish_scope, $redfish_id);
if ($redfish_id > 0 && $redfish_bmc === null) {
header('Location: redfish_bmcs.php');
exit;
}
top_header();
$redfish_page->renderEdit($redfish_bmc);
bottom_footer();
exit;
default:
top_header();
$redfish_page->renderList();
bottom_footer();
}