Koozali.org: home of the SME Server

My Own DHCP Leases Server Panel

Offline rmclay

  • *
  • 10
  • +0/-0
My Own DHCP Leases Server Panel
« on: November 08, 2008, 12:02:05 AM »
Hi Guys,

I've cobbled together a quick and dirty active DHCP Leases script.  I've been using esmith-esi-lanlord on 6.x, but noticed the only version of this type of thing for SME 7.x was dhcpactiv.sh.  While this is a good script, I wanted to package my own script and create a server panel for it, as a lesson in coding.

So far I have the following script:

Code: [Select]
#!/bin/sh
##############################################################################
#         RMCLAY'S DHCP MANAGER        AN EXCERCISE IN SME 7.X CODING        #
##############################################################################
#                                                                            #
# v0.7 --- Almost done, also prints number of hours DHCP leases last.  Also  #
#          puts info into an html file using txt2html.                       #
# v0.6 --- Cleaner code, have started pulling hostname from dhcp.leases.     #
# v0.5 --- Now working out the ARP hostname & printing them to screen.       #
# v0.4 --- Printing a pretty table now.  Need to encode to table format.     #
# v0.2 --- Various sed & grep wiredness giving our output formatted, but no  #
#          table yet.                                                        #
#                                                                            #
# Done in stages, it's poor coding; but I need to understand it in 6 months. #
#                                                                            #
# TODO: replace 2008 with 08 (date +M), remove " from hostnames with sed, I  #
#       also need to make the html file into a server panel                  #
#                                                                            #
# Consider this script GPL V3                                                #
##############################################################################

# Read in the dhcp.conf file, and work out how long our leases last for
lease=`cat /etc/dhcpd.conf | grep default-lease | awk '{ print $2 }' | sed "s/;//g"`
sec=`echo $(($lease/3600))`

# Get current leases, remove the comments (### Blah) & put them on one line...
cat /var/lib/dhcp/dhcpd.leases | grep -v "#" | grep -v uid | grep -v bind | tr "{\n" '\t' | tr '}' '\n' > /tmp/tmp.$$

# clean the whitespace.  Make sure each line only has 1 space between columns
cat /tmp/tmp.$$ | tr '\t' ' ' | sed 's/^[ \t]*//;s/[ \t]*$//' > /tmp/tmp2.$$

# remove some text dhcpd likes, but we don't care for...
cat /tmp/tmp2.$$ | sed "s/starts//" | sed "s/ends//" | sed "s/hardware//" | sed "s/ethernet//" | sed "s/client-hostname//" | sed "s/tstp//" | tr ';' ' ' > /tmp/tmp.$$
cat /tmp/tmp.$$ | sed "s/ 0 //g" | sed "s/ 2 //g" | sed "s/ 3 //g" | sed "s/ 6 //g" | sed "s/lease//g" > /tmp/tmp3.$$
cat /tmp/tmp3.$$ | sed "s/   / /g" > /tmp/tmp2.$$

# show only the lines we want to see
cat /tmp/tmp2.$$ | awk '{ print $1" "$2" "$3" "$6" "$7 }' > /tmp/tmp3.$$

# Append the lease time in hrs to end of the info
rm /tmp/tmp.$$
while read info
 do
  echo "$info" "  $sec"hrs >> /tmp/tmp.$$
 done < /tmp/tmp3.$$

# Echo the header info into our leases list
echo > /tmp/test.$$
echo '[Current DHCP Leases:]' >> /tmp/test.$$
echo ':c' >> /tmp/test.$$
echo >> /tmp/test.$$
echo '[IP Address],[Hostname],[MAC Address],[Lease Obtained],[Lease Length]' >> /tmp/test.$$
cat /tmp/tmp.$$ | awk '{ printf "%-15s %-15s %-17s %-8s %-10s %-6s \n", $1, "," $5, "," $4, "," $2, " " $3, "," $NF }' > /tmp/tmp2.$$

