16 为 VMware 创建自定义性能计数器名称

概述

VMware性能计数器路径采用 group/counter[rollup]格式,其中:

  • group - 性能计数器组,例如cpu
  • counter - 性能计数器名称,例如usagemhz
  • rollup - 性能计数器聚合类型,例如average

因此上述示例将生成以下计数器路径: cpu/usagemhz[average]

性能计数器组描述、计数器名称及聚合类型可在VMware documentation中查阅。

通过使用Zabbix中的监控项脚本,可获取内部名称并创建自定义性能计数器名称。

配置

  1. 在主VMware 主机上创建禁用状态的脚本监控项(存在eventlog[] 监控项的位置),参数设置如下:

  • 名称:VMware指标
  • 类型:脚本
  • 键值:vmware.metrics
  • 信息类型:文本
  • 脚本:复制粘贴下方提供的脚本
  • 超时:10
  • 历史存储周期:不保留历史
  • 启用:取消勾选

脚本

try {
           Zabbix.log(4, 'vmware指标脚本');
       
           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 '响应代码: '+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 '响应代码: '+req.getStatus();
           }
           Zabbix.log(4, 'vmware指标=' + 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 '响应代码: '+req.getStatus();
           }
       
       } catch (error) {
           Zabbix.log(4, 'vmware调用失败: '+error);
           result = {};
       }
       
       return result;

配置完监控项后,点击测试按钮,然后点击获取值

将接收到的XML复制到任意XML格式化工具中查找所需指标。

单个指标的XML示例:

<PerfCounterInfo xsi:type="PerfCounterInfo">
           <key>6</key>
           <nameInfo>
               <label>使用量(MHz)</label>
               <summary>间隔期间CPU使用量(兆赫兹)</summary>
               <key>usagemhz</key>
           </nameInfo>
           <groupInfo>
               <label>CPU</label>
               <summary>CPU</summary>
               <key>cpu</key>
           </groupInfo>
           <unitInfo>
               <label>MHz</label>
               <summary>兆赫兹</summary>
               <key>megaHertz</key>
           </unitInfo>
           <rollupType>average</rollupType>
           <statsType>rate</statsType>
           <level>1</level>
           <perDeviceLevel>3</perDeviceLevel>
       </PerfCounterInfo>

使用XPath从接收的XML中提取计数器路径。对于上述示例,XPath为:

字段 xPath
group //groupInfo[../key=6]/key cpu
counter //nameInfo[../key=6]/key usagemhz
rollup //rollupType[../key=6] average

最终性能计数器路径为:cpu/usagemhz[average]