-
Notifications
You must be signed in to change notification settings - Fork 0
/
iperf3.inc
174 lines (163 loc) · 6.74 KB
/
iperf3.inc
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
<?php
/**
* Copyright (C) 2013-2024 OpenMediaVault Plugin Developers
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
class OMVRpcServiceIperf3 extends \OMV\Rpc\ServiceAbstract
{
public function getName()
{
return "iPerf3";
}
public function initialize()
{
$this->registerMethod("getSettings");
$this->registerMethod("setSettings");
$this->registerMethod('getJobList');
$this->registerMethod('getJob');
$this->registerMethod('setJob');
$this->registerMethod('deleteJob');
$this->registerMethod('doJob');
$this->registerMethod('getLog');
}
public function getSettings($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ["role" => OMV_ROLE_ADMINISTRATOR]);
// Get the configuration object.
$db = \OMV\Config\Database::getInstance();
$object = $db->get("conf.service.iperf3");
// Remove useless properties from the object.
$object->remove("jobs");
return $object->getAssoc();
}
public function setSettings($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ["role" => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.iperf3.setsettings");
// Get the existing configuration object.
$db = \OMV\Config\Database::getInstance();
$object = $db->get("conf.service.iperf3");
$object->setAssoc($params);
$db->set($object);
// Remove useless properties from the object.
$object->remove("jobs");
// Return the configuration object.
return $object->getAssoc();
}
public function getJobList($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ['role' => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.common.getlist");
// Get configuration data.
$db = \OMV\Config\Database::getInstance();
$objects = $db->getAssoc("conf.service.iperf3.job");
// Filter the result.
return $this->applyFilter($objects, $params['start'], $params['limit'],
$params['sortfield'], $params['sortdir']);
}
function getJob($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ['role' => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.common.objectuuid");
// Get the configuration object.
$db = \OMV\Config\Database::getInstance();
$object = $db->get("conf.service.iperf3.job", $params['uuid']);
return $object->getAssoc();
}
function setJob($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ['role' => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.iperf3.setjob");
// Prepare the configuration object.
$object = new \OMV\Config\ConfigObject("conf.service.iperf3.job");
$db = \OMV\Config\Database::getInstance();
$object->setAssoc($params);
// Set the configuration object.
$db->set($object);
// Return the configuration object.
return $object->getAssoc();
}
public function deleteJob($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ['role' => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.common.objectuuid");
// Delete the configuration object.
$db = \OMV\Config\Database::getInstance();
$object = $db->get("conf.service.iperf3.job", $params['uuid']);
$db->delete($object);
// Return the deleted configuration object.
return $object->getAssoc();
}
public function doJob($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ['role' => OMV_ROLE_ADMINISTRATOR]);
// Validate the parameters of the RPC service method.
$this->validateMethodParams($params, "rpc.common.objectuuid");
// Get the configuration object.
$db = \OMV\Config\Database::getInstance();
$object = $db->get("conf.service.iperf3.job", $params['uuid']);
return $this->execBgProc(function($bgStatusFilename, $bgOutputFilename)
use ($object) {
$output = [];
$cmdArgs = [];
$cmdArgs[] = 'iperf3';
$cmdArgs[] = sprintf("-c '%s'", $object->get('server'));
if ($object->get('reverse')) {
$cmdArgs[] = '-R';
}
if ($object->get('verbose')) {
$cmdArgs[] = '--verbose';
}
if ($object->get('json')) {
$cmdArgs[] = '--json';
}
if ($object->get('log')) {
$cmdArgs[] = '| tee -a /var/log/omv-iperf3-jobs.log';
}
$cmd = new \OMV\System\Process($cmdArgs);
$cmdLine = $cmd->getCommandLine();
if (0 !== $this->exec($cmdLine, $output, $bgOutputFilename)) {
throw new \OMV\ExecException($cmdLine, $output);
}
return $output;
}
);
}
function getLog($params, $context)
{
// Validate the RPC caller context.
$this->validateMethodContext($context, ["role" => OMV_ROLE_ADMINISTRATOR]);
// Create a temporary file which will contain the content of the requested log file.
$log = '/var/log/omv-iperf3-jobs.log';
// Return values required by generic download RPC implementation.
return [
'filename' => basename($log),
'filepath' => $log,
'unlink' => false
];
}
}