Skip to content

Instantly share code, notes, and snippets.

@sandlbn
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save sandlbn/0224625fbe5aa3fca911 to your computer and use it in GitHub Desktop.

Select an option

Save sandlbn/0224625fbe5aa3fca911 to your computer and use it in GitHub Desktop.
fsyslog
import time
import socket
import fcntl
import struct
from sys import argv, exit
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def get_hw_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
return ''.join(['%02x' % ord(char) for char in info[18:24]])[:-1]
def putMetric(tsdbHost, port, metric, timestamp, metricValue, tags):
constant="\r\n"
outputStr="put "+metric
outputStr=outputStr+ " "+ str(timestamp)
outputStr=outputStr+ " "+ str(metricValue)
for key, value in tags.iteritems():
outputStr=outputStr+ " "+key+"="+value
outputStr=outputStr+constant
socket.setdefaulttimeout(10)
try:
con=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
con.connect((tsdbHost, port))
#outputStr="put system 1417694790 20.0 type=metric0 host=someIP mac=a4badbf04053 role=somerole hostname=host001\r\n"
con.send(outputStr)
#time.sleep(0.005)
con.close()
print "Metrics trasnmitted to TSDB"
except socket.error, v:
print v
def follow_syslog(syslog_file):
syslog_file.seek(0,2) # Go to the end of the file
while True:
line = syslog_file.readline()
if not line:
time.sleep(0.1) # Sleep briefly
continue
yield line
if __name__ == "__main__":
if len(argv) > 2:
stack_name = argv[1]
tsdbHost = argv[2]
else:
print "please specify stackname as a first argument eg. $./csyslog.py stack_name 10.1.0.81"
exit()
syslog_file= open('/var/log/syslog')
tsdbPort = 8080
inteface = 'eth0'
ip = get_ip_address(inteface)
mac = get_hw_address(inteface)
hostname = socket.gethostname()
tags = {'type': 'frame_drop', 'hostname': hostname, 'host': ip, 'mac':mac, 'role':'vm', 'stackname': stack_name}
counter = 0
timestamp_start = int(time.time())
for line in follow_syslog(syslog_file):
if "late buffer for mux input" in line:
counter+=1
timestamp_end = int(time.time())
if timestamp_end - timestamp_start >= 1:
timestamp = int(time.time())
putMetric(tsdbHost, tsdbPort, 'vlc_metrics', timestamp, counter, tags)
counter = 0
timestamp_start = int(time.time())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment