forked from outlyerapp/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrackspace.py
More file actions
111 lines (78 loc) · 3.15 KB
/
rackspace.py
File metadata and controls
111 lines (78 loc) · 3.15 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
from rackspace_monitoring.providers import get_driver
from rackspace_monitoring.types import Provider
import sys
import socket
import time
"""
Rackspace Cloud Monitor Metrics Nagios Plugin
Description:
Pulls in Rackspace Cloud Monitoring metrics from Rackspace's Cloud Monitoring API for the instance the script is working
on
Setup:
You need to install the Raxmon client (http://www.rackspace.com/knowledge_center/article/getting-started-with-rackspace-monitoring-cli)
Use the command:
sudo pip install rackspace-monitoring-cli
And run this plugin in the 'System Default' shell to work properly. You will also need to apply your Rackspace cloud console username
and API Key (founder under Account Settings in your Cloud Console) to the top of the script
"""
# Set Variables to access Rackspace Rest APIs
RS_USERNAME = 'USERNAME'
RS_APIKEY = 'API KEY'
# Use this to override the hostname to get metrics for, for testing. Leave blank if using OS hostname
OVERRIDE_HOSTNAME = ''
# Main method
def main():
entity = _get_entity()
values = _get_metric_values(entity)
output = 'OK | '
for key, value in values.iteritems():
output = output + key + "={};;;; ".format(value)
print output
sys.exit(0)
# Get the API Entity ID for the current host running the script
def _get_entity():
hostname = OVERRIDE_HOSTNAME
if OVERRIDE_HOSTNAME == '':
hostname = socket.gethostname()
driver = _get_driver()
results = driver.list_entities()
for entity in results:
if entity.label == hostname:
return driver.get_entity(entity_id= entity.id)
return None
# List alarms created by checks on Rackspace sever
def _get_metric_values(entity):
driver = _get_driver()
checks = _list_entity_checks(entity)
values = {}
to_timestamp = str(int(time.time()) * 1000);
from_timestamp = str((int(time.time()) * 1000) - 60000)
# Iterate through all the checks for an instance
for check in checks:
metrics = driver.list_metrics(entity_id=entity.id, check_id=check.id)
# Iterate through all the metrics for the check
for metric in metrics:
datapoint = driver.fetch_data_point(entity_id=entity.id, check_id=check.id, metric_name=metric.name,
from_timestamp=from_timestamp, to_timestamp=to_timestamp)
if not datapoint:
values[check.id + "." + metric.name] = 0
else:
values[check.id + "." + metric.name] = datapoint[0].average
return values
# List checks running for a particular Rackspace server
def _list_entity_checks(entity):
driver = _get_driver()
checks = driver.list_checks(entity=entity)
return checks
# Get a Rackspace API driver to make requests to APIs
def _get_driver():
driver = get_driver(Provider.RACKSPACE)
kwargs = {}
kwargs['ex_force_base_url'] = 'https://monitoring.api.rackspacecloud.com/v1.0'
kwargs['ex_force_auth_url'] = 'https://identity.api.rackspacecloud.com/v2.0/tokens'
kwargs['ex_force_auth_token'] = None
instance = driver(RS_USERNAME, RS_APIKEY, **kwargs)
return instance
# Run main
main()