Skip to content

Instantly share code, notes, and snippets.

@icanhazdevops
Last active December 20, 2015 05:59
Show Gist options
  • Save icanhazdevops/6082110 to your computer and use it in GitHub Desktop.
Save icanhazdevops/6082110 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Copyright 2012 Rackspace
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import sys
import pyrax
from os.path import expanduser
home = expanduser("~")
pyrax.set_setting("identity_type", "rackspace")
creds_file = os.path.expanduser("~/.rackspace_cloud_credentials")
pyrax.set_credential_file(creds_file)
cs = pyrax.cloudservers
# Get the generator of limits
cslimits = cs.limits.get()
# Convert the generator to a list
cslimits_list = [rate for rate in cslimits.absolute]
# Pull out max_ram api limit and total used ram from list
max_ram = [x.value for x in cslimits_list if x.name == "maxTotalRAMSize"][0]
total_ram = [x.value for x in cslimits_list if x.name == "totalRAMUsed"][0]
#Get the percent ram used and round it up for clean output
percent_ram = (float(total_ram) / float(max_ram)) * 100
percent_ram_used = round(float(("%.2f" % percent_ram)))
#test percent ram used against threshold
#Print status and metric lines that Cloud Monitoring Agent Plugin needs
if percent_ram_used < 90.0:
print "status ok Percent RAM Used", percent_ram_used
print "metric percent_ram_used float", percent_ram_used
else:
print "status err Percent RAM Used", percent_ram_used
print "metric percent_ram_used float", percent_ram_used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment