[MOVED] RHN API & python script - works partially
Hello,
I am trying to play with RHN using python and would like to list all servers with their ip addresses:
#!/usr/bin/python
import xmlrpclib
SATELLITE_URL = "http://xmlrpc.rhn.redhat.com/rpc/api"
SATELLITE_LOGIN = "xxxxxx"
SATELLITE_PASSWORD = "xxxxxx"client = xmlrpclib.Server(SATELLITE_URL, verbose=0)
key = client.auth.login(SATELLITE_LOGIN, SATELLITE_PASSWORD)
list = client.system.listUserSystems(key)for group in list:
print '%-40s' % group.get('name'),
sid=group.get('id')
ip=client.system.getNetwork(key,int(sid))
print ip.get('ip')client.auth.logout(key)
But when I run it:
# ./rhn-list-systems-with-ip.py > out.file
I get list of some of these hosts in out.file (not all !) and then this script breaks and I get the following error:
Traceback (most recent call last):
File "./rhn-list-systems-with-ip.py", line 18, in ?
ip=client.system.getNetwork(key,int(sid))
File "/usr/lib64/python2.4/xmlrpclib.py", line 1096, in __call__
return self.__send(self.__name, args)
File "/usr/lib64/python2.4/xmlrpclib.py", line 1383, in __request
verbose=self.__verbose
File "/usr/lib64/python2.4/xmlrpclib.py", line 1137, in request
headers
xmlrpclib.ProtocolError: <ProtocolError for xmlrpc.rhn.redhat.com/rpc/api: 502 Proxy Error>
Can anybody tell me what is going on ?
Responses
I know you said you wanted a python script, but here's one I have that's written in perl. If I have time tonight I'll write one in python.
$ perl system.listSystems.pl
system id: 1000010044 and name of: rhel6u1-64.example.com has ip address of 102.13.211.244
system id: 1000010021 and name of: rhel6u3-64.example.com has ip address of 102.11.211.159
system id: 1000010104 and name of: rhel6u3-64.example.com has ip address of 102.11.211.77
system id: 1000010064 and name of: pmg has ip address of 102.11.145.112
system id: 1000010042 and name of: rhel63.goobus has ip address of 192.168.122.159
system id: 1000010043 and name of: unknown has ip address of 192.168.122.119
To the extent possible under law, Red Hat, Inc. has dedicated all copyright to this software to the public domain worldwide, pursuant to the CC0 Public Domain Dedication. This software is distributed without any warranty. See <http://creativecommons.org/publicdomain/zero/1.0/>.
$ cat /data/scripts/system.listSystems.pl
#!/usr/bin/perl
use Frontier::Client;
my $HOST = 'satellite.example.com';
my $user = 'adminuser';
my $pass = 'adminpassword';
my $client = new Frontier::Client(url => "https://$HOST/rpc/api");#, debug=>1);
my $session = $client->call('auth.login',$user, $pass);
# $systems is an array reference
my $systems = $client->call('system.listSystems', $session);
# dereference (@) the perl array to loop
for (@{$systems}) {
my $systemid = $_->{'id'};
print "system id: " . $systemid;
print " and name of: " . $_->{'name'} . " has ip address of ";
my $getNetwork = $client->call('system.getNetwork', $session, $systemid);
print $getNetwork->{'ip'};
print "\n";
}
$client->call('auth.logout', $session);
You'll want to use system.listUserSystems rather than system.listSystems, but that should be the only change I think.
Pete
The satellite's api uses system.listSystems while the RHN api uses system.listUserSystems. That's why you'd need to change it.
I used your python script and it bombed out in pretty close to the same way as when you used it.
$ python system.listUserSystems.py > out.file
Traceback (most recent call last):
File "system.listUserSystems.py", line 16, in <module>
ip=client.system.getNetwork(key,int(sid))
File "/usr/lib64/python2.6/xmlrpclib.py", line 1199, in __call__
return self.__send(self.__name, args)
File "/usr/lib64/python2.6/xmlrpclib.py", line 1489, in __request
verbose=self.__verbose
File "/usr/lib64/python2.6/xmlrpclib.py", line 1243, in request
headers
xmlrpclib.ProtocolError: <ProtocolError for xmlrpc.rhn.redhat.com/rpc/api: 502 Proxy Error>
$ wc -l out.file
191 out.file
I ran the perl script and it had more output, but still, it failed. Adding debugging didn't give any more helpful info.
$ perl system.listSystems.pl.getNetwork.pl > out.file.perl
502 Proxy Error
$ wc -l out.file.perl
202 out.file.perl
I'll look into this.
Thanks
Pete
How many systems are you trying to list? Googling for the specific error string brought up this RHT Bugzilla ticket that talks about similar proxy errors with RHN and yum https://bugzilla.redhat.com/show_bug.cgi?id=818786
It could simply be that RHN can't process the data you are asking for and eventually times out. This may not be a scripting issue.
-Matt
So the node names would be one call, then you turn around and make a few hundreds of calls back to RHN (one per getIP). That could be causing issues with RHN and it's request handling since there isn't any sort of persistent sessions to make these calls over.
If you break it up in to smaller chunks (say 100 per loop) does it work for each chunk?
Your script worked just fine for me when printing to console and when I redirect to out.file. I have 411 RHEL 6 systems and the script returned each one with the ip.
I don't think your issue is with your script.
Przemyslaw,
Since we're closing the Groups area down soon, do you have any objection (eg privacy of data) to me moving this topic over to the new Discussions area, which is publicly accessible?
I've migrated this discussion across to the new platform, so it's locked. Feel free to pick it up here: https://access.redhat.com/site/discussions/450023
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
