Heres a handy piece of Python that will output a few totals of instances running in CloudStack. It’s handy for Nagios monitoring or some Cacti graphing. Figures include:
- VMware instances
- XenServer instances
- Virtual routers
- System VMs
I don’t have any KVM boxes in my lab so I haven’t added that in yet. I’m using authenticationless port 8096 or the API because I’m lazy, so you’ll need that enabled to use this.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import json import urllib2 # CloudStack host or IP cshost='192.168.10.10' proto='http' port=8096 # Some variables vmwareCount=0 xenCount=0 routerCount=0 svmCount=0 # VMware count tmp='%s://%s:%d/client/api?command=listVirtualMachines&listall=true&state=running&hypervisor=VMware&response=json' % (proto,cshost,port) vmwareURL = urllib2.urlopen(tmp) vmwareDict = json.load(vmwareURL) if len(vmwareDict['listvirtualmachinesresponse']) > 0: vmwareCount = vmwareDict['listvirtualmachinesresponse']['count'] # XenServer count tmp='%s://%s:%d/client/api?command=listVirtualMachines&listall=true&state=running&hypervisor=XenServer&response=json' % (proto,cshost,port) xenURL = urllib2.urlopen(tmp) xenDict = json.load(xenURL) if len(xenDict['listvirtualmachinesresponse']) > 0: xenCount = xenDict['listvirtualmachinesresponse']['count'] # Router count tmp='%s://%s:%d/client/api?command=listRouters&listall=true&state=running&response=json' % (proto,cshost,port) routerURL = urllib2.urlopen(tmp) routerDict = json.load(routerURL) if len(routerDict['listroutersresponse']) > 0: routerCount = routerDict['listroutersresponse']['count'] # System VM count tmp='%s://%s:%d/client/api?command=listSystemVms&state=running&response=json' % (proto,cshost,port) svmURL = urllib2.urlopen(tmp) svmDict = json.load(svmURL) if len(svmDict['listsystemvmsresponse']) > 0: svmCount = svmDict['listsystemvmsresponse']['count'] # Print it all out print 'vmware: %d ' % vmwareCount print 'xen: %d ' % xenCount print 'router: %d ' % routerCount print 'svm: %d ' % svmCount |
|
1 2 3 4 5 |
[root@cslab]# python cs-vm-numbers.py vmware: 55 xen: 104 router: 11 svm: 3 |
Yes I know using CloudMonkey is probably easier but I like my way better 

Leave a Reply