Created
January 5, 2017 11:39
-
-
Save brianwells/8bffc8ba491a11694e69fb7ddf389ecf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import socket | |
import time | |
import re | |
import subprocess | |
import pickle | |
import struct | |
def get_volumes(): | |
info = subprocess.Popen(["/usr/sbin/cvadmin", "-e", "select" ], stdout=subprocess.PIPE).communicate()[0].split("\n") | |
p = re.compile(r' \d+>[\*| ](\w+)') | |
return list(set([p.match(x).group(1) for x in info if p.match(x)])) | |
def get_stat(now, volume): | |
# get info from cvadmin | |
tuples = [] | |
info = subprocess.Popen(["/usr/sbin/cvadmin", "-F", volume, "-e", "stat" ], stdout=subprocess.PIPE).communicate()[0].split("\n") | |
prefix = "stornext.volumes.%s" % volume | |
connections = [x for x in info if x.startswith(" Active Connections:")] | |
totalblocks = [x for x in info if x.startswith(" Fs Blocks :")] | |
freeblocks = [x for x in info if x.startswith(" Fs Blocks Free :")] | |
if connections: | |
tuples.append(("%s.connections" % prefix, | |
(now,int(connections[0].split("\t")[1].split(' ')[0])))) | |
if totalblocks: | |
totalblocks = int(totalblocks[0].split("\t")[1].split(' ')[0]) | |
tuples.append(("%s.total_blocks" % prefix,(now,totalblocks))) | |
if freeblocks: | |
freeblocks = int(freeblocks[0].split("\t")[1].split(' ')[0]) | |
tuples.append(("%s.free_blocks" % prefix,(now,freeblocks))) | |
if totalblocks and freeblocks: | |
tuples.append(("%s.percent_used" % prefix,(now,round(100.0 - 100.0*freeblocks/totalblocks,2)))) | |
return tuples | |
def get_pools(now, volume): | |
tuples = [] | |
info = subprocess.Popen(["/usr/sbin/cvadmin", "-F", volume, "-e", "show" ], stdout=subprocess.PIPE).communicate()[0].split("\n") | |
p = re.compile(r'Stripe Group \d+ \[(\w+)\]') | |
pool_names = [p.match(x).group(1) for x in info if p.match(x)] | |
p = re.compile(r' Total Blocks:(\d+) .*Reserved:(\d+) .*Free:(\d+) ') | |
pool_blocks = [p.findall(x)[0] for x in info if p.match(x)] | |
for idx, pool in enumerate(pool_names): | |
blocks = pool_blocks[idx] | |
prefix = "stornext.volumes.%s.pools.%s" % (volume, pool) | |
tuples.append(("%s.total_blocks" % prefix,(now,int(blocks[0])))) | |
tuples.append(("%s.reserved_blocks" % prefix,(now,int(blocks[1])))) | |
tuples.append(("%s.free_blocks" % prefix,(now,int(blocks[2])))) | |
tuples.append(("%s.percent_used" % prefix,(now,round(100.0 - 100.0*int(blocks[2])/int(blocks[0]),2)))) | |
return tuples | |
def run(sock, delay): | |
while True: | |
now = int(time.time()) | |
tuples = [] | |
for volume in get_volumes(): | |
tuples.extend(get_stat(now, volume)) | |
tuples.extend(get_pools(now, volume)) | |
if sock: | |
payload = pickle.dumps(tuples, protocol=2) | |
header = struct.pack("!L", len(payload)) | |
message = header + payload | |
sock.sendall(message) | |
else: | |
# print info | |
print tuples | |
if delay > 0: | |
time.sleep(delay) | |
else: | |
break | |
def main(): | |
delay = 0 | |
sock = None | |
server = None | |
if len(sys.argv) > 1: | |
server = sys.argv[1] | |
delay = 60 | |
if len(sys.argv) > 2: | |
arg = sys.argv[2] | |
if arg.isdigit(): | |
delay = int(arg) | |
else: | |
sys.stderr.write("Invalid delay of '%s' specified. Ignoring..." % arg) | |
if server: | |
sock = socket.socket() | |
try: | |
sock.connect((server, 2004)) | |
except socket.error: | |
raise SystemExit("Could not connect to carbon server %s" % server) | |
try: | |
run(sock, delay) | |
except KeyboardInterrupt: | |
sys.stderr.write("\nExiting...") | |
sys.exit(0) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment