-
Notifications
You must be signed in to change notification settings - Fork 65
/
api_limit_ram_usage.py
executable file
·90 lines (78 loc) · 3.77 KB
/
api_limit_ram_usage.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python
'''Rackspace Cloud Servers RAM usage monitor'''
'''Pulls metrics from API for Total RAM allowed for Cloud Servers and Total RAM currently used (for API limits) and returns percent_ram_used metric to Cloud Monitoring API. Threshold percentage of when to set status to warning is set at command line argument along with username and api key'''
# Copyright 2013 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 sys
import pyrax
import argparse
def main():
'''Script execution'''
parser = argparse.ArgumentParser(description='get percent of api limit '
'of ram used')
parser.add_argument('-u', '--username', help='Rackspace Username',
required=True)
parser.add_argument('-a', '--apikey', help='Rackspace API Key',
required=True)
parser.add_argument('-m', '--maxthreshold',
help='API Percent Used Threshold, integer between '
'1-99',
required=True)
parser.add_argument('-r', '--region', help='Rackspace Regional Datacenter',
required=True)
parser.add_argument('--human',
help='Format output for humans, not Cloud Monitoring',
action='store_true')
args = parser.parse_args()
if int(args.maxthreshold) < 1 or int(args.maxthreshold) > 99:
print "You must enter a valid integer from 1-99 for maxthreshold"
sys.exit(2)
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_credentials(args.username, args.apikey)
(ram_used, ram_allowed) = getlimits(args.region)
display_usage(ram_used, ram_allowed, args.maxthreshold, args.human)
def getlimits(region):
'''Returns the RAM usage and limits'''
compute = pyrax.cloudservers
cslimits = compute.limits.get(region)
# 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]
return (total_ram, max_ram)
def display_usage(ram_used, ram_allowed, alert_percentage, human):
'''Print RAM usage information'''
percent_ram = (float(ram_used) / float(ram_allowed)) * 100
percent_ram_used = round(float(("%.2f" % percent_ram)))
if human:
print "Current RAM Usage: %sMB" % ram_used
print "Max RAM API Limit: %sMB" % ram_allowed
if percent_ram_used >= float(alert_percentage):
print "WARNING: Percent of API Limit Used: %d%%" % (
percent_ram_used)
else:
print "OK: Percent of API Limit Used: %0d%%" % percent_ram_used
else:
# Cloud Monitoring-aware output
if percent_ram_used < float(alert_percentage):
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
if __name__ == "__main__":
main()