Created
July 19, 2012 08:07
-
-
Save growse/3141499 to your computer and use it in GitHub Desktop.
Python port of a https://gist.github.com/915862 to send disk stats in solaris to a remote graphite server.
This file contains 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 | |
""" | |
Run this with something like iostat -nx 60 2 | iostat_to_graphite.py | |
""" | |
import sys, os, re, socket, time | |
"""ARGF from Ruby in Python. | |
Released into the public domain by Andrew Gwozdziewycz, 2010 | |
""" | |
class _ARGF(object): | |
def __init__(self): | |
self.lineno = 0 | |
self.file = None | |
def __iter__(self): | |
return self.next() | |
def next(self): | |
files = filter(os.path.isfile, sys.argv[1:]) | |
pairs = [(f, open(f)) for f in files] \ | |
if files else [('STDIN', sys.stdin)] | |
for name, fobj in pairs: | |
self.file = name | |
for line in fobj.xreadlines(): | |
self.file = 'STDIN' | |
self.lineno += 1 | |
yield line | |
ARGF = _ARGF() | |
TCP_IP='192.168.0.14' | |
TCP_PORT=2003 | |
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
s.connect((TCP_IP,TCP_PORT)) | |
firstheaderflag=False | |
secondheaderflag=False | |
for line in ARGF: | |
parts = re.findall(r'[a-zA-Z0-9\.]+', line) | |
prefix = "iostat.%s"%os.uname()[1] | |
if "extended device statistics" in line: | |
if firstheaderflag: | |
secondheaderflag=True | |
else: | |
firstheaderflag=True | |
timestamp=int(time.time()) | |
if len(parts) == 11 and secondheaderflag: | |
device = parts[10] | |
s.sendall("%s.%s.iops.reads %s %s\n"%(prefix,device,parts[0],timestamp)) | |
s.sendall("%s.%s.iops.writes %s %s\n"%(prefix,device,parts[1],timestamp)) | |
s.sendall("%s.%s.bps.reads %s %s\n"%(prefix,device,float(parts[2])*1024,timestamp)) | |
s.sendall("%s.%s.bps.writes %s %s\n"%(prefix,device,float(parts[3])*1024,timestamp)) | |
s.sendall("%s.%s.latency %s %s\n"%(prefix,device,parts[7],timestamp)) | |
s.sendall("%s.%s.pc_wait %s %s\n"%(prefix,device,parts[8],timestamp)) | |
s.sendall("%s.%s.pc_busy %s %s\n"%(prefix,device,parts[9],timestamp)) | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment