1313 */
1414class Servers
1515{
16+ use Concerns \Throttling;
17+
1618 /**
1719 * The events to listen for.
1820 *
@@ -35,51 +37,49 @@ public function __construct(
3537 */
3638 public function record (SharedBeat $ event ): void
3739 {
38- if ($ event ->time ->second % 15 !== 0 ) {
39- return ;
40- }
41-
42- $ server = $ this ->config ->get ('pulse.recorders. ' .self ::class.'.server_name ' );
43- $ slug = Str::slug ($ server );
40+ $ this ->throttle (15 , $ event , function ($ event ) {
41+ $ server = $ this ->config ->get ('pulse.recorders. ' .self ::class.'.server_name ' );
42+ $ slug = Str::slug ($ server );
4443
45- $ memoryTotal = match (PHP_OS_FAMILY ) {
46- 'Darwin ' => intval (`sysctl hw.memsize | grep -Eo '[0-9]+' ` / 1024 / 1024 ),
47- 'Linux ' => intval (`cat /proc/meminfo | grep MemTotal | grep -E -o '[0-9]+' ` / 1024 ),
48- 'Windows ' => intval (((int ) trim (`wmic ComputerSystem get TotalPhysicalMemory | more +1 `)) / 1024 / 1024 ),
49- 'BSD ' => intval (`sysctl hw.physmem | grep -Eo '[0-9]+' ` / 1024 / 1024 ),
50- default => throw new RuntimeException ('The pulse:check command does not currently support ' .PHP_OS_FAMILY ),
51- };
44+ $ memoryTotal = match (PHP_OS_FAMILY ) {
45+ 'Darwin ' => intval (`sysctl hw.memsize | grep -Eo '[0-9]+' ` / 1024 / 1024 ),
46+ 'Linux ' => intval (`cat /proc/meminfo | grep MemTotal | grep -E -o '[0-9]+' ` / 1024 ),
47+ 'Windows ' => intval (((int ) trim (`wmic ComputerSystem get TotalPhysicalMemory | more +1 `)) / 1024 / 1024 ),
48+ 'BSD ' => intval (`sysctl hw.physmem | grep -Eo '[0-9]+' ` / 1024 / 1024 ),
49+ default => throw new RuntimeException ('The pulse:check command does not currently support ' .PHP_OS_FAMILY ),
50+ };
5251
53- $ memoryUsed = match (PHP_OS_FAMILY ) {
54- 'Darwin ' => $ memoryTotal - intval (intval (`vm_stat | grep 'Pages free' | grep -Eo '[0-9]+' `) * intval (`pagesize `) / 1024 / 1024 ), // MB
55- 'Linux ' => $ memoryTotal - intval (`cat /proc/meminfo | grep MemAvailable | grep -E -o '[0-9]+' ` / 1024 ), // MB
56- 'Windows ' => $ memoryTotal - intval (((int ) trim (`wmic OS get FreePhysicalMemory | more +1 `)) / 1024 ), // MB
57- 'BSD ' => intval (intval (`( sysctl vm.stats.vm.v_cache_count | grep -Eo '[0-9]+' ; sysctl vm.stats.vm.v_inactive_count | grep -Eo '[0-9]+' ; sysctl vm.stats.vm.v_active_count | grep -Eo '[0-9]+' ) | awk '{s+=$1} END {print s}' `) * intval (`pagesize `) / 1024 / 1024 ), // MB
58- default => throw new RuntimeException ('The pulse:check command does not currently support ' .PHP_OS_FAMILY ),
59- };
52+ $ memoryUsed = match (PHP_OS_FAMILY ) {
53+ 'Darwin ' => $ memoryTotal - intval (intval (`vm_stat | grep 'Pages free' | grep -Eo '[0-9]+' `) * intval (`pagesize `) / 1024 / 1024 ), // MB
54+ 'Linux ' => $ memoryTotal - intval (`cat /proc/meminfo | grep MemAvailable | grep -E -o '[0-9]+' ` / 1024 ), // MB
55+ 'Windows ' => $ memoryTotal - intval (((int ) trim (`wmic OS get FreePhysicalMemory | more +1 `)) / 1024 ), // MB
56+ 'BSD ' => intval (intval (`( sysctl vm.stats.vm.v_cache_count | grep -Eo '[0-9]+' ; sysctl vm.stats.vm.v_inactive_count | grep -Eo '[0-9]+' ; sysctl vm.stats.vm.v_active_count | grep -Eo '[0-9]+' ) | awk '{s+=$1} END {print s}' `) * intval (`pagesize `) / 1024 / 1024 ), // MB
57+ default => throw new RuntimeException ('The pulse:check command does not currently support ' .PHP_OS_FAMILY ),
58+ };
6059
61- $ cpu = match (PHP_OS_FAMILY ) {
62- 'Darwin ' => (int ) `top -l 1 | grep -E "^CPU" | tail -1 | awk '{ print $3 + $5 }' `,
63- 'Linux ' => (int ) `top -bn1 | grep -E '^(%Cpu|CPU)' | awk '{ print $2 + $4 }' `,
64- 'Windows ' => (int ) trim (`wmic cpu get loadpercentage | more +1 `),
65- 'BSD ' => (int ) `top -b -d 2| grep 'CPU: ' | tail -1 | awk '{print$10}' | grep -Eo '[0-9]+\.[0-9]+' | awk '{ print 100 - $1 }' `,
66- default => throw new RuntimeException ('The pulse:check command does not currently support ' .PHP_OS_FAMILY ),
67- };
60+ $ cpu = match (PHP_OS_FAMILY ) {
61+ 'Darwin ' => (int ) `top -l 1 | grep -E "^CPU" | tail -1 | awk '{ print $3 + $5 }' `,
62+ 'Linux ' => (int ) `top -bn1 | grep -E '^(%Cpu|CPU)' | awk '{ print $2 + $4 }' `,
63+ 'Windows ' => (int ) trim (`wmic cpu get loadpercentage | more +1 `),
64+ 'BSD ' => (int ) `top -b -d 2| grep 'CPU: ' | tail -1 | awk '{print$10}' | grep -Eo '[0-9]+\.[0-9]+' | awk '{ print 100 - $1 }' `,
65+ default => throw new RuntimeException ('The pulse:check command does not currently support ' .PHP_OS_FAMILY ),
66+ };
6867
69- $ this ->pulse ->record ('cpu ' , $ slug , $ cpu , $ event ->time )->avg ()->onlyBuckets ();
70- $ this ->pulse ->record ('memory ' , $ slug , $ memoryUsed , $ event ->time )->avg ()->onlyBuckets ();
71- $ this ->pulse ->set ('system ' , $ slug , json_encode ([
72- 'name ' => $ server ,
73- 'cpu ' => $ cpu ,
74- 'memory_used ' => $ memoryUsed ,
75- 'memory_total ' => $ memoryTotal ,
76- 'storage ' => collect ($ this ->config ->get ('pulse.recorders. ' .self ::class.'.directories ' )) // @phpstan-ignore argument.templateType argument.templateType
77- ->map (fn (string $ directory ) => [
78- 'directory ' => $ directory ,
79- 'total ' => $ total = intval (round (disk_total_space ($ directory ) / 1024 / 1024 )), // MB
80- 'used ' => intval (round ($ total - (disk_free_space ($ directory ) / 1024 / 1024 ))), // MB
81- ])
82- ->all (),
83- ], flags: JSON_THROW_ON_ERROR ), $ event ->time );
68+ $ this ->pulse ->record ('cpu ' , $ slug , $ cpu , $ event ->time )->avg ()->onlyBuckets ();
69+ $ this ->pulse ->record ('memory ' , $ slug , $ memoryUsed , $ event ->time )->avg ()->onlyBuckets ();
70+ $ this ->pulse ->set ('system ' , $ slug , json_encode ([
71+ 'name ' => $ server ,
72+ 'cpu ' => $ cpu ,
73+ 'memory_used ' => $ memoryUsed ,
74+ 'memory_total ' => $ memoryTotal ,
75+ 'storage ' => collect ($ this ->config ->get ('pulse.recorders. ' .self ::class.'.directories ' )) // @phpstan-ignore argument.templateType argument.templateType
76+ ->map (fn (string $ directory ) => [
77+ 'directory ' => $ directory ,
78+ 'total ' => $ total = intval (round (disk_total_space ($ directory ) / 1024 / 1024 )), // MB
79+ 'used ' => intval (round ($ total - (disk_free_space ($ directory ) / 1024 / 1024 ))), // MB
80+ ])
81+ ->all (),
82+ ], flags: JSON_THROW_ON_ERROR ), $ event ->time );
83+ });
8484 }
8585}
0 commit comments