1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file has functions dealing with server config. |
5
|
|
|
* |
6
|
|
|
* @package ElkArte Forum |
7
|
|
|
* @copyright ElkArte Forum contributors |
8
|
|
|
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file) |
9
|
|
|
* |
10
|
|
|
* This file contains code covered by: |
11
|
|
|
* copyright: 2011 Simple Machines (http://www.simplemachines.org) |
12
|
|
|
* |
13
|
|
|
* @version 2.0 dev |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Returns the current server load for nix systems |
19
|
|
|
* |
20
|
|
|
* - Used to enable / disable features based on current system overhead |
21
|
|
|
*/ |
22
|
|
|
function detectServerLoad() |
23
|
|
|
{ |
24
|
2 |
|
if (stristr(PHP_OS, 'win')) |
25
|
|
|
{ |
26
|
|
|
return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
2 |
|
$cores = detectServerCores(); |
30
|
|
|
|
31
|
|
|
// The internal function should always be available |
32
|
2 |
|
if (function_exists('sys_getloadavg') && sys_getloadavg() !== false) |
33
|
|
|
{ |
34
|
2 |
|
$sys_load = sys_getloadavg(); |
35
|
|
|
|
36
|
2 |
|
return $sys_load[0] / $cores; |
37
|
|
|
} |
38
|
|
|
// Maybe someone has a custom compile |
39
|
|
|
else |
40
|
|
|
{ |
41
|
|
|
$load_average = @file_get_contents('/proc/loadavg'); |
42
|
|
|
|
43
|
|
|
if (!empty($load_average) && preg_match('~^([^ ]+?) ([^ ]+?) ([^ ]+)~', $load_average, $matches) != 0) |
44
|
|
|
{ |
45
|
|
|
return (float) $matches[1] / $cores; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (($load_average = @`uptime`) !== null && preg_match('~load average[s]?: (\d+\.\d+), (\d+\.\d+), (\d+\.\d+)~i', $load_average, $matches) != 0) |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
return (float) $matches[1] / $cores; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Determines the number of cpu cores available |
59
|
|
|
* |
60
|
|
|
* - Used to normalize server load based on cores |
61
|
|
|
* |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
|
|
function detectServerCores() |
65
|
|
|
{ |
66
|
2 |
|
if (strpos(PHP_OS_FAMILY, 'Win') === 0) |
67
|
|
|
{ |
68
|
2 |
|
$cores = getenv("NUMBER_OF_PROCESSORS") + 0; |
69
|
|
|
|
70
|
2 |
|
return $cores ?? 1; |
71
|
2 |
|
} |
72
|
|
|
|
73
|
2 |
|
$cores = @file_get_contents('/proc/cpuinfo'); |
74
|
|
|
if (!empty($cores)) |
75
|
|
|
{ |
76
|
|
|
$cores = preg_match_all('~^physical id~m', $cores); |
77
|
|
|
if (!empty($cores)) |
78
|
|
|
{ |
79
|
|
|
return $cores; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return 1; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Return the total disk used and remaining free space. Returns array as |
88
|
|
|
* 10.34MB, 110MB (used, free) |
89
|
|
|
* |
90
|
|
|
* @return bool|array |
91
|
|
|
*/ |
92
|
|
|
function detectDiskUsage() |
93
|
|
|
{ |
94
|
|
|
global $boarddir; |
95
|
|
|
|
96
|
|
|
$diskTotal = disk_total_space($boarddir); |
97
|
|
|
$diskFree = disk_free_space($boarddir); |
98
|
|
|
|
99
|
|
|
if ($diskFree === false || $diskTotal === false) |
100
|
|
|
{ |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return [thousands_format($diskTotal - $diskFree), thousands_format($diskFree)]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Determine the number of days the server has been running since last reboot. *nix only |
109
|
|
|
* |
110
|
|
|
* @return bool|int |
111
|
|
|
*/ |
112
|
|
|
function detectUpTime() |
113
|
|
|
{ |
114
|
|
|
if (strpos(PHP_OS_FAMILY, 'Win') === 0) |
115
|
|
|
{ |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$upTime = trim(@file_get_contents('/proc/uptime')); |
|
|
|
|
120
|
|
|
if (!empty($upTime)) |
121
|
|
|
{ |
122
|
|
|
$upTime = (int) preg_replace('~\.\d+~', '', $upTime); |
123
|
|
|
|
124
|
|
|
return floor($upTime / 86400); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return 0; |
128
|
|
|
} |
129
|
|
|
|