-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.php
More file actions
291 lines (259 loc) · 10.9 KB
/
class.php
File metadata and controls
291 lines (259 loc) · 10.9 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
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
<?php
/**
* @about SolusVM client api wrapper
* @author corbpie
* @version 1.2
*/
class solusClientApi
{
private const BASE_URL = '';//Base url eg "https://hostname/api/client/command.php"
private const KEY = '';//Api key
private const HASH = '';//Api hash
protected function doCall(string $method, array $params): bool|string
{
$curl = curl_init();
if ($method === 'POST') {
curl_setopt($curl, CURLOPT_POST, 1);
}
if ($params) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}
curl_setopt($curl, CURLOPT_URL, self::BASE_URL);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Expect:']);
$result = curl_exec($curl);
preg_match('~<status>([^{]*)</status>~i', $result, $er_match);
if (isset($er_match[1]) && $er_match[1] === 'error') {
throw new Exception("Error with API call. Check key, hash and url are correct");
}
curl_close($curl);
return $result;
}
public function getStatus(): bool
{
preg_match('~<vmstat>([^{]*)</vmstat>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info', 'status' => 'true')), $match);
return $match[1] === 'online';
}
public function ipCount(): int
{
preg_match('~<ipaddr>([^{]*)</ipaddr>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info', 'ipaddr' => 'true')), $match);
return count(explode(',', $match[1]));
}
public function ipMain(): string
{
preg_match('~<ipaddress>([^{]*)</ipaddress>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info')), $match);
return $match[1];
}
public function ip(int $number = 1): string
{
preg_match('~<ipaddr>([^{]*)</ipaddr>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info', 'ipaddr' => 'true')), $match);
$ip_arr = explode(',', $match[1]);
return $ip_arr[($number - 1)];
}
public function type(): string
{
preg_match('~<type>([^{]*)</type>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info')), $match);
return $match[1];
}
public function hostname(): string
{
preg_match('~<hostname>([^{]*)</hostname>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info')), $match);
return $match[1];
}
public function node(): string
{
preg_match('~<node>([^{]*)</node>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info')), $match);
return $match[1];
}
public function location(): string
{
preg_match('~<location>([^{]*)</location>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info', 'location' => 'true')), $match);
return $match[1];
}
public function reboot(): string
{
preg_match('~<status>([^{]*)</status>~i', $this->doCall('POST', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'reboot')), $match);
return $match[1];
}
public function boot(): string
{
preg_match('~<status>([^{]*)</status>~i', $this->doCall('POST', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'boot')), $match);
return $match[1];
}
public function shutdown(): string
{
preg_match('~<status>([^{]*)</status>~i', $this->doCall('POST', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'shutdown')), $match);
return $match[1];
}
public function memMain(): string
{
preg_match('~<mem>([^{]*)</mem>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info', 'mem' => 'true')), $match);
return $match[1];
}
public function totalMem(string $convert_to = 'MB', int $decimals = 2): string
{
$value = explode(',', $this->memMain());
if ($convert_to === 'MB') {
return number_format($value[0] / 1048576, $decimals);
} elseif ($convert_to === 'KB') {
return number_format($value[0] / 1024, $decimals);
} elseif ($convert_to === 'GB') {
return number_format($value[0] / 1073741824, $decimals);
} else {
return $value[0];
}
}
public function memAval(string $convert_to = 'MB', int $decimals = 2): string
{
$value = explode(',', $this->memMain());
if ($convert_to === 'MB') {
return number_format($value[1] / 1048576, $decimals);
} elseif ($convert_to === 'KB') {
return number_format($value[1] / 1024, $decimals);
} elseif ($convert_to === 'GB') {
return number_format($value[1] / 1073741824, $decimals);
} else {
return $value[0];
}
}
public function memUsedPercent(int $decimals = 2): float
{
$value = explode(',', $this->memMain());
return number_format($value[2], $decimals);
}
public function memFreePercent(int $decimals = 2): float
{
$value = explode(',', $this->memMain());
return number_format($value[3], $decimals);
}
public function hddMain(): string
{
preg_match('~<hdd>([^{]*)</hdd>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info', 'hdd' => 'true')), $match);
return $match[1];
}
public function totalHdd(string $convert_to = 'MB', int $decimals = 2): float
{
$value = explode(',', $this->hddMain());
if ($convert_to === 'MB') {
return number_format($value[0] / 1048576, $decimals);
} elseif ($convert_to === 'KB') {
return number_format($value[0] / 1024, $decimals);
} elseif ($convert_to === 'GB') {
return number_format($value[0] / 1073741824, $decimals);
} else {
return $value[0];
}
}
public function HddAval(string $convert_to = 'MB', int $decimals = 2): float
{
$value = explode(',', $this->hddMain());
if ($convert_to === 'MB') {
return number_format($value[1] / 1048576, $decimals);
} elseif ($convert_to === 'KB') {
return number_format($value[1] / 1024, $decimals);
} elseif ($convert_to === 'GB') {
return number_format($value[1] / 1073741824, $decimals);
} else {
return $value[0];
}
}
public function hddUsedPercent(int $decimals = 2): float
{
$value = explode(',', $this->hddMain());
return number_format($value[2], $decimals);
}
public function hddFreePercent(int $decimals = 2): float
{
$value = explode(',', $this->hddMain());
return number_format($value[3], $decimals);
}
public function bwMain(): string
{
preg_match('~<bw>([^{]*)</bw>~i', $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info', 'hdd' => 'true')), $match);
return $match[1];
}
public function totalBw(string $convert_to = 'MB', int $decimals = 2)
{
$value = explode(',', $this->bwMain());
if ($convert_to === 'MB') {
return number_format($value[0] / 1048576, $decimals);
} elseif ($convert_to === 'KB') {
return number_format($value[0] / 1024, $decimals);
} elseif ($convert_to === 'GB') {
return number_format($value[0] / 1073741824, $decimals);
} else {
return $value[0];
}
}
public function BwAval(string $convert_to = 'MB', int $decimals = 2): float
{
$value = explode(',', $this->bwMain());
if ($convert_to === 'MB') {
return number_format($value[1] / 1048576, $decimals);
} elseif ($convert_to === 'KB') {
return number_format($value[1] / 1024, $decimals);
} elseif ($convert_to === 'GB') {
return number_format($value[1] / 1073741824, $decimals);
} else {
return $value[0];
}
}
public function BwUsedPercent(int $decimals = 2): float
{
$value = explode(',', $this->bwMain());
return number_format($value[2], $decimals);
}
public function BwFreePercent(int $decimals = 2): float
{
$value = explode(',', $this->bwMain());
return number_format($value[3], $decimals);
}
public function allInfo(): array
{
$data = $this->doCall('GET', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'info', 'bw' => 'true', 'hdd' => 'true', 'mem' => 'true', 'ipaddr' => 'true', 'location' => 'true', 'status' => 'true'));
preg_match('~<vmstat>([^{]*)</vmstat>~i', $data, $status);
preg_match('~<hostname>([^{]*)</hostname>~i', $data, $hostname);
preg_match('~<location>([^{]*)</location>~i', $data, $location);
preg_match('~<type>([^{]*)</type>~i', $data, $type);
preg_match('~<node>([^{]*)</node>~i', $data, $node);
preg_match('~<ipaddr>([^{]*)</ipaddr>~i', $data, $ip_arr);
$ips = array();
foreach (explode(',', $ip_arr[1]) as $an_ip) {
$ips[] = $an_ip;
}
preg_match('~<bw>([^{]*)</bw>~i', $data, $bw);
$bw_main = explode(',', $bw[1]);
preg_match('~<mem>([^{]*)</mem>~i', $data, $mem);
$mem_main = explode(',', $mem[1]);
preg_match('~<hdd>([^{]*)</hdd>~i', $data, $hdd);
$hdd_main = explode(',', $hdd[1]);
return array(
'status' => $status[1],
'hostname' => $hostname[1],
'location' => $location[1],
'type' => $type[1],
'node' => $node[1],
'ip_count' => count(explode(',', $ip_arr[1])),
'ip_list' => $ips,
'mem_total' => number_format($mem_main[0] / 1048576, 1),
'mem_used' => number_format(($mem_main[0] - $mem_main[1]) / 1048576, 1),
'mem_used_percent' => (int)number_format($mem_main[2] / 1048576, 0),
'mem_data_type' => 'MB',
'bw_total' => number_format($bw_main[0] / 1073741824, 1),
'bw_used' => number_format($bw_main[1] / 1048576, 1),
'bw_used_percent' => (int)number_format($bw_main[3] / 1073741824, 0),
'bw_data_type' => 'GB',
'hdd_total' => number_format($hdd_main[0] / 1073741824, 1),
'hdd_used' => number_format(($hdd_main[0] - $hdd_main[1]) / 1048576, 1),
'hdd_used_percent' => (int)number_format($hdd_main[3] / 1073741824, 0),
'hdd_data_type' => 'GB',
'datetime' => date('Y-m-d H:i:s')
);
}
public function rdns(string $ip, string $rdns): string
{
return $this->doCall('POST', array('key' => self::KEY, 'hash' => self::HASH, 'action' => 'rdns', 'ip' => $ip, 'rdns' => $rdns));
}
}