def bwmonitor(interface):
    client_id = util.get_client_id()

    MAX_32UINT = (2**32) - 1
    STATS_PATH = "/sys/class/net/{}/statistics".format(interface)

    try:
        rx_bytes_file = open("{}/rx_bytes".format(STATS_PATH))
    except:
        sys.exit(2)
    try:
        tx_bytes_file = open("{}/tx_bytes".format(STATS_PATH))
    except:
        sys.exit(2)

    last_rx_bytes = None
    last_tx_bytes = None
    last_time = None
    dbq = db_queue()
    while True:
        loop_start_time = time.time()
        rx_bytes_file.seek(0)
        rx_bytes = long(rx_bytes_file.read().strip())
        tx_bytes_file.seek(0)
        tx_bytes = long(tx_bytes_file.read().strip())
        if last_time is not None:
            if last_rx_bytes > rx_bytes:
                # rollover occurred
                rx_bytes_delta = rx_bytes + (MAX_32UINT - last_rx_bytes)
            else:
                rx_bytes_delta = rx_bytes - last_rx_bytes
            if last_tx_bytes > tx_bytes:
                # rollover occurred
                tx_bytes_delta = tx_bytes + (MAX_32UINT - last_tx_bytes)
            else:
                tx_bytes_delta = tx_bytes - last_tx_bytes
            time_delta = loop_start_time - last_time
            rx_bps = float(rx_bytes_delta * 8) / time_delta
            tx_bps = float(tx_bytes_delta * 8) / time_delta
            bw_data = { "type" : "bandwidth", \
                         "data" : {  "client_id" : client_id, \
                                   "timestamp" : loop_start_time, \
                                   "rx_bytes" : rx_bytes_delta, \
                                   "tx_bytes" : tx_bytes_delta, \
                                   "rx_bps" : rx_bps, \
                                   "tx_bps" : tx_bps}
                     }
            dbq.write(bw_data)

        last_time = loop_start_time
        last_rx_bytes = rx_bytes
        last_tx_bytes = tx_bytes

        # sleep off remaining time
        sleeptime = 1.0 - (time.time() - loop_start_time)
        if (sleeptime > 0):
            try:
                time.sleep(sleeptime)
            except:
                pass
Exemple #2
0
def bwmonitor(interface):
	client_id = util.get_client_id()
	os_word_size = 64 if sys.maxsize > 2**32 else 32
	MAXUINT = (2 ** os_word_size) - 1
	STATS_PATH="/sys/class/net/{}/statistics".format(interface)
	try:
		rx_bytes_file = open("{}/rx_bytes".format(STATS_PATH))
	except:
		sys.exit(2)
	try:
		tx_bytes_file = open("{}/tx_bytes".format(STATS_PATH))
	except:
		sys.exit(2)
	last_rx_bytes = None
	last_tx_bytes = None
	last_time = None
	dbq = db_queue()
	while True:
		loop_start_time = time.time()
		rx_bytes_file.seek(0)
		rx_bytes = int(rx_bytes_file.read().strip())
		tx_bytes_file.seek(0)
		tx_bytes = int(tx_bytes_file.read().strip())
		if last_time is not None:
			rx_bytes_delta = rx_bytes - last_rx_bytes if rx_bytes >= last_rx_bytes else MAXUINT - last_rx_bytes + rx_bytes + 1
			tx_bytes_delta = tx_bytes - last_tx_bytes if tx_bytes >= last_tx_bytes else MAXUINT - last_tx_bytes + tx_bytes + 1
			time_delta = loop_start_time - last_time
			rx_bps = float(rx_bytes_delta * 8) / time_delta
			tx_bps = float(tx_bytes_delta * 8) / time_delta
			bw_data = { "type" : "bandwidth", \
						"data" : {  "client_id" : client_id, \
						"timestamp" : loop_start_time, \
						"rx_bytes" : rx_bytes_delta, \
						"tx_bytes" : tx_bytes_delta, \
						"rx_bps" : rx_bps, \
						"tx_bps" : tx_bps}
			}
			dbq.write(bw_data)
		last_time = loop_start_time
		last_rx_bytes = rx_bytes
		last_tx_bytes = tx_bytes

		# sleep off remaining time
		sleeptime = 1.0 - (time.time() - loop_start_time)
		if (sleeptime > 0):
			try:
				time.sleep (sleeptime)
			except:
				pass
