本文介绍基于Zabbix LLD(low_level_discovery)自动发现机制创建自动发现脚本及对详细进程进行监控。
执行过程:
1:基于py脚本寻找KVM主机上的虚拟机名称,返回JSON格式
2:配置Zabbix模板,自动生成监控列表信息
3:基于ps命令获取监控值
代码文件:
获取虚拟机名称列表:
1 #!/usr/bin/env python 2 import os 3 import json 4 t=os.popen("""ps -e -o 'pid,comm,pcpu,rsz,vsz,stime,user,uid,cmd' |awk '$7=="qemu" {print $11}'""") 5 kvms = [] 6 for kvm in t.readlines(): 7 virshName = os.path.basename(kvm.strip()) 8 kvms += [{'{#QEMU}':virshName}] 9 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模板
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <zabbix_export> 3 <version>2.0</version> 4 <date>2017-06-04T01:51:44Z</date> 5 <groups> 6 <group> 7 <name>QEMU_KVM</name> 8 </group> 9 </groups> 10 <templates> 11 <template> 12 <template>Template QEMU_KVM</template> 13 <name>Template QEMU_KVM</name> 14 <description/> 15 <groups> 16 <group> 17 <name>QEMU_KVM</name> 18 </group> 19 </groups> 20 <applications> 21 <application> 22 <name>qemuKVM</name> 23 </application> 24 <application> 25 <name>QEMU_memory_usage</name> 26 </application> 27 <application> 28 <name>QEMU_Process_usage</name> 29 </application> 30 <application> 31 <name>QEMU_rsz_usage</name> 32 </application> 33 <application> 34 <name>QEMU_sz_usage</name> 35 </application> 36 <application> 37 <name>QEMU_vsz_usage</name> 38 </application> 39 </applications> 40 <items/> 41 <discovery_rules> 42 <discovery_rule> 43 <name>discovery_qemuKVM</name> 44 <type>0</type> 45 <snmp_community/> 46 <snmp_oid/> 47 <key>qemuKVM.discovery</key> 48 <delay>3600</delay> 49 <status>0</status> 50 <allowed_hosts/> 51 <snmpv3_contextname/> 52 <snmpv3_securityname/> 53 <snmpv3_securitylevel>0</snmpv3_securitylevel> 54 <snmpv3_authprotocol>0</snmpv3_authprotocol> 55 <snmpv3_authpassphrase/> 56 <snmpv3_privprotocol>0</snmpv3_privprotocol> 57 <snmpv3_privpassphrase/> 58 <delay_flex/> 59 <params/> 60 <ipmi_sensor/> 61 <authtype>0</authtype> 62 <username/> 63 <password/> 64 <publickey/> 65 <privatekey/> 66 <port/> 67 <filter> 68 <evaltype>0</evaltype> 69 <formula/> 70 <conditions> 71 <condition> 72 <macro>{#QEMU}</macro> 73 <value/> 74 <operator>8</operator> 75 <formulaid>A</formulaid> 76 </condition> 77 </conditions> 78 </filter> 79 <lifetime>30</lifetime> 80 <description/> 81 <item_prototypes> 82 <item_prototype> 83 <name>{#QEMU}_memory_usage</name> 84 <type>0</type> 85 <snmp_community/> 86 <multiplier>0</multiplier> 87 <snmp_oid/> 88 <key>qemuKVM[memory_usage,{#QEMU}]</key> 89 <delay>30</delay> 90 <history>90</history> 91 <trends>365</trends> 92 <status>0</status> 93 <value_type>0</value_type> 94 <allowed_hosts/> 95 <units>%</units> 96 <delta>0</delta> 97 <snmpv3_contextname/> 98 <snmpv3_securityname/> 99 <snmpv3_securitylevel>0</snmpv3_securitylevel> 100 <snmpv3_authprotocol>0</snmpv3_authprotocol> 101 <snmpv3_authpassphrase/> 102 <snmpv3_privprotocol>0</snmpv3_privprotocol> 103 <snmpv3_privpassphrase/> 104 <formula>1</formula> 105 <delay_flex/> 106 <params/> 107 <ipmi_sensor/> 108 <data_type>0</data_type> 109 <authtype>0</authtype> 110 <username/> 111 <password/> 112 <publickey/> 113 <privatekey/> 114 <port/> 115 <description/> 116 <inventory_link>0</inventory_link> 117 <applications> 118 <application> 119 <name>QEMU_memory_usage</name> 120 </application> 121 </applications> 122 <valuemap/> 123 <logtimefmt/> 124 </item_prototype> 125 <item_prototype> 126 <name>{#QEMU}_Process_usage</name> 127 <type>0</type> 128 <snmp_community/> 129 <multiplier>0</multiplier> 130 <snmp_oid/> 131 <key>qemuKVM[CPU_usage,{#QEMU}]</key> 132 <delay>30</delay> 133 <history>90</history> 134 <trends>365</trends> 135 <status>0</status> 136 <value_type>0</value_type> 137 <allowed_hosts/> 138 <units>%</units> 139 <delta>0</delta> 140 <snmpv3_contextname/> 141 <snmpv3_securityname/> 142 <snmpv3_securitylevel>0</snmpv3_securitylevel> 143 <snmpv3_authprotocol>0</snmpv3_authprotocol> 144 <snmpv3_authpassphrase/> 145 <snmpv3_privprotocol>0</snmpv3_privprotocol> 146 <snmpv3_privpassphrase/> 147 <formula>1</formula> 148 <delay_flex/> 149 <params/> 150 <ipmi_sensor/> 151 <data_type>0</data_type> 152 <authtype>0</authtype> 153 <username/> 154 <password/> 155 <publickey/> 156 <privatekey/> 157 <port/> 158 <description/> 159 <inventory_link>0</inventory_link> 160 <applications> 161 <application> 162 <name>QEMU_Process_usage</name> 163 </application> 164 </applications> 165 <valuemap/> 166 <logtimefmt/> 167 </item_prototype> 168 <item_prototype> 169 <name>{#QEMU}_rsz_usage</name> 170 <type>0</type> 171 <snmp_community/> 172 <multiplier>0</multiplier> 173 <snmp_oid/> 174 <key>qemuKVM[rsz_usage,{#QEMU}]</key> 175 <delay>30</delay> 176 <history>90</history> 177 <trends>365</trends> 178 <status>0</status> 179 <value_type>3</value_type> 180 <allowed_hosts/> 181 <units>B</units> 182 <delta>0</delta> 183 <snmpv3_contextname/> 184 <snmpv3_securityname/> 185 <snmpv3_securitylevel>0</snmpv3_securitylevel> 186 <snmpv3_authprotocol>0</snmpv3_authprotocol> 187 <snmpv3_authpassphrase/> 188 <snmpv3_privprotocol>0</snmpv3_privprotocol> 189 <snmpv3_privpassphrase/> 190 <formula>1</formula> 191 <delay_flex/> 192 <params/> 193 <ipmi_sensor/> 194 <data_type>0</data_type> 195 <authtype>0</authtype> 196 <username/> 197 <password/> 198 <publickey/> 199 <privatekey/> 200 <port/> 201 <description/> 202 <inventory_link>0</inventory_link> 203 <applications> 204 <application> 205 <name>QEMU_rsz_usage</name> 206 </application> 207 </applications> 208 <valuemap/> 209 <logtimefmt/> 210 </item_prototype> 211 <item_prototype> 212 <name>{#QEMU}_sz_usage</name> 213 <type>0</type> 214 <snmp_community/> 215 <multiplier>0</multiplier> 216 <snmp_oid/> 217 <key>qemuKVM[sz_usage,{#QEMU}]</key> 218 <delay>30</delay> 219 <history>90</history> 220 <trends>365</trends> 221 <status>0</status> 222 <value_type>3</value_type> 223 <allowed_hosts/> 224 <units>B</units> 225 <delta>0</delta> 226 <snmpv3_contextname/> 227 <snmpv3_securityname/> 228 <snmpv3_securitylevel>0</snmpv3_securitylevel> 229 <snmpv3_authprotocol>0</snmpv3_authprotocol> 230 <snmpv3_authpassphrase/> 231 <snmpv3_privprotocol>0</snmpv3_privprotocol> 232 <snmpv3_privpassphrase/> 233 <formula>1</formula> 234 <delay_flex/> 235 <params/> 236 <ipmi_sensor/> 237 <data_type>0</data_type> 238 <authtype>0</authtype> 239 <username/> 240 <password/> 241 <publickey/> 242 <privatekey/> 243 <port/> 244 <description/> 245 <inventory_link>0</inventory_link> 246 <applications> 247 <application> 248 <name>QEMU_sz_usage</name> 249 </application> 250 </applications> 251 <valuemap/> 252 <logtimefmt/> 253 </item_prototype> 254 <item_prototype> 255 <name>{#QEMU}_vsz_usage</name> 256 <type>0</type> 257 <snmp_community/> 258 <multiplier>0</multiplier> 259 <snmp_oid/> 260 <key>qemuKVM[vsz_usage,{#QEMU}]</key> 261 <delay>30</delay> 262 <history>90</history> 263 <trends>365</trends> 264 <status>0</status> 265 <value_type>3</value_type> 266 <allowed_hosts/> 267 <units>B</units> 268 <delta>0</delta> 269 <snmpv3_contextname/> 270 <snmpv3_securityname/> 271 <snmpv3_securitylevel>0</snmpv3_securitylevel> 272 <snmpv3_authprotocol>0</snmpv3_authprotocol> 273 <snmpv3_authpassphrase/> 274 <snmpv3_privprotocol>0</snmpv3_privprotocol> 275 <snmpv3_privpassphrase/> 276 <formula>1</formula> 277 <delay_flex/> 278 <params/> 279 <ipmi_sensor/> 280 <data_type>0</data_type> 281 <authtype>0</authtype> 282 <username/> 283 <password/> 284 <publickey/> 285 <privatekey/> 286 <port/> 287 <description/> 288 <inventory_link>0</inventory_link> 289 <applications> 290 <application> 291 <name>QEMU_vsz_usage</name> 292 </application> 293 </applications> 294 <valuemap/> 295 <logtimefmt/> 296 </item_prototype> 297 </item_prototypes> 298 <trigger_prototypes> 299 <trigger_prototype> 300 <expression>{Template QEMU_KVM:qemuKVM[CPU_usage,{#QEMU}].min(300)}>200 or {Template QEMU_KVM:qemuKVM[CPU_usage,{#QEMU}].min(600)}>80</expression> 301 <name>{#QEMU}_CPU_usage_100Percent</name> 302 <url/> 303 <status>1</status> 304 <priority>3</priority> 305 <description/> 306 <type>0</type> 307 </trigger_prototype> 308 <trigger_prototype> 309 <expression>{Template QEMU_KVM:qemuKVM[CPU_usage,{#QEMU}].min(300)}>300 or {Template QEMU_KVM:qemuKVM[CPU_usage,{#QEMU}].min(900)}>80</expression> 310 <name>{#QEMU}_CPU_usage_is_too_hight</name> 311 <url/> 312 <status>1</status> 313 <priority>4</priority> 314 <description/> 315 <type>0</type> 316 </trigger_prototype> 317 </trigger_prototypes> 318 <graph_prototypes> 319 <graph_prototype> 320 <name>memory_usage</name> 321 <width>900</width> 322 <height>200</height> 323 <yaxismin>0.0000</yaxismin> 324 <yaxismax>100.0000</yaxismax> 325 <show_work_period>1</show_work_period> 326 <show_triggers>1</show_triggers> 327 <type>0</type> 328 <show_legend>1</show_legend> 329 <show_3d>0</show_3d> 330 <percent_left>0.0000</percent_left> 331 <percent_right>0.0000</percent_right> 332 <ymin_type_1>0</ymin_type_1> 333 <ymax_type_1>0</ymax_type_1> 334 <ymin_item_1>0</ymin_item_1> 335 <ymax_item_1>0</ymax_item_1> 336 <graph_items> 337 <graph_item> 338 <sortorder>0</sortorder> 339 <drawtype>0</drawtype> 340 <color>00C800</color> 341 <yaxisside>0</yaxisside> 342 <calc_fnc>2</calc_fnc> 343 <type>0</type> 344 <item> 345 <host>Template QEMU_KVM</host> 346 <key>qemuKVM[memory_usage,{#QEMU}]</key> 347 </item> 348 </graph_item> 349 <graph_item> 350 <sortorder>1</sortorder> 351 <drawtype>0</drawtype> 352 <color>C80000</color> 353 <yaxisside>0</yaxisside> 354 <calc_fnc>4</calc_fnc> 355 <type>0</type> 356 <item> 357 <host>Template QEMU_KVM</host> 358 <key>qemuKVM[memory_usage,{#QEMU}]</key> 359 </item> 360 </graph_item> 361 <graph_item> 362 <sortorder>2</sortorder> 363 <drawtype>0</drawtype> 364 <color>0000C8</color> 365 <yaxisside>0</yaxisside> 366 <calc_fnc>1</calc_fnc> 367 <type>0</type> 368 <item> 369 <host>Template QEMU_KVM</host> 370 <key>qemuKVM[memory_usage,{#QEMU}]</key> 371 </item> 372 </graph_item> 373 </graph_items> 374 </graph_prototype> 375 <graph_prototype> 376 <name>Process_usage</name> 377 <width>900</width> 378 <height>200</height> 379 <yaxismin>0.0000</yaxismin> 380 <yaxismax>100.0000</yaxismax> 381 <show_work_period>1</show_work_period> 382 <show_triggers>1</show_triggers> 383 <type>0</type> 384 <show_legend>1</show_legend> 385 <show_3d>0</show_3d> 386 <percent_left>0.0000</percent_left> 387 <percent_right>0.0000</percent_right> 388 <ymin_type_1>0</ymin_type_1> 389 <ymax_type_1>0</ymax_type_1> 390 <ymin_item_1>0</ymin_item_1> 391 <ymax_item_1>0</ymax_item_1> 392 <graph_items> 393 <graph_item> 394 <sortorder>0</sortorder> 395 <drawtype>0</drawtype> 396 <color>00C800</color> 397 <yaxisside>0</yaxisside> 398 <calc_fnc>2</calc_fnc> 399 <type>0</type> 400 <item> 401 <host>Template QEMU_KVM</host> 402 <key>qemuKVM[CPU_usage,{#QEMU}]</key> 403 </item> 404 </graph_item> 405 <graph_item> 406 <sortorder>1</sortorder> 407 <drawtype>0</drawtype> 408 <color>C80000</color> 409 <yaxisside>0</yaxisside> 410 <calc_fnc>4</calc_fnc> 411 <type>0</type> 412 <item> 413 <host>Template QEMU_KVM</host> 414 <key>qemuKVM[CPU_usage,{#QEMU}]</key> 415 </item> 416 </graph_item> 417 <graph_item> 418 <sortorder>2</sortorder> 419 <drawtype>0</drawtype> 420 <color>0000C8</color> 421 <yaxisside>0</yaxisside> 422 <calc_fnc>1</calc_fnc> 423 <type>0</type> 424 <item> 425 <host>Template QEMU_KVM</host> 426 <key>qemuKVM[CPU_usage,{#QEMU}]</key> 427 </item> 428 </graph_item> 429 </graph_items> 430 </graph_prototype> 431 </graph_prototypes> 432 <host_prototypes/> 433 </discovery_rule> 434 </discovery_rules> 435 <macros/> 436 <templates/> 437 <screens/> 438 </template> 439 </templates> 440 </zabbix_export>
监控效果略。
官方文档:
https://www.zabbix.com/documentation/3.0/manual/discovery/low_level_discovery