forked from cms-sw/cms-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-node-stats
executable file
·83 lines (74 loc) · 2.4 KB
/
build-node-stats
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
#!/usr/bin/env python
from __future__ import print_function
import sys
sys.exit(0) # TODO script not run anymore?
from _py2with3compatibility import run_cmd
from socket import gethostname
from argparse import ArgumentParser
import time
import pickle
import struct
import socket
def chunks(l, n):
for i in range(0, len(l), n):
yield l[i:i+n]
def format(s, **kwds):
return s % kwds
CARBON_SERVER="localhost"
CARBON_PORT=2004
# 100 metrics at the time
def sendMetrics(metrics):
for l in chunks(metrics, 100):
payload = pickle.dumps(l)
print(len(payload))
header = struct.pack("!L", len(payload))
message = header + payload
sock = socket.socket()
print(CARBON_SERVER, CARBON_PORT)
sock.connect((CARBON_SERVER, CARBON_PORT))
sock.sendall(message)
sock.close()
def aggregate(metrics):
aggregated = {}
for x in metrics:
print(x[0])
if x[0] in list(aggregated.keys()):
aggregated[x[0]][1][1] += x[1][1]
print(x[0],"now", aggregated[x[0]][1][1])
else:
aggregated[x[0]] = x
return [x for x in list(aggregated.values())]
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--dest", dest="server", help="destination carbon server", default="localhost:2004")
opts = parser.parse_args()
if not ":" in opts.server:
opts.dest += ":2004"
CARBON_SERVER, CARBON_PORT = opts.server.split(":", 1)
CARBON_PORT=int(CARBON_PORT)
fields = ("pid", "comm", "pcpu", "rss", "vsize")
cmd = 'ps -U cmsbuild -o %s= -o %s= -o %s= -o %s= -o %s=' % fields
err, out = run_cmd(cmd)
if err:
print("Error while reading process data")
exit(1)
print(out)
metrics = []
timestamp = time.time()
for l in out.split("\n"):
data = dict(list(zip(fields, [x for x in l.split(" ") if x])))
try:
data["pid"] = int(data["pid"])
data["pcpu"] = float(data["pcpu"])
data["rss"] = float(data["rss"])
data["vsize"] = float(data["vsize"])
except:
continue
if data["pcpu"] < 1:
continue
hostname = gethostname().split(".",1)[0]
timestamp = time.time()
metrics.append([".".join(["RunStats", hostname, data["comm"], "pcpu"]), [timestamp, data["pcpu"]]])
metrics.append([".".join(["RunStats", hostname, data["comm"], "rss"]), [timestamp, data["rss"]]])
metrics.append([".".join(["RunStats", hostname, data["comm"], "vsize"]), [timestamp, data["vsize"]]])
sendMetrics(aggregate(metrics))