root / pykota / trunk / pykota / requesters / snmp.py @ 1068

Revision 1068, 3.2 kB (checked in by jalet, 21 years ago)

Lots of small fixes with the help of PyChecker?

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1# PyKota
2#
3# PyKota - Print Quotas for CUPS and LPRng
4#
5# (c) 2003 Jerome Alet <alet@librelogiciel.com>
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19#
20# $Id$
21#
22# $Log$
23# Revision 1.11  2003/07/07 11:49:24  jalet
24# Lots of small fixes with the help of PyChecker
25#
26# Revision 1.10  2003/05/22 12:56:06  jalet
27# Uses SNMP version 1 instead of version 2c by default, which is probably
28# a safer bet.
29#
30# Revision 1.9  2003/05/21 10:07:09  jalet
31# Now works with net-snmp v5.0 and above.
32# (It already worked, but the sample configuration file didn't contain
33# appropriate values)
34#
35# Revision 1.8  2003/04/23 22:13:57  jalet
36# Preliminary support for LPRng added BUT STILL UNTESTED.
37#
38# Revision 1.7  2003/03/29 13:45:27  jalet
39# GPL paragraphs were incorrectly (from memory) copied into the sources.
40# Two README files were added.
41# Upgrade script for PostgreSQL pre 1.01 schema was added.
42#
43# Revision 1.6  2003/02/10 11:47:39  jalet
44# Moved some code down into the requesters
45#
46# Revision 1.5  2003/02/10 00:42:17  jalet
47# External requester should be ok (untested)
48# New syntax for configuration file wrt requesters
49#
50# Revision 1.4  2003/02/09 13:05:43  jalet
51# Internationalization continues...
52#
53# Revision 1.3  2003/02/07 13:12:41  jalet
54# Bad old comment
55#
56# Revision 1.2  2003/02/05 23:00:12  jalet
57# Forgotten import
58# Bad datetime conversion
59#
60# Revision 1.1  2003/02/05 21:28:17  jalet
61# Initial import into CVS
62#
63#
64#
65
66import os
67from pykota.requester import PyKotaRequesterError
68
69class Requester :
70    """A class to send queries to printers via SNMP."""
71    def __init__(self, printername, arguments) :
72        """Sets instance vars depending on the current printer."""
73        self.printername = printername
74        args = [x.strip() for x in arguments.split(',')]
75        self.community = args[0]
76        self.oid = args[1]
77       
78    def getPrinterPageCounter(self, hostname) :
79        """Returns the page counter from the hostname printer via SNMP.
80       
81           Currently uses the snmpget external command. TODO : do it internally
82        """
83        if hostname is None :
84            raise PyKotaRequesterError, _("Unknown printer address in SNMP(%s, %s) for printer %s") % (self.community, self.oid, self.printername)
85        answer = os.popen("snmpget -v1 -c %s -Ov %s %s" % (self.community, hostname, self.oid))
86        try :
87            pagecounter = int(answer.readline().split()[-1].strip())
88        except IndexError :   
89            raise PyKotaRequesterError, _("Unable to query printer %s via SNMP(%s, %s)") % (hostname, self.community, self.oid) 
90        answer.close()
91        return pagecounter
92   
Note: See TracBrowser for help on using the browser.