Discussion:
how to count IPs in use
Dima Fadeyev
2009-10-05 09:32:08 UTC
Permalink
Hi, everyone

I'd like to find out how many IPs are in use (from the point of view of
my dhcp server). First I thought it would just be a matter of counting
the leases that have "binding state active" in dhcpd.leases. However
there are quite a few leases that have this line, but also have the
"ends" date in the past. So, I'm a bit confused.

...
lease 81.143.246.98 {
starts 1 2009/10/05 09:24:16;
ends 1 2009/10/05 10:24:16;
binding state active;
...

# date
Mon Oct 5 11:26:16 CEST 2009

Please, could anyone give me a hand. How do I find a number of IPs in
use?

Thanks and best regards.
Holger Honert
2009-10-05 09:41:53 UTC
Permalink
Hi Dima,

http://dhcpstatus.sourceforge.net/

simple, elegant and useful (for me ;-))

Kind Regards

Holger
Post by Dima Fadeyev
Hi, everyone
I'd like to find out how many IPs are in use (from the point of view of
my dhcp server). First I thought it would just be a matter of counting
the leases that have "binding state active" in dhcpd.leases. However
there are quite a few leases that have this line, but also have the
"ends" date in the past. So, I'm a bit confused.
...
lease 81.143.246.98 {
starts 1 2009/10/05 09:24:16;
ends 1 2009/10/05 10:24:16;
binding state active;
...
# date
Mon Oct 5 11:26:16 CEST 2009
Please, could anyone give me a hand. How do I find a number of IPs in
use?
Thanks and best regards.
_______________________________________________
dhcp-users mailing list
https://lists.isc.org/mailman/listinfo/dhcp-users
SIGNAL Krankenversicherung a. G., Sitz: Dortmund, HR B 2405, AG Dortmund
IDUNA Vereinigte Lebensversicherung aG für Handwerk, Handel und Gewerbe,
Sitz: Hamburg, HR B 2740, AG Hamburg
Deutscher Ring Krankenversicherungsverein a.G., Sitz: Hamburg,
HR B 4673, AG Hamburg,
SIGNAL IDUNA Allgemeine Versicherung AG, Sitz: Dortmund, HR B 19108,
AG Dortmund
Vorstände: Reinhold Schulte (Vorsitzender),
Wolfgang Fauter (stellv. Vorsitzender), Dr. Karl-Josef Bierth,
Jens O. Geldmacher, Marlies Hirschberg-Tafel,
Michael Johnigk, Ulrich Leitermann, Michael Petmecky,
Dr. Klaus Sticker, Prof. Dr. Markus Warg
Vorsitzender der Aufsichtsräte: Günter Kutz
SIGNAL IDUNA Gruppe Hauptverwaltungen, Internet: www.signal-iduna.de
44121 Dortmund, Hausanschrift: Joseph-Scherer-Str. 3, 44139 Dortmund
20351 Hamburg, Hausanschrift: Neue Rabenstraße 15-19, 20354 Hamburg
Alan Buxey
2009-10-05 09:52:12 UTC
Permalink
Hi,
Post by Holger Honert
http://dhcpstatus.sourceforge.net/
simple, elegant and useful (for me ;-))
agreed. been using it for years. very useful to show
our paranoid folk that we have plenty or spare addresses
on their range and they dont need to apply for a bigger
subnet allocation. okay...had to redefine the configurations
so the $$ works for range titles...but small issue.

alan
Simon Hobson
2009-10-05 11:27:37 UTC
Permalink
Post by Dima Fadeyev
However
there are quite a few leases that have this line, but also have the
"ends" date in the past. So, I'm a bit confused.
...
lease 81.143.246.98 {
starts 1 2009/10/05 09:24:16;
ends 1 2009/10/05 10:24:16;
binding state active;
...
# date
Mon Oct 5 11:26:16 CEST 2009
11:26 CEST is 09:26 UTC, 09:26 is earlier than 10:24, therefore the
lease expiry time is still in the future (at the time you rote your
email).

At the top of your DHCP leases file you will find :

# All times in this file are in UTC (GMT), not your local timezone. This is
# not a bug, so please don't ask about it. There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature. If this is inconvenient or confusing to you, we sincerely
# apologize. Seriously, though - don't ask.
--
Simon Hobson

Visit http://www.magpiesnestpublishing.co.uk/ for books by acclaimed
author Gladys Hobson. Novels - poetry - short stories - ideal as
Christmas stocking fillers. Some available as e-books.
David W. Hankins
2009-10-06 17:14:15 UTC
Permalink
Post by Simon Hobson
# All times in this file are in UTC (GMT), not your local timezone. This is
# not a bug, so please don't ask about it. There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature. If this is inconvenient or confusing to you, we sincerely
# apologize. Seriously, though - don't ask.
This comment doesn't appear in the lease db anymore...not since we
added the db-time-format config option;

The db‐time‐format statement

db‐time‐format [ default | local ] ;

The DHCP server software outputs several timestamps when writing
leases to persistent storage. This configuration parameter selects
one of two output formats. The default format prints the day, date,
and time in UTC, while the local format prints the system seconds‐
since‐epoch, and helpfully provides the day and time in the system
timezone in a comment. The time formats are described in detail in
the dhcpd.leases(5) manpage.

So if you prefer to see local timestamps in dhcpd.leases, you may use
'local' setting, and it appears in a comment to the right of the date.

Note this will break any other tools that parse the information in the
old format (why we didn't make this the new default).


dhcpstatus is cool but when I'm in CLI, I often just "Control-R" and
find this in my bash history;

awk '/^lease / { curlease = $2; } /^ binding state/ { lstates[curlease] = $3; } END { for (curl in lstates) { tstates[lstates[curl]]++; } for (curs in tstates) { print curs, tstates[curs]; } }' /var/db/dhcpd.leases

It prints output like;

active; 48
free; 81
backup; 158

Which is useful to me as a spot-check.
--
David W. Hankins "If you don't do it right the first time,
Software Engineer you'll just have to do it again."
Internet Systems Consortium, Inc. -- Jack T. Hankins
Peter Laws
2009-10-07 13:39:44 UTC
Permalink
Post by David W. Hankins
dhcpstatus is cool but when I'm in CLI, I often just "Control-R" and
find this in my bash history;
awk '/^lease / { curlease = $2; } /^ binding state/ { lstates[curlease] = $3; } END { for (curl in lstates) { tstates[lstates[curl]]++; } for (curs in tstates) { print curs, tstates[curs]; } }' /var/db/dhcpd.leases
It prints output like;
active; 48
free; 81
backup; 158
Which is useful to me as a spot-check.
Very nice! One of my servers serves many networks and I'm really only
concerned about one of them. Adding the first three octets of the IP
address to "^lease " didn't seem to do the trick ... pointers?

Other than to the O'Reilly sed and awk book, I mean. :-)
--
Peter Laws / N5UWY
National Weather Center / Network Operations Center
University of Oklahoma Information Technology
***@ou.edu
-----------------------------------------------------------------------
Feedback? Contact my director, Craig Cochell, ***@ou.edu. Thank you!
Randall C Grimshaw
2009-10-07 14:12:16 UTC
Permalink
I tend to look in the log for the lease balance entries

Oct 7 00:20:01 mumble/mumble dhcpd: [ID 702911 local3.info] pool 804d50 8021x total 6210 free 490 backup 390 lts -50

there are some problems with this that make it difficult to automate, but a discerning eye can sort out the chaf. In this case the pool is 6210 with 880 available.... (5330 in use)

Feature request: With the ability to name pools added to the config options this could become very helpful.

Randy

________________________________________
From: dhcp-users-***@lists.isc.org [dhcp-users-***@lists.isc.org] On Behalf Of Peter Laws [***@ou.edu]
Sent: Wednesday, October 07, 2009 9:39 AM
To: Users of ISC DHCP
Subject: Re: Re: how to count IPs in use
Post by David W. Hankins
dhcpstatus is cool but when I'm in CLI, I often just "Control-R" and
find this in my bash history;
awk '/^lease / { curlease = $2; } /^ binding state/ { lstates[curlease] = $3; } END { for (curl in lstates) { tstates[lstates[curl]]++; } for (curs in tstates) { print curs, tstates[curs]; } }' /var/db/dhcpd.leases
It prints output like;
active; 48
free; 81
backup; 158
Which is useful to me as a spot-check.
Very nice! One of my servers serves many networks and I'm really only
concerned about one of them. Adding the first three octets of the IP
address to "^lease " didn't seem to do the trick ... pointers?

Other than to the O'Reilly sed and awk book, I mean. :-)

--
Peter Laws / N5UWY
National Weather Center / Network Operations Center
University of Oklahoma Information Technology
***@ou.edu
-----------------------------------------------------------------------
Feedback? Contact my director, Craig Cochell, ***@ou.edu. Thank you!
David Forrest
2009-10-07 15:01:28 UTC
Permalink
Post by Peter Laws
Post by David W. Hankins
dhcpstatus is cool but when I'm in CLI, I often just "Control-R" and
find this in my bash history;
awk '/^lease / { curlease = $2; } /^ binding state/ { lstates[curlease]
= $3; } END { for (curl in lstates) { tstates[lstates[curl]]++; } for (curs
in tstates) { print curs, tstates[curs]; } }' /var/db/dhcpd.leases
It prints output like;
active; 48
free; 81
backup; 158
Which is useful to me as a spot-check.
Very nice! One of my servers serves many networks and I'm really only
concerned about one of them. Adding the first three octets of the IP address
to "^lease " didn't seem to do the trick ... pointers?
Other than to the O'Reilly sed and awk book, I mean. :-)
Peter, Add a match statement for the subnet like this:

awk '/^lease / { curlease = $2; } /^ binding state/ { match(curlease,
/192.168.102/) && lstates[curlease] = $3; } END { for (curl in lstates)
{tstates[lstates[curl]]++; } for (curs in tstates) { print curs,
tstates[curs]; } }' /var/db/dhcpd.leases

