Skip to content

Instantly share code, notes, and snippets.

@koemu
Created July 24, 2014 05:29
Show Gist options
  • Save koemu/1954030a391481bfb65a to your computer and use it in GitHub Desktop.
Save koemu/1954030a391481bfb65a to your computer and use it in GitHub Desktop.
Get KVM guest information for Mackerel
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import commands
import datetime
argv = sys.argv
if len( argv ) < 2:
print "usage " + sys.argv[0] + " info_name"
sys.exit( -1 )
info_name = sys.argv[1]
unix_time = datetime.datetime.today().strftime('%s')
retval = commands.getstatusoutput( "virsh list --name" )
if retval[0] != 0:
sys.exit( -1 )
vm_list = retval[1].split( "\n" )
for vm_name in vm_list:
if vm_name == "":
continue
vm_dominfo = commands.getstatusoutput( "virsh dominfo %s | grep -w '%s' | awk -F: '{print $2}'" % ( vm_name, info_name ) )
if vm_dominfo[0] != 0:
continue
vm_status = vm_dominfo[1].strip().split( " " )
vm_value = vm_status[0]
if len( vm_status ) > 1 and vm_status[1] == 'KiB':
vm_value = int( vm_value ) * 1024
info_metrics_name = info_name.replace( " ", "" ).lower()
print "%s.%s.%s\t%s\t%s" % ( "kvm.guest", info_metrics_name, vm_name, vm_value, unix_time )
# Please append this lines to your config file.
[plugin.metrics.kvm_guest_maxmemory]
command = "/usr/local/bin/kvm_guest_dominfo.py 'Max memory'"
type = "metric"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment