Skip to content

Instantly share code, notes, and snippets.

@mdamien
Last active June 23, 2016 10:36
Show Gist options
  • Save mdamien/d017a581c3046811e207ba590ff976f4 to your computer and use it in GitHub Desktop.
Save mdamien/d017a581c3046811e207ba590ff976f4 to your computer and use it in GitHub Desktop.
Freeswitch + Sentry
import re
import subprocess
import datetime
import sys
import pickle
from raven import Client
client = Client('your_thing', context='')
SERVER_NAME = subprocess.check_output('hostname')
extra = {}
try:
for command in ('freeswitch -version',
'cat /etc/freeswitch/config.FS0',
'cat /etc/freeswitch/freeswitch.xml'):
extra[command] = subprocess.check_output(command.split())
except Exception as e:
print('FAILED TO GET VERSION:', e)
pass
LAST_CHECKED_FILE = 'last_checked'
# TODO: - include systemd file in report
# - fix possible double reporting of errors (many even in the same microsecond)
def read_last_checked():
try:
ts = float(open(LAST_CHECKED_FILE).read())
time = datetime.datetime.fromtimestamp(ts)
print('LAST CHECKED:', time.strftime('%Y-%m-%d %H:%M:%S'))
return time
except FileNotFoundError as e:
print('FIRST RUN EVAAAARR')
def write_last_checked(time):
print('LAST CHECKED [NEW]:', time.strftime('%Y-%m-%d %H:%M:%S'))
open(LAST_CHECKED_FILE,'w').write(str(time.timestamp()))
def print_dat_args(*args, **kwargs):
print('REPORTT')
print('args:', args)
print('kwargs:', kwargs)
# client.captureMessage = print_dat_args
last_checked = read_last_checked()
date = None
regex = re.compile(r"(?P<date>\S+ \S+) \[(?P<level>\S+)\] (?P<culprit>\S+) (?P<message>.*)")
for line in open(sys.argv[1] if len(sys.argv) > 1 else "/var/log/freeswitch/freeswitch.log"):
matches = regex.match(line)
if matches:
infos = matches.groupdict()
level = infos['level']
message = infos['message']
culprit = infos['culprit']
date = datetime.datetime.strptime(infos['date'], '%Y-%m-%d %H:%M:%S.%f')
if not last_checked or date >= last_checked:
if level in ('ALERT', 'CRIT', 'ERR'):
extra['full_message'] = line
client.captureMessage(message, extra=extra, data={
'server_name': SERVER_NAME,
'environment': '',
'culprit': culprit,
}, date=date)
write_last_checked(date)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment