Koozali.org: home of the SME Server

Obsolete Releases => SME Server 7.x => Topic started by: TeNeCo on March 03, 2007, 12:30:42 PM

Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 03, 2007, 12:30:42 PM
I'm using SME 7.1.1 in gateway mode. How can I track the work of its DHCP Server? I would like to know the given IP-address, the DNS-Name, the lease time.
I've added sme7admin.

Further I would like to force certain IP addresses for certain MAC addresses.
Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 03, 2007, 01:24:51 PM
dhcpd activity is logged in /var/log/dhcpd/current

To configure dhcp reservations use "Hostnames and addresses" under "Configuration" in the server-manager.
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 03, 2007, 02:55:08 PM
Quote from: "mmccarn"
dhcpd activity is logged in /var/log/dhcpd/current


OK, found it, thanks. But it would be very helpfull to have a summary, a table of the actual IP adresses in the server-manager.
Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 03, 2007, 07:52:48 PM
I can't find any way to list the active dhcp leases in server-manager; you might consider opening a 'feature request' in the bug tracker for this.

Here's a script that (at least on my SME 7.0 system) shows your active leases:
Code: [Select]
#!/bin/sh
#
# dhcpactiv.sh
#
# First, collapse all dhcp leases onto one line
# Second, extract the 'active' entries,
# Last, simplify the output format
#
sed -e :a -e '$!N;s/\n\ / /;ta' -e 'P;D' /var/lib/dhcp/dhcpd.leases \
  | grep active \
  | sed -e 's/{.*ends ./expiration/' \
        -e 's/\;  binding.*ethernet/ address/' \
        -e 's/\;.*client-hostname \"/ name /' \
        -e 's/\"\;//'
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 03, 2007, 09:24:21 PM
OK, thanks; but could you please explain me how to use that script.
Title: how to track the work of the DHCP Server?
Post by: byte on March 03, 2007, 10:48:59 PM
Quote from: "TeNeCo"
OK, thanks; but could you please explain me how to use that script.


Just create a new file make it excuteable and hey presto!
Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 03, 2007, 10:50:48 PM
Option 1:
Option 2:pico -w ~/dhcpactiv.sh
copy and paste the contents of the script from here to your command shell
press ^X to close pico, saving your file as you exit
run using sh ~/dhcpactiv.sh[/list]Here's another version of the script that listsarp -a[/list]
(Note: This version must be saved as a script using "Option 2" or the output is unreadable...)
Code: [Select]
#!/bin/sh
#
# dhcpactiv.sh
#
# v.004
# - rewritten again to pull data from dhcpd files according to labels instead of position
#
# v.003
# - add code to remove tstp information from dpcpd.leases
#
# v.002
# - rewritten to use awk more for parsing
# - added "GMT" indicator to Expiration col head
#
echo "Source        Host       MAC Address       IP Address      Expiration (GMT)"
echo "============= ========== ================= =============== ================"
#
awk ' { out = ""} \
      { $1=="lease"||$1=="client-hostname" ? out=" " $2 : out=out } \
      { $1=="binding"||$1=="hardware" ? out= " " $3: out=out } \
      { $1=="ends"? out=" " $3 " " $4: out=out } \
      { $1=="}"? out="\n": out=out } \
      { printf out," " }' /var/lib/dhcp/dhcpd.leases \
  | grep active \
  | sed -e s/'[{};" ]'/\ /g  \
  | awk '{ printf "%-13s %-10s %-17s %-15s %-10s %-5s\n", "dhcpd.leases", $6, $5, $1, $2, $3 }'

#
# Now do the same for /etc/dhcpd.conf
#
 awk ' { out = ""}
       { $1=="host"||$1=="fixed-address" ? out=" " $2 : out=out } \
       { $1=="hardware" ? out= " " $3: out=out } \
       { $1=="}"? out="\n": out=out } \
       { printf out," " }' /etc/dhcpd.conf \
  | grep : \
  | sed -e  s/'[{};\" ]'/\ /g -e  s/\.`config get DomainName`// \
  | awk  '{ printf "%-13s %-10s %-17s %-15s %-15s \n", "dhcpd.conf", $1, $2, $3, "reservation"}'
#
# Finally, grab the current arp table
#
arp -a \
  |  sed -e s/\\..*\(/\ / -e s/\)// \
  |  awk '{ printf "%-13s %-10s %-17s %-15s %-15s \n", "arp", $1, $4, $2, "n/a"}'

Title: how to track the work of the DHCP Server?
Post by: byte on March 03, 2007, 10:52:24 PM
Or do as mmccarn mentions  8)
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 04, 2007, 12:45:36 PM
Fine, thanks a lot. I took option 2 and the second script. Fine thanks. There are only two flaws:

