Skip to content

Commit

Permalink
forgot to change the version
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoldverg committed Jan 19, 2024
1 parent 32900cc commit 034f5ea
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 31 deletions.
2 changes: 1 addition & 1 deletion _version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.4"
__version__ = "0.0.12"
2 changes: 1 addition & 1 deletion odscli/odscli.py → odscli/ods_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from odscli.sdk.endpoint_management import endpoint_cli
from odscli.sdk.query_transfer_data import query_cli
from odscli.sdk.login import auth_cli
from odscli.sdk.carbon_measure import measure_cli
from odscli.sdk.measure import measure_cli

odscli = click.CommandCollection(sources=[schedule_cli, credential_cli, endpoint_cli, query_cli, auth_cli, measure_cli])

Expand Down
56 changes: 56 additions & 0 deletions odscli/sdk/measure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import click
import logging
import os
from odscli.sdk.measure.measure_carbon import traceroute, compute_carbon_per_ip, geo_locate_ips
from odscli.sdk.measure.measure_host_network import begin_measuring
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)


@click.group('measure_cli')
@click.pass_context
def measure_cli():
pass


@measure_cli.group('measure')
def measure():
pass


@measure.command("carbon")
@click.option('--ip', default="", help="The IP address of the destination host", type=click.STRING)
@click.option('--max_hops', default=64, type=click.INT)
def carbon_measure(ip, max_hops):
ip_list = traceroute(ip, max_hops)
print(f"IP's to source {ip_list}")
ip_df = geo_locate_ips(ip_list)
compute_carbon_per_ip(ip_df)

@measure.command("host")
@click.option('--nic','--interface', required=True, help='The network interface to measure, the default is the interface with UP status')
@click.option('-K', '--kernel', 'measure_kernel', default=False, type=bool, help='Use if you wish to measure the kernel')
@click.option('-N', '--network', 'measure_network', default=False, type=bool, help='Use if you wish to measure the network interface')
@click.option('-U', '--udp', 'measure_udp', default=False, type=bool, help='Use if you wish to monitor UDP')
@click.option('-T', '--tcp', 'measure_tcp', default=False, type=bool, help='Use if you wish to monitor TCP')
@click.option('-S', '--std-out', 'disable_std_out', default=False, type=bool, help='Disable printing the results to standard output')
@click.option('-F','--file-path', 'file_path', default=os.path.join(os.path.expanduser("~"), '.config', 'pmeter_measure.txt'), type=click.Path(), help='Set the file path used to measure')
@click.option('-I','--interval', default='00:00:01', help='Set the time to run the measurement in the format HH:MM:SS')
@click.option('-M','--measurements', default=1, type=int, help='The max number of times to measure your system')
@click.option('-L','--length', default='10s', help='The amount of time to run for: 5w, 4d 3h, 2m, 1s are some examples of 5 weeks, 4 days, 3 hours, 2 min, 1 sec')
def host_measure(interface, measure_kernel, measure_network, measure_udp, measure_tcp, disable_std_out, file_path, interval, measurements, length):
# Your measurement logic goes here
click.echo(f"Measuring network activity on {interface}")
click.echo(f"Measure Kernel: {measure_kernel}")
click.echo(f"Measure Network: {measure_network}")
click.echo(f"Measure UDP: {measure_udp}")
click.echo(f"Measure TCP: {measure_tcp}")
click.echo(f"Enable Std Out: {disable_std_out}")
click.echo(f"File Path: {file_path}")
click.echo(f"Interval: {interval}")
click.echo(f"Measurements: {measurements}")
click.echo(f"Length: {length}")
begin_measuring(interface=interface, measure_kernel=measure_kernel, measure_network=measure_network,
measure_udp=measure_udp, measure_tcp=measure_tcp, print_to_std_out=disable_std_out, file_path=file_path,
interval=interval, measurement=measurements, length=length)


Empty file added odscli/sdk/metrics/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
import click
import requests
import pandas as pd
import os
import logging

logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.layers.inet import IP, ICMP
from scapy.sendrecv import sr1
from scapy.config import conf

conf.use_pcap = True

import requests
import pandas as pd


@click.group('measure_cli')
@click.pass_context
def measure_cli():
pass


@measure_cli.group('measure')
def measure():
pass


