VMware¶
VMware はクラスタ状態、ホスト状態、VM 状態などのメトリクスを表示します
設定¶
事前準備¶
- VMware にアクセスできる社内ネットワーク上のマシンに
Datakitと Python 3+ 環境が必要です - 以下の依存ライブラリが必要です:
- requests
- pyvmomi == 7.0
- openssl == 1.1.1w
注意:異なるパッケージバージョンを使用すると、パッケージ競合の問題が発生します
導入手順¶
-
Datakitのpython.dコレクタを有効にし、DataKit のインストールディレクトリ内の conf.d/pythond ディレクトリに移動して、pythond.conf.sample をコピーし、pythond.conf に名前を変更します。例は以下のとおりです。# {"version": "1.16.0", "desc": "do NOT edit this line"} [[inputs.pythond]] # Python input name name = 'some-python-inputs' # required # Python を実行するためのシステム環境 #envs = ['LD_LIBRARY_PATH=/path/to/lib:$LD_LIBRARY_PATH',] # Python のパス(抽象化された Python パスを推奨) cmd = "/root/anaconda3/envs/py3.9/bin/python" # required. python3 is recommended. # Python スクリプトの相対パス dirs = ["vm"]注意:ここで
cmdは自分の Python 環境のパスに変更してください。Python スクリプトの実行に使用し、dirsはスクリプトの配置ディレクトリです -
datakit/python.dディレクトリ内に "Python パッケージ名" で名前を付けたディレクトリを作成し、その中に Python スクリプト(.py) を作成します。パッケージ名をvmとした場合、パス構造は以下のとおりです。ここではdemo.pyが Python スクリプトです。Python スクリプトのファイル名は任意に指定できます。 -
vm.pyスクリプトを作成し、以下のスクリプト例を記入したうえで、vcenter_host、vcenter_port、vcenter_user、vcenter_passwordに、それぞれ VMware を収集する対象のホスト、ポート、ユーザー情報を設定してください。Python スクリプト例
from datakit_framework import DataKitFramework from pyVim.connect import SmartConnect, Disconnect import requests from pyVmomi import vim import ssl import datetime import time class vm(DataKitFramework): name = "vm" interval = 10 def run(self): print("vm") s = ssl.SSLContext(ssl.PROTOCOL_TLSv1) s.verify_mode = ssl.CERT_NONE datakit_url_object = 'http://0.0.0.0:9529/v1/write/custom_object' datakit_url_metric = 'http://0.0.0.0:9529/v1/write/metric' vcenter_host = "10.200.14.20" vcenter_port = 443 vcenter_user = "reader@zy.ops" vcenter_password = "Zhuyun@0906" # データは JSON 形式で Datakit に送信するため、POST リクエストには head を追加する必要があります header = { "Content-Type": "application/json" } # 送信データ dk_object = [] dk_metric = [] # vCenter Server に接続 c = SmartConnect(host=vcenter_host, user=vcenter_user, pwd=vcenter_password, port=vcenter_port, sslContext=s) content = c.RetrieveContent() container = content.rootFolder # starting point to look into viewType = [vim.HostSystem, vim.VirtualMachine] # object types to look for recursive = True # whether we should look into it recursively # vCenter の数と詳細を取得 vcenter_data = content.rootFolder.childEntity print("vCenter 数: ", len(vcenter_data)) for i in vcenter_data: dk_metric.append({ "measurement": "vmware", "tags": { "vcenter_name": str(i) }, "fields": { "vcenter_total": int(len(vcenter_data)) }, "time": int(time.time()) }) # datastore の数と詳細を取得 datastores = content.rootFolder.childEntity[0].datastore for ds in datastores: dk_metric.append({ "measurement": "vmware", "tags": { "vcenter_datastore_name": str(ds.name) }, "fields": { "vcenter_datastore_total_capacity": int(ds.summary.capacity), "vcenter_datastore_total_free_space": int(ds.summary.freeSpace), "vcenter_datastore_utilization_rate": int((ds.summary.capacity - ds.summary.freeSpace) / ds.summary.capacity), "vcenter_datastore_total": int(len(datastores)) }, "time": int(time.time()) }) containerView = content.viewManager.CreateContainerView(container, viewType, recursive) children = containerView.view print("クラスタ数: ", len(children)) for child in children: if isinstance(child, vim.HostSystem): boot_time = child.runtime.bootTime print("datae", datetime.datetime.now(boot_time.tzinfo) - boot_time) print("date2", int((datetime.datetime.now(boot_time.tzinfo) - boot_time).total_seconds())) dk_object.append({ "measurement": "vmware_esxi", "tags": { "name": str(child.name), "model": str(child.hardware.systemInfo.model), "vendor": str(child.hardware.systemInfo.vendor), "uptime": str((datetime.datetime.now(boot_time.tzinfo) - boot_time)) }, "fields": { "cpu_cores": str(child.hardware.cpuInfo.numCpuCores), "memory": str(child.hardware.memorySize) }, "time": int(time.time()) }) dk_metric.append({ "measurement": "vmware", "tags": { "esxi_name": str(child.name), "esxi_model": str(child.hardware.systemInfo.model), "esxi_vendor": str(child.hardware.systemInfo.vendor) }, "fields": { "cpu_cores": int(child.hardware.cpuInfo.numCpuCores), "memory": int(child.hardware.memorySize), "cpu_usage": int(child.summary.quickStats.overallCpuUsage), "mem_usage": int(child.summary.quickStats.overallMemoryUsage), "mem_utilization_rate": int(child.summary.quickStats.overallMemoryUsage / child.hardware.memorySize), "uptime": int((datetime.datetime.now(boot_time.tzinfo) - boot_time).total_seconds()) }, "time": int(time.time()) }) elif isinstance(child, vim.VirtualMachine): if child.runtime.powerState == 'poweredOn': # poweredOn の VM のみを出力 boot_time = child.runtime.bootTime dk_object.append({ "measurement": "vmware_vm", "tags": { "name": str(child.name), "host_name": str(child.guest.hostName), "guest_os": str(child.config.guestFullName), "ip_address": str(child.guest.ipAddress), "uptime": str((datetime.datetime.now(boot_time.tzinfo) - boot_time)) }, "fields": { "cpu_cores": str(child.config.hardware.numCPU), "memory": str(child.config.hardware.memoryMB) }, "time": int(time.time()) }) dk_metric.append({ "measurement": "vmware", "tags": { "vm_name": str(child.name), "vm_host_name": str(child.guest.hostName), "vm_ip_address": str(child.guest.ipAddress) }, "fields": { "cpu_cores": int(child.config.hardware.numCPU), "memory": int(child.config.hardware.memoryMB), "cpu_usage": int(child.summary.quickStats.overallCpuUsage), "mem_usage": int(child.summary.quickStats.guestMemoryUsage), "mem_utilization_rate": int(child.summary.quickStats.guestMemoryUsage / child.config.hardware.memoryMB), "disk_usage": int(child.summary.storage.committed), "uptime": int((datetime.datetime.now(boot_time.tzinfo) - boot_time).total_seconds()) }, "time": int(time.time()) }) containerView.Destroy() # response_object = requests.post(datakit_url_object, headers=header, json=dk_object) # response_metric = requests.post(datakit_url_metric, headers=header, json=dk_metric) in_data = { 'CO': dk_object, 'input': 'vmware_esxi,vmware_vm' } self.report(in_data) in_data2 = { 'M': dk_metric, 'input': 'vmware' } self.report(in_data2) Disconnect(c) print("--------------Success---------------") -
DataKit を再起動します:
メトリクス¶
VMware 監視を設定すると、デフォルトのメトリクスセットは以下のとおりです
| メトリクス | メトリクス名 | 単位 |
|---|---|---|
vcenter_total |
vCenter 数 | 個 |
vcenter_datastore_total_capacity |
vCenter データストア総容量 | B |
vcenter_datastore_total_free_space |
vCenter データストアの空き容量 | B |
vcenter_datastore_total |
vCenter データストア数 | 個 |
cpu_cores |
CPU コア数 | 個 |
memory |
メモリサイズ | MB |
cpu_usage |
CPU 使用量 | MHz |
mem_usage |
メモリ使用量 | MB |
uptime |
稼働時間 | 秒 |
disk_usage |
ディスク使用量 | B |
オブジェクト¶
"measurement": "vmware_esxi",
"tags": {
"name": "10.200.14.10",
"model": "PowerEdge R740",
"vendor": "Dell Inc.",
"uptime": "120 days, 6:04:50.399418"
},
"fields": {
"cpu_cores": "20",
"memory": "410686889984"
}
"measurement": "vmware_vm",
"tags": {
"name": "fanjun-test",
"host_name": "zy-infra-sh-vm-fanjun",
"guest_os": "CentOS 7 (64-bit)",
"ip_address": "10.200.14.178",
"uptime": "104 days, 5:02:26.586303"
},
"fields": {
"cpu_cores": "2",
"memory": "4096"
}
各フィールドの説明は以下のとおりです。
| フィールド | 型 | 説明 |
|---|---|---|
name |
String | ホスト名 |
model |
String | ハードウェアモデル。 |
vendor |
String | ハードウェアベンダー |
uptime |
String | 稼働時間 |
guest_os |
String | 仮想マシンのバージョン。 |
ip_address |
String | 仮想マシンの IP アドレス。 |
cpu_cores |
String | CPU コア数。 |
memory |
String | メモリサイズ( Byte)。 |