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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
|
Introduction
============
The PyNUT module provides an abstraction class `PyNUTClient` written in
Python, which allows to connect to a NUT (Network UPS Tools) server and
execute different commands without an application developer needing to
know the NUT communication protocol. It also includes a small self-test
application as a consumer of the module.
The project was originally started in 2008 by David Goncalves <david@lestat.st>
at https://www.lestat.st/informatique/projets/pynut and was soon added to
the main NUT codebase and further evolved there.
The `PyNUTClient` class was born from another project the original author
had -- a graphical application to monitor and manage the UPSes connected
to a NUT server. It is now known as `NUT-Monitor` and is also provided
along with NUT sources.
With that GUI application being written in Python too, it was decided
to split the project in two parts (the PyNUT class + GUI application,
serving as model + view) in order to keep the two parts independent.
That class was subsequently renamed to `PyNUTClient` since version 1.1
of the original project, allowing to develop other classes nearby later.
It is published to PyPI repository as
link:https://pypi.org/project/PyNUTClient/[PyNUTClient]
and so should be installable with `pip` tooling.
Note that the file name of the distribution tarball is lower-cased per
PEP-0625 requirements, while the module name is camel-cased historically.
Currently the module is regularly tested to work with Python interpreter
versions 2.7, 3.4, 3.5, 3.7, 3.11 and 3.13.
[NOTE]
======
Text fields returned by methods are byte sequences (not locale-aware
strings), except NUT protocol error codes quoted into `PyNUTError`
exceptions which are decoded into strings as originally `ascii` text.
This is of little concern for Python 2, but is important in Python 3.
The `ups` argument to methods is handled as a string, so conversion may
be needed to compare with names returned in the lists and dictionaries,
e.g.:
----
strUps = bUps.decode('ascii')
bUps = strUps.encode('ascii')
----
Only the names returned by `GetUPSNames()` method specifically are
converted into string type.
Since Python 3 support was added just recently, module code may later be
converted to use string types like `str` or `unicode` in the returned
dictionaries and sets instead, if the community deems this to be more
idiomatic.
Examples below *do not* specify the `b'some text'` markup that would be
pedantically correct (for Python 3 at least).
======
.List of methods in the class
------
class PyNUTClient :
def __init__( self, host='127.0.0.1', port=3493,
login=None, password=None, debug=False, timeout=5 ) :
def help( self ) :
def ver( self ) :
def DeviceLogin( self, ups="" ) :
def FSD( self, ups="" ) :
def CheckUPSAvailable( self, ups="" ) :
def GetRWVars( self, ups='' ) :
def GetUPSCommands( self, ups='' ) :
def GetUPSList( self ) :
def GetUPSNames( self ) :
def GetUPSVars( self, ups='' ) :
def ListClients( self, ups = None ) :
def RunUPSCommand( self, ups='', command='' ) :
def SetRWVar( self, ups='', var='', value='' ) :
------
The module also provides the `PyNUTError` class to represent any exceptions
raised by `PyNUTClient` logic.
Documentation
-------------
Although the original project was written in French, for the reasons of general
distribution with NUT, all of its code is commented in English. While this file
contains the description from https://www.lestat.st/informatique/projets/pynut
the class `PyNUTClient` is compatible with the Python module PyDOC, so you can
type `pydoc PyNUT` to obtain succinct documentation on your current version of
the module.
For more examples see the provided test script and the NUT-Monitor application
in the NUT sources.
Commands below are listed in alphabetic order.
__init__
~~~~~~~~
When you initialize the class instance, it performs the connection to the NUT
data server or raises a Python exception from the `__connect()` method called
internally.
help
~~~~
Sends the `HELP` command to the NUT data server and returns whatever bits of
wisdom it has to offer.
ver
~~~
Sends the `VER` command to the NUT data server and returns its self-reported
identification such as version, product or distribution it may be bundled with.
Note that the NUT client interactions should not rely on reported versions,
but follow the protocol as defined.
DeviceLogin
~~~~~~~~~~~
Establish a "client" session with the specified UPS. This uses credentials
specified earlier to `__init__()` this class instance, and on server side
(in `upsd.users` file) these credentials must have one of `upsmon` roles.
The command should return `OK` if everything went well, or raise an exception
in case of failure, such as invalid or insufficiently privileged credentials.
Note there is no `DeviceLogout()`, just the general connection termination.
FSD
~~~
Send the FSD (Forced ShutDown) command to the specified UPS.
The command should return `OK` if everything went well, or raise an exception
in case of failure, such as that this server does not allow to manage that UPS
as a "primary" (or "master" before NUT 2.8.0).
CheckUPSAvailable
~~~~~~~~~~~~~~~~~
Returns a boolean state whether the specified UPS is recognized and available
(`True`), or is not (`False`).
Internally, requests a listing of commands supported by the device name, and
evaluates the server response.
.Example
-----
import PyNUT
ups = PyNUT.PyNUTClient( host='server', login='upsadmin', password='upspass' )
result = ups.CheckUPSAvailable( ups='ups1' )
print( result )
>> True
-----
See also: `GetUPSCommands()`
GetRWVars
~~~~~~~~~
Returns a list of modifiable variables on the specified UPS, as a dictionary
of "variable"-"current value" pairs.
.Example
-----
import PyNUT
ups = PyNUT.PyNUTClient( host='server', login='upsadmin', password='upspass' )
result = ups.GetRWVars( ups='ups1' )
print( result )
>> {'battery.date': '10/25/07', 'ups.id': 'test'}
-----
See also: `GetUPSVars()`, `SetRWVar()`
GetUPSCommands
~~~~~~~~~~~~~~
Returns a list of commands supported by the specified UPS.
Note that certain commands are not usable without first getting necessary
rights on the NUT data server.
The result is presented as a dictionary of "command"-"English description"
pairs.
.Example
-----
import PyNUT
ups = PyNUT.PyNUTClient( host='server', login='upsadmin', password='upspass' )
result = ups.GetRWVars( ups='ups1' )
print( result )
>> {'test.battery.start' : 'Start a battery test',
'calibrate.stop' : 'Stop run time calibration',
'shutdown.stayoff' : 'Turn off the load and remain off',
'test.battery.stop' : 'Stop the battery test',
'test.panel.start' : 'Start testing the UPS panel',
'calibrate.start' : 'Start run time calibration',
'load.off' : 'Turn off the load immediately',
'test.failure.start' : 'Start a simulated power failure',
'shutdown.return' : 'Turn off the load and return when power is back'}
-----
See also: `RunUPSCommand()`
GetUPSList
~~~~~~~~~~
Returns the list of UPSes represented by the NUT server, as a dictionary of
"name"-"description" pairs.
.Example
-----
import PyNUT
ups = PyNUT.PyNUTClient( host='server', login='upsadmin', password='upspass' )
result = ups.GetUPSList()
print( result )
>> {'UPS1': 'Smart UPS 3000 File server',
'UPS2': 'Smart UPS 1000 Serveur de mail'}
-----
GetUPSNames
~~~~~~~~~~~
Returns just the list of available UPS names from the NUT server.
The result is a set of `str` objects (comparable with `ups="somename"` and
useful as arguments to other methods). Helps work around Python2/Python3
string API changes (where `b'string' != 'string'` and not even a type
comparable to it), and is primarily used as a helper internally in some
methods.
.Example
-----
import PyNUT
ups = PyNUT.PyNUTClient( host='Serveur', login='upsadmin', password='upspass' )
result = ups.GetUPSNames()
print( result )
>> ['UPS1', 'UPS2']
-----
GetUPSVars
~~~~~~~~~~
Returns a list of all variables on the specified UPS, as a dictionary
of "variable"-"current value" pairs.
.Example
-----
import PyNUT
ups = PyNUT.PyNUTClient( host='Serveur', login='upsadmin', password='upspass' )
result = ups.GetUPSVars( ups='UPS1' )
print( result )
>> {'input.transfer.high' : '253',
'battery.charge' : '100.0',
'ups.mfr' : 'APC',
'battery.voltage.nominal' : '024',
'input.transfer.reason' : 'S',
'ups.test.interval' : '1209600',
'input.transfer.low' : '208',
'output.voltage' : '234.0',
'driver.version' : '2.2.1-',
'battery.charge.restart' : '00',
'ups.id' : 'test',
'driver.parameter.pollinterval' : '2',
'driver.parameter.port' : '/dev/ttyS0',
'battery.voltage' : '27.10',
'ups.test.result' : 'NO',
'ups.status' : 'OL',
'battery.date' : '10/25/07',
'ups.model' : 'Smart-UPS SC1000',
'ups.serial' : 'XXXXXXXXXXXX',
'output.voltage.nominal' : '230',
'ups.mfr.date' : '10/25/07',
'driver.version.internal' : '1.99.8',
'input.voltage' : '234.0',
'battery.runtime.low' : '120',
'input.sensitivity' : 'H',
'ups.load' : '001.9',
'driver.name' : 'apcsmart',
'input.voltage.maximum' : '234.0',
'input.frequency' : '50.00',
'ups.delay.shutdown' : '060',
'ups.delay.start' : '000',
'input.voltage.minimum' : '232.0',
'input.quality' : 'FF',
'battery.runtime' : '29040',
'ups.firmware' : '737.3.I',
'battery.alarm.threshold' : '0'}
-----
See also: `GetRWVars()`
ListClients
~~~~~~~~~~~
Returns the list of connected clients (such as the `NUT-Monitor` application
or an `upsmon` service) from the NUT server, for a particular UPS or all of
them by default.
The result is a dictionary containing "upsname"-"client list" pairing 'UPSName'
and a list of clients for each device if the information was retrieved from
the NUT data server successfully, or an exception is raised otherwise.
RunUPSCommand
~~~~~~~~~~~~~
Executes the specified command on the specified UPS. It should be one of the
commands returned by the `GetUPSCommands()` function.
Note that certain commands are not usable without first getting necessary
rights on the NUT data server.
The command should return `OK` if everything went well, or raise an exception
in case of failure.
.Example
-----
import PyNUT
ups = PyNUT.PyNUTClient( host='Serveur', login='upsadmin', password='upspass' )
result = ups.RunUPSCommand( ups='UPS1', command='test.panel.start' )
print( result )
>> OK
-----
See also: `GetUPSCommands()`
SetRWVar
~~~~~~~~
This method adjusts the value of a writable NUT variable on the specified UPS.
It should be one of the variables listed by the `GetRWVars()` method.
Note that you may need to first get necessary rights on the NUT data server.
The command should return `OK` if everything went well, or raise an exception
in case of failure.
.Example
-----
import PyNUT
ups = PyNUT.PyNUTClient( host='Serveur', login='upsadmin', password='upspass' )
result = ups.SetRWVar( ups='UPS1', var='battery.date', value='06/17/08' )
print( result )
>> OK
-----
See also: `GetRWVars()`
|