Created
January 21, 2019 15:24
-
-
Save soudai/85e437749c578e2e206670f961ed7279 to your computer and use it in GitHub Desktop.
Mackerelのメトリック監視に合わせてメモリーを数値化する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* [plugin.checks.check_memory] | |
* command = "php /usr/local/bin/check_memory.php -c 90" | |
* max_check_attempts = 5 | |
* check_interval = 1 | |
* action = { command = "/etc/init.d/httpd graceful" } | |
* memo = "httpdによってmemoryが圧迫されたらhttpdをgracefulする" | |
**/ | |
$check_option = get_check_option_int(); | |
// $meminfo = get_test_data(); | |
$meminfo = `cat /proc/meminfo`; | |
if (empty($meminfo)) { | |
echo "Empty meminfo." . PHP_EOL ; | |
exit(2); | |
} | |
$percent_used = get_percent_used_memory($meminfo); | |
check($percent_used, $check_option); | |
function check($percent_used, $check_option) | |
{ | |
if ($percent_used >= $check_option) { | |
echo "Critical {$check_option}% over, Server {$percent_used}% used." . PHP_EOL; | |
exit(2); | |
} | |
} | |
function get_check_option_int() | |
{ | |
// -c hoge or --critical hoge | |
$check_option = getopt("c:", ["critical:"]); | |
if (empty($check_option)) { | |
echo "Empty option" . PHP_EOL; | |
exit(2); | |
} | |
$option = (int)$check_option["c"] ?? (int)$check_option["critical"]; | |
return $option; | |
} | |
function get_percent_used_memory($meminfo) | |
{ | |
$meminfo_array = explode("\n", $meminfo); | |
/** | |
* https://github.com/mackerelio/mackerel-agent/blob/master/metrics/linux/memory.go | |
* Metrics "used" is calculated here like (total - available) for ease. | |
*/ | |
$meminfo_total = $meminfo_array[0]; | |
$meminfo_available = $meminfo_array[2]; | |
$total = get_parse_value($meminfo_total); | |
$available = get_parse_value($meminfo_available); | |
$used = $total - $available; | |
$percent_used = $used / $total * 100; | |
return (int)$percent_used; | |
} | |
function get_parse_value($meminfo_value) | |
{ | |
$meminfo_value_parse_array = array_filter(explode(' ', $meminfo_value)); | |
//使ってない | |
$name = array_shift($meminfo_value_parse_array); | |
$value = array_shift($meminfo_value_parse_array); | |
return (int)$value; | |
} | |
/** | |
* cat /proc/meminfo | |
* @return string | |
*/ | |
function get_test_data() | |
{ | |
$input = <<<EOF | |
MemTotal: 3980400 kB | |
MemFree: 2161708 kB | |
MemAvailable: 2732460 kB | |
Buffers: 142300 kB | |
Cached: 367132 kB | |
SwapCached: 2128 kB | |
Active: 1061712 kB | |
Inactive: 253028 kB | |
Active(anon): 769028 kB | |
Inactive(anon): 58324 kB | |
Active(file): 292684 kB | |
Inactive(file): 194704 kB | |
Unevictable: 0 kB | |
Mlocked: 0 kB | |
SwapTotal: 524284 kB | |
SwapFree: 509948 kB | |
Dirty: 1452 kB | |
Writeback: 0 kB | |
AnonPages: 803420 kB | |
Mapped: 68868 kB | |
Shmem: 22044 kB | |
Slab: 414952 kB | |
SReclaimable: 372636 kB | |
SUnreclaim: 42316 kB | |
KernelStack: 9392 kB | |
PageTables: 46416 kB | |
NFS_Unstable: 0 kB | |
Bounce: 0 kB | |
WritebackTmp: 0 kB | |
CommitLimit: 2514484 kB | |
Committed_AS: 5428708 kB | |
VmallocTotal: 34359738367 kB | |
VmallocUsed: 0 kB | |
VmallocChunk: 0 kB | |
AnonHugePages: 204800 kB | |
ShmemHugePages: 0 kB | |
ShmemPmdMapped: 0 kB | |
HugePages_Total: 0 | |
HugePages_Free: 0 | |
HugePages_Rsvd: 0 | |
HugePages_Surp: 0 | |
Hugepagesize: 2048 kB | |
DirectMap4k: 67560 kB | |
DirectMap2M: 3016704 kB | |
DirectMap1G: 1048576 kB | |
EOF; | |
return $input; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment