Blogs

Scripting How-To: poesched helps save energy and costs

By Erdem posted 08-14-2015 14:55

  
Overview

 

Disable PoE ports during non-business hours to save energy and cut costs. This applies to SLAX version 1.0 and higher.

Description

Save power and reduce costs by automatically disabling some of the network power consumption during non-business hours. Automatically turn power on and off to PoE ports on a specific schedule for defined locations.

Once activated, this script will be triggered once every hour, comparing the on and off configurations with the actual times of the configured locations. The PoE ports will be disabled and enabled according to the configured schedules.

Set Up

  1. Copy the file poesched.slax to the /var/db/scripts/event directory on the router.
  2. Use a text editor to create the configuration below. On the lines marked EDIT HERE, customize each of the off and on timeframes to match the location needs. The hours are recorded in 24-hour notation. Also change all interface names to match valid names.
    01 poe {
    02     apply-macro scheduler-admin {
    03         off 8; /* EDIT HERE */
    04         on 4; /* EDIT HERE */
    05     }
    06      apply-macro scheduler-finance {
    07         off 1; /* EDIT HERE */
    08         on 9; /* EDIT HERE */
    09     }
    10     interface ge-0/0/0 { /* EDIT HERE */
    11         apply-macro scheduler {
    12             group admin;
    13         }
    14 }
    15     interface ge-0/0/1 { /* EDIT HERE */
    16         apply-macro scheduler {
    17             group admin;
    18         }
    19     }
    20 }
  3. Save this configuration and copy it to the clipboard.
  4. Enable the script by adding the file statement and script filename to the [edit event-options event-script] hierarchy level in the configuration. Only superusers can enable scripts in the configuration.
    1 [edit event-options event-script]
    2 user@host# set file poesched.slax
  5. On the device, enter configuration mode:
    1 user@host% cli
    2 user@host> configure
    3 Entering configuration mode
    4 [edit]
    5 user@host#
  6. Use the load merge terminal command to load the configuration information and merge it with the current configuration:
    1 [edit]
    2 user@host# load merge terminal
    3 [Type ^D at a new line to end input]
    4 > Paste the contents of the clipboard here <
  7. At the prompt, paste the contents of the clipboard.
  8. Press Enter.
  9. Press Ctrl+D.
  10. Commit the configuration.
    1 [edit]
    2 user@host# commit and-quit
 

How to Run

 

This event script will be launched on the occurrence of the event poe-scheduler-start, which will be triggered once in every hour. For simplicity, this script will be executed within an hour of the specified time. It does not guarantee changes on the exact time.

Source Code

GitHub Links

Example Configuration

 
 
01
Step 1: Configure
02
 
03
a.      Copy the file 'poesched.slax' to '/var/db/scripts/event/'.
04
 
05
b.  Using a text editor, create following configuration and copy it to
06
the clipboard
07
    poe {
08
        apply-macro scheduler-admin {
09
            off 8; /* EDIT HERE */
10
            on 4; /* EDIT HERE */
11
        }
12
        apply-macro scheduler-finance {
13
            off 1; /* EDIT HERE */
14
            on 9; /* EDIT HERE */
15
        }
16
        interface ge-0/0/0 { /* EDIT HERE */
17
            apply-macro scheduler {
18
                group admin;
19
            }
20
        }
21
        interface ge-0/0/1 { /* EDIT HERE */
22
            apply-macro scheduler {
23
                group admin;
24
            }
25
        }
26
    }
27
 
28
NOTE: In the above mentioned configuration the time against 'on/off'
29
keywords need to be customized according to the need (It is in 24-hour
30
notation). The interface names should also be changed to valid interface
31
names (Look for the EDIT HERE keyword).
32
 
33
c.      Add the script to the [edit event-options event-script] hierarchy
34
level using the file statement. Only users in the superuser class can
35
configure the event scripts.
36
 
37
                [edit event-options event-script]
38
                user@host# set file poesched.slax
39
 
40
d.  On the device, enter configuration mode
41
                user@host% cli
42
                user@host> configure
43
                Entering configuration mode
44
 
45
                [edit]
46
                user@host#
47
e.  Load the configuration file:
48
        [edit]
49
                user@host# load merge terminal
50
f.  At the prompt, past the contents of the clipboard
51
        [edit]
52
        user@host# load merge terminal
53
        [Type ^D at a new line to end input]
54
        > Paste the contents of the clipboard here <
55
g.  Hit Enter
56
h.  Enter Ctrl+d
57
i.  Commit the configuration
58
                [edit]
59
            user@host# commit and-quit
60
 
61
Step 2: Run
62
This event script will get executed on the occurrence of the event
63
'poe-scheduler-start', which will be triggered once in every hour.
64
 
65
Note: An event is triggered every hour but not necessarily at the top
66
of the hour, it could trigger 15 minutes into every hour, 42 minutes into
67
every hour, etc.  Also, if the poe interface has a macro configured that
68
specifies that poe should be turned on/off during that hour then the
69
config will change.

