Intermediary EEM scripting: more fun with Power-over-Ethernet

In my last post I described how you can power off PoE on a range of switchports at certain times. But what if you’re lazy like me and don’t want to spend time figuring out which ports are utilizing Power-over-Ethernet? Can’t mr. switch do this himself? Well yes he can! Here you go:

event manager applet SelectivePowerOff
 action 0.0 cli command "enable"
 action 0.1 cli command "show power inline"
 action 0.2 foreach line "$_cli_result" "\n"
 action 1.1  regexp "^([^[:space:]]*)[[:space:]]*[^[:space:]]*[[:space:]]*on.*$" "$line" temp interface
 action 1.2  if $_regexp_result eq 1
 action 1.3   cli command "conf t"
 action 1.4   cli command "interface $interface"
 action 1.5   cli command "power inline never"
 action 1.6   syslog msg "Turned off PoE on $interface"
 action 1.7  end
 action 2.1 end

Basically what we’re doing here is using the “show power inline” command and processing it line by line. If we find an interface where the PoE operational status (column “Oper”) is on, we run the command “power inline never” on that interface. We also log a message to syslog telling we’ve turned off the power on that interface.

event manager applet SelectivePowerOn
 action 0.0 cli command "enable"
 action 0.1 cli command "show power inline"
 action 0.2 foreach line "$_cli_result" "\n"
 action 1.1  regexp "^([^[:space:]]*)[[:space:]]*off.*$" "$line" temp interface
 action 1.2  if $_regexp_result eq 1
 action 1.3   cli command "conf t"
 action 1.4   cli command "interface $interface"
 action 1.5   cli command "no power inline never"
 action 1.6   syslog msg "Turned on PoE on $interface"
 action 1.7  end
 action 2.1 end
 exit

Here we go through the same “show power inline” and check if the PoE administrative status (column “Admin”) is off. If it is, we run the command “no power inline never” on that interface. We also log a message telling we’ve turned on the power.

If you combine this with the event timer, you’ll have a fully automated solution.

Turning off Power-over-Ethernet ports at night to save power

Goal

Turn off Power-over-Ethernet on selected switch ports at selected times using the Cisco IOS EEM (Embedded Event Manager). Ultimately we want to reduce the energy consumption by turning off equipment that’s not being used at selected times.

In this example we have 7 switchports (FastEthernet 1/0/12 through 1/0/18) that have PoE equipment (phones) attached to them. As the company is not expecting calls to be conducted before 7:00 and after 22:00, there’s no need to keep the phones on, so we can power them off.

Prerequisites

For this configuration you’ll need one switch or router that:

  • has Power-over-Ethernet ports
  • supports EEM
  • has a correctly configured clock

Basically almost anything that runs Cisco IOS will have EEM. The configuration steps below are being done on a Catalyst 3750-48PS-S running IOS 12.2(55)SE8. As the events are time-based, your switch should have an accurate clock. Configure NTP first if you haven’t.

Configure the switch

The event manager works by way of applets: you define an applet which contains:

  • What to do: an action (or actions)
  • When to do it: an event (or events)

We’re going to need two applets: one that turns off the PoE at 22:00 and one that turns on the PoE at 7:00.

Start by defining a new applet, the one that’ll turn off the PoE. Let’s call it “PowerOff”:

l3s1(config-applet)#?
Event Manager Applet Entry Configuration Commands:
  action       Add or modify an action statement
  description  Add or modify an applet description
  event        Add or modify event information
  exit         Exit from Event Manager applet configuration submode
  help         Description of the interactive help system
  no           Negate a command or set its defaults
  trigger      Enter applet trigger configuration submode

l3s1(config-applet)#

As we look in the help we see two things that are important to us: “action” defining a set of actions this applet will execute, “event” which will describe when “action” will take place, the event being “time”.

Defining the actions

Notice the use of the label. The label dictates in which order the actions will be executed, in an ASCII sorted manner. You don’t have to use these particular labels, but how I’m using them seems to be a common way.

l3s1(config-applet)#action 0.0 cli command enable
l3s1(config-applet)#action 1.0 cli command conf t
l3s1(config-applet)#action 1.1 cli command interface range FastEthernet 1/0/12 - 18
l3s1(config-applet)#action 1.2 cli command power inline never
l3s1(config-applet)#action 2.0 cli command end

warning-145066_640 If you’re thinking about just turning off PoE on every port, keep in mind that “power inline never” can flap the port. So always exclude ports that link to devices that should not be disturbed (like as servers, uplinks to other switches, etc…).

Defining the events

