-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.py
More file actions
209 lines (185 loc) · 7.01 KB
/
Copy pathcache.py
File metadata and controls
209 lines (185 loc) · 7.01 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python
import socket
import sys
import time
class Cache(object):
def __init__(self, host='127.0.0.1', port=2003, max_size=120):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.max_size = max_size
self.counter_cache = {}
self.timer_cache = {}
self.percenter_cache = {}
self.connect()
def connect(self):
self.sock.connect((self.host, self.port))
def reconnect(self):
try:
self.sock.close()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect()
except Exception:
pass
def write_graphite(self, msg):
ip = '10.77.96.122'
port = 2003
try:
self.graphite_sock.send(msg)
except Exception:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
sock.send(msg)
self.graphite_sock = sock
def incr(self, key, timestamp, value=1):
if self.counter_cache.has_key(timestamp):
if self.counter_cache[timestamp].has_key(key):
self.counter_cache[timestamp][key] += value
else:
self.counter_cache[timestamp][key] = value
else:
if self.is_full('counter'):
self.send('counter')
self.counter_cache[timestamp] = {}
self.counter_cache[timestamp][key] = value
def create_timer(self, value, num=1):
timer = {}
timer['count'] = num
timer['sum'] = value
return timer
def create_percenter(self, value):
percenter = {}
percenter['list'] = []
percenter['list'].append(value)
return percenter
def timing(self, key, timestamp, value, num=1):
if self.timer_cache.has_key(timestamp):
if self.timer_cache[timestamp].has_key(key):
self.timer_cache[timestamp][key]['count'] += num
self.timer_cache[timestamp][key]['sum'] += value
else:
new_timer = self.create_timer(value, num)
self.timer_cache[timestamp][key] = {}
self.timer_cache[timestamp][key] = new_timer
else:
if self.is_full('timer'):
self.send('timer')
new_timer = self.create_timer(value, num)
self.timer_cache[timestamp] = {}
self.timer_cache[timestamp][key] = {}
self.timer_cache[timestamp][key] = new_timer
def percentile(self, key, timestamp, value):
if self.percenter_cache.has_key(timestamp):
if self.percenter_cache[timestamp].has_key(key):
self.percenter_cache[timestamp][key]['list'].append(value)
else:
new_percenter = self.create_percenter(value)
self.percenter_cache[timestamp][key] = {}
self.percenter_cache[timestamp][key] = new_percenter
else:
if self.is_full('percenter'):
self.send('percenter')
new_percenter = self.create_percenter(value)
self.percenter_cache[timestamp] = {}
self.percenter_cache[timestamp][key] = {}
self.percenter_cache[timestamp][key] = new_percenter
def is_full(self, category):
if category == 'counter':
if len(self.counter_cache) == self.max_size:
return True
elif category == 'timer':
if len(self.timer_cache) == self.max_size:
return True
elif category == 'percenter':
if len(self.percenter_cache) == self.max_size:
return True
return False
def counter_format(self, timestamp, item):
msg = ''
keys = item.keys()
for key in keys:
value = item[key]
line = key + ' ' + str(value) + ' ' + str(timestamp) + '\n'
if msg:
msg += line
else:
msg = line
return msg
def timer_format(self, timestamp, item):
msg = ''
keys = item.keys()
for key in keys:
count = item[key]['count']
sum = item[key]['sum']
value = float(sum) / float(count)
line = key + '.mean ' + str(value) + ' ' + str(timestamp) + '\n'
if msg:
msg += line
else:
msg = line
return msg
def percenter_format(self, timestamp, item):
msg = ''
keys = item.keys()
for key in keys:
list = item[key]['list']
length = len(list)
list.sort()
line = ''
percent50 = int(length*0.5) - 1
line += key + '.50% ' + str(list[percent50]) + ' ' + str(timestamp) + '\n'
percent70 = int(length*0.7) - 1
line += key + '.70% ' + str(list[percent70]) + ' ' + str(timestamp) + '\n'
percent90 = int(length*0.9) - 1
line += key + '.90% ' + str(list[percent90]) + ' ' + str(timestamp) + '\n'
percent99 = int(length*0.99) - 1
line += key + '.99% ' + str(list[percent99]) + ' ' + str(timestamp) + '\n'
percent_min = 0
line += key + '.min ' + str(list[percent_min]) + ' ' + str(timestamp) + '\n'
percent_max = length - 1
line += key + '.max ' + str(list[percent_max]) + ' ' + str(timestamp) + '\n'
mean = sum(list) / length
line += key + '.mean ' + str(mean) + ' ' + str(timestamp) + '\n'
msg += line
return msg
def send(self, category):
if category == 'counter':
cache = self.counter_cache
elif category == 'timer':
cache = self.timer_cache
elif category == 'percenter':
cache = self.percenter_cache
keys = cache.keys()
keys.sort()
for key in keys:
if len(cache) <= (self.max_size/2):
break
item = cache.pop(key)
if category == 'counter':
msg = self.counter_format(key, item)
elif category == 'timer':
msg = self.timer_format(key, item)
elif category == 'percenter':
msg = self.percenter_format(key, item)
if category == 'percenter':
self.write_graphite(msg)
else:
try:
self.sock.send(msg)
except Exception:
self.reconnect()
class vCache(Cache):
def __init__(self, host='10.77.96.122', port=33333, max_size=60):
Cache.__init__(self, host, port, max_size)
def timer_format(self, timestamp, item):
msg = ''
keys = item.keys()
for key in keys:
count = item[key]['count']
sum = item[key]['sum']
line = key + ' ' + str(count) + ' ' + str(sum) + ' ' + str(timestamp) + '\n'
if msg:
msg += line
else:
msg = line
return msg