Skip to content

How It Works

Yoichi Takizawa edited this page Feb 14, 2026 · 1 revision

How It Works

Authentication Flow

APC PowerChute Serial Shutdown for Business uses Java EE form-based authentication. The login flow is:

1. GET /status
   → Redirects to /logon (JSESSIONID cookie is set)

2. Check login page
   → If "already logged on" message: GET /logoff, then retry from step 1
   → Extract formtoken and formtokenid from hidden fields

3. POST /j_security_check
   Body: j_username, j_password, login, formtoken, formtokenid
   Content-Type: application/x-www-form-urlencoded
   → 302 redirect to /status (success) or /logon (failure)

4. GET /status
   → Returns the UPS status page HTML

Key Points

  • The POST to /j_security_check must not follow redirects automatically. The script uses a NoRedirectHandler to check the redirect location for success/failure.
  • PowerChute only allows one active session. If another session exists, the script detects the "already logged on" message and logs off first.
  • A CSRF token (formtoken / formtokenid) is required for login.

Data Extraction

The status page contains UPS data in <div> elements with specific IDs:

<div class="value" id="value_RealPowerPct">19.0</div>
<div class="unit">%</div>

<div class="value" id="value_RuntimeRemaining">29</div>
<div class="unit"></div>

The script uses a regex to extract values:

re.search(r'id="element_id"[^>]*>([^<]+)<', html)

Units are in separate elements and are not captured.

SSL Certificate

PowerChute uses a self-signed SSL certificate. The script disables certificate verification:

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

This is necessary because the certificate is not signed by a trusted CA.

Session Cleanup

The script always calls /logoff in a finally block to ensure the session is properly closed, even if an error occurs. This prevents the "already logged on" issue for subsequent runs.

Zabbix Sender

When --zabbix-send is used, data is sent via the zabbix_sender command using stdin (-i -):

hostname apc.status Online
hostname apc.load 19.0
hostname apc.runtime 29
hostname apc.voltage 102.0
hostname apc.battery 100.0
hostname apc.batteryvoltage 13.7

All items are sent in a single zabbix_sender invocation for efficiency.

Clone this wiki locally