设为首页 收藏本站
查看: 2863|回复: 1

[经验分享] Zabbix监控qemuKVM进程-low_level_discovery

[复制链接]

尚未签到

发表于 2017-6-26 07:51:43 | 显示全部楼层 |阅读模式
  本文介绍基于Zabbix LLD(low_level_discovery)自动发现机制创建自动发现脚本及对详细进程进行监控。
  执行过程:
  1:基于py脚本寻找KVM主机上的虚拟机名称,返回JSON格式
  2:配置Zabbix模板,自动生成监控列表信息
  3:基于ps命令获取监控值
  代码文件:
  获取虚拟机名称列表:



#!/usr/bin/env python
import os
import json
t=os.popen("""ps -e -o 'pid,comm,pcpu,rsz,vsz,stime,user,uid,cmd' |awk '$7=="qemu" {print $11}'""")
kvms = []
for kvm in  t.readlines():
     virshName = os.path.basename(kvm.strip())
     kvms += [{'{#QEMU}':virshName}]
print json.dumps({'data':kvms},sort_keys=True,indent=4,separators=(',',':'))
  获取监控详情:



#!/bin/bash
psfile=/tmp/$$.$$
ps -e -o 'pid,pcpu,pmem,sz,rsz,vsz,user,cmd' | awk '
BEGIN {
printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\n","pid","pcpu","pmem","sz","rsz","vsz","user","CMD"
}
$7=="qemu" {
printf "%s\t%s\t%s\t%.2f\t%.2f\t%.2f\t%s\t%s\n",$1,$2,$3,$4*1024,$5*1024,$6*1024,$7,$10
}
' > ${psfile}
PIDD=`cat ${psfile} | grep $2 | awk '{print $1}'`
#PIDD=$(ps -ef | grep $2 | grep -v grep| awk '{print $2}')
case $1 in
CPU_usage)
query_result=`/usr/bin/top -b -p $PIDD -n 1 | awk '/qemu/ {print $9}'`
;;
memory_usage)
query_result=`cat ${psfile} | grep $2 | awk '{print $3}'`
;;
sz_usage)
query_result=`cat ${psfile} | grep $2 | awk '{print $4}'`
;;
rsz_usage)
query_result=`cat ${psfile} | grep $2 | awk '{print $5}'`
;;
vsz_usage)
query_result=`cat ${psfile} | grep $2 | awk '{print $6}'`
;;
*)
echo ERROR
exit 1;
esac
rm -rf ${psfile}
echo ${query_result}
  配置Zabbix模板


DSC0000.gif DSC0001.gif


