2022-06-29 10:37:36 +00:00
|
|
|
#!@PYTHON@
|
2010-03-25 23:20:59 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright (C) 2008 David Goncalves <david@lestat.st>
|
|
|
|
#
|
|
|
|
# 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; either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
# 2008-01-14 David Goncalves
|
|
|
|
# PyNUT is an abstraction class to access NUT (Network UPS Tools) server.
|
|
|
|
#
|
|
|
|
# 2008-06-09 David Goncalves
|
|
|
|
# Added 'GetRWVars' and 'SetRWVar' commands.
|
2011-01-26 09:35:08 +00:00
|
|
|
#
|
|
|
|
# 2009-02-19 David Goncalves
|
|
|
|
# Changed class PyNUT to PyNUTClient
|
|
|
|
#
|
|
|
|
# 2010-07-23 David Goncalves - Version 1.2
|
|
|
|
# Changed GetRWVars function that fails is the UPS is not
|
|
|
|
# providing such vars.
|
2012-06-01 13:55:19 +00:00
|
|
|
#
|
|
|
|
# 2011-07-05 René Martín Rodríguez <rmrodri@ull.es> - Version 1.2.1
|
|
|
|
# Added support for FSD, HELP and VER commands
|
|
|
|
#
|
|
|
|
# 2012-02-07 René Martín Rodríguez <rmrodri@ull.es> - Version 1.2.2
|
|
|
|
# Added support for LIST CLIENTS command
|
|
|
|
#
|
2015-04-30 13:53:36 +00:00
|
|
|
# 2014-06-03 george2 - Version 1.3.0
|
|
|
|
# Added custom exception class, fixed minor bug, added Python 3 support.
|
|
|
|
#
|
2022-06-29 10:37:36 +00:00
|
|
|
# 2021-09-27 Jim Klimov <jimklimov+nut@gmail.com> - Version 1.4.0
|
|
|
|
# Revise strings used to be byte sequences as required by telnetlib
|
|
|
|
# in Python 3.9, by spelling out b"STR" or str.encode('ascii');
|
|
|
|
# the change was also tested to work with Python 2.7, 3.4, 3.5 and
|
|
|
|
# 3.7 (to the extent of accompanying test_nutclient.py at least).
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
import telnetlib
|
|
|
|
|
2015-04-30 13:53:36 +00:00
|
|
|
class PyNUTError( Exception ) :
|
|
|
|
""" Base class for custom exceptions """
|
|
|
|
|
|
|
|
|
2010-03-25 23:20:59 +00:00
|
|
|
class PyNUTClient :
|
|
|
|
""" Abstraction class to access NUT (Network UPS Tools) server """
|
|
|
|
|
|
|
|
__debug = None # Set class to debug mode (prints everything useful for debuging...)
|
|
|
|
__host = None
|
|
|
|
__port = None
|
|
|
|
__login = None
|
|
|
|
__password = None
|
|
|
|
__timeout = None
|
|
|
|
__srv_handler = None
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
__version = "1.4.0"
|
|
|
|
__release = "2021-09-27"
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def __init__( self, host="127.0.0.1", port=3493, login=None, password=None, debug=False, timeout=5 ) :
|
|
|
|
""" Class initialization method
|
|
|
|
|
|
|
|
host : Host to connect (default to localhost)
|
|
|
|
port : Port where NUT listens for connections (default to 3493)
|
|
|
|
login : Login used to connect to NUT server (default to None for no authentication)
|
|
|
|
password : Password used when using authentication (default to None)
|
|
|
|
debug : Boolean, put class in debug mode (prints everything on console, default to False)
|
|
|
|
timeout : Timeout used to wait for network response
|
|
|
|
"""
|
|
|
|
self.__debug = debug
|
|
|
|
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] Class initialization..." )
|
|
|
|
print( "[DEBUG] -> Host = %s (port %s)" % ( host, port ) )
|
|
|
|
print( "[DEBUG] -> Login = '%s' / '%s'" % ( login, password ) )
|
|
|
|
|
|
|
|
self.__host = host
|
|
|
|
self.__port = port
|
|
|
|
self.__login = login
|
|
|
|
self.__password = password
|
|
|
|
self.__timeout = 5
|
|
|
|
|
|
|
|
self.__connect()
|
|
|
|
|
|
|
|
# Try to disconnect cleanly when class is deleted ;)
|
|
|
|
def __del__( self ) :
|
|
|
|
""" Class destructor method """
|
|
|
|
try :
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( b"LOGOUT\n" )
|
2010-03-25 23:20:59 +00:00
|
|
|
except :
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __connect( self ) :
|
|
|
|
""" Connects to the defined server
|
|
|
|
|
|
|
|
If login/pass was specified, the class tries to authenticate. An error is raised
|
|
|
|
if something goes wrong.
|
|
|
|
"""
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] Connecting to host" )
|
|
|
|
|
|
|
|
self.__srv_handler = telnetlib.Telnet( self.__host, self.__port )
|
|
|
|
|
|
|
|
if self.__login != None :
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("USERNAME %s\n" % self.__login).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n", self.__timeout )
|
|
|
|
if result[:2] != b"OK" :
|
|
|
|
raise PyNUTError( result.replace( b"\n", b"" ) )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
if self.__password != None :
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("PASSWORD %s\n" % self.__password).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n", self.__timeout )
|
|
|
|
if result[:2] != b"OK" :
|
|
|
|
raise PyNUTError( result.replace( b"\n", b"" ) )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
def GetUPSList( self ) :
|
|
|
|
""" Returns the list of available UPS from the NUT server
|
|
|
|
|
2011-01-26 09:35:08 +00:00
|
|
|
The result is a dictionary containing 'key->val' pairs of 'UPSName' and 'UPS Description'
|
2010-03-25 23:20:59 +00:00
|
|
|
"""
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] GetUPSList from server" )
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( b"LIST UPS\n" )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if result != b"BEGIN LIST UPS\n" :
|
|
|
|
raise PyNUTError( result.replace( b"\n", b"" ) )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
result = self.__srv_handler.read_until( b"END LIST UPS\n" )
|
2010-03-25 23:20:59 +00:00
|
|
|
ups_list = {}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
for line in result.split( b"\n" ) :
|
|
|
|
if line[:3] == b"UPS" :
|
|
|
|
ups, desc = line[4:-1].split( b'"' )
|
|
|
|
ups_list[ ups.replace( b" ", b"" ) ] = desc
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
return( ups_list )
|
|
|
|
|
|
|
|
def GetUPSVars( self, ups="" ) :
|
|
|
|
""" Get all available vars from the specified UPS
|
|
|
|
|
2011-01-26 09:35:08 +00:00
|
|
|
The result is a dictionary containing 'key->val' pairs of all
|
2010-03-25 23:20:59 +00:00
|
|
|
available vars.
|
|
|
|
"""
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] GetUPSVars called..." )
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("LIST VAR %s\n" % ups).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if result != ("BEGIN LIST VAR %s\n" % ups).encode('ascii') :
|
|
|
|
raise PyNUTError( result.replace( b"\n", b"" ) )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
ups_vars = {}
|
2022-06-29 10:37:36 +00:00
|
|
|
result = self.__srv_handler.read_until( ("END LIST VAR %s\n" % ups).encode('ascii') )
|
|
|
|
offset = len( ("VAR %s " % ups ).encode('ascii') )
|
|
|
|
end_offset = 0 - ( len( ("END LIST VAR %s\n" % ups).encode('ascii') ) + 1 )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
for current in result[:end_offset].split( b"\n" ) :
|
|
|
|
var = current[ offset: ].split( b'"' )[0].replace( b" ", b"" )
|
|
|
|
data = current[ offset: ].split( b'"' )[1]
|
2010-03-25 23:20:59 +00:00
|
|
|
ups_vars[ var ] = data
|
|
|
|
|
|
|
|
return( ups_vars )
|
|
|
|
|
|
|
|
def GetUPSCommands( self, ups="" ) :
|
|
|
|
""" Get all available commands for the specified UPS
|
|
|
|
|
|
|
|
The result is a dict object with command name as key and a description
|
|
|
|
of the command as value
|
|
|
|
"""
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] GetUPSCommands called..." )
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("LIST CMD %s\n" % ups).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if result != ("BEGIN LIST CMD %s\n" % ups).encode('ascii') :
|
|
|
|
raise PyNUTError( result.replace( b"\n", b"" ) )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
ups_cmds = {}
|
2022-06-29 10:37:36 +00:00
|
|
|
result = self.__srv_handler.read_until( ("END LIST CMD %s\n" % ups).encode('ascii') )
|
|
|
|
offset = len( ("CMD %s " % ups).encode('ascii') )
|
|
|
|
end_offset = 0 - ( len( ("END LIST CMD %s\n" % ups).encode('ascii') ) + 1 )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
for current in result[:end_offset].split( b"\n" ) :
|
|
|
|
var = current[ offset: ].split( b'"' )[0].replace( b" ", b"" )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
# For each var we try to get the available description
|
|
|
|
try :
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("GET CMDDESC %s %s\n" % ( ups, var )).encode('ascii') )
|
|
|
|
temp = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if temp[:7] != b"CMDDESC" :
|
2015-04-30 13:53:36 +00:00
|
|
|
raise PyNUTError
|
2010-03-25 23:20:59 +00:00
|
|
|
else :
|
2022-06-29 10:37:36 +00:00
|
|
|
off = len( ("CMDDESC %s %s " % ( ups, var )).encode('ascii') )
|
|
|
|
desc = temp[off:-1].split(b'"')[1]
|
2010-03-25 23:20:59 +00:00
|
|
|
except :
|
|
|
|
desc = var
|
|
|
|
|
|
|
|
ups_cmds[ var ] = desc
|
|
|
|
|
|
|
|
return( ups_cmds )
|
|
|
|
|
|
|
|
def GetRWVars( self, ups="" ) :
|
|
|
|
""" Get a list of all writable vars from the selected UPS
|
|
|
|
|
2011-01-26 09:35:08 +00:00
|
|
|
The result is presented as a dictionary containing 'key->val' pairs
|
2010-03-25 23:20:59 +00:00
|
|
|
"""
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] GetUPSVars from '%s'..." % ups )
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("LIST RW %s\n" % ups).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if ( result != ("BEGIN LIST RW %s\n" % ups).encode('ascii') ) :
|
|
|
|
raise PyNUTError( result.replace( b"\n", b"" ) )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
result = self.__srv_handler.read_until( ("END LIST RW %s\n" % ups).encode('ascii') )
|
|
|
|
offset = len( ("VAR %s" % ups).encode('ascii') )
|
|
|
|
end_offset = 0 - ( len( ("END LIST RW %s\n" % ups).encode('ascii') ) + 1 )
|
2010-03-25 23:20:59 +00:00
|
|
|
rw_vars = {}
|
|
|
|
|
2011-01-26 09:35:08 +00:00
|
|
|
try :
|
2022-06-29 10:37:36 +00:00
|
|
|
for current in result[:end_offset].split( b"\n" ) :
|
|
|
|
var = current[ offset: ].split( b'"' )[0].replace( b" ", b"" )
|
|
|
|
data = current[ offset: ].split( b'"' )[1]
|
2011-01-26 09:35:08 +00:00
|
|
|
rw_vars[ var ] = data
|
|
|
|
|
|
|
|
except :
|
|
|
|
pass
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
return( rw_vars )
|
|
|
|
|
|
|
|
def SetRWVar( self, ups="", var="", value="" ):
|
|
|
|
""" Set a variable to the specified value on selected UPS
|
|
|
|
|
|
|
|
The variable must be a writable value (cf GetRWVars) and you must have the proper
|
|
|
|
rights to set it (maybe login/password).
|
|
|
|
"""
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("SET VAR %s %s %s\n" % ( ups, var, value )).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if ( result == b"OK\n" ) :
|
2010-03-25 23:20:59 +00:00
|
|
|
return( "OK" )
|
|
|
|
else :
|
2015-04-30 13:53:36 +00:00
|
|
|
raise PyNUTError( result )
|
2010-03-25 23:20:59 +00:00
|
|
|
|
|
|
|
def RunUPSCommand( self, ups="", command="" ) :
|
|
|
|
""" Send a command to the specified UPS
|
|
|
|
|
|
|
|
Returns OK on success or raises an error
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] RunUPSCommand called..." )
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("INSTCMD %s %s\n" % ( ups, command )).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if ( result == b"OK\n" ) :
|
2010-03-25 23:20:59 +00:00
|
|
|
return( "OK" )
|
|
|
|
else :
|
2022-06-29 10:37:36 +00:00
|
|
|
raise PyNUTError( result.replace( b"\n", b"" ) )
|
2012-06-01 13:55:19 +00:00
|
|
|
|
|
|
|
def FSD( self, ups="") :
|
|
|
|
""" Send FSD command
|
|
|
|
|
|
|
|
Returns OK on success or raises an error
|
2022-06-29 10:37:36 +00:00
|
|
|
|
|
|
|
NOTE: API changed since NUT 2.8.0 to replace MASTER with PRIMARY
|
|
|
|
(and backwards-compatible alias handling)
|
2012-06-01 13:55:19 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
if self.__debug :
|
2022-06-29 10:37:36 +00:00
|
|
|
print( "[DEBUG] PRIMARY called..." )
|
|
|
|
|
|
|
|
self.__srv_handler.write( ("PRIMARY %s\n" % ups).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if ( result != b"OK PRIMARY-GRANTED\n" ) :
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] Retrying: MASTER called..." )
|
|
|
|
self.__srv_handler.write( ("MASTER %s\n" % ups).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if ( result != b"OK MASTER-GRANTED\n" ) :
|
|
|
|
raise PyNUTError( ( "Primary level functions are not available", "" ) )
|
2012-06-01 13:55:19 +00:00
|
|
|
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] FSD called..." )
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("FSD %s\n" % ups).encode('ascii') )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if ( result == b"OK FSD-SET\n" ) :
|
2012-06-01 13:55:19 +00:00
|
|
|
return( "OK" )
|
|
|
|
else :
|
2022-06-29 10:37:36 +00:00
|
|
|
raise PyNUTError( result.replace( b"\n", b"" ) )
|
2012-06-01 13:55:19 +00:00
|
|
|
|
|
|
|
def help(self) :
|
|
|
|
""" Send HELP command
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] HELP called..." )
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( b"HELP\n" )
|
|
|
|
return self.__srv_handler.read_until( b"\n" )
|
2012-06-01 13:55:19 +00:00
|
|
|
|
|
|
|
def ver(self) :
|
|
|
|
""" Send VER command
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] VER called..." )
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( b"VER\n" )
|
|
|
|
return self.__srv_handler.read_until( b"\n" )
|
2012-06-01 13:55:19 +00:00
|
|
|
|
|
|
|
def ListClients( self, ups = None ) :
|
|
|
|
""" Returns the list of connected clients from the NUT server
|
|
|
|
|
|
|
|
The result is a dictionary containing 'key->val' pairs of 'UPSName' and a list of clients
|
|
|
|
"""
|
|
|
|
if self.__debug :
|
|
|
|
print( "[DEBUG] ListClients from server" )
|
|
|
|
|
|
|
|
if ups and (ups not in self.GetUPSList()):
|
2015-04-30 13:53:36 +00:00
|
|
|
raise PyNUTError( "%s is not a valid UPS" % ups )
|
2012-06-01 13:55:19 +00:00
|
|
|
|
|
|
|
if ups:
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( ("LIST CLIENTS %s\n" % ups).encode('ascii') )
|
2012-06-01 13:55:19 +00:00
|
|
|
else:
|
2022-06-29 10:37:36 +00:00
|
|
|
self.__srv_handler.write( b"LIST CLIENTS\n" )
|
|
|
|
result = self.__srv_handler.read_until( b"\n" )
|
|
|
|
if result != b"BEGIN LIST CLIENTS\n" :
|
|
|
|
raise PyNUTError( result.replace( b"\n", b"" ) )
|
2012-06-01 13:55:19 +00:00
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
result = self.__srv_handler.read_until( b"END LIST CLIENTS\n" )
|
2012-06-01 13:55:19 +00:00
|
|
|
ups_list = {}
|
|
|
|
|
2022-06-29 10:37:36 +00:00
|
|
|
for line in result.split( b"\n" ):
|
|
|
|
if line[:6] == b"CLIENT" :
|
|
|
|
host, ups = line[7:].split(b' ')
|
|
|
|
ups.replace(b' ', b'')
|
2012-06-01 13:55:19 +00:00
|
|
|
if not ups in ups_list:
|
|
|
|
ups_list[ups] = []
|
|
|
|
ups_list[ups].append(host)
|
|
|
|
|
|
|
|
return( ups_list )
|