Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saved search added and tested #5

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
18 changes: 15 additions & 3 deletions hoover/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def emit(self, record):
async_post_to_endpoint(self.endpoint, msg)


class LogglySyslogHandler(SysLogHandler):
class LogglySyslogHandler(logging.handlers.SysLogHandler):
def __init__(self, session=None, port=None, inputname='', input=None,
announce=False, authorize=True, **kwargs):
#TODO: avoid duplication with __init__ above
Expand All @@ -66,7 +66,19 @@ def __init__(self, session=None, port=None, inputname='', input=None,
if ('tcp' in input.service['name'] and sys.version_info >= (2, 7)
and not 'socktype' in kwargs):
kwargs['socktype'] = socket.SOCK_STREAM
self.socktype=socket.SOCK_STREAM
else:
self.socktype=socket.SOCK_DGRAM
self.port = port
session = session or LogglySession
SysLogHandler.__init__(self, address=(session.proxy, port),
self.session = session or LogglySession
SysLogHandler.__init__(self, address=(self.session.proxy, self.port),
**kwargs)

def emit(self, record):
if isinstance(record.msg, (list, dict)):
record.msg = dumps(record.msg, cls=self.json_class, default=str)
msg = self.format(record)
sock=socket.socket(socket.AF_INET,self.socktype)
sock.connect((self.session.proxy,self.port))
sock.sendall(msg+'\n')
sock.close()
39 changes: 38 additions & 1 deletion hoover/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class LogglySession(object):
domain = 'loggly.com'
proxy = 'logs.loggly.com'

ssdict={'order':'order','from':'starttime','rows':'rows','until':'endtime'}
def __init__(self, subdomain, username, password, domain=None, proxy=None,
secure=True):
'''pass in subdomain, username, and password to authorize all API
Expand Down Expand Up @@ -103,6 +103,43 @@ def facets(self, q='*', facetby='date', **kwargs):
kwargs['q'] = q
return self._api_help('api/facets/%s' % facetby, kwargs)

@time_translate
def savedsearch(self,q=""):
"""
Runs one of your saved searches
"""
query=Http(timeout=10)
query.add_credentials(self.username,self.password)
resp, cont=query.request("http://"+self.subdomain+".loggly.com/api/savedsearches","GET")
content=loads(cont)
saved=None
for search in content:
if search['name']==q:
saved=search
if saved==None:
raise ValueError("Your account does not have a search of that name,\
please go to "+self.subdomain+".loggly.com to check your saved searches")
params=saved['context']
opts={}
inputs=""
devices=""
for x in params:
if x!="terms" and x!="inputs" and x!="devices":
opts[self.ssdict[x]]=params[x]
if params['inputs']:
inputs+=" AND (inputname:"+" OR inputname:".join(params['inputs'])+")"
if params['devices']:
devices+=" AND (ip:"+" OR ip:".join(params['devices'])+")"
return self.search(q=params['terms']+inputs+devices,**opts)
def findsavedsearchnames(self):
query=Http(timeout=10)
query.add_credentials(self.username,self.password)
resp, cont=query.request("http://"+self.subdomain+".loggly.com/api/savedsearches","GET")
content=loads(cont)
names=[x['name'] for x in content]
return names


def create_input(self, name, service='syslogudp', description='',
json=False):
'''Creates a new input on your loggly account. Service can be any of:
Expand Down