Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistance total traffic (Tx/Rx Bytes) #221

Open
AuthorShin opened this issue Jan 29, 2025 · 2 comments
Open

Persistance total traffic (Tx/Rx Bytes) #221

AuthorShin opened this issue Jan 29, 2025 · 2 comments

Comments

@AuthorShin
Copy link

Hello,
Thank you for making and maintaining this great tool.
I wan wondering if there is a way to monitor/log total traffics that being passed on each interface I mean Tx/Rx Bytes which is on Winbox is under Traffic tab for each Interface or /interface> print stats & /interface> print stats-detail via CLI and have them persistent so if device get rebooted it doesn't reset the counted traffic and reset them on the dashboard.

@M0r13n
Copy link
Contributor

M0r13n commented Feb 2, 2025

That is something that I am also interested in. Currently, mktxp operates in a stateless manner, relying solely on the Mikrotik router's counters as the data source. However, these counters reset after every router reboot, and since the script does not persist any state, it cannot accurately track cumulative metrics over time. Additionally, restarting the script itself results in a loss of continuity.

To address this, I propose a solution where the script writes its state (e.g., the last counter value and cumulative total) to a local file. This file-based persistence allows the script to resume tracking accurately after restarts or router reboots, without requiring external dependencies like a database. The implementation would detect counter resets and adjust the cumulative total accordingly, ensuring accurate metrics for Prometheus.

This approach keeps the script lightweight and standalone while solving the continuity issue.

I am thinking about something like this:

# Load the last known state from a file
def load_state():
    # Read from a local file (e.g., JSON or plain text)
    return {"last_value": 0, "total": 0}

# Save the current state to a file
def save_state(state):
    # Write the state (e.g., last_value, total) to a local file
    pass

# Fetch the current counter from the Mikrotik API
def fetch_counter():
    # Stub for API call to fetch the current counter value
    return current_counter

# Update the total bytes, handling counter resets
def update_total(state, current_counter):
    if current_counter < state["last_value"]: 
         # Detect reset
        state["total"] += state["last_value"]  # Add last known value to total
    state["total"] += current_counter - state["last_value"]  # Update total
    state["last_value"] = current_counter  # Update last value
    save_state()
    return state

The only real downside would be the added dependency on a local file that acts somewhat like a database, with concerns around file integrity, permissions, and portability. However, these issues seem solvable. I wonder what @akpw thinks of this. :)

@akpw
Copy link
Owner

akpw commented Feb 13, 2025

@M0r13n thanks for the detailed analysis. As to my take, adding persistent state this way could be problematic for use cases with distributed setups, etc. Being a stateless exporter has its virtues, keeping extra complexity at bay. Might be cleaner to implement this as a separate solution, then expose a modified data source for mktxp to connect to without knowing the extra data handling aspects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants