From 6a63df5a724a5debbfabf6723d62ffd72f2cb531 Mon Sep 17 00:00:00 2001 From: David Wahlstrom Date: Wed, 3 Jun 2015 00:09:27 -0700 Subject: [PATCH] Initial commit of openstack_vms.py This patch provides an module for tracking the number of active and non-active VMs in a given openstack cluster. When non-"ACTIVE" VMs are above a given threshold, the text will (by default) change to red, thus indicating an issue. Otherwise, the text will be green. --- i3pystatus/openstack_vms.py | 69 +++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 i3pystatus/openstack_vms.py diff --git a/i3pystatus/openstack_vms.py b/i3pystatus/openstack_vms.py new file mode 100644 index 0000000..c22a5b5 --- /dev/null +++ b/i3pystatus/openstack_vms.py @@ -0,0 +1,69 @@ +from i3pystatus import IntervalModule +from .core.util import round_dict +# requires python-novaclient +from novaclient.v2 import client +import webbrowser + + +class Openstack_vms(IntervalModule): + """ + Displays the number of VMs in an openstack cluster in ACTIVE and + non-ACTIVE states. + """ + + settings = ( + ("auth_url", "OpenStack cluster authentication URL (OS_AUTH_URL)"), + ("username", "Username for OpenStack authentication (OS_USERNAME)"), + ("password", "Password for Openstack authentication (OS_PASSWORD)"), + ("tenant_name", "Tenant/Project name to view (OS_TENANT_NAME)"), + ("color", "Display color when non-active VMs are =< `threshold` " + "(default: #00FF00"), + ("crit_color", "Display color when non-active VMs are => `threshold` " + "(default: #FF0000"), + ("threshold", "Set critical indicators when non-active VM pass this " + "number (default: 0)"), + ("horizon_url", "When clicked, open this URL in a browser") + ) + required = ("auth_url", "password", "tenant_name", "username") + color = "#00FF00" + crit_color = "#FF0000" + threshold = 0 + horizon_url = None + format = "{tenant_name}: {active_servers} active, {nonactive_servers} non-active" + + on_leftclick = "openurl" + + def run(self): + nclient = client.Client( + self.username, + self.password, + self.tenant_name, + self.auth_url + ) + + active_servers = 0 + nonactive_servers = 0 + server_list = nclient.servers.list() + for server in server_list: + if server.status == 'ACTIVE': + active_servers = active_servers + 1 + else: + nonactive_servers = nonactive_servers + 1 + + if nonactive_servers > self.threshold: + display_color = self.crit_color + else: + display_color = self.color + cdict = { + "tenant_name": self.tenant_name, + "active_servers": active_servers, + "nonactive_servers": nonactive_servers, + } + + self.output = { + "full_text": self.format.format(**cdict), + "color": display_color + } + + def openurl(self): + webbrowser.open_new_tab(self.horizon_url)