Ubuntu Pastebin

Paste from octoid at Fri, 23 Jan 2015 12:27:29 +0000

Download as text
  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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/bin/sh
# /etc/sensorsd/lowbattery.sh
# sensorsd script for querying Xorg user on remaining battery capacity.

### Provided by sensorsd(8)
xuser="$1"
sd_status="$2"
sd_current="$3"
sd_low="$4"

### Use sysctl(8) values instead of apm(8).
use_sysctl="YES" # "NO"

## Required sysctls
# AC power indicator
sc_ac="hw.sensors.acpiac0.indicator0"
# CRITICAL battery capacity
sc_bat_crit="hw.sensors.acpibat0.watthour2"
# Override CRITICAL battery capacity returned value
sc_bat_crit_override="2.00 Wh"

## Optional sysctls, for percentage/minute calculation.
sc_bat_rate="hw.sensors.power0"
sc_bat_capacity="hw.sensors.watthour0"

action() {
    local msg_type=$1
    local msg_text=$2
    local log_msg="lowbattery: ${msg_type}: ${msg_text}"

    echo "${log_msg}" | /usr/bin/logger

    case "${msg_type}" in
        WARNING|ERROR)
            echo "${log_msg}" | /usr/bin/wall
            ;;
        CRITICAL)
            echo "${log_msg}" | /sbin/shutdown -p +60
            ;;
        IGNORE)
            ;;
        SLEEP)
            /usr/sbin/apm -z
            ;;
        HIBERNATE)
            /usr/sbin/apm -Z
            ;;
        HALT)
            /sbin/shutdown -p now
            ;;
    esac
}

query_sc() {
    # Trim sysctls to device level.
    sc_ac_device="${sc_ac%.*}"
    sc_bat_device="${sc_bat_crit%.*}"

    # Populate sysctl array
    set -A sc_array $(/usr/sbin/sysctl "${sc_ac_device}" "${sc_bat_device}")

    # Extract available sysctl values.
    if [ "${#sc_array[@]}" -le 1 ]; then
        action "ERROR" "Failed to retrieve sensor values from /usr/sbin/sysctl."
        exit 1
    else
        for sc_elem in "${sc_array[@]}"; do
            case "${sc_elem%=*}" in
                "${sc_ac}")
                    sc_ac_val="${sc_elem#*=}"
                    ;;
                "${sc_bat_crit}")
                    sc_bat_crit_val="${sc_elem#*=}"
                    ;;
                "${sc_bat_rate}")
                    sc_bat_rate_val="${sc_elem#*=}"
                    ;;
                "${sc_bat_capacity}")
                    sc_bat_capacity_val="${sc_elem#*=}"
                    ;;
            esac
        done
    fi

    # Check AC power status
    [[ ${sc_ac_val} == On* ]] && ac_online="YES" || ac_online="NO"

    # Check battery critical status (use override value if set).
    [ "${sc_bat_crit_override}" ] && sc_bat_crit_val="${sc_bat_crit_override}"
    bat_crit_check=$(echo "${sd_current}-${sc_bat_crit_val%% *}" | /usr/bin/bc)
    [[ ${bat_crit_check} == -* ]] && bat_crit="YES" || bat_crit="NO"

    # Check battery remaining percentage/minutes if required sysctls set.
    if [ "${sc_bat_rate_val}" -a "${sc_bat_capacity_val}" ]; then
        bat_mins=$(echo "scale=2;(${sd_current}/${sc_bat_rate_val})*60"\
            | /usr/bin/bc)
        bat_mins="${bat_mins%.*}"
        bat_percent=$(echo "scale=2;(${sd_current}/${sc_bat_capacity_val})*100"\
            | /usr/bin/bc)
        bat_percent="${bat_percent%.*}"
    else
        bat_mins="unknown"
        bat_percent="unknown"
    fi
    bat_text="Battery ${bat_percent}% (${bat_mins} mins)"
}

