I've realized that my PyNUT version is newer than the repository one. I
sent a patch to David Goncalves some time ago, I don't know what is the
reason why he didn't commit the patch, anyway, I'll send the complete patch
again.

2012/2/7 Rene Martín Rodríguez <[email protected]>

>
>
> 2012/2/1 Arnaud Quette <[email protected]>
>
>> 2012/1/25 Rene Martín Rodríguez <[email protected]>:
>> > Hello!!
>>
>> ¡Hola René Martin ;-)
>>
>> Hi again!
>
> I've implemented your suggestion, everything but -c option for upsc. hope
> you like it!
>
>
>
>> indeed, it's an interesting feature.
>>
>> after thinking a bit, here is a potentially more useful approach, that
>> would complement "GET NUMLOGINS":
>> Add a new "LIST CLIENT" command to the network protocol [1]
>>
>> Form:
>>
>>    LIST CLIENT [device_name]
>>
>> Response:
>>
>>    BEGIN LIST CLIENT
>>    CLIENT <client IP address> <device name>
>>    ...
>>    END LIST CLIENT
>>
>>    BEGIN LIST CLIENT
>>    CLIENT ::1 ups1
>>    CLIENT ::1 ups2
>>    CLIENT 166.99.250.2 ups2
>>    END LIST CLIENT
>>
>> This would be completed by a new upsc option ("-c" for example) to get
>> this list.
>>
>> Note that this is more an RFC, since it's just a base idea (I've not
>> checked all details).
>> So feedback and comments welcome...
>> Would the above suits your needs René Martin?
>>
>> > Thank you for your nice work!!
>>
>> thanks for your contribution.
>>
>> cheers,
>> Arnaud
>> --
>> [1]
>> http://www.networkupstools.org/docs/developer-guide.chunked/ar01s09.html
>> --
>> Linux / Unix Expert R&D - Eaton - http://powerquality.eaton.com
>> Network UPS Tools (NUT) Project Leader - http://www.networkupstools.org/
>> Debian Developer - http://www.debian.org
>> Free Software Developer - http://arnaud.quette.free.fr/
>>
>
>
Index: PyNUT.py
===================================================================
--- PyNUT.py	(revisión: 3431)
+++ PyNUT.py	(copia de trabajo)
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/python
 # -*- coding: utf-8 -*-
 
 #   Copyright (C) 2008 David Goncalves <[email protected]>
@@ -17,7 +17,7 @@
 #   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.
+#            PyNUT is an abstraction class to acces NUT (Network UPS Tools) server.
 #
 # 2008-06-09 David Goncalves
 #            Added 'GetRWVars' and 'SetRWVar' commands.
@@ -28,6 +28,13 @@
 # 2010-07-23 David Goncalves - Version 1.2
 #            Changed GetRWVars function that fails is the UPS is not
 #            providing such vars.
+#
+# 2011-07-05 René Martín Rodríguez <[email protected]> - Version 1.2.1
+#            Added support for FSD, HELP and VER commands
+#
+# 2012-02-07 René Martín Rodríguez <[email protected]> - Version 1.2.2
+#            Added support for LIST CLIENTS command
+#
 
 import telnetlib
 
@@ -105,7 +112,7 @@
     def GetUPSList( self ) :
         """ Returns the list of available UPS from the NUT server
 
-The result is a dictionary containing 'key->val' pairs of 'UPSName' and 'UPS Description'
+The result is a dictionnary containing 'key->val' pairs of 'UPSName' and 'UPS Description'
         """
         if self.__debug :
             print( "[DEBUG] GetUPSList from server" )
@@ -128,7 +135,7 @@
     def GetUPSVars( self, ups="" ) :
         """ Get all available vars from the specified UPS
 
-The result is a dictionary containing 'key->val' pairs of all
+The result is a dictionnary containing 'key->val' pairs of all
 available vars.
         """
         if self.__debug :
@@ -192,7 +199,7 @@
     def GetRWVars( self,  ups="" ) :
         """ Get a list of all writable vars from the selected UPS
 
-The result is presented as a dictionary containing 'key->val' pairs
+The result is presented as a dictionnary containing 'key->val' pairs
         """
         if self.__debug :
             print( "[DEBUG] GetUPSVars from '%s'..." % ups )
@@ -247,3 +254,78 @@
             return( "OK" )
         else :
             raise Exception, result.replace( "\n", "" )
+
+    def FSD( self, ups="") :
+        """ Send FSD command
+
+Returns OK on success or raises an error
+        """
+
+        if self.__debug :
+            print( "[DEBUG] MASTER called..." )
+
+        self.__srv_handler.write( "MASTER %s\n" % ups )
+        result = self.__srv_handler.read_until( "\n" )
+        if ( result != "OK MASTER-GRANTED\n" ) :
+            raise Exception, ( "Master level function are not available", "" )
+
+        if self.__debug :
+            print( "[DEBUG] FSD called..." )
+        self.__srv_handler.write( "FSD %s\n" % ups )
+        result = self.__srv_handler.read_until( "\n" )
+        if ( result == "OK FSD-SET\n" ) :
+            return( "OK" )
+        else :
+            raise Exception, result.replace( "\n", "" )
+
+    def help(self) :
+        """ Send HELP command
+        """
+
+        if self.__debug :
+            print( "[DEBUG] HELP called..." )
+
+        self.__srv_handler.write( "HELP\n")
+        return self.__srv_handler.read_until( "\n" )
+
+    def ver(self) :
+        """ Send VER command
+        """
+
+        if self.__debug :
+            print( "[DEBUG] VER called..." )
+
+        self.__srv_handler.write( "VER\n")
+        return self.__srv_handler.read_until( "\n" )
+
+    def ListClients( self, ups = None ) :
+        """ Returns the list of connected clients from the NUT server
+
+The result is a dictionnary 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()):
+            raise Exception, "%s is not a valid UPS" % ups
+
+        if ups:
+            self.__srv_handler.write( "LIST CLIENTS %s\n" % ups)
+        else:
+            self.__srv_handler.write( "LIST CLIENTS\n" )
+        result = self.__srv_handler.read_until( "\n" )
+        if result != "BEGIN LIST CLIENTS\n" :
+            raise Exception, result.replace( "\n", "" )
+
+        result = self.__srv_handler.read_until( "END LIST CLIENTS\n" )
+        ups_list = {}
+
+        for line in result.split( "\n" ):
+            if line[:6] == "CLIENT" :
+                host, ups = line[7:].split(' ')
+                ups.replace(' ', '')
+                if not ups in ups_list:
+                    ups_list[ups] = []
+                ups_list[ups].append(host)
+
+        return( ups_list )
_______________________________________________
Nut-upsdev mailing list
[email protected]
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/nut-upsdev

Reply via email to