SLAX Script Contents

 
001
/*
002
 * Author        : Patricio Giecco
003
 * Version       : 1.0
004
 * Last Modified :
005
 * JUNOS Release : 9.3 and above
006
 * Platform      : EX Series
007
 *
008
 * Description   : poesched.slax
009
 * This event script is executed on the occurrence of 'poe-scheduler-start'
010
 * event. According to the configuration mentioned in this file, under
011
 * 'event-definition', this event is triggered in every 1 hour and this
012
 * script get executed. This script disables/enables some interfaces for
013
 * poe, based on the configured schedule for a particular time.
014
 */
015
 
016
version 1.0;
017
 
018
ns junos = "http://xml.juniper.net/junos/*/junos";
019
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
020
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
021
 
022
import "../import/junos.xsl";
023
 
024
var $event-definition = {
025
    <event-options> {
026
        <generate-event> {
027
            <name> 'poe-scheduler-start';
028
            <time-interval> '3600';
029
        }
030
        <policy> {
031
            <name> 'poe-scheduler';
032
            <events> 'poe-scheduler-start';
033
            <then> {
034
                <event-script> {
035
                    <name> 'poesched.slax';
036
                }
037
            }
038
        }
039
    }
040
}
041
 
042
var $arguments = <argument> {
043
    <name> "time";
044
    <description> "execute the scheduled action at the specified time";
045
}
046
 
047
/*
048
 * This script is executed on the occurrence of event 'poe-scheduler-start'
049
 * which occurs in every one hour. On execution of this script POE on/off
050
 * on some ports based on a configured schedule. If the time was passed as
051
 * a parameter then use the passed value otherwise get the system time-date
052
 * and extract the time portion of it i.e. current time (only the hour).
053
 */
054
param $time = {
055
    var $uptime = jcs:invoke("get-system-uptime-information");
056
    var $date = $uptime//current-time/date-time;
057
     
058
    expr substring($date, 12, 2);
059
}
060
 
061
match / {
062
 
063
    /*
064
     * Get the poe configuration
065
     */
066
    var $poe-rpc = <get-configuration> {
067
        <configuration> {
068
            <poe>;
069
        }
070
    }
071
 
072
    var $poe = jcs:invoke($poe-rpc);
073
 
074
    /*
075
     * Open a connection with mgd
076
     */
077
    var $con = jcs:open();
078
 
079
    if (not($con)) {
080
        <xnm:error> {
081
            <message> "Not able to connect with local mgd";
082
        }
083
    }
084
 
085
    /*
086
     * Generate the xml formated config delta
087
     */
088
    var $xml := {
089
        <configuration> {
090
            <poe> {
091
                /*
092
                 * Go through all the groups that are scheduled at this time
093
                 */
094
                for-each ($poe//apply-macro[starts-with(name, "scheduler-") &&
095
                          data/value == number($time)]) {
096
                    var $splitName = jcs:regex("scheduler-(.+)", name);
097
                    var $group = $splitName[2];
098
                    var $action = data[value == number($time)]/name;
099
 
100
                    /*
101
                     * select all the interfaces belonging to the sceduled
102
                     * groups
103
                     */
104
                    for-each ($poe//interface[apply-macro/name == "scheduler" &&
105
                              apply-macro/data/name == "group" &&
106
                              apply-macro/data/value == $group]) {
107
                        if ($action == "on" && disable) {
108
                            <interface> {
109
                                <name> name;
110
                                <disable delete = "delete">;
111
                            }
112
                        } else if ($action == "off" && not(disable)) {
113
                            <interface> {
114
                                <name> name;
115
                                <disable>;
116
                            }
117
                        }
118
                    }
119
                }
120
            }
121
        }
122
    }
123
     
124
    /*
125
     * If some interface statements are present in the $xml/configuration/poe,
126
     * then commit the changes otherwise not required
127
     */
128
    if ($xml/configuration/poe/interface) {
129
        var $results = {
130
            call jcs:load-configuration($connection = $con,
131
                                        $configuration = $xml);
132
        }
133
    }
134
}

XML Script Contents

 
01 <?xml version="1.0"?>
02 <script>
03 <title>poesched.slax</title>
04 <author>pgiecco</author>
05 <synopsis>
06 POE schedule management. Customer can disable POE during non-working hours to save energy and cut costs.
07 </synopsis>
08 <coe>event</coe>
09 <type>poe</type>
10  
11 <description>
12 This event script is executed on the occurrence of 'poe-scheduler-start' event. This event is triggered in every 1 hour and this script get executed. This script disables/enables some interfaces for poe, based on the configured schedule for a particular time.
13    
14  
15 </description>
16  
17  <example>
18  <title>Example</title>
19  <config>example-1.conf</config>
20  </example>
21  
22 <xhtml:script xmlns:xhtml="http://www.w3.org/1999/xhtml"
23 src="../../../../../web/leaf.js"
24 type="text/javascript"/>
25 </script>

#eventscript
#POE
#Slax
#How-To
#ethernet-switching
#ScriptingHow-To