Dave
--
David Forrest
St. Louis, Missouri
Peter Laws
2009-10-07 16:17:00 UTC
Permalink
awk '/^lease / { curlease =2; } /^ binding state/ { match(curlease,
/192.168.102/) && lstates[curlease] =3; } END { for (curl in lstates)
{tstates[lstates[curl]]++; } for (curs in tstates) { print curs,
tstates[curs]; } }' /var/db/dhcpd.leases
Thanks - this did it. The net in question is a /19, so my regular
expression was a little different, but it gives the answer I need.

Thanks, all!
--
Peter Laws / N5UWY
National Weather Center / Network Operations Center
University of Oklahoma Information Technology
***@ou.edu
-----------------------------------------------------------------------
Feedback? Contact my director, Craig Cochell, ***@ou.edu. Thank you!
John Hascall
2009-10-05 12:28:25 UTC
Permalink
Post by Dima Fadeyev
I'd like to find out how many IPs are in use (from the point of view of
my dhcp server). First I thought it would just be a matter of counting
the leases that have "binding state active" in dhcpd.leases. However
there are quite a few leases that have this line, but also have the
"ends" date in the past. So, I'm a bit confused.
[See previous replies about dhcpstatus]

If you are of a mind to 'roll your own' (we do because
we have it report into Zymon (nee hoBBit (nee BB))),
you need to know that a lease can be in 4 states:

free - not mentioned in lease file at all
abandoned - binding state abandoned
expired - binding state active/free & end is in the past
leased - binding state active & end is not in the past

note that multiple lease records for an ip address can
be found in the lease file - only the last one matters.

John
Timothy Gavin
2009-10-05 15:47:06 UTC
Permalink
If the UTC thing is confusing, you can find current UTC on a *nix machine with "date -u"

I just set a second clock up that always shows UTC.


------Original Message------
From: Simon Hobson
Sender: dhcp-users-***@lists.isc.org
To: Users of ISC DHCP
ReplyTo: Users of ISC DHCP
Subject: Re: how to count IPs in use
Sent: Oct 5, 2009 04:27
Post by Dima Fadeyev
However
there are quite a few leases that have this line, but also have the
"ends" date in the past. So, I'm a bit confused.
...
lease 81.143.246.98 {
starts 1 2009/10/05 09:24:16;
ends 1 2009/10/05 10:24:16;
binding state active;
...
# date
Mon Oct 5 11:26:16 CEST 2009
11:26 CEST is 09:26 UTC, 09:26 is earlier than 10:24, therefore the
lease expiry time is still in the future (at the time you rote your
email).

At the top of your DHCP leases file you will find :

# All times in this file are in UTC (GMT), not your local timezone. This is
# not a bug, so please don't ask about it. There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature. If this is inconvenient or confusing to you, we sincerely
# apologize. Seriously, though - don't ask.


--
Simon Hobson

Visit http://www.magpiesnestpublishing.co.uk/ for books by acclaimed
author Gladys Hobson. Novels - poetry - short stories - ideal as
Christmas stocking fillers. Some available as e-books.
_______________________________________________
dhcp-users mailing list
dhcp-***@lists.isc.org
https://lists.isc.org/mailman/listin
John Hascall
2009-10-07 15:14:39 UTC
Permalink
Post by Peter Laws
Post by David W. Hankins
dhcpstatus is cool but when I'm in CLI, I often just "Control-R" and
find this in my bash history;
awk '/^lease / { curlease = $2; } /^ binding state/ { lstates[curlease]
= $3; } END { for (curl in lstates) { tstates[lstates[curl]]++; } for (curs in
tstates) { print curs, tstates[curs]; } }' /var/db/dhcpd.leases

If only there were a way to file away these sorts of shell scripts.... :)
Post by Peter Laws
Very nice! One of my servers serves many networks and I'm really only
concerned about one of them. Adding the first three octets of the IP
address to "^lease " didn't seem to do the trick ... pointers?
The main problem with that is the /^ binding state/ pattern
will still match leases you don't care about. The other is
you may have forgotten that '.' is match-any-char in a
regular expression.

So, maybe something like:

awk '/^lease / {OK=0} /^lease 192\.168\.1\./ { OK=1; curlease = $2; } /^ binding state/ { if (OK) lstates[curlease] = $3; } END { for (curl in lstates) { tstates[lstates[curl]]++; } for (curs in tstates) { print curs, tstates[curs]; } }' /var/db/dhcpd.leases


John
David Forrest
2009-10-07 16:00:51 UTC
Permalink
Post by David W. Hankins
Post by David W. Hankins
dhcpstatus is cool but when I'm in CLI, I often just "Control-R" and
find this in my bash history;
awk '/^lease / { curlease = $2; } /^ binding state/ { lstates[curlease]
= $3; } END { for (curl in lstates) { tstates[lstates[curl]]++; } for (curs in
tstates) { print curs, tstates[curs]; } }' /var/db/dhcpd.leases
If only there were a way to file away these sorts of shell scripts.... :)
I cut this out of the email, pasted it into /usr/local/sbin/leases, and
made it executable.
--
David Forrest
St. Louis, Missouri
Continue reading on narkive:
Loading...