def main():
    if ((len(sys.argv) < 2) or (len(sys.argv) > 2)):
        print_usage()
        sys.exit(1)
    dbq = db_queue()

    with open("/opt/netperf/config/interfaces.json", "r") as config_file:
        interface_info = json.load(config_file)
        test_exec_namespace = interface_info["test_exec_namespace"]
        if sys.argv[1] == 'local':
            interfaces = interface_info["interfaces"]
            for i in interfaces:
                if interfaces[i]["namespace"] != test_exec_namespace:
                    test_local_network(test_exec_namespace,
                                       interfaces[i]["alias"], dbq)
        else:
            if sys.argv[1] == 'isp':
                db_filename = NETPERF_SETTINGS.get_db_filename()
                db = netperf_db(db_filename)
                enforce_quota = NETPERF_SETTINGS.get_speedtest_enforce_quota()
                data_usage_quota_GB = NETPERF_SETTINGS.get_data_usage_quota_GB(
                )
                data_usage_GB = float(
                    db.get_data_usage()["rxtx_bytes"]) / float(1e9)
                test_log.info("data usage GB: {:0.2f}".format(data_usage_GB))
                if enforce_quota == True:
                    st_data_usage = db.get_speedtest_data_usage(
                        datetime.today())
                    test_count = st_data_usage[0]["test_count"]
                    if test_count > 0:
                        rxtx_GB = float(
                            st_data_usage[0]["rxtx_bytes"]) / float(1e9)
                        avg_rxtx_GB = float(rxtx_GB) / float(test_count)
                    else:
                        rxtx_GB = float(0)
                        avg_rxtx_GB = float(0)
                    if (data_usage_GB + avg_rxtx_GB) > data_usage_quota_GB:
                        quota_reached = True
                    else:
                        quota_reached = False

                if not (enforce_quota == True and quota_reached == True):
                    test_ok = test_isp(test_exec_namespace, dbq)
                    if not test_ok:
                        # speedtest failed, test for an Internet outage outage
                        ping_results = pingtest(test_exec_namespace, "8.8.8.8",
                                                dbq)
                        (client_id, timestamp, remote_host, min, avg, max,
                         mdev) = ping_results
                        if min == 0 or max == 0:
                            # log an outage
                            outage_data = {"type": "isp_outage",\
                              "data" : { \
                               "client_id" : client_id, \
                               "timestamp" : timestamp} \
                              }
                            dbq.write(outage_data)
                else:
                    test_log.error(
                        "Data usage quota has been reached, speedtest was cancelled. Data usage quota: {:0.2f} Data usage since last reset: {:0.2} GB, average data usage per test: {:0.2f} GB"
                        .format(data_usage_quota_GB, data_usage_GB,
                                avg_rxtx_GB))
            else:
                if sys.argv[1] == 'dns':
                    dns_ok = test_name_resolution(test_exec_namespace, dbq)
                    if not dns_ok:
                        # dns lookup failures, test for an Internet outage
                        ping_results = pingtest(test_exec_namespace, "8.8.8.8",
                                                dbq)
                        (client_id, timestamp, remote_host, min, avg, max,
                         mdev) = ping_results
                        if min == 0 or max == 0:
                            # log an outage
                            outage_data = {"type": "isp_outage",\
                              "data" : { \
                              "client_id" : client_id, \
                              "timestamp" : timestamp} \
                             }
                            dbq.write(outage_data)
                else:
                    if sys.argv[1] == 'internet_ping':
                        ping_results = pingtest(test_exec_namespace, "8.8.8.8",
                                                dbq)
                        (client_id, timestamp, remote_host, min, avg, max,
                         mdev) = ping_results
                        message = {     "type" : "ping", \
                          "data" : {  "client_id" : client_id, \
                            "timestamp" : timestamp, \
                            "remote_host" : remote_host, \
                            "min" : min, \
                            "avg" : avg, \
                            "max" : max, \
                            "mdev" : mdev}}
                        #dbq.write(message)

                        if min == 0 or max == 0:
                            test_log.info("Internet outage detected.")
                            outage_data = {"type": "isp_outage",\
                              "data" : { \
                               "client_id" : client_id, \
                               "timestamp" : timestamp} \
                              }
                            dbq.write(outage_data)
                    else:
                        print_usage()
                        sys.exit(1)
#!/usr/bin/env python
# This file is part of the Network Performance Monitor which is released under the GNU General Public License v3.0
# See the file LICENSE for full license details.

import sys
import util
from datetime import datetime, timedelta
from netperf_db import db_queue
from util import get_client_id

client_id = get_client_id()

dbq = db_queue()

message = { "type" : "data_usage_reset",\
     "data" : { "client_id" : client_id } }
dbq.write(message)