Ejemplo n.º 1
0
    def submitplatform(self,f):
        """ submit platform details """
        try:
            # tokenize
            ds = nmp.data2dict(nmp.tokenize(f),'PLATFORM')

            sql = """
                   insert into platform (sid,os,dist,version,name,kernel,machine,
                                         pyvers,pycompiler,pylibcvers,pybits,
                                         pylinkage,region)
                   values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
                  """
            self._curs.execute(sql,(self._sid,ds['os'],ds['dist'],ds['osv'],
                                    ds['name'],ds['kernel'],ds['machine'],ds['pyv'],
                                    ds['compiler'],ds['libcv'],ds['bits'],ds['link'],
                                    ds['regdom']))
        except nmp.NMPException as e:
            raise NidusDBSubmitParseException("submit platform %s" % e)
        except psql.Error as e:
            # roll back
            self._conn.rollback()
            raise NidusDBSubmitException("submit platform %s: %s" % (e.pgcode,e.pgerror))
        except Exception as e:
            # blanket exception
            self._conn.rollback()
            raise NidusDBSubmitException("submit platform type(%s) %s" % (type(e),e.__repr__()))
        else:
            self._conn.commit()
Ejemplo n.º 2
0
    def submitplatform(self, f):
        """ submit platform details """
        try:
            # tokenize
            ds = nmp.data2dict(nmp.tokenize(f), 'PLATFORM')

            sql = """
                   insert into platform (sid,os,dist,version,name,kernel,machine,
                                         pyvers,pycompiler,pylibcvers,pybits,
                                         pylinkage,region)
                   values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
                  """
            self._curs.execute(
                sql, (self._sid, ds['os'], ds['dist'], ds['osv'], ds['name'],
                      ds['kernel'], ds['machine'], ds['pyv'], ds['compiler'],
                      ds['libcv'], ds['bits'], ds['link'], ds['regdom']))
        except nmp.NMPException as e:
            raise NidusDBSubmitParseException("submit platform %s" % e)
        except psql.Error as e:
            # roll back
            self._conn.rollback()
            raise NidusDBSubmitException("submit platform %s: %s" %
                                         (e.pgcode, e.pgerror))
        except Exception as e:
            # blanket exception
            self._conn.rollback()
            raise NidusDBSubmitException("submit platform type(%s) %s" %
                                         (type(e), e.__repr__()))
        else:
            self._conn.commit()
Ejemplo n.º 3
0
 def submitantenna(self, f):
     """ submitantenna - submit the string fields to the database """
     try:
         # tokenize f and convert to dict and insert into db
         ds = nmp.data2dict(nmp.tokenize(f), 'ANTENNA')
         sql = """
                insert into antenna (mac,ind,gain,loss,x,y,z,type,ts)
                values (%s,%s,%s,%s,%s,%s,%s,%s,%s);
               """
         self._curs.execute(
             sql, (ds['mac'], ds['index'], ds['gain'], ds['loss'], ds['x'],
                   ds['y'], ds['z'], ds['type'], ds['ts']))
     except nmp.NMPException as e:
         raise NidusDBSubmitParseException("submit antenna %s" % e)
     except psql.Error as e:
         self._conn.rollback()
         raise NidusDBSubmitException("submit antenna %s: %s" %
                                      (e.pgcode, e.pgerror))
     except Exception as e:
         # blannket
         self._conn.rollback()
         raise NidusDBSubmitException("submit antenna type(%s) %s" %
                                      (type(e), e.__repr__()))
     else:
         self._conn.commit()
