forked from outlyerapp/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcouchbase.py
More file actions
executable file
·106 lines (79 loc) · 2.7 KB
/
Copy pathcouchbase.py
File metadata and controls
executable file
·106 lines (79 loc) · 2.7 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
#!/usr/bin/env python
import sys
import requests
from requests.auth import HTTPBasicAuth
# settings
USER = ''
PASSWORD = ''
URL = 'http://localhost:8091'
# constants
success_message = "OK | "
failure_message = "FAILED! | "
perf_data = ""
healthy = True
auth = HTTPBasicAuth(USER, PASSWORD)
# functions
def average_list(l):
return reduce(lambda x, y: x + y, l) / float(len(l))
def flatten(structure, key="", path="", flattened=None):
if flattened is None:
flattened = {}
if type(structure) not in (dict, list):
flattened[((path + ".") if path else "") + key] = structure
elif isinstance(structure, list):
if len(structure) > 1:
flattened[path + '.' + key] = average_list(structure)
else:
for i, item in enumerate(structure):
flatten(item, "%d" % i, path + "." + key, flattened)
else:
for new_key, value in structure.items():
flatten(value, new_key, path + "." + key, flattened)
return flattened
def bytes_to_gb(num):
return round(float(num) / 1024 / 1024 / 1024, 2)
# pools
try:
default_pool = requests.get(URL + '/pools/default', auth=auth, timeout=60).json()
except Exception, e:
print "Plugin Failed! Unable to connect to %s: %s" % (URL, e)
sys.exit(2)
# health
for node in default_pool['nodes']:
if node['status'] != 'healthy':
healthy = False
# storage
storage_totals = default_pool['storageTotals']
for k, v in flatten(storage_totals, key='totals', path='storage').iteritems():
if type(v) is int or type(v) is float:
metric_path = k.lower()
perf_data += metric_path + '=' + str(v) + ';;;; '
perf_data += metric_path + '_gb=' + str(bytes_to_gb(v)) + 'GB;;;; '
nodes = default_pool['nodes']
# nodes
for node in nodes:
if node['thisNode']:
for k, v in flatten(node, key='stats', path='node').iteritems():
if type(v) is int or type(v) is float:
metric_path = k.lower()
perf_data += metric_path + '=' + str(v) + ';;;; '
# buckets
try:
buckets = requests.get(URL + '/pools/default/buckets', auth=auth, timeout=60).json()
except:
buckets = []
bucket_names = []
for bucket in buckets:
bucket_names.append(bucket['name'])
for name in bucket_names:
bucket = requests.get(URL + '/pools/default/buckets/%s/stats?zoom=minute' % name, auth=auth, timeout=60).json()
for k, v in flatten(bucket, key=name, path='bucket').iteritems():
if type(v) is int or type(v) is float:
metric_path = k.lower()
perf_data += metric_path + '=' + str(v) + ';;;; '
if healthy:
print success_message + perf_data
sys.exit(0)
else:
print failure_message + perf_data
sys.exit(2)