@measure.command("carbon")
@click.option('--ip', default="", help="The IP address of the destination host", type=click.STRING)
@click.option('--max_hops', default=64, type=click.INT)
def carbon_measure(ip, max_hops):
ip_list = traceroute(ip, max_hops)
print(f"IP's to source {ip_list}")
ip_df = geo_locate_ips(ip_list)
compute_carbon_per_ip(ip_df)
# pass


def traceroute(destination, max_hops):
ip_list = []
Expand Down
90 changes: 90 additions & 0 deletions odscli/sdk/metrics/measure_host_network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import time

from datetime import datetime, timedelta

from odscli.sdk.measure.metrics_pojo import ODS_Metrics


def begin_measuring(file_path, interface, measure_tcp=True, measure_udp=True,
measure_kernel=True, measure_network=True, print_to_std_out=False, interval=1,
latency_host="http://google.com", measurement=1, length="0s", user=""):
metric = ODS_Metrics()
metric.set_user(user)
interface_list = []
if interface == "all":
for inter_name in metric.get_system_interfaces():
interface_list.append(inter_name)
else:
interface_list.append(interface)
if measurement == 0:
print("Measuring by using the duration specified with interval")
measure_using_length(interface_list=interface_list, metric=metric, file_path=file_path, measure_tcp=measure_tcp, measure_udp=measure_udp,
measure_kernel=measure_kernel, measure_network=measure_network,
print_to_std_out=print_to_std_out, latency_host=latency_host, interval=interval,
length=length)
if length == '0':
print("Measuring by using the number of measurments to perform with interval")
measure_using_measurements(interface_list=interface_list, metric=metric, file_path=file_path, measure_tcp=measure_tcp,
measure_udp=measure_udp, measure_kernel=measure_kernel,
measure_network=measure_network, print_to_std_out=print_to_std_out,
latency_host=latency_host, interval=interval, measurement=measurement)
else:
measurements_counter = 0
end_date = convert_to_endate(length)
current_date = datetime.now()
while current_date < end_date and measurements_counter < measurement:
print("Current date= ", str(current_date), "is less than end date=", str(end_date), " is =",
current_date < end_date)
print("currentMeasurement is less than the max measurements", measurements_counter < measurement)
for intr_name in interface_list:
metric.measure(intr_name, measure_tcp, measure_udp, measure_kernel, measure_network, print_to_std_out,
latency_host)
metric.to_file(file_path)
current_date = datetime.now()
measurements_counter += 1
time.sleep(interval)


def measure_using_length(interface_list, metric, file_path, measure_tcp=True,
measure_udp=True, measure_kernel=True, measure_network=True, print_to_std_out=False,
latency_host="http://google.com", interval=1, length="0s"):
end_date = convert_to_endate(length)
current_date = datetime.now()
while (current_date < end_date):
print("Current date is less than end date=", current_date < end_date)
metric.measure_latency_rtt(latency_host)
for intr_name in interface_list:
metric.measure(intr_name, measure_tcp, measure_udp, measure_kernel, measure_network, print_to_std_out,
latency_host)
metric.to_file(file_path=file_path)
current_date = datetime.now()
time.sleep(interval)


def measure_using_measurements(interface_list, metric, file_path, measure_tcp=True,
measure_udp=True, measure_kernel=True, measure_network=True, print_to_std_out=False,
latency_host="http://google.com", interval=1, measurement=1):
for i in range(0, measurement):
metric.measure_latency_rtt(latency_host)
print("measurement: ", i)
for intr_name in interface_list:
metric.measure(intr_name, measure_tcp, measure_udp, measure_kernel, measure_network, print_to_std_out,
latency_host)
metric.to_file(file_path=file_path)
time.sleep(interval)


def convert_to_endate(length):
end_date = datetime.now()
num = int(length[:-1])
if "s" in length:
end_date += timedelta(seconds=num)
if "m" in length:
end_date += timedelta(minutes=num)
if "d" in length:
end_date += timedelta(days=num)
if "w" in length:
end_date += timedelta(weeks=num)
if "h" in length:
end_date += timedelta(hours=num)
return end_date
133 changes: 133 additions & 0 deletions odscli/sdk/metrics/metrics_pojo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import os
import datetime
from platform import system, platform
from statistics import mean
import requests
from psutil import net_connections, cpu_freq, net_io_counters, net_io_counters, net_if_stats, net_io_counters
from json import dumps
from multiprocessing import cpu_count
from tcp_latency import measure_latency
from pathlib import Path


class ODS_Metrics():

