This file is indexed.

/usr/sbin/powerwake-monitor is in powerwaked 2.21-0ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! /usr/bin/python
#
#    Copyright (C) 2011 Canonical Ltd.
#
#    Authors: Andres Rodriguez <andreserl@ubuntu.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os, sys, commands
from optparse import OptionParser
from powerwake import powerwake

global PKG
PKG = "powerwake-monitor"

# If not running as root, exit program and display message
if not os.geteuid()==0:
    sys.exit("This utility may only be run by the root user.")

def error(error):
    print " ERROR: %s" % error
    sys.exit(1)

def info(info):
    print " INFO: %s" % info

# Add IP to monitor
def add_host_to_monitor(powerwake, host, macaddr, monitor):
    # obtain the list of monitored machines
    host_to_mac = powerwake.get_monitored_hosts(monitor.lower())
    for ip, mac in host_to_mac.iteritems():
        if host == ip:
            info("Updating information for [%s] in [%s] monitor" % (host, monitor))
            mac = powerwake.get_mac_or_ip_from_arp(host)
            if not mac:
                error("Could not automatically determine MAC address for [%s]. Update cancelled." % host)
            host_to_mac[host] = mac
            powerwake.set_monitored_hosts(host_to_mac, monitor)
            return

    # add new
    # 1. If only IP is specified, try to automatically determine MAC
    if powerwake.is_ip(host) and macaddr is None:
        mac = powerwake.get_mac_or_ip_from_arp(host)
        if not mac:
            error("Could not automatically determine MAC Address for [%s]. Please specify MAC address." % host)
        # if mac given, look for ip
        host_to_mac[host] = mac

    # 2. If only MAC is specified, try to aumatically deftermine IP
    if powerwake.is_mac(host) and macaddr is None:
        ip = powerwake.get_mac_or_ip_from_arp(host)
        if not ip:
            error("Could not automatically determine IP address for [%s]. Please specify IP address." % host)
        host_to_mac[ip] = host

    # 3. If both MAC and IP are specified.
    if host and macaddr:
        info("Adding [%s - %s] to monitored hosts for [%s] monitor" % (host, macaddr, monitor))
        host_to_mac[host] = macaddr

    powerwake.set_monitored_hosts(host_to_mac, monitor)

def delete_host_from_monitor(powerwake, host, monitor):
    host_to_mac = powerwake.get_monitored_hosts(monitor.lower())
    for ip, mac in host_to_mac.iteritems():
        if host == ip or host == mac:
            info("Deleting %s from [%s] monitor" % (ip, monitor))
            del host_to_mac[ip]
            powerwake.set_monitored_hosts(host_to_mac, monitor)
            break
 
# List IP's to monitor
def list_monitored_hosts(powerwake, monitor):
    hosts = powerwake.get_monitored_hosts(monitor.lower())
    print "          HOST       -        MAC"
    try:
        for ip, mac in hosts.iteritems():
            print "%+20s - %-20s" % (ip, mac)
    except:
        error("Could not obtain monitored hosts")

def list_available_monitors(powerwake):
    print " Listing available PowerWaked Monitors\n"
    for monitor in powerwake.MONITORS:
        print " [enabled] %+20s" % monitor['monitor']

if __name__ == '__main__':
    powerwake = powerwake.PowerWake()
    hasOptions = False
    # Option Parser
    usage = "usage: %prog <parameters>\n\
            \n\
    %prog is a utility that allows to enable and disable available PowerNap\n\
    actions.\n\
    \n\
    %prog --add <IP> [-c <MAC>] [-m <monitor>]\n\
    %prog --del <IP> | <MAC> [-m <monitor>]\n\
    %prog --list [--monitor <monitor>]\n\
    %prog --list-monitors"

    parser = OptionParser(usage)
    parser.add_option('-a', '--add', action='store', type='string', dest='add',
                      help='Add host to Monitor', metavar='IP')
    parser.add_option('-d', '--del', action='store', type='string', dest='delete',
                      help='Delete monitored host', metavar='IP')
    parser.add_option('-c', '--mac', action='store', type='string', dest='mac',
                      help='specify MAC address', metavar='MAC', default=None)
    parser.add_option('-m', '--monitor', action='store', type='string', dest='monitor',
                      help='specify monitor to add host', metavar='MONITOR', default='general')
    parser.add_option('-l', '--list', action='store_true', dest='list', help='List monitored hosts')
    parser.add_option('-o', '--list-monitors', action='store_true', dest='list_monitors', help='List enabled monitors')

    (opt, args) = parser.parse_args()

    if opt.list and opt.add:
        error("Options -l (--list) and -e (--enable) are mutually exclusive")
        sys.exit(1)

    if opt.list and opt.delete:
        error("Options -l (--list) and -d (--disable) are mutually exclusive")
        sys.exit(1)

    if opt.add and opt.delete:
        error("Options -e (--enable) and -d (--disable) are mutually exclusive")
        sys.exit(1)

    if opt.add:
        hasOptions = True
        add_host_to_monitor(powerwake, opt.add, opt.mac, opt.monitor)

    if opt.delete:
        hasOptions = True
        delete_host_from_monitor(powerwake, opt.delete, opt.monitor)

    if opt.list:
        hasOptions = True
        list_monitored_hosts(powerwake, opt.monitor)

    if opt.list_monitors:
        hasOptions = True
        list_available_monitors(powerwake)

    if not hasOptions:
        print parser.get_usage()