Skip to content

DHCP pool statistics from Kea DHCP API#3397

Merged
lunkwill42 merged 17 commits intomasterfrom
kea-dhcp-stats-cronjob
Aug 20, 2025
Merged

DHCP pool statistics from Kea DHCP API#3397
lunkwill42 merged 17 commits intomasterfrom
kea-dhcp-stats-cronjob

Conversation

@jorund1
Copy link
Copy Markdown
Collaborator

@jorund1 jorund1 commented Jun 19, 2025

Scope and purpose

Fixes #2931 and #2369. Deprecates #2937.

This pull request

  • Adds a package nav.dhcpstats where a Kea DHCP API client is defined in nav.dhcpstats.kea_dhcp.

  • Adds a cronjob script called navdhcpstats that runs every 5 minutes. It fetches DHCP stats using Kea DHCP API clients and stores the stats in graphite.

  • Adds a dhcpstats.conf file that configures which DHCP servers navdhcpstats should fetch from and how.

  • Stores stats as graphite metrics in nav.dhcp.4.pools.<server-name>.<pool-name>.<range-start>.<range-end>.{assigned,total,declined}.

    • Including <range-start> and <range-end> is necessary to map graphite metrics to NAV's internal Vlan and Prefix models. It also makes it easy to show the user how DHCP IP-ranges have changed over time.
    • By including <pool-name>, stats can be displayed/grouped on a per-pool basis such that any changes in <range-start> or <range-end> won't change how stats are displayed/grouped. Having a flexible scheme for how the names of pools are obtained (usually from a DHCP server's configuration), administrators should be able to choose for themselves what a <pool-name> represents, e.g. the name of a VLAN, the name of a subnet, the name of a set of IP-ranges, or the name of a single IP-range, and thus how stats are displayed/grouped.
    • By including <server-name>, there won't be any conflicts due to two independent DHCP servers being configured the same way. That is, there won't be any conflicts when NATed pools are named the same on two independent DHCP servers. Since <server-name> is specified in dhcpstats.conf we can ensure server names are unique whenever navdhcpstats is being used to store stats in graphite (the implementation in this PR does sort-of inherently ensure this, given that administrators follow advice given in dhcpstats.conf and abstain from using characters not supported by graphite/carbon).
  • Adds an explicit dependency to urllib3.

Contributor Checklist

  • Added a changelog fragment for towncrier
  • Added/amended tests for new/changed code
  • Added/changed documentation (documentation will be added in separate pull requests)
  • Linted/formatted the code with black and ruff, easiest by using pre-commit
  • The first line of the commit message continues the sentence "If applied, this commit will ...", starts with a capital letter, does not end with punctuation and is 50 characters or less long. See https://cbea.ms/git-commit/
  • This pull request is based on the correct upstream branch: For a patch/bugfix affecting the latest stable version, it should be based on that version's branch (<major>.<minor>.x). For a new feature or other additions, it should be based on master.
  • If applicable: Created new issues if this PR does not fix the issue completely/there is further work to be done.
  • If this results in changes in the UI: Added screenshots of the before and after.
  • If this adds a new Python source code file: Added the boilerplate header to that file

jorund1 added 4 commits June 18, 2025 23:21
...the client fetches stats through the Kea API which is connected to
a Kea DHCP server.
...stats are obtained from clients defined in nav.dhcpstats and sendt
to the Carbon backend.
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jun 19, 2025

Test results

   12 files     12 suites   12m 5s ⏱️
2 355 tests 2 355 ✅ 0 💤 0 ❌
6 600 runs  6 600 ✅ 0 💤 0 ❌

Results for commit 639601f.

♻️ This comment has been updated with latest results.

Copy link
Copy Markdown
Contributor

@johannaengland johannaengland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions, but mostly just nitpicks on typos/phrasing

Co-authored-by: Johanna England <johanna.england@sikt.no>
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Jul 6, 2025

@johannaengland johannaengland self-requested a review July 25, 2025 06:49
Copy link
Copy Markdown
Contributor

@johannaengland johannaengland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, but @lunkwill42 should have a look too, since I know very little about DHCP itself

Copy link
Copy Markdown
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reviewed the code over your shoulder a couple of times by now, so most of this should be good by now. Now we're just down to the nitty-gritty of make this a robust tool in production use...

@jorund1
Copy link
Copy Markdown
Collaborator Author

jorund1 commented Aug 19, 2025

Force pushing to fix history of last four commits

@jorund1 jorund1 force-pushed the kea-dhcp-stats-cronjob branch from 671a931 to 84ac480 Compare August 19, 2025 11:00
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 19, 2025

Codecov Report

❌ Patch coverage is 81.78571% with 51 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.96%. Comparing base (a526f75) to head (639601f).
⚠️ Report is 623 commits behind head on master.

Files with missing lines Patch % Lines
python/nav/bin/dhcpstats.py 53.42% 34 Missing ⚠️
python/nav/dhcpstats/kea_dhcp.py 91.23% 17 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #3397       +/-   ##
===========================================
+ Coverage        0   60.96%   +60.96%     
===========================================
  Files           0      608      +608     
  Lines           0    44509    +44509     
  Branches        0       43       +43     
===========================================
+ Hits            0    27135    +27135     
- Misses          0    17364    +17364     
- Partials        0       10       +10     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@lunkwill42 lunkwill42 self-requested a review August 20, 2025 08:56
Copy link
Copy Markdown
Member

@lunkwill42 lunkwill42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks nice, thank you! 🎉

We are still targeting Python 3.9 compatibility, but hopefully not
beyond 2025.
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Aug 20, 2025

Quality Gate Passed Quality Gate passed

Issues
0 New issues
2 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarQube Cloud

@lunkwill42 lunkwill42 merged commit 18b7a4e into master Aug 20, 2025
17 checks passed
@lunkwill42 lunkwill42 deleted the kea-dhcp-stats-cronjob branch August 20, 2025 09:35
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

Successfully merging this pull request may close these issues.

Fetch DHCP pool statistics from KEA Control Agent

3 participants