def __init__(self):
# kernel metrics
self.interface = ""
self.ods_user = ""
self.active_core_count = 0
self.cpu_frequency_max = 0.0
self.cpu_frequency_current = 0.0
self.cpu_frequency_min = 0.0
self.energy_consumed = 0.0
self.cpu_arch = ""
# network metrics
self.rtt = 0.0
self.bandwidth = 0.0
self.bandwidth_delay_product = 0.0
self.packet_loss_rate = 0.0
self.link_capacity = 0.0
self.bytes_sent = 0.0
self.bytes_recv = 0.0
self.bytes_sent_delta = 0
self.bytes_recv_delta = 0
self.packets_sent = 0
self.packets_recv = 0
self.packets_sent_delta = 0
self.packets_recv_delta = 0
self.dropin = 0
self.dropout = 0
self.dropin_delta = 0
self.dropout_delta = 0
self.nic_speed = 0 # this is in mb=megabits
self.nic_mtu = 0 # max transmission speed of nic
# identifying properties
self.start_time = ""
self.end_time = ""
self.count = 0
self.latency = []

def set_user(self, user_passed):
user = os.getenv('ODS_USER', '')
if len(user_passed) > 0:
self.ods_user = user_passed
else:
self.ods_user = user

def measure(self, interface='', measure_tcp=True, measure_udp=True, measure_kernel=True, measure_network=True,
print_to_std_out=False, latency_host="http://google.com"):
self.start_time = datetime.now().__str__()
self.interface = interface
if measure_kernel:
self.measure_kernel()
if measure_network:
print('Getting metrics of: ' + interface)
# we could take the average of all speeds that every socket experiences and thus get a rough estimate of bandwidth??
self.measure_network(interface)
self.measure_latency_rtt(latency_host)
if measure_tcp:
print('Measuring tcp')
net_connections(kind="tcp")
if measure_udp:
print('Measuring udp')
net_connections(kind="udp")
self.end_time = datetime.now().__str__()
if (print_to_std_out):
print("\n", dumps(self.__dict__), "\n")
# self.to_file()

def measure_kernel(self):
if system() != 'Darwin':
freq = cpu_freq()
self.cpu_frequency_max = freq[2]
self.cpu_frequency_current = freq[0]
self.cpu_frequency_min = freq[1]
print(freq)
self.cpu_arch = platform()
self.active_core_count = cpu_count()

def measure_latency_rtt(self, latency_host="http://google.com"):
self.latency = mean(measure_latency(host="google.com")) # in miliseconds and a list
r = requests.get(latency_host)
self.rtt = r.elapsed.microseconds / 1000
print('Latency =', self.latency, " RTT=", self.rtt)
print('LatencyType =', type(self.latency), " RTT Type=", type(self.rtt))

def measure_network(self, interface):
nic_counter_dic = net_io_counters(pernic=True, nowrap=True)
if interface not in nic_counter_dic:
raise Exception("The interface passed was not found on your system")
interface_counter_tuple = nic_counter_dic[interface]
self.bytes_sent = interface_counter_tuple[0]
self.bytes_recv = interface_counter_tuple[1]
self.packets_sent = interface_counter_tuple[2]
self.packets_recv = interface_counter_tuple[3]
self.errin = interface_counter_tuple[4]
self.errout = interface_counter_tuple[5]
self.dropin = interface_counter_tuple[6]
self.dropout = interface_counter_tuple[7]
sys_interfaces = net_if_stats()
interface_stats = sys_interfaces[self.interface]
self.nic_mtu = interface_stats[3]
self.nic_speed = interface_stats[2]

def get_system_interfaces(self):
nic_counter_dic = net_io_counters(pernic=True, nowrap=True)
return nic_counter_dic.keys()

def to_file(self, file_path):
os.makedirs(os.path.dirname(file_path), exist_ok=True)
j = dumps(self.__dict__)
with open(file_path, "a+") as f:
f.write(j + "\n")

def do_deltas(self, old_metric):
self.bytes_sent_delta = self.bytes_sent - old_metric.bytes_sent
self.bytes_recv_delta = self.bytes_recv - old_metric.bytes_recv
self.packets_sent_delta = self.packets_sent - old_metric.packets_sent
self.packets_recv_delta = self.packets_recv - old_metric.packets_recv
self.errin_delta = self.errin - old_metric.errin
self.errout_delta = self.errout - old_metric.errout
self.dropin_delta = self.dropin - old_metric.dropin
self.dropout_delta = self.dropout - old_metric.dropout

0 comments on commit 034f5ea

Please sign in to comment.