[cups.general] How to query the originating hostname from the C API?

Jerome Alet alet at librelogiciel.com
Wed Jan 17 10:32:31 PST 2007


Hi,

On Wed, Jan 17, 2007 at 06:58:22AM -0500, Dominik Kubla wrote:
> 
> I maintain a simple in-house monitoring application. I would like 
> to extended the list of queued jobs to display the origination 
> hostname. 

Not sure how I'd do it with the CUPS' C API, but in Python I'd download
pkipplib from http://www.pykota.com and then I'd do it from the Python
interactive interpreter :

--- CUT ---
jerome at nordine:~$ python
Python 2.4.4 (#2, Oct 20 2006, 00:23:25) 
[GCC 4.1.2 20061015 (prerelease) (Debian 4.1.1-16.1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pkipplib import pkipplib
>>> cups = pkipplib.CUPS()        # http://localhost:631 by default
>>> answer = cups.getJobAttributes(500) # Retrieve job 500's attributes
>>> if answer is None :
....     print "ERROR"
.... else :
....     print answer.job["job-originating-host-name"]
.... 
[('nameWithoutLanguage', 'localhost')]
>>> hostname = answer.job["job-originating-host-name"][0][1]
>>> print hostname
localhost
--- CUT ---

Or in a non-interactive way, save the following file as 'getjohn' and
then do a 'chmod +x getjohn' :

--- CUT ---
#! /usr/bin/env python
import sys
from pkipplib import pkipplib

if len(sys.argv) != 2 :
    sys.stderr.write("usage : getjohn jobid\n")
else :    
    jobid = int(sys.argv[1])
    cups = pkipplib.CUPS()
    answer = cups.getJobAttributes(jobid)
    if answer is None :
        sys.stderr.write("ERROR when talking to CUPS...\n")
    else :    
        try :
            print answer.job["job-originating-host-name"][0][1]
        except KeyError :    
            sys.stderr.write("ERROR, probably incorrect jobid...\n")
--- CUT ---

Then :

        $ ./getjohn 500
        
would print the job originating host name for job 500, or an error        
message if not available.

hoping this helps

bye

Jerome Alet





More information about the cups mailing list