ブログ - macosのメモリ使用量をSNMPで取得する
macosのメモリ使用量をSNMPで取得できないので,vmstatの値をsnmpに挿入するスクリプトを動かす.
今回使ったのはmacOS High Sierra.
emeidi/macosx-memory-snmp
https://github.com/emeidi/macosx-memory-snmp
ディレクトリを作成してcloneでダウンロード.
入手したファイルを確認.
SNMPDの設定ファイルのサンプルを確認.
この/path/to/memory.pyのパス部分を,実際に設置したファイルパスに設定して,/etc/snmp/snmpd.confに追加する.
snmpdを再起動する.
snmpwalkで値を取得する.
値部分は次の様に取得.
ちなみに,vm_statコマンドを実行するとこの様な感じ.
数値はページ数.1ページが4KByte.
物理メモリは次の方法で確認できる.
16GB搭載.
この,4つの指標を足す.
物理メモリが17179869184byteで,vm_statsの合計値が15374807040byteなので,差分が1805062144(1.8GB)となる.
今回使ったのはmacOS High Sierra.
emeidi/macosx-memory-snmp
https://github.com/emeidi/macosx-memory-snmp
ディレクトリを作成してcloneでダウンロード.
$ mkdir mac_memory🆑
[mars:server 16:29:55 ~/bin ]
$ cd mac_memory🆑
[mars:server 16:30:01 ~/bin/mac_memory ]
$ git clone https://github.com/emeidi/macosx-memory-snmp🆑
Cloning into 'macosx-memory-snmp'...
remote: Enumerating objects: 45, done.
remote: Total 45 (delta 0), reused 0 (delta 0), pack-reused 45
Unpacking objects: 100% (45/45), done.
[mars:server 16:30:19 ~/bin/mac_memory ]
$
$ cd macosx-memory-snmp/🆑
[mars:server 16:30:33 ~/bin/mac_memory/macosx-memory-snmp ]
$ ls -la
合計 76
drwxr-xr-x 11 server staff 352 1 18 16:30 .
drwxr-xr-x 3 server staff 96 1 18 16:30 ..
drwxr-xr-x 12 server staff 384 1 18 16:30 .git
-rw-r--r-- 1 server staff 15521 1 18 16:30 cacti_graph_template_mac_os_x_-_memory_usage_-_graph_template.xml
-rw-r--r-- 1 server staff 15441 1 18 16:30 check_snmp_extend.py
-rw-r--r-- 1 server staff 883 1 18 16:30 mac_memory.sh
-rw-r--r-- 1 server staff 21571 1 18 16:30 mac_memory_usage.png
-rwxr-xr-x 1 server staff 2492 1 18 16:30 memory.py
-rw-r--r-- 1 server staff 3842 1 18 16:30 readme.md
-rwxr-xr-x 1 server staff 381 1 18 16:30 restart-snmpd.sh
-rw-r--r-- 1 server staff 519 1 18 16:30 snmpd.partial.conf
[mars:server 16:30:35 ~/bin/mac_memory/macosx-memory-snmp ]
$
$ cat snmpd.partial.conf🆑
# Please add your local configuration settings here
# ...
# This is where the magic happens:
# Adjust /path/to/memory.py to the location of the memory.py script, e.g. /usr/local/bin/memory.py
exec memory_free /usr/bin/python /path/to/memory.py free
exec memory_wired /usr/bin/python /path/to/memory.py wired
exec memory_active /usr/bin/python /path/to/memory.py active
exec memory_inactive /usr/bin/python /path/to/memory.py inactive
exec memory_used /usr/bin/python /path/to/memory.py used
[mars:server 16:32:50 ~/bin/mac_memory/macosx-memory-snmp ]
$
snmpdを再起動する.
$ cat restart-snmpd.sh🆑
#!/bin/sh
# Purpose: Restarts snmpd on Mac OS X
# Author: Mario Aeby, info@eMeidi.com
# Version: 0.1
# Home: github.com/emeidi/macosx-memory-snmp
echo "Stopping SNMP daemon ..."
sudo launchctl unload /System/Library/LaunchDaemons/org.net-snmp.snmpd.plist
echo "Starting SNMP daemon ..."
sudo launchctl load -w /System/Library/LaunchDaemons/org.net-snmp.snmpd.plist
exit 0
[mars:server 16:35:15 ~/bin/mac_memory/macosx-memory-snmp ]
$ bash restart-snmpd.sh🆑
Stopping SNMP daemon ...
Starting SNMP daemon ...
[mars:server 16:35:32 ~/bin/mac_memory/macosx-memory-snmp ]
$ snmpwalk -v 1 -c public localhost .1.3.6.1.4.1.2021.8.1🆑
UCD-SNMP-MIB::extIndex.1 = INTEGER: 1
UCD-SNMP-MIB::extIndex.2 = INTEGER: 2
UCD-SNMP-MIB::extIndex.3 = INTEGER: 3
UCD-SNMP-MIB::extIndex.4 = INTEGER: 4
UCD-SNMP-MIB::extIndex.5 = INTEGER: 5
UCD-SNMP-MIB::extIndex.6 = INTEGER: 6
UCD-SNMP-MIB::extIndex.7 = INTEGER: 7
UCD-SNMP-MIB::extIndex.8 = INTEGER: 8
UCD-SNMP-MIB::extNames.1 = STRING: echotest
UCD-SNMP-MIB::extNames.2 = STRING: web_status
UCD-SNMP-MIB::extNames.3 = STRING: netboot
UCD-SNMP-MIB::extNames.4 = STRING: memory_free 🈁
UCD-SNMP-MIB::extNames.5 = STRING: memory_wired 🈁
UCD-SNMP-MIB::extNames.6 = STRING: memory_active 🈁
UCD-SNMP-MIB::extNames.7 = STRING: memory_inactive 🈁
UCD-SNMP-MIB::extNames.8 = STRING: memory_used 🈁
UCD-SNMP-MIB::extCommand.1 = STRING: /bin/echo
UCD-SNMP-MIB::extCommand.2 = STRING: /Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin
UCD-SNMP-MIB::extCommand.3 = STRING: /Applications/Server.app/Contents/ServerRoot/usr/sbin/serveradmin
UCD-SNMP-MIB::extCommand.4 = STRING: /usr/bin/python
UCD-SNMP-MIB::extCommand.5 = STRING: /usr/bin/python
UCD-SNMP-MIB::extCommand.6 = STRING: /usr/bin/python
UCD-SNMP-MIB::extCommand.7 = STRING: /usr/bin/python
UCD-SNMP-MIB::extCommand.8 = STRING: /usr/bin/python
UCD-SNMP-MIB::extResult.1 = INTEGER: 0
UCD-SNMP-MIB::extResult.2 = INTEGER: 0
UCD-SNMP-MIB::extResult.3 = INTEGER: 0
UCD-SNMP-MIB::extResult.4 = INTEGER: 0
UCD-SNMP-MIB::extResult.5 = INTEGER: 0
UCD-SNMP-MIB::extResult.6 = INTEGER: 0
UCD-SNMP-MIB::extResult.7 = INTEGER: 0
UCD-SNMP-MIB::extResult.8 = INTEGER: 0
UCD-SNMP-MIB::extOutput.1 = STRING: hello world
UCD-SNMP-MIB::extOutput.2 = STRING: web:state = \"RUNNING\"
UCD-SNMP-MIB::extOutput.3 = STRING: netboot:state = \"STOPPED\"
UCD-SNMP-MIB::extOutput.4 = STRING: 6194262016 🈁
UCD-SNMP-MIB::extOutput.5 = STRING: 1810624512 🈁
UCD-SNMP-MIB::extOutput.6 = STRING: 5007630336 🈁
UCD-SNMP-MIB::extOutput.7 = STRING: 2061684736 🈁
UCD-SNMP-MIB::extOutput.8 = STRING: 8879984640 🈁
UCD-SNMP-MIB::extErrFix.1 = INTEGER: noError(0)
UCD-SNMP-MIB::extErrFix.2 = INTEGER: noError(0)
UCD-SNMP-MIB::extErrFix.3 = INTEGER: noError(0)
UCD-SNMP-MIB::extErrFix.4 = INTEGER: noError(0)
UCD-SNMP-MIB::extErrFix.5 = INTEGER: noError(0)
UCD-SNMP-MIB::extErrFix.6 = INTEGER: noError(0)
UCD-SNMP-MIB::extErrFix.7 = INTEGER: noError(0)
UCD-SNMP-MIB::extErrFix.8 = INTEGER: noError(0)
UCD-SNMP-MIB::extErrFixCmd.1 = STRING:
UCD-SNMP-MIB::extErrFixCmd.2 = STRING:
UCD-SNMP-MIB::extErrFixCmd.3 = STRING:
UCD-SNMP-MIB::extErrFixCmd.4 = STRING:
UCD-SNMP-MIB::extErrFixCmd.5 = STRING:
UCD-SNMP-MIB::extErrFixCmd.6 = STRING:
UCD-SNMP-MIB::extErrFixCmd.7 = STRING:
UCD-SNMP-MIB::extErrFixCmd.8 = STRING:
[mars:server 16:40:15 ~/bin/mac_memory/macosx-memory-snmp ]
$
$ snmpwalk -v 1 -c public localhost UCD-SNMP-MIB::extOutput.4🆑
UCD-SNMP-MIB::extOutput.4 = STRING: 6169989120
[mars:server 16:52:55 ~/bin/mac_memory/macosx-memory-snmp ]
$
$ vm_stat🆑
Mach Virtual Memory Statistics: (page size of 4096 bytes)
Pages free: 1504376.
Pages active: 1228557.
Pages inactive: 508472.
Pages speculative: 512210.
Pages throttled: 0.
Pages wired down: 440415.
Pages purgeable: 20507.
"Translation faults": 220111260.
Pages copy-on-write: 21606306.
Pages zero filled: 75205883.
Pages reactivated: 19350.
Pages purged: 1347.
File-backed pages: 1145758.
Anonymous pages: 1103481.
Pages stored in compressor: 0.
Pages occupied by compressor: 0.
Decompressions: 0.
Compressions: 0.
Pageins: 1614277.
Pageouts: 0.
Swapins: 0.
Swapouts: 0.
[mars:server 16:54:36 ~/bin/mac_memory/macosx-memory-snmp ]
$
物理メモリは次の方法で確認できる.
$ sysctl hw.memsize🆑
hw.memsize: 17179869184🈁
$
この,4つの指標を足す.
Pages free: 1504376.
Pages active: 1228557.
Pages inactive: 508472.
Pages speculative: 512210.
(1504376+1228557+508472+512210)*4096=15374807040
物理メモリが17179869184byteで,vm_statsの合計値が15374807040byteなので,差分が1805062144(1.8GB)となる.