Now that’s we’ve told the PowerOff applet what to do, now we need to tell it when to do it. The event we’re basing on is “time”, and the time specification is in cron format:

l3s1(config-applet)#event timer cron cron-entry "0 22 * * *"
l3s1(config-applet)#end
l3s1#

Let’s verify the result:

l3s1#show event manager policy registered
No.  Class     Type    Event Type          Trap  Time Registered           Secu  Name
1    applet    user    timer cron          Off   Sun Aug 24 12:23:16 2014  none  PowerOff
 cron entry {0 22 * * *}
 maxrun 20.000
 action 0.0 cli command "enable"
 action 1.0 cli command "conf t"
 action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
 action 1.2 cli command "power inline never"
 action 1.3 cli command "end"

l3s1#

We’re going to need to power on the equipment again. We’ll do that by way of a PowerOn applet:

l3s1#conf t
l3s1(config)#event manager applet PowerOn
l3s1(config-applet)#action 0.0 cli command "enable"
l3s1(config-applet)#action 1.0 cli command "conf t"
l3s1(config-applet)#action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
l3s1(config-applet)#action 1.2 cli command "no power inline never"
l3s1(config-applet)#action 2.0 cli command "end"
l3s1(config-applet)#event timer cron cron-entry "0 7 * * *"
l3s1(config-applet)#end
l3s1#

Verify:

l3s1#show event manager policy registered
No.  Class     Type    Event Type          Trap  Time Registered           Secu  Name
1    applet    user    timer cron          Off   Sun Aug 24 12:23:16 2014  none  PowerOff
 cron entry {0 22 * * *}
 maxrun 20.000
 action 0.0 cli command "enable"
 action 1.0 cli command "conf t"
 action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
 action 1.2 cli command "power inline never"
 action 1.3 cli command "end"

2    applet    user    timer cron          Off   Sun Aug 24 12:23:41 2014  none  PowerOn
 cron entry {0 7 * * *}
 maxrun 20.000
 action 0.0 cli command "enable"
 action 1.0 cli command "conf t"
 action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
 action 1.2 cli command "no power inline never"
 action 1.3 cli command "end"

l3s1#

Full configuration

event manager applet PowerOff
 event timer cron cron-entry "0 22 * * *"
 action 0.0 cli command "enable"
 action 1.0 cli command "conf t"
 action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
 action 1.2 cli command "power inline never"
 action 1.3 cli command "end"
event manager applet PowerOn
 event timer cron cron-entry "0 7 * * *"
 action 0.0 cli command "enable"
 action 1.0 cli command "conf t"
 action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
 action 1.2 cli command "no power inline never"
 action 1.3 cli command "end"

A more advanced time schedule

What if you wanted to turn off the PoE equipment on sundays, for the entire day?

As PoE is already being turned off every day at 22:00, we only need to prevent the PowerOn sequence on sundays. We can do this by adding a day-of-week to the cron schedule of the PowerOn applet:

l3s1(config)#event manager applet PowerOn
l3s1(config-applet)#event timer cron cron-entry "0 7 * * 1-6"
l3s1(config-applet)#end
l3s1#

Verify:

l3s1#show event manager policy registered
No.  Class     Type    Event Type          Trap  Time Registered           Secu  Name
1    applet    user    timer cron          Off   Sun Aug 24 12:23:16 2014  none  PowerOff
 cron entry {0 22 * * *}
 maxrun 20.000
 action 0.0 cli command "enable"
 action 1.0 cli command "conf t"
 action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
 action 1.2 cli command "power inline never"
 action 1.3 cli command "end"

2    applet    user    timer cron          Off   Sun Aug 24 12:26:51 2014  none  PowerOn
 cron entry {0 7 * * 1-6}
 maxrun 20.000
 action 0.0 cli command "enable"
 action 1.0 cli command "conf t"
 action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
 action 1.2 cli command "no power inline never"
 action 1.3 cli command "end"

l3s1#

Full configuration

event manager applet PowerOff
 event timer cron cron-entry "0 22 * * *"
 action 0.0 cli command "enable"
 action 1.0 cli command "conf t"
 action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
 action 1.2 cli command "power inline never"
 action 1.3 cli command "end"
 event manager applet PowerOn
event timer cron cron-entry "0 7 * * 1-6"
 action 0.0 cli command "enable"
 action 1.0 cli command "conf t"
 action 1.1 cli command "interface range FastEthernet 1/0/12 - 18"
 action 1.2 cli command "no power inline never"
 action 1.3 cli command "end"

Cron time format

A helpful reminder from wikipedia how cron time is specified:

cron