Version 14 of Cisco IOS Scripting with Tcl

Updated 2014-08-13 16:43:30 by jerryhobson

::cisco::eem::event_register_timer cron name gps_geofence.tcl cron_entry "0,15,30,45 * * * *" maxrun 55

##- # Copyright (c) 2014 Dan Frey <[email protected]> # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # Installation Steps # Place this file on the router media, typically the flash drive. # Execute these two CLI commands in global config mode: # event manager directory user policy "flash:/" # event manager policy <filename> type user # Date: Mar 7, 2014 # Summary - If the user has placed environment variables for latitude and/or # longitude use them for the home location. If not the router will use the # current location as the home location. The user should input the variable # and the format of the coordinates exactly like this. If the user does not # input the variables for LAT/LONG then the script will automatically add # these as environmental variables and use the current location for position. # If the home location needs to be reset the user must rem ove these two # environmental variables from the running config. # event manager environment _homelongitude 78 9 59 West # event manager environment _homelatitude 38 54 2 North # Router will find the current location and compare it to the home location. # Compare current to home location. # ($HOME_Lat - $CURRENT_lat) ($HOME_Long - $CURRENT_long) # If user inputs a boundary tolerance in seconds then use it. If it does not # exist then default to 4 seconds. If either difference is greater than # $_boundary or less than -$_boundary then the router is OUT OF BOUNDS. # event manager environment _boundary 4 # If router is out of bounds send a syslog message # If these two environment variables are present, then send an SMS message. # event manager environment _cellinterface cellular 0 # event manager environment _phonenumber 5403330168 # If these two variables are present then shutdown the interface. # Note: The _shutdown variable does not hold a value it only has to exist. # event manager environment _shutdown # event manager environment _cellinterface cellular 0 # Recovery from the shutting the interface will require manual intervention. # This EEM policy is built to manage the geofence of one 4G LTE eHWIC card or # the 800 series router (819) with embedded LTE. Additional logic will need # to be added to manage two or more eHWIC 4G LTE cards in a single router. # Version 0.5

namespace import ::cisco::eem::* namespace import ::cisco::lib::* array set _sinfo sys_reqinfo_routername set host $_sinfo(routername)

# Open the CLI if catch {cli_open} result {

   error $result $errorInfo

} else {

    array set cli1 $result

}

# Go into enable mode if catch {cli_exec $cli1(fd) "enable"} result {

    error $result $errorInfo

} if catch {cli_exec $cli1(fd) "conf terminal" } result {

        error $result $errorInfo

}

if {!info exists _homelatitude} { array set snmp_arr sys_reqinfo_snmp oid 1.3.6.1.4.1.9.9.661.1.4.1.1.1.4 get_type next set _homelatitude $snmp_arr(value) regexp {(\d+)\s+Deg\s+(\d+)\s+Min\s+(\d+)\s+Sec\s+(a-zA-Z+)} $_homelatitude match latdeg latmin latsec pole

if catch {cli_exec $cli1(fd) "event manager environment _homelatitude $latdeg $latmin $latsec $pole" } result {

        error $result $errorInfo

} action_syslog msg "GEOFENCE-6-LATITUDE $host home setting: $_homelatitude"

}

if {!info exists _homelongitude} { array set snmp_arr sys_reqinfo_snmp oid 1.3.6.1.4.1.9.9.661.1.4.1.1.1.5 get_type next set _homelongitude $snmp_arr(value) regexp {(\d+)\s+Deg\s+(\d+)\s+Min\s+(\d+)\s+Sec\s+(a-zA-Z+)} $_homelongitude match longdeg longmin longsec hsphere if catch {cli_exec $cli1(fd) "event manager environment _homelongitude $longdeg $longmin $longsec $hsphere" } result {

        error $result $errorInfo

} if catch {cli_exec $cli1(fd) "end" } result {

        error $result $errorInfo

} if catch {cli_exec $cli1(fd) "copy run start\r\r" } result {

        error $result $errorInfo

} action_syslog msg "GEOFENCE-6-LONGITUDE $host home setting: $_homelongitude"

} catch {cli_close $cli1(fd) $cli1(tty_id)}

if {!info exists _boundary} { set _boundary 4 }

#Open the CLI if catch {cli_open} result {

   error $result $errorInfo

} else {

    array set cli1 $result

}

# Go into enable mode if catch {cli_exec $cli1(fd) "enable"} result {

    error $result $errorInfo

}

# GET Current GPS coordinates

array set snmp_arr sys_reqinfo_snmp oid 1.3.6.1.4.1.9.9.661.1.4.1.1.1.4 get_type next set currentlatitude $snmp_arr(value) regexp {(\d+)\s+Deg\s+(\d+)\s+Min\s+(\d+)\s+Sec\s+(a-zA-Z+)} $currentlatitude match clatdeg clatmin clatsec cpole set clatsum expr ($clatdeg * 3600) + ($clatmin * 60) + $clatsec

array set snmp_arr sys_reqinfo_snmp oid 1.3.6.1.4.1.9.9.661.1.4.1.1.1.5 get_type next set currentlongitude $snmp_arr(value) regexp {(\d+)\s+Deg\s+(\d+)\s+Min\s+(\d+)\s+Sec\s+(a-zA-Z+)} $currentlongitude match clongdeg clongmin clongsec chsphere set clongsum expr ($clongdeg * 3600) + ($clongmin * 60) + $clongsec #Home location coordinates if catch {cli_exec $cli1(fd) "show run | inc event manager environment _homelatitude" } result {

        error $result $errorInfo

} regexp {event manager environment _homelatitude (\d+)\s+(\d+)\s+(\d+)\s+a-zA-Z+} $result match hlatdeg hlatmin hlatsec set hlatsum expr ($hlatdeg * 3600) + ($hlatmin * 60) + $hlatsec

if catch {cli_exec $cli1(fd) "show run | inc event manager environment _homelongitude" } result {

        error $result $errorInfo

} regexp {event manager environment _homelongitude (\d+)\s+(\d+)\s+(\d+)\s+a-zA-Z+} $result match hlongdeg hlongmin hlongsec set hlongsum expr ($hlongdeg * 3600) + ($hlongmin * 60) + $hlongsec

#Compare Home location to Current Location set latdiff expr $hlatsum - $clatsum set longdiff expr $hlongsum - $clongsum puts "latdiff = $latdiff and longdiff = $longdiff" if {$latdiff >= $_boundary || $longdiff >= $_boundary} { set condition 1 } if {$latdiff <= -$_boundary || $longdiff <= -$_boundary} { set condition 1 }

if {info exists condition} { action_syslog msg "GEOFENCE-4-OUTOFBOUNDS $host is outside the geofence boundary (current location is $currentlatitude $currentlongitude)"

if {info exists _phonenumber && info exists _cellinterface} { if catch {cli_exec $cli1(fd) "$_cellinterface lte sms send $_phonenumber $host OUTOFBOUNDS Current location is $currentlatitude $currentlongitude" } result {

        error $result $errorInfo

} }

if {info exists _shutdown && info exists _cellinterface} { if catch {cli_exec $cli1(fd) "conf terminal" } result {

        error $result $errorInfo

} if catch {cli_exec $cli1(fd) "interface $_cellinterface" } result {

        error $result $errorInfo

} if catch {cli_exec $cli1(fd) "shutdown" } result {

        error $result $errorInfo

} if catch {cli_exec $cli1(fd) "end" } result {

        error $result $errorInfo

} action_syslog msg "$host is outside the geofence boundary and $_cellinterface has been shutdown"

} } catch {cli_close $cli1(fd) $cli1(tty_id)}