[comment]: # ({2ff5f110-e03ef403})
# 15 VMwareのカスタムパフォーマンスカウンター名作成

[comment]: # ({/2ff5f110-e03ef403})

[comment]: # ({24b7e9a0-eaab320d})
### 概要

VMwareのパフォーマンスカウンターパスは
`group/counter[rollup]` という形式であり、以下のようになります。

-   `group` - パフォーマンスカウンターグループ（例：*cpu*）
-   `counter` - パフォーマンスカウンター名（例：*usagemhz*）
-   `rollup` - パフォーマンスカウンターのロールアップタイプ（例：*average*）

したがって、上記の例では次のようなカウンターパスになります：
`cpu/usagemhz[average]`

パフォーマンスカウンターグループの説明、カウンター名、ロールアップタイプについては、[VMwareのドキュメント](https://developer.broadcom.com/xapis/vsphere-web-services-api/latest/)を参照してください。

Zabbixのスクリプトアイテムを使用することで、内部名を取得したり、カスタムのパフォーマンスカウンター名を作成したりすることができます。

[comment]: # ({/24b7e9a0-eaab320d})

[comment]: # ({6cddc3e3-2d867bd9})
### 設定

1. メインの VMware ホスト（**eventlog[]** アイテムが存在するホスト）に、以下のパラメータで無効化された Script アイテムを作成します。

![](../../../../assets/en/manual/appendix/items/perf_counter_item.png){width="600"}

- *Name*:  VMware metrics
- *Type*: Script
- *Key*: vmware.metrics
- *Type of information*: Text
- *Script*: 下記の [script](#script) をコピーして貼り付けます
- *Timeout*: 10
- *History*: 保存しない
- *Enabled*: 無効

#### Script

    try {
        Zabbix.log(4, 'vmware metrics script');

        var result, resp,
        req = new HttpRequest();
        req.addHeader('Content-Type: application/xml');
        req.addHeader('SOAPAction: "urn:vim25/6.0"');

        login = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:vim25">\
        <soapenv:Header/>\
        <soapenv:Body>\
            <urn:Login>\
                <urn:_this type="SessionManager">SessionManager</urn:_this>\
                <urn:userName>{$VMWARE.USERNAME}</urn:userName>\
                <urn:password>{$VMWARE.PASSWORD}</urn:password>\
            </urn:Login>\
        </soapenv:Body>\
    </soapenv:Envelope>'
        resp = req.post("{$VMWARE.URL}", login);
        if (req.getStatus() != 200) {
            throw 'Response code: '+req.getStatus();
        }

        query = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:vim25">\
    <soapenv:Header/>\
        <soapenv:Body>\
            <urn:RetrieveProperties>\
                <urn:_this type="PropertyCollector">propertyCollector</urn:_this>\
                <urn:specSet>\
                    <urn:propSet>\
                       <urn:type>PerformanceManager</urn:type>\
                       <urn:pathSet>perfCounter</urn:pathSet>\
                    </urn:propSet>\
                    <urn:objectSet>\
                       <urn:obj type="PerformanceManager">PerfMgr</urn:obj>\
                    </urn:objectSet>\
                </urn:specSet>\
            </urn:RetrieveProperties>\
        </soapenv:Body>\
    </soapenv:Envelope>'
        resp = req.post("{$VMWARE.URL}", query);
        if (req.getStatus() != 200) {
            throw 'Response code: '+req.getStatus();
        }
        Zabbix.log(4, 'vmware metrics=' + resp);
        result = resp;

        logout = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:vim25">\
        <soapenv:Header/>\
        <soapenv:Body>\
            <urn:Logout>\
                <urn:_this type="SessionManager">SessionManager</urn:_this>\
            </urn:Logout>\
        </soapenv:Body>\
    </soapenv:Envelope>'

        resp = req.post("{$VMWARE.URL}",logout);         
        if (req.getStatus() != 200) {
            throw 'Response code: '+req.getStatus();
        }

    } catch (error) {
        Zabbix.log(4, 'vmware call failed : '+error);
        result = {};
    }

    return result;


アイテムを設定したら、*Test* ボタンをクリックし、次に *Get value* をクリックします。 

![](../../../../assets/en/manual/appendix/items/perf_counter_item1.png){width=600}

受信した XML を任意の XML フォーマッターにコピーし、目的のメトリクスを見つけます。

1 つのメトリクスに対する XML の例:

    <PerfCounterInfo xsi:type="PerfCounterInfo">
        <key>6</key>
        <nameInfo>
            <label>Usage in MHz</label>
            <summary>CPU usage in megahertz during the interval</summary>
            <key>usagemhz</key>
        </nameInfo>
        <groupInfo>
            <label>CPU</label>
            <summary>CPU</summary>
            <key>cpu</key>
        </groupInfo>
        <unitInfo>
            <label>MHz</label>
            <summary>Megahertz</summary>
            <key>megaHertz</key>
        </unitInfo>
        <rollupType>average</rollupType>
        <statsType>rate</statsType>
        <level>1</level>
        <perDeviceLevel>3</perDeviceLevel>
    </PerfCounterInfo>

受信した XML からカウンターパスを抽出するには、XPath を使用します。上記の例では、XPath は次のようになります。

|field |xPath | value |
|--|--|--|
|group | //groupInfo[../key=6]/key | cpu |
|counter |//nameInfo[../key=6]/key |usagemhz |
|rollup |//rollupType[../key=6] |average |

この場合のパフォーマンスカウンターパスは `cpu/usagemhz[average]` です

[comment]: # ({/6cddc3e3-2d867bd9})