the sonyvaio in the first line is currently offline, the MAC address is missing and the hostname moves to the MAC-address column. And there is a strange ( in the last line:

Why gets the formatting always lost in a posting?

Source        Host       MAC Address       IP Address      Expiration
============= ========== ================= =============== ================
dhcpd.leases             sonyvaio          192.168.2.239   2007/03/04 12:17:31
dhcpd.leases  BRN_72BB06 00:80:77:72:bb:06 192.168.2.238   2007/03/04 21:43
dhcpd.leases  Wireless   00:11:6b:b0:11:35 192.168.2.240   2007/03/05 00:48
dhcpd.leases  WSUSSERVER 00:0b:6a:08:3c:16 192.168.2.176   2007/03/05 08:52
dhcpd.leases  BRN_72BB06 00:80:77:72:bb:06 192.168.2.238   2007/03/05 09:43
arp           pc-00176   00:0B:6A:08:3C:16 192.168.2.176   n/a
arp           localhost  00:15:0C:9B:D0:75 (192.168.178.2  n/a
Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 04, 2007, 04:30:45 PM
Quote
the sonyvaio in the first line is currently offline
The DHCP lease doesn't depend on the computer being connected and will be listed as "active" until the lease time expires.  The "default-lease-time" in  /etc/dhcpd.conf is 24 hours and is controlled by the template-fragment /etc/e-smith/templates/etc/dhcpd.conf/25LeaseTimeDefault.  Also, the "Expiration" time listed by my script is GMT, not your local time zone - this is how dhcpd tracks leases.

Quote
the MAC address is missing and the hostname moves to the MAC-address column
Oops; I suspect my script-writing abilities are at fault here... Please run
Code: [Select]
sed -e :a -e '$!N;s/\n\ / /;ta' -e 'P;D' /var/lib/dhcp/dhcpd.leases   | grep active and post the results back here; I'll try to figure out what's up.

Quote
And there is a strange ( in the last line:
Yes, I see it. Now you know why I don't write bash scripts professionally!  if you run arp -a and post the results I'll try to fix that, too.  Do you know why "localhost" is at "192.168.178.2" - this seems a bit odd to me...

Quote
Why gets the formatting always lost in a posting?
This is a feature of the forum (probably because html works this way, too...).  You have to surround text with "[code]" tags to use a fixed-width font, preserve white space, and prevent automatic line wrap. I use "[code]...[/code]" tags for long lines of code or output where line wrapping would be dangerous, and "" for short commands or references to files on the SME server.
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 04, 2007, 08:02:59 PM
lease 192.168.2.238 {  starts 0 2007/03/04 09:43:47;  ends 1 2007/03/05 09:43:47;  tstp 1 2007/03/05 09:43:47;  binding state active;  next binding state free;  hardware ethernet 00:80:77:72:bb:06;  client-hostname "BRN_72BB06";
lease 192.168.2.239 {  starts 0 2007/03/04 13:34:06;  ends 1 2007/03/05 13:34:06;  tstp 1 2007/03/05 13:34:06;  binding state active;  next binding state free;  hardware ethernet 00:11:6b:60:e5:c6;  uid "\001\000\021k`\345\306";  client-hostname "sonyvaio";
lease 192.168.2.240 {  starts 0 2007/03/04 14:18:31;  ends 1 2007/03/05 14:18:31;  tstp 1 2007/03/05 14:18:31;  binding state active;  next binding state free;  hardware ethernet 00:11:6b:b0:11:35;  uid "\001\000\021k\260\0215";  client-hostname "Wireless Access Point";
lease 192.168.2.176 {  starts 0 2007/03/04 18:22:13;  ends 1 2007/03/05 18:22:13;  binding state active;  next binding state free;  hardware ethernet 00:0b:6a:08:3c:16;  uid "\001\000\013j\010<\026";  client-hostname "WSUSSERVER";

arp -a is

fritz.fonwlan.box (192.168.178.2) at 00:15:0C:9B:D0:75 [ether] on eth1
pc-00176.teneco.homeftp.net (192.168.2.176) at 00:0B:6A:08:3C:16 [ether] on eth0

"localhost" is at "192.168.178.2":

This is the address of the "FritzBox" infront of the SME; it's own external IP is 192.168.178.3
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 04, 2007, 09:05:50 PM
ahhh - voilĂ :

Code: [Select]

Source        Host       MAC Address       IP Address      Expiration
============= ========== ================= =============== ================
dhcpd.leases             BRN_72BB06        192.168.2.238   2007/03/05 09:43:47
dhcpd.leases             sonyvaio          192.168.2.239   2007/03/05 13:34:06
dhcpd.leases  Access     Wireless          192.168.2.240   2007/03/05 14:18:31
dhcpd.leases  WSUSSERVER 00:0b:6a:08:3c:16 192.168.2.176   2007/03/05 18:22
arp           fritz      00:15:0C:9B:D0:75 192.168.178.2   n/a
arp           pc-00176   00:0B:6A:08:3C:16 192.168.2.176   n/a
Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 04, 2007, 10:23:31 PM
"localhost" is supposed to be a special case that always points to "127.0.0.1" as far as I know.  I don't know what happens when it points to 192.168.178.2...

[edit 3/6/07]
The script that was in this post has been moved to the bug tracker and can now be viewed at http://bugs.contribs.org/attachment.cgi?id=947 or downloaded directly to your SME server using:
wget -O dhcpactiv.sh http://bugs.contribs.org/attachment.cgi?id=947

[/edit]
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 04, 2007, 10:52:17 PM
Quote from: "mmccarn"
"localhost" is supposed to be a special case that always points to "127.0.0.1" as far as I know.  I don't know what happens when it points to 192.168.178.2...

I can find this address in SME only as the external gateway address.
Here the new layout:

Code: [Select]

[root@gateway ~]# sh ~/dhcpactiv.sh
Source        Host       MAC Address       IP Address      Expiration (GMT)
============= ========== ================= =============== ================
dhcpd.leases  00:80:77:72:bb:06 state             192.168.2.238   2007/03/05 09:43:47
dhcpd.leases  00:11:6b:60:e5:c6 state             192.168.2.239   2007/03/05 13:34:06
dhcpd.leases  00:11:6b:b0:11:35 state             192.168.2.240   2007/03/05 14:18:31
dhcpd.leases  00:0b:6a:08:3c:16 state             192.168.2.176   2007/03/05 18:22:13
dhcpd.leases  sonyvaio   00:11:6b:60:e5:c6 192.168.2.239   2007/03/05 21:35:08
dhcpd.leases  sonyvaio   00:11:6b:60:e5:c6 192.168.2.239   2007/03/05 21:36:01
dhcpd.leases             00:80:77:72:bb:06 192.168.2.238   2007/03/05 21:37:18
arp           localhost  00:15:0C:9B:D0:75 (192.168.178.2  n/a
arp           pc-00176   00:0B:6A:08:3C:16 192.168.2.176   n/a
[root@gateway ~]#

Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 05, 2007, 05:30:59 AM
OK - I've updated the earlier script again.  This time I'm pulling the fields out of the dhcpd files by fieldname instead of trying to pull them out with sed.
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 05, 2007, 08:31:46 AM
Code: [Select]

Source        Host       MAC Address       IP Address      Expiration (GMT)
============= ========== ================= =============== ================
dhcpd.leases  WSUSSERVER 00:0b:6a:08:3c:16 192.168.2.176   2007/03/05 18:22:13
dhcpd.leases  BRN_72BB06 00:80:77:72:bb:06 192.168.2.238   2007/03/05 21:37:18
dhcpd.leases  Wireless   00:11:6b:b0:11:35 192.168.2.240   2007/03/06 03:48:37
dhcpd.leases  sonyvaio   00:11:6b:60:e5:c6 192.168.2.239   2007/03/06 06:26:34
dhcpd.leases  WSUSSERVER 00:0b:6a:08:3c:16 192.168.2.176   2007/03/06 06:40:51
dhcpd.leases  Sempron    00:01:6c:d8:f4:24 192.168.2.241   2007/03/06 06:47:32
arp           pc-00176   00:0B:6A:08:3C:16 192.168.2.176   n/a
arp           localhost  00:15:0C:9B:D0:75 (192.168.178.2  n/a


I think that I have solved the miracle of the starnge localhost: I have given the sme server the name "gateway" but the local gateway ist that router at 192.168.178.2. Although I've changed the name of SME this morning the entry for that local host is still in de shell- output.
Title: how to track the work of the DHCP Server?
Post by: soprom on March 05, 2007, 02:50:33 PM
Also see contrib "Sme7admin". It has what you are looking for.
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 05, 2007, 03:18:45 PM
Quote from: "soprom"
Also see contrib "Sme7admin". It has what you are looking for.

Where? I have that Sme7admin but I can't find the actual IP-adresses with its windows names in there like here:
Code: [Select]

Source        Host       MAC Address       IP Address      Expiration (GMT)
============= ========== ================= =============== ================
dhcpd.leases  BRN_72BB06 00:80:77:72:bb:06 192.168.2.238   2007/03/05 21:37:18
dhcpd.leases  Wireless   00:11:6b:b0:11:35 192.168.2.240   2007/03/06 03:48:37
dhcpd.leases  sonyvaio   00:11:6b:60:e5:c6 192.168.2.239   2007/03/06 06:26:34
dhcpd.leases  WSUSSERVER 00:0b:6a:08:3c:16 192.168.2.176   2007/03/06 06:40:51
dhcpd.leases  Sempron    00:01:6c:d8:f4:24 192.168.2.241   2007/03/06 06:47:32
dhcpd.leases  Wireless   00:11:6b:b0:11:35 192.168.2.240   2007/03/06 09:15:06
dhcpd.leases  BRN_72BB06 00:80:77:72:bb:06 192.168.2.238   2007/03/06 09:37:25
arp           fritz      00:15:0C:9B:D0:75 192.168.178.2   n/a
arp           pc-00176   00:0B:6A:08:3C:16 192.168.2.176   n/a
Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 05, 2007, 03:35:22 PM
Quote from: "soprom"
Also see contrib "Sme7admin"

Doh! I use sme7admin on several hosts -- but I'm not using any of them for dhcp so I never noticed!
Title: how to track the work of the DHCP Server?
Post by: bpivk on March 05, 2007, 07:34:57 PM
I get the folowing error:
Code: [Select]
Source        Host       MAC Address       IP Address      Expiration (GMT)
============= ========== ================= =============== ================
awk: cmd. line:1:  { out = ""} \
awk: cmd. line:1:              ^ backslash not last character on line
test.sh: line 24: syntax error near unexpected token `|'
'est.sh: line 24: `  | grep active \
Title: how to track the work of the DHCP Server?
Post by: Tib on March 06, 2007, 04:43:18 AM
Quote
Also see contrib "Sme7admin". It has what you are looking for


Not quite ... it displays host name as eg: pc-00249.tibors.net ... it does not give you the pc name.

If SME7admin displayed the PC name as well it would be complete in my book.

I requested the creator to put that in when it was first made but nothing happened.

Regards,

Tib
Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 06, 2007, 03:23:42 PM
bpivk:

Make sure your 'test.sh' hasn't acquired any spaces after the various backslashes in the script...  the backslash is used to "escape" the <newline>, but if there's a space after it then it escapes that, instead, and breaks the command...
Title: how to track the work of the DHCP Server?
Post by: bpivk on March 06, 2007, 06:07:58 PM
Well i did a copy & paste in windows and linux twice and i had the same problem. But i won't give up. :D
Title: how to track the work of the DHCP Server?
Post by: RedBeard on March 06, 2007, 07:30:42 PM
Quote from: "bpivk"
Well i did a copy & paste in windows and linux twice and i had the same problem. But i won't give up. :D


bpivk, I had the same problem.  Pasting puts an extra space at the end of the line.  Open the file with pico or other editor and go to the end of each line with a / and delete any spaces after the / and save the file.  

It should work fine after that at least it did for me.
Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 06, 2007, 08:29:38 PM
I have double-checked the post with the script on page one and don't see any extraneous spaces in the post itself; I don't know why they're showing up when you copy/paste the script into pico...
Title: how to track the work of the DHCP Server?
Post by: bpivk on March 06, 2007, 08:50:51 PM
Yes the problem was in the empty spaces in pico. I had to go from line to line and delete them. It works now.

Thanks
Title: how to track the work of the DHCP Server?
Post by: CharlieBrady on March 06, 2007, 10:52:31 PM
Quote from: "mmccarn"
OK - I've updated the earlier script again.


Mike (I think) could you please post your code into a New Feature Request in the bug tracker. It'll just get lost here.
Title: how to track the work of the DHCP Server?
Post by: mmccarn on March 07, 2007, 01:10:07 AM
New Feature Request created: http://bugs.contribs.org/show_bug.cgi?id=2641

Script can be downloaded from: http://bugs.contribs.org/attachment.cgi?id=947

Or, download directly to your SME server using:
wget -O dhcpactiv.sh http://bugs.contribs.org/attachment.cgi?id=947
Title: how to track the work of the DHCP Server?
Post by: calisun on March 08, 2007, 01:48:38 AM
install sme7admin contrib, it gives you the information you need plus a lot more.
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 11, 2007, 09:13:18 PM
One more question:

how can I influence the work of the DHCP-Server? It would be nice if a certain MAC address would get always the same IP from the DHCP server.
Title: how to track the work of the DHCP Server?
Post by: CharlieBrady on March 11, 2007, 10:37:35 PM
Quote from: "TeNeCo"

It would be nice if a certain MAC address would get always the same IP from the DHCP server.


Hostnames and addresses panel. Have you read the User Manual?
Title: how to track the work of the DHCP Server?
Post by: TeNeCo on March 18, 2007, 09:48:42 AM
Fine.

And how can I add the call of the script to the suface of the server-manager?
Title: how to track the work of the DHCP Server?
Post by: cjensen on May 13, 2007, 06:40:59 AM
For the server-manager to display your dhcp data you would require a 'panel' (see the dev manual).

I have a nice script which will display this info in a browser.  Let me know if you are interested.

Craig
Title: Re: how to track the work of the DHCP Server?
Post by: rick1908 on December 07, 2007, 08:24:51 PM
What i did was:

Create a directory called /opt/dhcpactive

Put the dhcpactiv.sh script in there:

Code: [Select]
#!/bin/sh
#
# dhcpactiv.sh
#
# v.004
# - rewritten again to pull data from dhcpd files according to labels instead of position
#
# v.003
# - add code to remove tstp information from dpcpd.leases
#
# v.002
# - rewritten to use awk more for parsing
# - added "GMT" indicator to Expiration col head
#
date

echo "Source        Host                      MAC Address            IP Address      Expiration (GMT)"
echo "============= ========================= ====================== =============== ================"
#
awk ' { out = ""} \
      { $1=="lease"||$1=="client-hostname" ? out=" " $2 : out=out } \
      { $1=="binding"||$1=="hardware" ? out= " " $3: out=out } \
      { $1=="ends"? out=" " $3 " " $4: out=out } \
      { $1=="}"? out="\n": out=out } \
      { printf out," " }' /var/lib/dhcp/dhcpd.leases \
  | grep active \
  | sed -e s/'[{};" ]'/\ /g  \
  | awk '{ printf "%-13s %-25s %-22s %-15s %-10s %-5s\n", "dhcpd.leases", $6, $5, $1, $2, $3 }'

#
# Now do the same for /etc/dhcpd.conf~
#
 awk ' { out = ""}
       { $1=="host"||$1=="fixed-address" ? out=" " $2 : out=out } \
       { $1=="hardware" ? out= " " $3: out=out } \
       { $1=="}"? out="\n": out=out } \
       { printf out," " }' /etc/dhcpd.conf \
  | grep : \
  | sed -e  s/'[{};\" ]'/\ /g -e  s/\.`config get DomainName`// \
  | awk  '{ printf "%-13s %-25s %-22s %-15s %-15s \n", "dhcpd.conf", $1, $2, $3, "reservation"}'
#
# Finally, grab the current arp table
#
arp -a \
  |  sed -e s/\\..*\(/\ / -e s/\)// \
  |  awk '{ printf "%-13s %-25s %-22s %-15s %-15s \n", "arp", $1, $4, $2, "n/a"}'

Note I changed the script ever so slightly to include the current date/time in the output as well as making the columns for the output just a little bit wider so it was easier to read.

Then created another script called dhcplog:
Code: [Select]
#!/bin/sh
#
# Creats a log file from the dhcpactiv.sh script
./dhcpactiv.sh > ./dhcpleases.now


Which simply writes the output to a file in the /opt/dhcpactive directory.

Then, I created a symbolic link from /opt/dhcpactive/dhcpleases.now to /var/log

Code: [Select]
ln -s /opt/dhcpactive/dhcpleases.now /var/log/dhcpleases.now
Finally, I used Stephen Nobels dungog-cron contrib. http://www.dungog.net/wiki/Dungog-cron (http://www.dungog.net/wiki/Dungog-cron) to create a cron job that runs every 5 mins to update the /opt/dhcpactive/dhcpleases.now file. I can now take a look at it in the server-manager Administration -> View log files panel.

-Rick