Ejemplo n.º 4
0
    def submitgpsd(self,f):
        """ submitgpsd - submit the string fields to the database """
        try:
            # tokenize the string f and convert to dict
            ds = nmp.data2dict(nmp.tokenize(f),'GPSD')

            # submit to db
            # insert into the gpsd table (if it does not already exist)
            self._curs.execute("select gpsd_id from gpsd where devid=%s;",(ds['id'],))
            row = self._curs.fetchone()
            if not row:
                sql = """
                       insert into gpsd (devid,version,flags,driver,bps,tty)
                       values (%s,%s,%s,%s,%s,%s) returning gpsd_id;
                      """
                self._curs.execute(sql,(ds['id'],ds['vers'],ds['flags'],
                                        ds['driver'],ds['bps'],ds['path']))
                self._gid = self._curs.fetchone()[0]
            else:
                self._gid = row[0]

            # insert into the using table
            sql = """
                  insert into using_gpsd (sid,gid,period)
                  values (%s,%s,tstzrange(%s,NULL,'[]'));
                """
            self._curs.execute(sql,(self._sid,self._gid,ds['ts']))
        except nmp.NMPException as e:
            raise NidusDBSubmitParseException("submit gspd %s" % e)
        except psql.Error as e:
            # rollback so postgresql is not left in error state
            self._conn.rollback()
            raise NidusDBSubmitException("submit gpsde %s: %s" % (e.pgcode,e.pgerror))
        else:
            self._conn.commit()
Ejemplo n.º 5
0
    def submitflt(self, f):
        """ submitflt - submit the string fields to the database """
        try:
            # tokenize the string f and convert to dict, convert lat/lon to mgrs
            ds = nmp.data2dict(nmp.tokenize(f), 'FLT')

            # submit to db
            sql = """
                   insert into flt (sid,ts,coord,alt,spd,dir,
                                    fix,xdop,ydop,pdop,epx,epy)
                   values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
                  """
            self._curs.execute(sql,
                               (self._sid, ds['ts'], ds['coord'], ds['alt'],
                                ds['spd'], ds['dir'], ds['fix'], ds['xdop'],
                                ds['ydop'], ds['pdop'], ds['epx'], ds['epy']))
        except nmp.NMPException as e:
            raise NidusDBSubmitParseException("submit flt %s" % e)
        except psql.Error as e:
            self._conn.rollback()
            raise NidusDBSubmitException("submit flt %s: %s" %
                                         (e.pgcode, e.pgerror))
        except Exception as e:
            # blanket
            self._conn.rollback()
            raise NidusDBSubmitException("submit flt type(%s) %s" %
                                         (type(e), e.__repr__()))
        else:
            self._conn.commit()
Ejemplo n.º 6
0
    def submitflt(self,f):
        """ submitflt - submit the string fields to the database """
        try:
            # tokenize the string f and convert to dict, convert lat/lon to mgrs
            ds = nmp.data2dict(nmp.tokenize(f),'FLT')

            # submit to db
            sql = """
                   insert into flt (sid,ts,coord,alt,spd,dir,
                                    fix,xdop,ydop,pdop,epx,epy)
                   values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);
                  """
            self._curs.execute(sql,(self._sid,ds['ts'],ds['coord'],ds['alt'],
                                    ds['spd'],ds['dir'],ds['fix'],ds['xdop'],
                                    ds['ydop'],ds['pdop'],ds['epx'],ds['epy']))
        except nmp.NMPException as e:
            raise NidusDBSubmitParseException("submit flt %s" % e)
        except psql.Error as e:
            self._conn.rollback()
            raise NidusDBSubmitException("submit flt %s: %s" % (e.pgcode,e.pgerror))
        except Exception as e:
            # blanket
            self._conn.rollback()
            raise NidusDBSubmitException("submit flt type(%s) %s" % (type(e),e.__repr__()))
        else:
            self._conn.commit()
Ejemplo n.º 7
0
 def submitsingle(self, f):
     """ submitsingle - submit the string fields (single frame) to the database """
     # tokenize the data
     try:
         ds = nmp.data2dict(nmp.tokenize(f), 'FRAME')
     except nmp.NMPException as e:
         raise NidusDBSubmitParseException(e)
     self._submitframe(ds['mac'], ds['ts'], ds['frame'])
Ejemplo n.º 8
0
 def submitsingle(self,f):
     """ submitsingle - submit the string fields (single frame) to the database """
     # tokenize the data
     try:
         ds = nmp.data2dict(nmp.tokenize(f),'FRAME')
     except nmp.NMPException as e:
         raise NidusDBSubmitParseException(e)
     self._submitframe(ds['mac'],ds['ts'],ds['frame'])
