仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ jk.php 10.1 KB · 286 行 原始文件
Z zfhsh first commit 1 天前
1<?php
2/*
3 * 网页空间及数据库空间和流量使用情况监控文件
4 * 会自动判断是否超出大小以及暂停超出的站点
5 * 建议10分钟执行一次(不建议低于1分钟)
6 * 访问地址:http://搭建此系统的网站域名/jk.php?my=后台设置的api密钥&gn=需要监控的功能(wq为数据库空间和网页,fh为流量监控)
7 * 2022©梦奈
8 */
9include("./MPHX/common.php");
10include("./cf_up.php");
11if($mn_conf['xf']['qk'])
12{
13 exit('由于更新后必须进行一次系统修复,暂时无法使用这功能!');
14}
15if($_GET['my']!=$conf['api'])
16{
17 exit('密钥错误');
18}
19
20?>
21<?php
22include("./class.php");
23
24// ========== 辅助函数 ==========
25function jk_get_bt_config($ssbt, &$bt_cache) {
26 if (!isset($bt_cache[$ssbt])) return null;
27 return $bt_cache[$ssbt];
28}
29
30function jk_build_bt_url($cert) {
31 return ($cert['ptl']=='true'?'https':'http').'://'.$cert['btip'].':'.$cert['btdk'];
32}
33
34function jk_get_os_prefix($cert) {
35 global $conf;
36 return $cert['btos']=='1' ? $conf['hxi'].'/' : $conf['hxo'].'/';
37}
38
39function jk_is_expired($yhc, $date) {
40 if (strtotime($date)-strtotime($yhc['datae'])>0 && $yhc['datae']!='0000-00-00') return true;
41 if ($yhc['qk']==false) return true;
42 return false;
43}
44
45function jk_parse_size($raw) {
46 $suffix = substr($raw, -2);
47 $suffix_lower = strtolower($suffix);
48 if (in_array($suffix_lower, ['kb', 'mb'])) {
49 $val = str_ireplace($suffix,'',$raw);
50 return $suffix_lower === 'kb' ? $val : $val * 1000;
51 } elseif (in_array($suffix_lower, ['b '])) {
52 return str_ireplace($suffix,'',$raw) / 1000;
53 }
54 return '0';
55}
56
57// 检查是否需要暂停或恢复站点
58function jk_toggle_site($over_limit, $was_under, $api, $yhc, &$ztzj, &$ztyh_arr, $other_ok=true) {
59 global $DB;
60 if ($over_limit) {
61 if ($was_under) {
62 $api->ztweb($yhc['btid'], $yhc['sqldz']);
63 $api->ftpxg($yhc['ftpid'], $yhc['user'], '0');
64 // 暂停后回写 qk,保持数据库状态与宝塔实际状态一致
65 $DB->query_prepare("update MN_zj set qk='false' where user=?", [$yhc['user']]);
66 }
67 $ztzj++;
68 $ztyh_arr[] = $yhc['user'];
69 } else {
70 if (!$was_under && $other_ok) {
71 $api->qdweb($yhc['btid'], $yhc['sqldz']);
72 $api->ftpxg($yhc['ftpid'], $yhc['user'], '1');
73 // 恢复后回写 qk,保持数据库状态与宝塔实际状态一致
74 $DB->query_prepare("update MN_zj set qk='true' where user=?", [$yhc['user']]);
75 }
76 }
77}
78
79// 预先加载所有 BT 配置,按 btdh 索引
80$bt_cache = [];
81$bt_rows = $DB->get_all_prepare("SELECT * FROM MN_bt");
82if ($bt_rows) {
83 foreach ($bt_rows as $bt) {
84 $bt_cache[$bt['btdh']] = $bt;
85 }
86}
87
88if($_GET['gn']=='web'){ //WEB监控
89$all_zj=$DB->get_all_prepare("SELECT * FROM MN_zj");
90$ztzj='0';
91$ztyh_arr=[];
92foreach ($all_zj as $yhc)
93{
94if(jk_is_expired($yhc, $date)) continue;
95
96$cert=jk_get_bt_config($yhc['ssbt'], $bt_cache);
97if(!$cert) continue;
98$api = new bt_api(jk_build_bt_url($cert), $cert['btmy']);
99$r_data = $api->webkjjs(jk_get_os_prefix($cert).$yhc['sqldz']);
100$webkj=$r_data['size']/(1024*1000);
101
102$web_kjr=json_decode($yhc['hxa'],true);
103$r_js=$web_kjr;
104$r_js['dq']=$webkj;
105$DB->query_prepare("update `MN_zj` set `hxa` =? where `id`=?", [json_encode($r_js,256), $yhc['id']]);
106
107$k_qa=json_decode($yhc['llmax'],true);
108$k_qe=json_decode($yhc['hxb'],true);
109$other_ok = $k_qa['dq']<=$k_qa['max'] && $k_qe['dq']<=$k_qe['max'];
110jk_toggle_site($webkj>$web_kjr['max'], $web_kjr['dq']<$web_kjr['max'], $api, $yhc, $ztzj, $ztyh_arr, $other_ok);
111}
112echo '执行完成,有'.$ztzj.'个主机由于网页空间超过被暂停他们分别是:'.implode(',', $ztyh_arr);
113
114}elseif($_GET['gn']=='sql'){ //数据库空间监控
115
116$all_zj=$DB->get_all_prepare("SELECT * FROM MN_zj");
117$ztzj='0';
118$ztyh_arr=[];
119foreach ($all_zj as $yhc)
120{
121if(jk_is_expired($yhc, $date)) continue;
122
123$cert=jk_get_bt_config($yhc['ssbt'], $bt_cache);
124if(!$cert) continue;
125$api = new bt_api(jk_build_bt_url($cert), $cert['btmy']);
126$r_datb = $api->sqlkjhq($yhc['sqluser']);
127$sqlkj=jk_parse_size($r_datb['data_size']);
128$adft=$sqlkj/1024;
129
130$sql_kjr=json_decode($yhc['hxb'],true);
131$r_js=$sql_kjr;
132$r_js['dq']=$adft;
133$DB->query_prepare("update `MN_zj` set `hxb` =? where `id`=?", [json_encode($r_js,256), $yhc['id']]);
134
135$k_qa=json_decode($yhc['llmax'],true);
136$k_qe=json_decode($yhc['hxa'],true);
137$other_ok = $k_qa['dq']<=$k_qa['max'] && $k_qe['dq']<=$k_qe['max'];
138jk_toggle_site($adft>$sql_kjr['max'], $sql_kjr['dq']<$sql_kjr['max'], $api, $yhc, $ztzj, $ztyh_arr, $other_ok);
139}
140echo '执行完成,有'.$ztzj.'个主机由于数据库空间超过被暂停他们分别是:'.implode(',', $ztyh_arr);
141
142}elseif($_GET['gn']=='fh'){ //流量监控
143$all_zj=$DB->get_all_prepare("SELECT * FROM MN_zj");
144$ztzj='0';
145$ztyh_arr=[];
146foreach ($all_zj as $yhc)
147{
148if(jk_is_expired($yhc, $date)) continue;
149
150$cert=jk_get_bt_config($yhc['ssbt'], $bt_cache);
151if(!$cert) continue;
152$api = new bt_api(jk_build_bt_url($cert), $cert['btmy']);
153$r_js=json_decode($yhc['llmax'],true);
154$k_qa=json_decode($yhc['hxa'],true);
155$k_qe=json_decode($yhc['hxb'],true);
156$s_data=$api->getlog($yhc['sqldz']);
157if($s_data['status'] && $s_data['msg']!=''){
158$sfyr=explode(' - - ',$s_data['msg']);
159unset($sfyr[0]);
160$g_size=0;
161$latest_ts='';
162foreach($sfyr as $vfm){
163preg_match('/\[(.*?)\]/', $vfm, $tm);
164if(!($tm[1]??''))continue;
165if($tm[1]<=$r_js['statistics'])continue;
166$e_size=explode(' ',$vfm);
167if(!is_numeric($e_size[6]))continue;
168$g_size+=$e_size[6];
169if($tm[1]>$latest_ts)$latest_ts=$tm[1];
170}
171$r_jy=$r_js;
172$r_js['dq']+=$g_size;
173$r_js['statistics']=$latest_ts;
174$DB->query_prepare("update `MN_zj` set `llmax` =? where `id`=?", [json_encode($r_js,256), $yhc['id']]);
175
176$max_bytes=$r_js['max']*1024*1024*1024;
177$other_ok = $k_qa['dq']<=$k_qa['max'] && $k_qe['dq']<=$k_qe['max'];
178jk_toggle_site($r_js['dq']>=$max_bytes, $r_jy['dq']<$max_bytes, $api, $yhc, $ztzj, $ztyh_arr, $other_ok);
179}
180}
181echo '执行完成,有'.$ztzj.'个主机由于流量超过被暂停他们分别是:'.implode(',', $ztyh_arr);
182
183}elseif($_GET['gn']=='fhq'){ //清除统计的流量使用量(推荐每个月执行一次)
184$all_zj=$DB->get_all_prepare("SELECT * FROM MN_zj");
185foreach ($all_zj as $yhc)
186{
187if(jk_is_expired($yhc, $date)) continue;
188$r_js=json_decode($yhc['llmax'],true);
189if ($r_js['dq'] > 0) {
190 $month = date('Y-m');
191 if (!isset($r_js['history'])) $r_js['history'] = [];
192 $r_js['history'][$month] = $r_js['dq'];
193 $history = $r_js['history'];
194 ksort($history);
195 if (count($history) > 12) $history = array_slice($history, -12, null, true);
196 $r_js['history'] = $history;
197}
198$r_js['dq']=0;
199$DB->query_prepare("update `MN_zj` set `llmax` =? where `id`=?", [json_encode($r_js,256), $yhc['id']]);
200}
201echo '执行完成,所有主机的月使用流量清零完毕!';
202
203}
204elseif($_GET['gn'] == "ywjkdel")
205{
206 include_once("./mail.php");
207 $all_zj=$DB->get_all_prepare("SELECT * FROM MN_zj");
208 foreach ($all_zj as $yhc){
209 $cert=jk_get_bt_config($yhc['ssbt'], $bt_cache);
210 if(!$cert) continue;
211 $api = new bt_api(jk_build_bt_url($cert), $cert['btmy']);
212
213 if($conf['ymjkkg'] == "true" || $conf['mtyxfskg'])
214 {
215 $r_dataaa = $api->Getymlist($yhc['btid']);
216 $ymlistcount = count($r_dataaa);
217
218 if($ymlistcount == 1 && $r_dataaa[0]['name'] == $yhc["sqldz"])
219 {
220 echo $yhc['user'] . "没有绑定域名";
221 echo '<br>';
222 $create_time = new DateTime($yhc['data']);
223 $dq_time = new DateTime();
224 $xc_time = $dq_time->diff($create_time);
225 $xxx = $xc_time->days;
226 if($conf['ymjkkg']=="true")
227 {
228 if($yhc['mailuser'] != null || $yhc['mailuser'] != "")
229 {
230 if($xc_time->days > $conf['ymjktsyz'])
231 {
232
233 if($conf['optionzc'] == "stop")
234 {
235 $r_dataa = $api->stopjq($yhc['btid'],$yhc['sqldz']);
236 echo($yhc['user'].$r_dataa['msg']);
237 $message = "检测到".$yhc['user']. "的主机".$xxx."天内未使用超过了预定的天数已经暂停机器";
238 }
239 elseif($conf['optionzc'] == "del")
240 {
241 $r_dataa = $api->delsite($yhc['btid'],$yhc['sqldz']);
242 echo($yhc['sqldz'].$r_dataa['msg']);
243 $message = "检测到".$yhc['user']. "的主机".$xxx."天内未使用超过了预定的天数已经删除机器";
244 }
245 else
246 {
247 echo("数据库配置不对,没有读取到正常的配置");
248 }
249 if(sendEmail($yhc['mailuser'],"MN系统",$message))
250 {
251 echo "邮箱发送成功";
252 }
253 else{
254 echo "邮箱发送失败";
255 }
256
257 }
258 else
259 {
260 $message = "检测到".$yhc['user']. "的主机".$xxx."天内未使用,超过".$conf['ymjktsyz']."天将会暂停或删除";
261 sendEmail($yhc['mailuser'],"MN系统",$message);
262 }
263 }
264 else
265 {
266 echo "邮箱为空";
267 }
268
269 }
270 else
271 {
272 if($conf['mtyxfskg'] == "true")
273 {
274 $message = "检测到".$yhc['user']. "的主机天".$xxx."未使用请尽快使用";
275 sendEmail($yhc['mailuser'],"MN系统",$message);
276 }
277 }
278
279 }
280 }
281 }
282}
283else{
284exit('该功能不存在!');
285}
286?>