Skip to content

Instantly share code, notes, and snippets.

@koemu
Created July 24, 2014 05:26
Show Gist options
  • Save koemu/6040b298e452fd364328 to your computer and use it in GitHub Desktop.
Save koemu/6040b298e452fd364328 to your computer and use it in GitHub Desktop.
Get KVM guest CPU Usage for Mackerel
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import commands
import datetime
unix_time = datetime.datetime.today().strftime('%s')
retval = commands.getstatusoutput( "virt-top -b --stream -n 2 -d 1" )
if retval[0] != 0:
sys.exit( -1 )
top_count = 0
top_list = retval[1].split( "\n" )
for top_data in top_list:
if top_data.find( "virt-top time" ) >= 0:
top_count += 1
continue
if top_count != 2:
continue
if top_data == "":
continue
top_data_list = top_data.strip().split()
if top_data_list[0] == "ID":
continue
print "%s.%s.%s\t%s\t%s" % ( "kvm.guest", "cpu", top_data_list[9], top_data_list[6], unix_time )
# Please append this lines to your config file.
[plugin.metrics.kvm_guest_cpu]
command = "/usr/local/bin/kvm_guest_virttop.py"
type = "metric"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment