Beispiel #1
0
def main():
    """Main routine run by gprsd."""
    # Initialize some timestamps and connect to the ConfigDB.
    now = time.time()
    last_scrape_time = now
    last_event_generation_time = now
    last_removal_of_old_data = now
    config_db = ConfigDB()
    while True:
        now = time.time()
        # Get GPRS usage data and store it in the DB.
        if (now - last_scrape_time >
                config_db['gprsd_cli_scrape_period']):
            last_scrape_time = now
            utilities.gather_gprs_data()
        # Generate events for the EventStore with data from the GPRS table.
        if (now - last_event_generation_time >
                config_db['gprsd_event_generation_period']):
            last_event_generation_time = now
            start_time = now - config_db['gprsd_event_generation_period']
            utilities.generate_gprs_events(start_time, now)
        # Clean old records out of the GPRS table.
        if (now - last_removal_of_old_data >
                config_db['gprsd_cleanup_period']):
            last_removal_of_old_data = now
            utilities.clean_old_gprs_records(
                now - config_db['gprsd_max_data_age'])
        # Wait for a bit, then do it again.
        time.sleep(SLEEP_TIME)
Beispiel #2
0
    def test_unchanged_measurements(self):
        """Simulate an IMSI with no activity between measurements.

        In old versions this caused an error where byte deltas were measured
        from zero.
        """
        # Simulate results of our first data scrape.
        first_data = {
            'IMSI000432': {
                'ipaddr': '192.168.99.1',
                'uploaded_bytes': 100,
                'downloaded_bytes': 200,
            },
        }
        self.mock_subscriber.gprs_return_value = first_data
        utilities.gather_gprs_data()
        # And the results of our second scrape (the data is unchanged).
        second_data = {
            'IMSI000432': {
                'ipaddr': '192.168.99.1',
                'uploaded_bytes': 100,
                'downloaded_bytes': 200,
            },
        }
        self.mock_subscriber.gprs_return_value = second_data
        utilities.gather_gprs_data()
        # We should see two records and the second records should have deltas
        # computed correctly.
        records = self.gprs_db.get_records()
        self.assertEqual(2, len(records))
        self.assertEqual(0, records[1]['uploaded_bytes_delta'])
        self.assertEqual(0, records[1]['downloaded_bytes_delta'])
Beispiel #3
0
 def test_multiple_records(self):
     """We can record data for multiple registered subs."""
     data = {
         'IMSI000456': {
             'ipaddr': '192.168.99.1',
             'uploaded_bytes': 123,
             'downloaded_bytes': 456,
         },
         'IMSI000667': {
             'ipaddr': '192.168.99.2',
             'uploaded_bytes': 345,
             'downloaded_bytes': 678,
         },
     }
     self.mock_subscriber.gprs_return_value = data
     utilities.gather_gprs_data()
     # Inspect the records from the GPRSDB.
     records = self.gprs_db.get_records()
     self.assertEqual(2, len(records))
     record = self.gprs_db.get_latest_record('IMSI000456')
     self.assertEqual('192.168.99.1', record['ipaddr'])
     self.assertEqual(123, record['uploaded_bytes'])
     self.assertEqual(456, record['downloaded_bytes'])
     self.assertEqual(123, record['uploaded_bytes_delta'])
     self.assertEqual(456, record['downloaded_bytes_delta'])
     record = self.gprs_db.get_latest_record('IMSI000667')
     self.assertEqual('192.168.99.2', record['ipaddr'])
     self.assertEqual(345, record['uploaded_bytes'])
     self.assertEqual(678, record['downloaded_bytes'])
     self.assertEqual(345, record['uploaded_bytes_delta'])
     self.assertEqual(678, record['downloaded_bytes_delta'])
Beispiel #4
0
    def test_new_ipaddr(self):
        """We should correctly handle things when the sub gets a new ipaddr.

        If the sub uses GPRS for a bit but then goes idle, OpenBTS may assign
        it a new ipaddr when the sub becomes active again.  When this happens,
        the byte count is reset, so our deltas should take that into account.
        """
        # Simulate results of our first data scrape.
        first_data = {
            'IMSI000321': {
                'ipaddr': '192.168.99.1',
                'uploaded_bytes': 100,
                'downloaded_bytes': 200,
            },
        }
        self.mock_subscriber.gprs_return_value = first_data
        utilities.gather_gprs_data()
        # And the results of our second scrape (new ipaddr).
        second_data = {
            'IMSI000321': {
                'ipaddr': '192.168.99.10',
                'uploaded_bytes': 300,
                'downloaded_bytes': 600,
            },
        }
        self.mock_subscriber.gprs_return_value = second_data
        utilities.gather_gprs_data()
        # We should see two records and the second records should have deltas
        # computed correctly.
        records = self.gprs_db.get_records()
        self.assertEqual(2, len(records))
        self.assertEqual(300, records[1]['uploaded_bytes_delta'])
        self.assertEqual(600, records[1]['downloaded_bytes_delta'])
Beispiel #5
0
 def test_second_record_for_sub(self):
     """We compute byte deltas correctly."""
     # Simulate results of our first data scrape.
     first_data = {
         'IMSI000432': {
             'ipaddr': '192.168.99.1',
             'uploaded_bytes': 100,
             'downloaded_bytes': 200,
         },
     }
     self.mock_subscriber.gprs_return_value = first_data
     utilities.gather_gprs_data()
     # And the results of our second scrape.
     second_data = {
         'IMSI000432': {
             'ipaddr': '192.168.99.1',
             'uploaded_bytes': 300,
             'downloaded_bytes': 600,
         },
     }
     self.mock_subscriber.gprs_return_value = second_data
     utilities.gather_gprs_data()
     # We should see two records and the second records should have deltas
     # computed correctly.
     records = self.gprs_db.get_records()
     self.assertEqual(2, len(records))
     self.assertEqual(200, records[1]['uploaded_bytes_delta'])
     self.assertEqual(400, records[1]['downloaded_bytes_delta'])
Beispiel #6
0
 def test_unknown_imsi(self):
     """If the scraped data contains info for a non-sub, we ignore it."""
     self.mock_subscriber.get_subscriber_return_value = []
     self.mock_subscriber.gprs_return_value = {
         'IMSI901550000000084': {
             'ipaddr': '192.168.99.1',
             'uploaded_bytes': 123,
             'downloaded_bytes': 456,
         },
     }
     utilities.gather_gprs_data()
     # Inspect the records from the GPRSDB.
     records = self.gprs_db.get_records()
     self.assertEqual(0, len(records))
Beispiel #7
0
 def test_first_record_for_sub(self):
     """We record scraped data on a registered sub."""
     data = {
         'IMSI000456': {
             'ipaddr': '192.168.99.1',
             'uploaded_bytes': 123,
             'downloaded_bytes': 456,
         },
     }
     self.mock_subscriber.gprs_return_value = data
     utilities.gather_gprs_data()
     # Inspect the records from the GPRSDB.
     records = self.gprs_db.get_records()
     self.assertEqual(1, len(records))
     self.assertEqual('IMSI000456', records[0]['imsi'])
     self.assertEqual('192.168.99.1', records[0]['ipaddr'])
     self.assertEqual(123, records[0]['uploaded_bytes'])
     self.assertEqual(123, records[0]['uploaded_bytes_delta'])
     self.assertEqual(456, records[0]['downloaded_bytes'])
     self.assertEqual(456, records[0]['downloaded_bytes_delta'])
Beispiel #8
0
    def test_reassigned_ipaddr(self):
        """Compute deltas correctly when byte count resets.

        If the sub uses GPRS for a bit but then goes idle, OpenBTS may assign
        it a new ipaddr when the sub becomes active again.  If OpenBTS happens
        to assign the same IP address as before, the byte count will still be
        reset and will likely be less than the last measurement before the sub
        went idle.  So, in this case, when the byte count is lower than the
        previous measurement, we compute the delta by using a starting value of
        zero.
        """
        # Simulate results of our first data scrape.
        first_data = {
            'IMSI000765': {
                'ipaddr': '192.168.99.1',
                'uploaded_bytes': 1000,
                'downloaded_bytes': 2000,
            },
        }
        self.mock_subscriber.gprs_return_value = first_data
        utilities.gather_gprs_data()
        # And the results of our second scrape (same ipaddr but the byte counts
        # are lower than before, suggesting a reset of the counts).
        second_data = {
            'IMSI000765': {
                'ipaddr': '192.168.99.1',
                'uploaded_bytes': 200,
                'downloaded_bytes': 400,
            },
        }
        self.mock_subscriber.gprs_return_value = second_data
        utilities.gather_gprs_data()
        # We should see two records and the second records should have deltas
        # computed correctly.
        records = self.gprs_db.get_records()
        self.assertEqual(2, len(records))
        self.assertEqual(200, records[1]['uploaded_bytes_delta'])
        self.assertEqual(400, records[1]['downloaded_bytes_delta'])