Giter Club home page Giter Club logo

fritz's Introduction

fritz-log-parser

Actually more like fritz-log-saver, this repo started out as a parsing library.

Contributing

Contributions are welcome! Just open a pull request and I'll take a look as soon as I see it. Please make sure the following requrements are met:

  • If you change the database layout
    • Try to do so with a new migration using cargo sqlx migrate add --source ./data/migrations/ <MIGRATION-NAME> to not break existing databases.
    • Run cargo sqlx prepare so offline compilation still works.
  • Run cargo clippy and fix the warnings.
  • Make sure the code still compiles and builds as a docker container.

Note: The code in /src/db/connection.rs, specifically Database::append_new_logs tries to handle the weird way logs are saved in the FRITZ!Box. If you know a way to access the raw logs, please open an issue.

Environment variables

See Deploy section for an example configuration.

  • DATABASE_URL: See sqlx, must point to a SQLite database. Also see github.com/launchbadge/sqlx/issues/1114#issuecomment-827815038.
  • FRITZBOX_DOMAIN: Domain part of the FRITZ!Box URL. (e.g. 192.168.178.1 or fritz.box)
  • FRITZBOX_USERNAME: Username of the user this service should use.
  • FRITZBOX_PASSWORD: Password of the user this service should use.
  • FRITZBOX_REFRESH_PAUSE_SECONDS: How many seconds to wait between fetching logs.
  • FRITZBOX_ROOT_CERT_PATH: If you're using a custom certificate for the FRITZ!Box, you can set this path to point to the certificate of the CA (Certificate Authority) the certificate has been signed with. Otherwise all certificates will be accepted.
  • FRITZBOX_SAVE_RESPONSE: Whether to save responses received from the FRITZ!Box to a file, can be true or false or omitted.
  • FRITZBOX_SAVE_RESPONSE_PATH: A path to the folder where the received responses will be saved to, each response will be saved to its own file within this folder.

Deploy

1. Build the image

I run a local registry and use a script similar to this

#!/bin/bash
set -e

IMAGE_NAME=fritz-log-parser
REGISTRY=your.registry.com

docker build -t $IMAGE_NAME .
docker tag $IMAGE_NAME $REGISTRY/$IMAGE_NAME
docker push $REGISTRY/$IMAGE_NAME

2. Deploy the image

I use a docker-compose.yml similar to this

name: fritz
services:
  fritz:
    image: your.registry.com/fritz-log-parser
    restart: unless-stopped
    environment:
      TZ: Europe/Berlin
      DATABASE_URL: sqlite:///opt/fritz/database/logs.db3
      FRITZBOX_USERNAME: fritz6969
      FRITZBOX_PASSWORD: pASSword
      FRITZBOX_DOMAIN: 192.168.178.1
      FRITZBOX_ROOT_CERT_PATH: /opt/fritz/certificates/cert.pem
      FRITZBOX_REFRESH_PAUSE_SECONDS: 300
    volumes:
      - ./container-data/fritz/:/opt/fritz/

You can deploy it with docker compose -f docker-compose.yml up -d where -f specifies the compose file to use and -d starts the container in the background.

3. Check the logs

If you used the name from above you can check the logs with docker logs -f fritz-fritz-1 where -f specifies to follow the log output.

The logs should be similar to this

23:27:48 [WARN] couldn't load .env file: Io(Custom { kind: NotFound, error: "path not found" })
23:27:54 [INFO] login-challenge request to https://<OMITTED>/login_sid.lua?version=2 (GET - 200) took 5501ms (session-id: None)
23:27:55 [INFO] login-response request to https://<OMITTED>/login_sid.lua?version=2 (POST - 200) took 775ms (session-id: None)
23:27:55 [INFO] check-session-id request to https://<OMITTED>/login_sid.lua?version=2 (POST - 200) took 423ms (session-id: Some(<OMITTED>))
23:27:56 [INFO] logs request to https://<OMITTED>/data.lua (POST - 200) took 1122ms (session-id: Some(<OMITTED>))
23:27:56 [INFO] upserted 9 logs

4. Analyse the database

Fetch the database (see Commands section), open it using DataGrip, DB Browser for SQLite or anything else that works for you and run some queries.

Timezones

Need to set the TZ docker container environment variable to the same timezone as the FRITZ!Box because logs fetched from the FRITZ!Box are assumed to be in the chrono::Local Timezone.

For example, if the FRITZ!Box has the timezone Europe/Berlin set, TZ=Europe/Berlin should be passed to the docker container. This can be confirmed by running docker exec -it <CONTAINER-NAME> date.

Note: Before inserting logs into the database, they are converted from chrono::Local to chrono::Utc and when fetching logs from the database, they are assumed to be in chrono::Utc and will be converted to chrono::Local.

Commands

  • Create the database
    • cargo sqlx database setup --source ./data/migrations/ --sqlite-create-db-wal false
  • Reset the database
    • cargo sqlx database reset --source ./data/migrations/ --sqlite-create-db-wal false
  • Fetch the current database
    • scp <USER>@<HOST>:<PATH-TO-DB> <SAVE-PATH>

Queries

  • Convert timestamps to readable localtime
    • DATETIME(FLOOR(<FIELD-NAME> / 1000), 'unixepoch', 'localtime')
    • Divide by 1000 because timestamps have millisecond precision

Resources

fritz's People

Contributors

cryeprecision avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.