# Sort the IP Addresses chronologically.  Then append to info text file
cat /tmp/tmp2.$$ | sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n | uniq -w 15 | grep hrs >> /tmp/test.$$

# This stuff enables txt2html to make a nice pretty table
echo ":tb" >> /tmp/test.$$

# Use txt2html to convert into a pretty table
txt2html /tmp/test.$$ > /tmp/rmclay_dhcpman.html

# clean up
rm /tmp/tmp.$$ /tmp/tmp2.$$ /tmp/tmp3.$$$ /tmp/test.$$


I admit my code is not the best :)  But what I am hoping to do is the following:

1.) Add the hostname to this list

2.) Create a server manager panel.  In order to do this, can I simply pipe the output of my script into a text file and display that in a server manager panel...?

Thanks,
Robert
« Last Edit: November 09, 2008, 03:24:37 PM by rmclay »

Offline gzartman

  • *
  • 306
  • +0/-0
    • LEI Engineering & Surveying
Re: My Own DHCP Leases Server Panel
« Reply #1 on: November 08, 2008, 04:22:30 AM »

I admit my code is not the best :)  But what I am hoping to do is the following:

1.) Add the hostname to this list

I'm sure there many ways to do this.  dig -x ip_address will return the hostname. 

2.) Create a server manager panel.  In order to do this, can I simply pipe the output of my script into a text file and display that in a server manager panel...?


No, it is not that simple.  server-manager panels are somewhat complex to create, especially if it is your first panel.  Expect to spend at least half a dozen hrs on your first effort at creating a server-manager panel.

You are basically wanting to report information in "friendly" format.  This, in and of itself, does not really warrant the effort to create a panel.   I believe you could achieve the same goal with a properly formatted log file placed in /var/log.  You could then use the View log files server-manager panel to view your DHCP IP lease log file.

Greg 
« Last Edit: November 08, 2008, 04:24:07 AM by gzartman »
----
Greg J. Zartman
LEI Engineering & Surveying

SME user and community member since 2000.

Offline FraunhoferIFF

  • *
  • 932
  • +0/-0
  • Widerstand ist Zwecklos !
    • http://www.magic-figo.de
Re: My Own DHCP Leases Server Panel
« Reply #2 on: November 08, 2008, 08:23:57 AM »
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"}'

save as dhcpaktiv.sh
chmod 775

Marcel

Offline rmclay

  • *
  • 10
  • +0/-0
Re: My Own DHCP Leases Server Panel
« Reply #3 on: November 09, 2008, 02:19:01 AM »
Hi Marcel,

I have previously used the dhcpactive script to append current leases to the log files with no problem.  Although as mentioned in my first message, I want to have a go at creating a server panel.  While this is overkill for such a small function; I also want to expand my knowledge of SME Server.

As things stand now, I have a script reporting in HTML format (you will need to download the txt2html source and compile form here: http://www.cs.sandia.gov/~sjplimp/download.html Then move your txt2html binary to /usr/local/bin (I will package up a contrib once everything is working...)

I have another question:  Is it possible to display this static html page in the server manager without having to encode everything in XML...?  If not, can someone perhaps help me with turning this into a contrib...?

Thanks again,
Robert

Offline janet

  • *****
  • 4,812
  • +0/-0
Re: My Own DHCP Leases Server Panel
« Reply #4 on: November 09, 2008, 03:44:56 AM »
rmclay

The Developer Guide (ie Manual) gives an introduction to creating web panels in a simple step by step example. Click Wiki at the top of the Forums.

Read the code for other panels for more advanced functionality & how to do it.
You can even copy the code for the nearest closest panel functionality and modify to suit.

If you want specific help to create a contrib, then open a bug report for a new Contrib, and ask for help there. Developers will want justification for the project though before they commit too much resources.
Please search before asking, an answer may already exist.
The Search & other links to useful information are at top of Forum.