<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
     <version>2.0</version>
     <date>2017-06-04T01:51:44Z</date>
     <groups>
         <group>
             <name>QEMU_KVM</name>
         </group>
     </groups>
     <templates>
         <template>
             <template>Template QEMU_KVM</template>
             <name>Template QEMU_KVM</name>
             <description/>
             <groups>
                 <group>
                     <name>QEMU_KVM</name>
                 </group>
             </groups>
             <applications>
                 <application>
                     <name>qemuKVM</name>
                 </application>
                 <application>
                     <name>QEMU_memory_usage</name>
                 </application>
                 <application>
                     <name>QEMU_Process_usage</name>
                 </application>
                 <application>
                     <name>QEMU_rsz_usage</name>
                 </application>
                 <application>
                     <name>QEMU_sz_usage</name>
                 </application>
                 <application>
                     <name>QEMU_vsz_usage</name>
                 </application>
             </applications>
             <items/>
             <discovery_rules>
                 <discovery_rule>
                     <name>discovery_qemuKVM</name>
                     <type>0</type>
                     <snmp_community/>
                     <snmp_oid/>
                     <key>qemuKVM.discovery</key>
                     <delay>3600</delay>
                     <status>0</status>
                     <allowed_hosts/>
                     <snmpv3_contextname/>
                     <snmpv3_securityname/>
                     <snmpv3_securitylevel>0</snmpv3_securitylevel>
                     <snmpv3_authprotocol>0</snmpv3_authprotocol>
                     <snmpv3_authpassphrase/>
                     <snmpv3_privprotocol>0</snmpv3_privprotocol>
                     <snmpv3_privpassphrase/>
                     <delay_flex/>
                     <params/>
                     <ipmi_sensor/>
                     <authtype>0</authtype>
                     <username/>
                     <password/>
                     <publickey/>
                     <privatekey/>
                     <port/>
                     <filter>
                         <evaltype>0</evaltype>
                         <formula/>
                         <conditions>
                             <condition>
                                 <macro>{#QEMU}</macro>
                                 <value/>
                                 <operator>8</operator>
                                 <formulaid>A</formulaid>
                             </condition>
                         </conditions>
                     </filter>
                     <lifetime>30</lifetime>
                     <description/>
                     <item_prototypes>
                         <item_prototype>
                             <name>{#QEMU}_memory_usage</name>
                             <type>0</type>
                             <snmp_community/>
                             <multiplier>0</multiplier>
                             <snmp_oid/>
                             <key>qemuKVM[memory_usage,{#QEMU}]</key>
                             <delay>30</delay>
                             <history>90</history>
                             <trends>365</trends>
                             <status>0</status>
                             <value_type>0</value_type>
                             <allowed_hosts/>
                             <units>%</units>
                             <delta>0</delta>
                             <snmpv3_contextname/>
                             <snmpv3_securityname/>
                             <snmpv3_securitylevel>0</snmpv3_securitylevel>
                             <snmpv3_authprotocol>0</snmpv3_authprotocol>
                             <snmpv3_authpassphrase/>
                             <snmpv3_privprotocol>0</snmpv3_privprotocol>
                             <snmpv3_privpassphrase/>
                             <formula>1</formula>
                             <delay_flex/>
                             <params/>
                             <ipmi_sensor/>
                             <data_type>0</data_type>
                             <authtype>0</authtype>
                             <username/>
                             <password/>
                             <publickey/>
                             <privatekey/>
                             <port/>
                             <description/>
                             <inventory_link>0</inventory_link>
                             <applications>
                                 <application>
                                     <name>QEMU_memory_usage</name>
                                 </application>
                             </applications>
                             <valuemap/>
                             <logtimefmt/>
                         </item_prototype>
                         <item_prototype>
                             <name>{#QEMU}_Process_usage</name>
                             <type>0</type>
                             <snmp_community/>
                             <multiplier>0</multiplier>
                             <snmp_oid/>
                             <key>qemuKVM[CPU_usage,{#QEMU}]</key>
                             <delay>30</delay>
                             <history>90</history>
                             <trends>365</trends>
                             <status>0</status>
                             <value_type>0</value_type>
                             <allowed_hosts/>
                             <units>%</units>
                             <delta>0</delta>
                             <snmpv3_contextname/>
                             <snmpv3_securityname/>
                             <snmpv3_securitylevel>0</snmpv3_securitylevel>
                             <snmpv3_authprotocol>0</snmpv3_authprotocol>
                             <snmpv3_authpassphrase/>
                             <snmpv3_privprotocol>0</snmpv3_privprotocol>
                             <snmpv3_privpassphrase/>
                             <formula>1</formula>
                             <delay_flex/>
                             <params/>
                             <ipmi_sensor/>
                             <data_type>0</data_type>
                             <authtype>0</authtype>
                             <username/>
                             <password/>
                             <publickey/>
                             <privatekey/>
                             <port/>
                             <description/>
                             <inventory_link>0</inventory_link>
                             <applications>
                                 <application>
                                     <name>QEMU_Process_usage</name>
                                 </application>
                             </applications>
                             <valuemap/>
                             <logtimefmt/>
                         </item_prototype>
                         <item_prototype>
                             <name>{#QEMU}_rsz_usage</name>
                             <type>0</type>
                             <snmp_community/>
                             <multiplier>0</multiplier>
                             <snmp_oid/>
                             <key>qemuKVM[rsz_usage,{#QEMU}]</key>
                             <delay>30</delay>
                             <history>90</history>
                             <trends>365</trends>
                             <status>0</status>
                             <value_type>3</value_type>
                             <allowed_hosts/>
                             <units>B</units>
                             <delta>0</delta>
                             <snmpv3_contextname/>
                             <snmpv3_securityname/>
                             <snmpv3_securitylevel>0</snmpv3_securitylevel>
                             <snmpv3_authprotocol>0</snmpv3_authprotocol>
                             <snmpv3_authpassphrase/>
                             <snmpv3_privprotocol>0</snmpv3_privprotocol>
                             <snmpv3_privpassphrase/>
                             <formula>1</formula>
                             <delay_flex/>
                             <params/>
                             <ipmi_sensor/>
                             <data_type>0</data_type>
                             <authtype>0</authtype>
                             <username/>
                             <password/>
                             <publickey/>
                             <privatekey/>
                             <port/>
                             <description/>
                             <inventory_link>0</inventory_link>
                             <applications>
                                 <application>
                                     <name>QEMU_rsz_usage</name>
                                 </application>
                             </applications>
                             <valuemap/>
                             <logtimefmt/>
                         </item_prototype>
                         <item_prototype>
                             <name>{#QEMU}_sz_usage</name>
                             <type>0</type>
                             <snmp_community/>
                             <multiplier>0</multiplier>
                             <snmp_oid/>
                             <key>qemuKVM[sz_usage,{#QEMU}]</key>
                             <delay>30</delay>
                             <history>90</history>
                             <trends>365</trends>
                             <status>0</status>
                             <value_type>3</value_type>
                             <allowed_hosts/>
                             <units>B</units>
                             <delta>0</delta>
                             <snmpv3_contextname/>
                             <snmpv3_securityname/>
                             <snmpv3_securitylevel>0</snmpv3_securitylevel>
                             <snmpv3_authprotocol>0</snmpv3_authprotocol>
                             <snmpv3_authpassphrase/>
                             <snmpv3_privprotocol>0</snmpv3_privprotocol>
                             <snmpv3_privpassphrase/>
                             <formula>1</formula>
                             <delay_flex/>
                             <params/>
                             <ipmi_sensor/>
                             <data_type>0</data_type>
                             <authtype>0</authtype>
                             <username/>
                             <password/>
                             <publickey/>
                             <privatekey/>
                             <port/>
                             <description/>
                             <inventory_link>0</inventory_link>
                             <applications>
                                 <application>
                                     <name>QEMU_sz_usage</name>
                                 </application>
                             </applications>
                             <valuemap/>
                             <logtimefmt/>
                         </item_prototype>
                         <item_prototype>
                             <name>{#QEMU}_vsz_usage</name>
                             <type>0</type>
                             <snmp_community/>
                             <multiplier>0</multiplier>
                             <snmp_oid/>
                             <key>qemuKVM[vsz_usage,{#QEMU}]</key>
                             <delay>30</delay>
                             <history>90</history>
                             <trends>365</trends>
                             <status>0</status>
                             <value_type>3</value_type>
                             <allowed_hosts/>
                             <units>B</units>
                             <delta>0</delta>
                             <snmpv3_contextname/>
                             <snmpv3_securityname/>
                             <snmpv3_securitylevel>0</snmpv3_securitylevel>
                             <snmpv3_authprotocol>0</snmpv3_authprotocol>
                             <snmpv3_authpassphrase/>
                             <snmpv3_privprotocol>0</snmpv3_privprotocol>
                             <snmpv3_privpassphrase/>
                             <formula>1</formula>
                             <delay_flex/>
                             <params/>
                             <ipmi_sensor/>
                             <data_type>0</data_type>
                             <authtype>0</authtype>
                             <username/>
                             <password/>
                             <publickey/>
                             <privatekey/>
                             <port/>
                             <description/>
                             <inventory_link>0</inventory_link>
                             <applications>
                                 <application>
                                     <name>QEMU_vsz_usage</name>
                                 </application>
                             </applications>
                             <valuemap/>
                             <logtimefmt/>
                         </item_prototype>
                     </item_prototypes>
                     <trigger_prototypes>
                         <trigger_prototype>
                             <expression>{Template QEMU_KVM:qemuKVM[CPU_usage,{#QEMU}].min(300)}&gt;200 or {Template QEMU_KVM:qemuKVM[CPU_usage,{#QEMU}].min(600)}&gt;80</expression>
                             <name>{#QEMU}_CPU_usage_100Percent</name>
                             <url/>
                             <status>1</status>
                             <priority>3</priority>
                             <description/>
                             <type>0</type>
                         </trigger_prototype>
                         <trigger_prototype>
                             <expression>{Template QEMU_KVM:qemuKVM[CPU_usage,{#QEMU}].min(300)}&gt;300 or {Template QEMU_KVM:qemuKVM[CPU_usage,{#QEMU}].min(900)}&gt;80</expression>
                             <name>{#QEMU}_CPU_usage_is_too_hight</name>
                             <url/>
                             <status>1</status>
                             <priority>4</priority>
                             <description/>
                             <type>0</type>
                         </trigger_prototype>
                     </trigger_prototypes>
                     <graph_prototypes>
                         <graph_prototype>
                             <name>memory_usage</name>
                             <width>900</width>
                             <height>200</height>
                             <yaxismin>0.0000</yaxismin>
                             <yaxismax>100.0000</yaxismax>
                             <show_work_period>1</show_work_period>
                             <show_triggers>1</show_triggers>
                             <type>0</type>
                             <show_legend>1</show_legend>
                             <show_3d>0</show_3d>
                             <percent_left>0.0000</percent_left>
                             <percent_right>0.0000</percent_right>
                             <ymin_type_1>0</ymin_type_1>
                             <ymax_type_1>0</ymax_type_1>
                             <ymin_item_1>0</ymin_item_1>
                             <ymax_item_1>0</ymax_item_1>
                             <graph_items>
                                 <graph_item>
                                     <sortorder>0</sortorder>
                                     <drawtype>0</drawtype>
                                     <color>00C800</color>
                                     <yaxisside>0</yaxisside>
                                     <calc_fnc>2</calc_fnc>
                                     <type>0</type>
                                     <item>
                                         <host>Template QEMU_KVM</host>
                                         <key>qemuKVM[memory_usage,{#QEMU}]</key>
                                     </item>
                                 </graph_item>
                                 <graph_item>
                                     <sortorder>1</sortorder>
                                     <drawtype>0</drawtype>
                                     <color>C80000</color>
                                     <yaxisside>0</yaxisside>
                                     <calc_fnc>4</calc_fnc>
                                     <type>0</type>
                                     <item>
                                         <host>Template QEMU_KVM</host>
                                         <key>qemuKVM[memory_usage,{#QEMU}]</key>
                                     </item>
                                 </graph_item>
                                 <graph_item>
                                     <sortorder>2</sortorder>
                                     <drawtype>0</drawtype>
                                     <color>0000C8</color>
                                     <yaxisside>0</yaxisside>
                                     <calc_fnc>1</calc_fnc>
                                     <type>0</type>
                                     <item>
                                         <host>Template QEMU_KVM</host>
                                         <key>qemuKVM[memory_usage,{#QEMU}]</key>
                                     </item>
                                 </graph_item>
                             </graph_items>
                         </graph_prototype>
                         <graph_prototype>
                             <name>Process_usage</name>
                             <width>900</width>
                             <height>200</height>
                             <yaxismin>0.0000</yaxismin>
                             <yaxismax>100.0000</yaxismax>
                             <show_work_period>1</show_work_period>
                             <show_triggers>1</show_triggers>
                             <type>0</type>
                             <show_legend>1</show_legend>
                             <show_3d>0</show_3d>
                             <percent_left>0.0000</percent_left>
                             <percent_right>0.0000</percent_right>
                             <ymin_type_1>0</ymin_type_1>
                             <ymax_type_1>0</ymax_type_1>
                             <ymin_item_1>0</ymin_item_1>
                             <ymax_item_1>0</ymax_item_1>
                             <graph_items>
                                 <graph_item>
                                     <sortorder>0</sortorder>
                                     <drawtype>0</drawtype>
                                     <color>00C800</color>
                                     <yaxisside>0</yaxisside>
                                     <calc_fnc>2</calc_fnc>
                                     <type>0</type>
                                     <item>
                                         <host>Template QEMU_KVM</host>
                                         <key>qemuKVM[CPU_usage,{#QEMU}]</key>
                                     </item>
                                 </graph_item>
                                 <graph_item>
                                     <sortorder>1</sortorder>
                                     <drawtype>0</drawtype>
                                     <color>C80000</color>
                                     <yaxisside>0</yaxisside>
                                     <calc_fnc>4</calc_fnc>
                                     <type>0</type>
                                     <item>
                                         <host>Template QEMU_KVM</host>
                                         <key>qemuKVM[CPU_usage,{#QEMU}]</key>
                                     </item>
                                 </graph_item>
                                 <graph_item>
                                     <sortorder>2</sortorder>
                                     <drawtype>0</drawtype>
                                     <color>0000C8</color>
                                     <yaxisside>0</yaxisside>
                                     <calc_fnc>1</calc_fnc>
                                     <type>0</type>
                                     <item>
                                         <host>Template QEMU_KVM</host>
                                         <key>qemuKVM[CPU_usage,{#QEMU}]</key>
                                     </item>
                                 </graph_item>
                             </graph_items>
                         </graph_prototype>
                     </graph_prototypes>
                     <host_prototypes/>
                 </discovery_rule>
             </discovery_rules>
             <macros/>
             <templates/>
             <screens/>
         </template>
     </templates>
</zabbix_export>
Zabbix_Template QEMU_KVM  监控效果略。
  官方文档:
  https://www.zabbix.com/documentation/3.0/manual/discovery/low_level_discovery

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-388136-1-1.html 上篇帖子: 看 nova 下篇帖子: 原生态安装LAMP Web环境(Apache/MySQL/PHP)
累计签到:13 天
连续签到:1 天
发表于 2017-8-4 09:21:34 | 显示全部楼层
  回帖赚点积分哈···

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表