Skip to content

Instantly share code, notes, and snippets.

@growse
Created July 24, 2012 10:03
Show Gist options
  • Save growse/3169195 to your computer and use it in GitHub Desktop.
Save growse/3169195 to your computer and use it in GitHub Desktop.
Parse kstat -p output and fling it at graphite.
#!/usr/bin/python
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))
for line in ARGF:
parts = line.strip().split()
if len(parts)==2 and parts[1].isdigit():
nameparts = parts[0].split(':')
prefix = "%s.%s.%s"%(nameparts[0],os.uname()[1],nameparts[-1])
timestamp=int(time.time())
s.sendall("%s %d %d\n"%(prefix,int(parts[1]),timestamp))
#print "%s %d %d\n"%(prefix,int(parts[1]),timestamp)
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment