-
Notifications
You must be signed in to change notification settings - Fork 65
/
etcd.py
executable file
·98 lines (67 loc) · 2.58 KB
/
etcd.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
91
92
93
94
95
96
97
98
#!/usr/bin/env python
"""
Rackspace Cloud Monitoring plugin for etcd node stats.
Example:
$ ./etcd.py --url http://localhost:4001
Example alarm criteria:
if (metric['state'] != 'follower' && metric['state'] != 'leader') {
return new AlarmStatus(CRITICAL, 'Node is neither leader nor follower.');
}
if (metric['state'] == 'follower') {
return new AlarmStatus(OK, 'Node is following #{leader}.');
}
if (metric['state'] == 'leader') {
return new AlarmStatus(OK, 'Node is leading the cluster.');
}
Copyright 2014 Simon Vetter <[email protected]>
Based on Victor Watkins' elasticsearch plugin:
Copyright 2013 Victor Watkins <[email protected]>
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 urllib2
import json
from sys import exit
from optparse import OptionParser, OptionGroup
STATUS_OK = "status etcd returned a response"
def bug_out(why):
'''Something went wrong. Tell the agent what, then die.'''
print "status", why
exit(1)
def call_to_server(url, path):
'''Call a given path to the server and return JSON.'''
try:
r = urllib2.urlopen('{u}{p}'.format(u=url, p=path))
except (urllib2.URLError, ValueError) as e:
bug_out(e)
try:
response = json.loads(r.read())
except Exception as e: # improve this...
bug_out(e)
return response
def get_stats(url):
'''Return a dict of stats from /v2/stats/self'''
s = call_to_server(url, '/v2/stats/self')
# i've seen etcd return {"state":""}, so make sure the agent accepts it
if not s['state']:
s['state'] = "unknown"
print STATUS_OK
print "metric state string", s['state']
print "metric leader string", s['leaderInfo']['leader']
print "metric recvAppendRequestCnt uint64", s['recvAppendRequestCnt']
print "metric sendAppendRequestCnt uint64", s['sendAppendRequestCnt']
exit(0)
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("--url",
action="store", type="string", dest="url",
default="http://localhost:4001")
(options, args) = parser.parse_args()
get_stats(parser.values.url);