Ejemplo n.º 9
0
    def submitdevice(self, f, ip=None):
        """ submitdevice - submit the string fields to the database """
        try:
            # tokenize the string f, convert to dict
            ds = nmp.data2dict(nmp.tokenize(f), 'DEVICE')

            # what type of device -> note: radio and gpsd are set 'up
            # in submitradio and submitgpsd
            if not ds['type'] in ['sensor', 'radio', 'gpsd']:
                raise NidusDBSubmitParamException("Invalid Device type")
            if ds['type'] == 'sensor':
                try:
                    self._setsensor(ds['ts'], ds['id'], ds['state'], ip)
                except sse.SSEDBException as e:
                    # a thread failed to connect - reraise as dbsubmitexception
                    self._conn.rollback()
                    raise NidusDBSubmitException("submit device %s" %
                                                 e.__repr__())
                except psql.Error as e:
                    # most likely a sensor attempting to log in when it is
                    # data from it's most recent connect is still being processed
                    # NOTE: a runtimeerror will result in the nidus responehandler
                    #  attempting to submitdropped(). Since no sid was attained
                    #  this->submitdropped() will return without effect
                    self._conn.rollback()
                    raise RuntimeError("submit device %s: %s" %
                                       (e.pgcode, e.pgerror))
            elif ds['type'] == 'radio':
                self._setradio(ds['ts'], ds['id'], ds['state'])
            elif ds['type'] == 'gpsd':
                self._setgpsd(ds['ts'], ds['state'])
        except nmp.NMPException as e:
            raise NidusDBSubmitParseException(e)
        except psql.Error as e:
            # rollback so postgresql is not left in error state
            self._conn.rollback()
            raise NidusDBSubmitException("submit device %s: %s" %
                                         (e.pgcode, e.pgerror))
        except Exception as e:
            # blanket except
            self._conn.rollback()
            raise NidusDBSubmitException("submit device type(%s) %s" %
                                         (type(e), e.__repr__()))
        else:
            self._conn.commit()
Ejemplo n.º 10
0
 def submitbulk(self,f):
     """ submitbulk - submit string fields (multiple frames) to the database """
     # tokenize the data & decompress the frames
     try:
         ds = nmp.data2dict(nmp.tokenize(f),'BULK')
         frames = zlib.decompress(ds['frames'])
         mac = ds['mac']
         fs = frames.split('\x1FFE\x1E') # split by end delimiter
         for f in fs:
             if not f: continue
             f = f.split('\x1EFB\x1F')
             self._submitframe(mac,f[0].strip(),f[1])
     except nmp.NMPException as e:
         raise NidusDBSubmitParseException("submit bulk %s" % e)
     except zlib.error as e:
         raise NidusDBSubmitParseException("submit bulk zip error %s" % e)
     except Exception as e:
         # blanket
         self._conn.rollback()
         raise NidusDBSubmitException("submit bulk type(%s) %s" % (type(e),e.__repr__()))
Ejemplo n.º 11
0
 def submitradioevent(self,f):
     """ submitradioevent - submit the string fields to the database """
     try:
         # tokenize the string f and convert to dict and insert into db
         ds = nmp.data2dict(nmp.tokenize(f),'RADIO_EVENT')
         sql = """
                insert into radio_event (mac,state,params,ts)
                values (%s,%s,%s,%s);
               """
         self._curs.execute(sql,(ds['mac'],ds['event'],ds['params'],ds['ts']))
     except nmp.NMPException as e:
         raise NidusDBSubmitParseException("submit radio event" % e)
     except psql.Error as e:
         self._conn.rollback()
         raise NidusDBSubmitException("submit radio event %s: %s" % (e.pgcode,e.pgerror))
     except Exception as e:
         # blannket
         self._conn.rollback()
         raise NidusDBSubmitException("submit radio event type(%s) %s" % (type(e),e.__repr__()))
     else:
         self._conn.commit()
