-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path1.php
214 lines (152 loc) · 4.47 KB
/
1.php
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
<?php
function humanFileSize($size, $unit="") {
if( (!$unit && $size >= 1<<30) || $unit == "GB")
return number_format($size/(1<<30),2)." GB";
if( (!$unit && $size >= 1<<20) || $unit == "MB")
return number_format($size/(1<<20),2)." MB";
if( (!$unit && $size >= 1<<10) || $unit == "KB")
return number_format($size/(1<<10),2)." KB";
return number_format($size)." bytes";
}
function post($action) {
// Prepare the POST data
// VirMach KVM 启用API授权
// API Key: 54<<你懂得>>TS
// API Hash: 8d<<你懂得>>d8a
// <configure> :
//这里填写api的key
$postfields["key"] = "54<<你懂得>>TS";
//这里填写api的hash
$postfields["hash"] = "8d<<你懂得>>d8a";
$masterurl = "https://solusvm.virmach.com/";
// </configure>
$postfields["action"] = $action;
$postfields["status"] = "true";
if($action == "info") {
$postfields["hdd"] = "true";
$postfields["mem"] = "true";
$postfields["bw"] = "true";
}
// Prepare the POST request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $masterurl."api/client/command.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Expect: "));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Execute the request
$data = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($code != 200) {
$return['error'] = 1;
if($code == 405) {
$return['message'] = "Incorrect API credentials.";
return $return;
}
$return['message'] = "Invalid status code.";
return $return;
}
// Close the Curl handle
curl_close($ch);
if(!$data) {
$return['error'] = 1;
$return['message'] = "Error connecting to API.";
return $return;
}
// Extract the data
preg_match_all('/<(.*?)>([^<]+)<\/\\1>/i', $data, $match);
$result = array();
foreach ($match[1] as $x => $y) {
$result[$y] = $match[2][$x];
}
if($result['status'] == "error") {
$return['error'] = 1;
$return['message'] = $result['statusmsg'];
return $return;
}
$return = $result;
$return['error'] = 0;
return $return;
}
if(isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = "info";
}
switch($action)
{
case 'info':
$result = post("info");
if($result['error'] == 0) {
$return = $result;
$return['hdd'] = explode(",", $return['hdd']);
$return['mem'] = explode(",", $return['mem']);
$return['bw'] = explode(",", $return['bw']);
} else {
$return = $result;
}
break;
case 'reboot':
$result = post("reboot");
if($result['error'] == 0) {
$return = $result;
$return['message'] = "Server rebooting now.";
} else {
$return = $result;
}
break;
// case 'boot':
// $result = post("boot");
// if($result['error'] == 0) {
// $return = $result;
// $return['message'] = "Server booting now.";
// } else {
// $return = $result;
// }
// break;
// case 'shutdown':
// $result = post("shutdown");
// if($result['error'] == 0) {
// $return = $result;
// $return['message'] = "Server shutting down now.";
// } else {
// $return = $result;
// }
// break;
default:
$return['error'] = 1;
$return['message'] = "Invalid action specified.";
}
?>
<!DOCTYPE html>
<html id="spLianghui">
<head>
<title>服务器状态</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div style="width: 90%; max-width: 800px; margin: 0 auto;">
<h1>服务器状态</h1>
<hr/>
<?php if($return['error'] == 1) { ?>
<div class="alert alert-danger" role="alert"><?php echo $return['message']; ?></div>
<a class="btn btn-default btn-block" href="?action=info" role="button">返回到监控页面</a>
<?php } else { ?>
<?php if($action == "info") { ?>
<h3>主机名</h3>
<?php echo $return['hostname']; ?>
<h3>状态: <?php echo $return['vmstat']; ?> </h3>
<a class="btn btn-default btn-block" href="?action=reboot" role="button">重启</a>
<h3>带宽 <small> 已用 <?php echo humanFileSize($return['bw'][1]); ?> , 总量 <?php echo humanFileSize($return['bw'][0]); ?></small></h3>
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="<?php echo $return['bw'][3]; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php echo $return['bw'][3]; ?>%;">
<?php echo $return['bw'][3]; ?>%
</div>
</div>
<?php } ?>
<?php } ?>
</div>
</body>
</html>