-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-measurer.py
56 lines (42 loc) · 1.55 KB
/
time-measurer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# запускает переданную программу со всеми переданными ключами, меряет время выполнения и шлет в графит
# !!!important | - не обрабатывается
# 12/10/2014 Anatoly Burtsev [email protected]
#TODO read GRAPHITE_HOST and PORT from config
import os
import sys
import time
import subprocess as sb
import socket
GRAPHITE_HOST = "localhost"
GRAPHITE_PORT = 2003
HOST = socket.gethostname().split('.')[0]
if len(sys.argv) == 1:
exit(0)
command = sys.argv[1:]
script_name_position=1
# skip "/usr/bin/flock -w 0 /tmp/.flock_tmp"
# may be need improve
if sys.argv[script_name_position] == "/usr/bin/flock":
script_name_position = 5
SCRIPT = sys.argv[script_name_position]
#/usr/bin/gdb -> gdb
SCRIPT=SCRIPT.split('/')[-1]
#special for differ "get_smth.py thing1" and "get_smth.py thing2"
if len(sys.argv[script_name_position:]) >= 2 and sys.argv[script_name_position+1][0] not in '0123456789-':
SCRIPT = SCRIPT + '_' + sys.argv[script_name_position+1]
#delele extenstion
SCRIPT = SCRIPT.replace('.sh','').replace('.py','')
P = sb.Popen( command, stdout=sb.PIPE, stderr=sb.PIPE )
T0 = time.time()
out, err = P.communicate()
dT = time.time() - T0
sys.stdout.write(out)
sys.stderr.write(err)
#send to graphite
MESSAGE = 'stats.timemeasurer.%s.%s %d %d\n' % (HOST, SCRIPT, int(dT), int(time.time()))
#print(MESSAGE)
sock = socket.create_connection( (GRAPHITE_HOST, GRAPHITE_PORT))
sock.sendall( MESSAGE )
sock.close()