Ejemplo n.º 12
0
 def submitbulk(self, f):
     """ submitbulk - submit string fields (multiple frames) to the database """
     # tokenize the data & decompress the frames
     try:
         ds = nmp.data2dict(nmp.tokenize(f), 'BULK')
         frames = zlib.decompress(ds['frames'])
         mac = ds['mac']
         fs = frames.split('\x1FFE\x1E')  # split by end delimiter
         for f in fs:
             if not f: continue
             f = f.split('\x1EFB\x1F')
             self._submitframe(mac, f[0].strip(), f[1])
     except nmp.NMPException as e:
         raise NidusDBSubmitParseException("submit bulk %s" % e)
     except zlib.error as e:
         raise NidusDBSubmitParseException("submit bulk zip error %s" % e)
     except Exception as e:
         # blanket
         self._conn.rollback()
         raise NidusDBSubmitException("submit bulk type(%s) %s" %
                                      (type(e), e.__repr__()))
Ejemplo n.º 13
0
    def submitdevice(self,f,ip=None):
        """ submitdevice - submit the string fields to the database """
        try:
            # tokenize the string f, convert to dict
            ds = nmp.data2dict(nmp.tokenize(f),'DEVICE')

            # what type of device -> note: radio and gpsd are set 'up
            # in submitradio and submitgpsd
            if not ds['type'] in ['sensor','radio','gpsd']:
                raise NidusDBSubmitParamException("Invalid Device type")
            if ds['type'] == 'sensor':
                try:
                    self._setsensor(ds['ts'],ds['id'],ds['state'],ip)
                except sse.SSEDBException as e:
                    # a thread failed to connect - reraise as dbsubmitexception
                    self._conn.rollback()
                    raise NidusDBSubmitException("submit device %s" % e.__repr__())
                except psql.Error as e:
                    # most likely a sensor attempting to log in when it is
                    # data from it's most recent connect is still being processed
                    # NOTE: a runtimeerror will result in the nidus responehandler
                    #  attempting to submitdropped(). Since no sid was attained
                    #  this->submitdropped() will return without effect
                    self._conn.rollback()
                    raise RuntimeError("submit device %s: %s" % (e.pgcode,e.pgerror))
            elif ds['type'] == 'radio': self._setradio(ds['ts'],ds['id'],ds['state'])
            elif ds['type'] == 'gpsd': self._setgpsd(ds['ts'],ds['state'])
        except nmp.NMPException as e:
            raise NidusDBSubmitParseException(e)
        except psql.Error as e:
            # rollback so postgresql is not left in error state
            self._conn.rollback()
            raise NidusDBSubmitException("submit device %s: %s" % (e.pgcode,e.pgerror))
        except Exception as e:
            # blanket except
            self._conn.rollback()
            raise NidusDBSubmitException("submit device type(%s) %s" % (type(e),e.__repr__()))
        else:
            self._conn.commit()
Ejemplo n.º 14
0
 def submitantenna(self,f):
     """ submitantenna - submit the string fields to the database """
     try:
         # tokenize f and convert to dict and insert into db
         ds = nmp.data2dict(nmp.tokenize(f),'ANTENNA')
         sql = """
                insert into antenna (mac,ind,gain,loss,x,y,z,type,ts)
                values (%s,%s,%s,%s,%s,%s,%s,%s,%s);
               """
         self._curs.execute(sql,(ds['mac'],ds['index'],ds['gain'],ds['loss'],
                                 ds['x'],ds['y'],ds['z'],ds['type'],ds['ts']))
     except nmp.NMPException as e:
         raise NidusDBSubmitParseException("submit antenna %s" % e)
     except psql.Error as e:
         self._conn.rollback()
         raise NidusDBSubmitException("submit antenna %s: %s" % (e.pgcode,e.pgerror))
     except Exception as e:
         # blannket
         self._conn.rollback()
         raise NidusDBSubmitException("submit antenna type(%s) %s" % (type(e),e.__repr__()))
     else:
         self._conn.commit()
Ejemplo n.º 15
0
    def submitgpsd(self, f):
        """ submitgpsd - submit the string fields to the database """
        try:
            # tokenize the string f and convert to dict
            ds = nmp.data2dict(nmp.tokenize(f), 'GPSD')

            # submit to db
            # insert into the gpsd table (if it does not already exist)
            self._curs.execute("select gpsd_id from gpsd where devid=%s;",
                               (ds['id'], ))
            row = self._curs.fetchone()
            if not row:
                sql = """
                       insert into gpsd (devid,version,flags,driver,bps,tty)
                       values (%s,%s,%s,%s,%s,%s) returning gpsd_id;
                      """
                self._curs.execute(sql, (ds['id'], ds['vers'], ds['flags'],
                                         ds['driver'], ds['bps'], ds['path']))
                self._gid = self._curs.fetchone()[0]
            else:
                self._gid = row[0]

            # insert into the using table
            sql = """
                  insert into using_gpsd (sid,gid,period)
                  values (%s,%s,tstzrange(%s,NULL,'[]'));
                """
            self._curs.execute(sql, (self._sid, self._gid, ds['ts']))
        except nmp.NMPException as e:
            raise NidusDBSubmitParseException("submit gspd %s" % e)
        except psql.Error as e:
            # rollback so postgresql is not left in error state
            self._conn.rollback()
            raise NidusDBSubmitException("submit gpsde %s: %s" %
                                         (e.pgcode, e.pgerror))
        else:
            self._conn.commit()
Ejemplo n.º 16
0
 def submitradioevent(self, f):
     """ submitradioevent - submit the string fields to the database """
     try:
         # tokenize the string f and convert to dict and insert into db
         ds = nmp.data2dict(nmp.tokenize(f), 'RADIO_EVENT')
         sql = """
                insert into radio_event (mac,state,params,ts)
                values (%s,%s,%s,%s);
               """
         self._curs.execute(
             sql, (ds['mac'], ds['event'], ds['params'], ds['ts']))
     except nmp.NMPException as e:
         raise NidusDBSubmitParseException("submit radio event" % e)
     except psql.Error as e:
         self._conn.rollback()
         raise NidusDBSubmitException("submit radio event %s: %s" %
                                      (e.pgcode, e.pgerror))
     except Exception as e:
         # blannket
         self._conn.rollback()
         raise NidusDBSubmitException("submit radio event type(%s) %s" %
                                      (type(e), e.__repr__()))
     else:
         self._conn.commit()
Ejemplo n.º 17
0
    def submitradio(self,f):
        """ submitradio - submit the string fields to the database """
        try:
            # tokenize the string f and convert to dict
            ds = nmp.data2dict(nmp.tokenize(f),'RADIO')

            # submit to db
            # insert radio into radio table if it does not exist
            self._curs.execute("select * from radio where mac=%s;",(ds['mac'],))
            if not self._curs.fetchone():
                # truncate the strings to max length 20
                sql = """
                       insert into radio (mac,driver,chipset,channels,standards,description)
                       values (%s,%s,%s,%s,%s,%s);
                      """
                self._curs.execute(sql,(ds['mac'],ds['driver'][0:20],
                                        ds['chipset'][0:20],ds['channels'],
                                        ds['standards'][0:20],ds['desc'][0:100]))

            # insert using_radio
            sql = """
                   insert into using_radio (sid,mac,phy,nic,vnic,role,period)
                   values (%s,%s,%s,%s,%s,%s,tstzrange(%s,NULL,'[]'));
                  """
            self._curs.execute(sql,(self._sid,ds['mac'],ds['phy'],ds['nic'],
                                    ds['vnic'],ds['role'],ds['ts']))

            # insert initial events (spoof if any, and txpwr)
            sql = "insert into radio_event (mac,state,params,ts) values (%s,%s,%s,%s);"
            if ds['spoofed']: self._curs.execute(sql,(ds['mac'],'spoof',ds['spoofed'],ds['ts']))
            self._curs.execute(sql,(ds['mac'],'txpwr',ds['txpwr'],ds['ts']))

            # start save thread if save is enabled
            if self._raw['save']:
                self._qSave[ds['mac']] = Queue.Queue()
                self._tSave[ds['mac']] = sse.SaveThread(self._qSave[ds['mac']],
                                                        self._raw['path'],
                                                        self._raw['private'],
                                                        self._raw['sz'],
                                                        (self._storage['host'],
                                                         self._storage['port'],
                                                         self._storage['db'],
                                                         self._storage['user'],
                                                         self._storage['pwd']))
                self._tSave[ds['mac']].start()

        except nmp.NMPException as e:
            raise NidusDBSubmitParseException("submit radio %s" % e)
        except psql.Error as e:
            # rollback so postgresql is not left in error state
            self._conn.rollback()
            raise NidusDBSubmitException("submit radio %s: %s" % (e.pgcode,e.pgerror))
        except NidusDBException:
            # our save thread failed to connect to database - reraise
            self._conn.rollback()
            raise NidusDBSubmitException("submit radio connect error")
        except Exception as e:
            # blanket exception
             self._conn.rollback()
             raise NidusDBException("submit radio type(%s) %s" % (type(e),e.__repr__()))
        else:
            self._conn.commit()
