Skip to main content

Local API to collect and compute data on used device and running applications to give insight on their environmental impacts.

Project description

Local API / sidecar / companion of a running application that computes and gives insights to the application regarding its environmental impacts.


If no parameters are passed to the API to isolate the application, then the impact of the whole machine is calculated.

How to use

This is an API, you could use either your browser, cURL, or call it directly from an application (which is the main usecase).

Once the API is running, a Swagger interface is available on localhost:8000/docs.

Run natively

Boagent will not be able to return proper responses from its endpoints without root privileges in order to fetch hardware data. It also needs information from BoaviztAPI and Scaphandre, see the setup informations.

To run it :

Without poetry

apt update && apt install lshw nvme-cli -y
pip3 install -r requirements.txt
cd boagent/api/
uvicorn api:app --reload

With poetry

apt update && apt install lshw nvme-cli -y
poetry install --only main
poetry run uvicorn --reload boagent.api.api:app

Run in a docker container

You could pull the image with docker pull ghcr.io/boavizta/boagent:0.1.0.

Run in docker-compose (with all the requirements)

To get started you need docker and docker-compose installed on your machine. On a Debian or Ubuntu environment, run :

# apt update && apt install -y docker.io docker-compose

To get the full setup easily, you could run the stack in docker-compose with docker-compose up -d. docker-compose.yml, at the root of the project will build a Docker image from the source for Boagent, and setup a container for Scaphandre and another for the BoaviztAPI, allowing you to get the full evaluation easily on a physical machine.

Please see Configuration for the environment variables you can tweak in the Boagent container.

Use hardware_cli

To have an example of the retrieved hardware information by Boagent, you can run sudo ./hardware_cli.py. At the moment, it will output the formatted data for CPU, RAM and storage devices used by Boagent when sending a request to BoaviztAPI. sudo ./hardware_cli.py --output-file <file> can send the formatted output to a file.

Setup

Linux

Boagent parses output from lshw (a tool listing hardware components and characteristics) and nvme-cli (a tool listing information on SSD storage devices available through NVME interfaces). To get all actually parsed information (and for future developments), Boagent needs those two programs and to execute them with root privileges.

BoaviztAPI

You need either to use an existing BoaviztAPI endpoint, or to build the BoaviztAPI container image, then run the container locally on port 5000.

Depending or your setup, specify the endpoint to be used with the environment variable BOAVIZTAPI_ENDPOINT, see Configuration.

Ensure that the version of BoaviztAPI SDK installed (see requirements.txt or pyproject.toml) is the same as the version of the API running the endpoint you use.

Scaphandre

To get power consumption metrics, you need Scaphandre running in the background, with the JSON exporter. This will write power metrics to a file, that Boagent will read :

scaphandre json -s 5 -f power_data.json

Configuration

Boagent can be configured with the following variables :

  • DEFAULT_LIFETIME: machines lifetime used to compute the scope 3 / manufacturing, transport, end-of-life impacts
  • HARDWARE_FILE_PATH: path to the file containing the hardware list (output from lshw.py)
  • POWER_FILE_PATH: path to the file containing power measurements (output from Scaphandre with JSON exporter)
  • HARDWARE_CLI: path to the executable file to collect hardware information (lshw.py from this project)
  • BOAVIZTAPI_ENDPOINT: HTTP endpoint to the BoaviztAPI, in the form http://myendpoint.com:PORTNUMBER

You can set those variables in the following order (as interpreted by the tool):

  1. export the variable in the environment
  2. write it in the .env file in the same folder as api.py
  3. rely on default values from config.py

You can check the configuration applied by querying the /info route.

How it works

Currently, Boagent only works for Linux systems.

Boagent exposes multiple API endpoints, most notably /query and /metrics. Both will query an instance of BoaviztAPI in order to give the environmental impacts of the received hardware data. /query will return a response in JSON format, and /metrics will return a response parsable by a Prometheus instance. If needed, both those endpoints can return data from Scaphandre and give the energy consumption of components from the queried hardware.

