Exemplo n.º 1
0
    def send_data(self, eventdata, spooledevent=False):
        # only send if there is data to be sent
        success = None
        if eventdata:
            tries = 0
            r = None
            # If at first you don't succeed, try again. And again, and again, and again, and again.
            # Server must return 200 OK or we will assume the delivery failed.
#            while tries <= 5:
            try:
                headers = {'User-Agent': self.useragent,
                'Content-Type': 'application/json'}
                url = self.send_to
                if self.key:
                    r = requests.post(url, headers=headers, data=json.dumps(eventdata), 
                        verify=self.verify, cert=(self.cert, self.key))
                elif url.startswith('https'):
                    r = requests.post(url, headers=headers, data=json.dumps(eventdata), verify=self.verify)
                else:
                    r = requests.post(url, headers=headers, data=json.dumps(eventdata))
                success = r.status_code
                if not r.status_code == 200:
                    if not spooledevent:
                        spooler.spool_data(json.dumps(eventdata))
                        errorstr = "Warning: server unable to accept events. Spooling events to file."
                        print >> sys.stderr, errorstr
                else:
                    self.write_last_event(eventdata)
            except ConnectionError as e:
                if not spooledevent:
                    spooler.spool_data(json.dumps(eventdata))
                    errorstr = "Warning: could not reach {}.".format(self.send_to)
                    print >> sys.stderr, errorstr
        return success
Exemplo n.º 2
0
 def on_failed_send(eventdata):
     spooler.spool_data(json.dumps(eventdata))