Ejemplo n.º 18
0
    def submitradio(self, f):
        """ submitradio - submit the string fields to the database """
        try:
            # tokenize the string f and convert to dict
            ds = nmp.data2dict(nmp.tokenize(f), 'RADIO')

            # submit to db
            # insert radio into radio table if it does not exist
            self._curs.execute("select * from radio where mac=%s;",
                               (ds['mac'], ))
            if not self._curs.fetchone():
                # truncate the strings to max length 20
                sql = """
                       insert into radio (mac,driver,chipset,channels,standards,description)
                       values (%s,%s,%s,%s,%s,%s);
                      """
                self._curs.execute(
                    sql,
                    (ds['mac'], ds['driver'][0:20], ds['chipset'][0:20],
                     ds['channels'], ds['standards'][0:20], ds['desc'][0:100]))

            # insert using_radio
            sql = """
                   insert into using_radio (sid,mac,phy,nic,vnic,role,period)
                   values (%s,%s,%s,%s,%s,%s,tstzrange(%s,NULL,'[]'));
                  """
            self._curs.execute(sql,
                               (self._sid, ds['mac'], ds['phy'], ds['nic'],
                                ds['vnic'], ds['role'], ds['ts']))

            # insert initial events (spoof if any, and txpwr)
            sql = "insert into radio_event (mac,state,params,ts) values (%s,%s,%s,%s);"
            if ds['spoofed']:
                self._curs.execute(
                    sql, (ds['mac'], 'spoof', ds['spoofed'], ds['ts']))
            self._curs.execute(sql,
                               (ds['mac'], 'txpwr', ds['txpwr'], ds['ts']))

            # start save thread if save is enabled
            if self._raw['save']:
                self._qSave[ds['mac']] = Queue.Queue()
                self._tSave[ds['mac']] = sse.SaveThread(
                    self._qSave[ds['mac']], self._raw['path'],
                    self._raw['private'], self._raw['sz'],
                    (self._storage['host'], self._storage['port'],
                     self._storage['db'], self._storage['user'],
                     self._storage['pwd']))
                self._tSave[ds['mac']].start()

        except nmp.NMPException as e:
            raise NidusDBSubmitParseException("submit radio %s" % e)
        except psql.Error as e:
            # rollback so postgresql is not left in error state
            self._conn.rollback()
            raise NidusDBSubmitException("submit radio %s: %s" %
                                         (e.pgcode, e.pgerror))
        except NidusDBException:
            # our save thread failed to connect to database - reraise
            self._conn.rollback()
            raise NidusDBSubmitException("submit radio connect error")
        except Exception as e:
            # blanket exception
            self._conn.rollback()
            raise NidusDBException("submit radio type(%s) %s" %
                                   (type(e), e.__repr__()))
        else:
            self._conn.commit()