OPNsense Forum

English Forums => General Discussion => Topic started by: pankaj on September 08, 2021, 06:29:47 am

Title: Need help tweaking this script [Solved]
Post by: pankaj on September 08, 2021, 06:29:47 am
In another thread on Monit How To - https://forum.opnsense.org/index.php?topic=5303.msg21546#msg21546 (https://forum.opnsense.org/index.php?topic=5303.msg21546#msg21546)

A user (@docb) posted this script that can be used for generating notifications for new DHCP leases:

Code: [Select]
#!/bin/csh
set FILE = "/var/dhcpd/var/db/dhcpd.leases"
set LeaseCount = `grep -c "binding state active" $FILE`
echo "Active LeaseCount: $LeaseCount"
if ($LeaseCount > 0) then
 set LINE = ( `grep -n "binding state active" $FILE | cut -d: -f1` )
 foreach L ($LINE)
  echo "##################"
 # set EL = `expr $L + 8`
  set CL = `expr $L - 5`
  while ( $CL < `expr $L + 8` )
   if ( `sed -n "$CL p" $FILE | cut -d ' ' -f1` == "lease" ) then
    echo IP: `sed -n "$CL p" $FILE | cut -d ' ' -f2`
   endif
   if ( `sed -n "$CL p" $FILE | cut -d ' ' -f3` == "starts" ) then
    echo Start: `sed -n "$CL p" $FILE | cut -d ' ' -f5,6`
   endif
   if ( `sed -n "$CL p" $FILE | cut -d ' ' -f3` == "ends" ) then
    echo Ende: `sed -n "$CL p" $FILE | cut -d ' ' -f5,6`
   endif
   if ( `sed -n "$CL p" $FILE | cut -d ' ' -f3` == "hardware" ) then
    echo MAC: `sed -n "$CL p" $FILE | cut -d ' ' -f5`
   endif
   if ( `sed -n "$CL p" $FILE | cut -d ' ' -f3` == "client-hostname" ) then
    echo Name: `sed -n "$CL p" $FILE | cut -d ' ' -f4-`
   endif
   if ( `sed -n "$CL p" $FILE | cut -d ' ' -f3` == "set" ) then
    echo Device: `sed -n "$CL p" $FILE | cut -d ' ' -f6-`
   endif
   if ( `sed -n "$CL p" $FILE | cut -d ' ' -f1` == "}" && $CL > $L ) then
    set CL = `expr $CL + 8`
   endif
   @ CL++
  end
 end
 exit 1
else
 exit 0
endif


I am using OpenVPN on a OPNSense machine and all VPN clients (rare logins) are assigned to a separate subnet.

Is there a way to limit the scope of monitoring to a specified subnet?
Thanks
Title: Re: Need help tweaking this script
Post by: pankaj on September 10, 2021, 04:53:57 am
There is way to use Monit to ping an IP address and generate alerts based off the outcome.