query_apm() {
    # Populate apm array.
    set -A apm_array $(/usr/sbin/apm -ablm)
    if [ $? -ne 0 ]; then
        action "ERROR" "/usr/bin/apm returned non-zero exit status."
        exit 1
    else
        # 0=disconnected, 1=connected, 2=backup power, 255=unknown.
        apm_ac_status="${apm_array[3]}"
        # 0=high, 1=low, 2=critical, 3=charging, 4=absent, 255=unknown.
        apm_bat_status="${apm_array[2]}"
        apm_bat_percent="${apm_array[1]}"
        apm_bat_minutes="${apm_array[0]}"
    fi

    # Set ac/bat vars, battery status
    [ "${apm_ac_status}" = @(1|2) ] && ac_online="YES" || ac_online="NO"
    [ "${apm_bat_status}" = 2 ] && bat_crit="YES" || bat_crit="NO"
    bat_mins="${apm_bat_minutes}"
    bat_percent="${apm_bat_percent}"
    bat_text="Battery ${bat_percent}% (${bat_mins} mins)"
}

verify_xuser() {
    local msg_type=$1

    if xuser_entry=$(/usr/bin/getent passwd "${xuser}"); then
        xuser_home=$(echo "${xuser_entry}" | /usr/bin/cut -d : -f 6)
        if [ ! -s "${xuser_home}/.Xauthority" ]; then
            action "${msg_type}" "${bat_text}, no current Xorg user ${xuser}."
            return 1
        else
            return 0
        fi
    else
        action "${msg_type}" "${bat_text}, unknown user ${xuser}."
        return 1
    fi
}

xmsg() {
    local msg_type=$1

    if ! verify_xuser "${msg_type}" && exit 1

    xmsg_env="/usr/bin/env DISPLAY=:0 XAUTHORITY=${xuser_home}/.Xauthority"
    xmsg_cmd="/usr/X11R6/bin/xmessage -center -default Ignore \
        -buttons Ignore:2,Sleep:3,Hibernate:4,Halt:5 -timeout 20"
    xmsg_text="'${msg_type}: ${bat_text}'"

    /usr/bin/su "${xuser}" -c "${xmsg_env} ${xmsg_cmd} ${xmsg_text}"

    case "$?" in
        0)
            # $xuser failed to respond to dialog
            action "${msg_type}" "${bat_text}, Xorg user ${xuser} failed to \
                respond to warning dialog."
            ;;
        1)
            # xmessage reported an error status
            action "${msg_type}" "${bat_text}, /usr/X11R6/bin/xmessage \
                returned an error status."
            ;;
        2)
            # $xuser selected 'ignore'
            action "IGNORE" "${bat_text}, Xorg user ${xuser} opted to \
                ignore warning dialog."
            ;;
        3)
            # $xuser selected 'sleep'
            action "SLEEP" "${bat_text}, Xorg user ${xuser} opted to enter \
                suspend."
            ;;
        4)
            # $xuser selected 'hibernate'
            action "HIBERNATE" "${bat_text}, Xorg user ${xuser} opted to enter \
                hibernate."
            ;;
        5)
            # $xuser selected 'halt'
            action "HALT" "${bat_text}, Xorg user ${xuser} opted to halt the \
                system."
            ;;
    esac
}

check_sensorsd_text() {
    case "${sd_status}" in
        above|within)
            # No action required
            ;;
        below)
            # Battery below warning threshold, check AC and critical status
            [ "${use_sysctl}" = "NO" ] && query_apm || query_sc
            if [ "${ac_online}" = "NO" ]; then
                [ "${bat_crit}" = "NO" ] && xmsg "WARNING" || xmsg "CRITICAL"
            fi
            ;;
        invalid|uninitialised)
            # Recieved an invalid status from sensorsd, report if no AC
            [ "${use_sysctl}" = "NO" ] && query_apm || query_sc
            if [ "${ac_online}" = "NO" ]; then
                action "ERROR" "sensord reported invalid/uninitialised status."
            fi
            ;;
        *)
            # Recieved null/unexpected status from sensorsd, report if no AC
            [ "${use_sysctl}" = "NO" ] && query_apm || query_sc
            if [ "${ac_online}" = "NO" ]; then
                action "ERROR" "sensord reported unexpected status."
            fi
            ;;
    esac
}


lockfile="/var/tmp/lowbattery.pid"
if ( set -C; echo "$$" > "$lockfile" 2> /dev/null; then
    trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
    check_sensorsd_text
    rm -f "$lockfile"
    trap - INT TERM EXIT
else
    echo "LOCKED: $lockfile, $(cat $lockfile)"
fi
Download as text