Presently, Boagent gets hardware data through a parsing of the output of lshw, a common utility available for Linux distributions that lists a lot of information of all hardware components on a running computer. The code for this Lshw class is an adaptation of netbox-agent's implementation. lshw, to get all proper data needed by BoaviztAPI, needs to be executed as a privileged user with sudo. Boagent, executed with the available docker-compose file, will run as privileged and will be able to receive the needed hardware data. At the moment, only data for the CPU, RAM and storage (either HDD or SSD) are parsed and sent to BoaviztAPI in order to calculate impacts.

Another endpoint, process_embedded_impacts, allows to calculate the embedded impacts of a process running on the host, in relation to the host components (CPU, RAM and storage). It will give, for all the components, the average, maximum and minimum values between two timestamps for three environmental impact factors : Global Warming Potential (in KgCO2e), Abiotic Depletion Potential (in KgSbeq) and Primary Energy (in microjoules). To get these informations, a Linux Process ID has to be provided.

Deeper explanations

Environmental metrics

This project uses the Life Cycle Assessment (ISO 14040 / 14044) methodology as a reference.

This way, it is intended to evaluate the impacts on all life cycle phases (extraction, manufacturing, shipping, use, end of life). Today we only evaluate manufacturing and use phases.

Here are the impacts considered so far :

  • Green House Gas emissions / Global Warming Potential (see GHG protocol as a reference)
    • resources extraction (LCA) / scope 3 (GHG protocol) ✔️
    • use (LCA) / scope 2 (GHG protocol) ✔️
    • manufacturing (LCA) / scope 3 (GHG protocol) ✔️
    • shipping (LCA) / scope 3 (GHG protocol) ❌
    • end of life (LCA) / scope 3 (GHG protocol) ❌
  • Abiotic ressources depletion (minerals), criteria called ADP or Abiotic Depletion Potential
    • resources extraction (LCA) ✔️
    • use (LCA) ✔️
    • manufacturing (LCA) ✔️
    • shipping (LCA) ❌
    • end of life (LCA) ❌
  • Primary energy usage : PE
    • resources extraction (LCA) ✔️
    • use (LCA) ✔️
    • manufacturing (LCA) ✔️
    • shipping (LCA) ❌
    • end of life (LCA) ❌

Packaging

Dependencies installation for packaging

python3 -m pip install --upgrade build twine

Building a new package release

Build the package from the root of the repository:

python3 -m build

Upload to test index first

Upload to test pip package index:

python3 -m twine upload --repository testpypi dist/*

Testing a new pip package release

Create a virtualenv, then activate it to isolate the package installation and its dependencies:

virtualenv boagent-venv
source boagent-venv/bin/activate

First install the dependencies from the main pip index:

pip install ...

Install the test package, enabling main index to get dependencies:

pip install -i https://test.pypi.org/simple/ boagent

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

boagent-0.2.0.tar.gz (139.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

boagent-0.2.0-py3-none-any.whl (138.4 kB view details)

Uploaded Python 3

File details

Details for the file boagent-0.2.0.tar.gz.

File metadata

  • Download URL: boagent-0.2.0.tar.gz
  • Upload date:
  • Size: 139.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for boagent-0.2.0.tar.gz
Algorithm Hash digest
SHA256 020d4e71af4a313516c39fe6af1296d60e8e03949fc1ed4781e5e401cdfa6edd
MD5 19885f3c8f409a60fa6d5b40d8600bda
BLAKE2b-256 c49cbca291aa2895ae992966ad5b1cf3216973a1c5c9886a108b3736feda0906

See more details on using hashes here.

File details

Details for the file boagent-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: boagent-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 138.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for boagent-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3bc2554dd3b37ac44b3f60e51da38a050649dc87228e88b05a6b0a371c20cbba
MD5 a2c032671340e4fbc64c3d597dc1ba29
BLAKE2b-256 9ed991362ef6ef494d9a62a96a57662fe1b093ccf5f10390747a34c7c393409e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page