-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_grains.py
executable file
·81 lines (72 loc) · 2.64 KB
/
check_grains.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
#!/usr/bin/python -u
import os
import sys
from cassandra.cluster import Cluster
from cassandra import ConsistencyLevel
from cassandra.policies import ConstantReconnectionPolicy,DCAwareRoundRobinPolicy,RetryPolicy
from cassandra.query import SimpleStatement
import datetime
import time
import re
from optparse import OptionParser
from metric_groups import groups
def _connect_to_cassandra(keyspace):
"""
Connect to the Cassandra cluster and return the session.
"""
if 'BACKEND_STORAGE_IP' in os.environ:
host = os.environ['BACKEND_STORAGE_IP']
else:
host = '127.0.0.1'
retry=RetryPolicy()
retry.RETRY=10
cluster = Cluster(
['127.0.0.1',host,],
reconnection_policy=ConstantReconnectionPolicy(5.0, 100),
load_balancing_policy=DCAwareRoundRobinPolicy(local_dc="ASH2"),
default_retry_policy=retry
)
session = cluster.connect(keyspace)
session.default_timeout = 99999
session.default_fetch_size = 1000
return session
def days_to_str(from_time,to_time):
dates=''
from_date = datetime.datetime.fromtimestamp(from_time)
to_date = datetime.datetime.fromtimestamp(to_time)
step = datetime.timedelta(days=1)
while from_date <= to_date:
dates = dates + ',' + str(from_date.strftime('%Y%m%d'))
from_date += step
dates=re.sub(r"^,", "", dates)
dates=re.sub(r",$", "", dates)
return dates
def main(argv):
keyspace = 'metrics'
session = _connect_to_cassandra(keyspace)
usage="usage: %prog [options]"
parser = OptionParser(usage)
parser.add_option('-f', '--from', type='int', dest='roll_from', default=int(time.time()-2592000),
help='"from" epoch time, defaults to 0')
parser.add_option('-t', '--to', type='int', dest='roll_to', default=int(time.time()-86401),
help='"to" epoch time, defaults to current time')
parser.add_option('-v', '--verbose', action='store_true', dest='verbose', default=False,
help='View debugging information')
parser.add_option('-x', '--test', action='store_true', dest='test', default=False,
help='Do not execute CQL command, just print')
(options, args) = parser.parse_args()
from_time = int(options.roll_from)
to_time = int(options.roll_to)
dates = days_to_str(from_time,to_time)
for g in groups:
query = "SELECT * FROM "+str(g)+" WHERE date in ("+str(dates)+")"
if options.test==False:
result = session.execute(query)
if options.verbose==True:
print str(query)
for r in result:
print str(r)
else:
print str(query)
if __name__ == '__main__':
main(sys.argv[1:])