Seite 1 von 2

Retrieval computer Information via RPC API

Verfasst: 21 Okt 2014, 18:40
von jroch
Hello everyone, this is my first topic on this forum so be cool : D.

The purpose of my topic is: retrieve the name of machines which has the "actionRequest" Setup.

Via the web interface of opsi: https://XXX.yourdomain.fr/interface/extend/configed (opsiconfd interface page)
The Path on: interface / extend / configed
The Method on: productOnClient_getHashes

I get a list of my computer park:

Code: Alles auswählen

{
        "id": 1,
        "result": [
               {
                      "actionProgress": "",
                      "actionResult": "successful",
                      "clientId": "ORDINATEUR1",
                      "modificationTime": "2014-02-10 15:01:35",
                      "actionRequest": "none",
                      "targetConfiguration": "installed",
                      "productVersion": "9.20",
                      "productType": "LocalbootProduct",
                      "type": "ProductOnClient",
                      "lastAction": "setup",
                      "packageVersion": "3",
                      "actionSequence": -1,
                      "installationStatus": "installed",
                      "productId": "7-zip"
               },
               {
                      "actionProgress": "",
                      "actionResult": "successful",
                      "clientId": "ORDINATEUR2",
                      "modificationTime": "2014-02-12 12:43:20",
                      "actionRequest": "setup",
                      "targetConfiguration": "installed",
                      "productVersion": "9.20",
                      "productType": "LocalbootProduct",
                      "type": "ProductOnClient",
                      "lastAction": "setup",
                      "packageVersion": "3",
                      "actionSequence": -1,
                      "installationStatus": "installed",
                      "productId": "7-zip"
               },
Etc..

So I would find a way to retrieve only the name and productId of computers with the attribute "actionRequest" set to "setup".

I read a topic on the use of SOAP's, I also throw in the parsing of an HTML page to sort the computers via the source code of the web page in python but I think its not clean.

Do you have any advice to give me to reach my goal ??
Thank you much ;)

Re: Retrieval computer Information via RPC API

Verfasst: 22 Okt 2014, 09:58
von n.wenselowski
Hello jroch,

where do you want to use the output?
In a shell script or some other tool that talks to opsi?

I would not recommend scraping the pages because you can talk to the interface via JSON RPC.
The interface is using JSON RPC and can be accessed with nearly every modern language. If you want to use Python have a look at the opsi scripts themselves because they are written in Python. There is an example in the wiki that uses Ruby. And I have seen snippets in the forum that made use of Powerscript.


With kind regards

Niko

Re: Retrieval computer Information via RPC API

Verfasst: 22 Okt 2014, 11:49
von jroch
Hi thank you for your reply,

I want to use the output in python script this will allow me to use this list of computer to query an other API. :)

thanks

Re: Retrieval computer Information via RPC API

Verfasst: 23 Okt 2014, 09:43
von n.wenselowski
Hello,

then I'd stick with Python. If you have python-opsi installed where the script will run you could use the JSONRPCBackend class from OPSI.Backend.JSONRPC (code).

With kind regards

Niko

Re: Retrieval computer Information via RPC API

Verfasst: 23 Okt 2014, 17:56
von jroch
In fact i want to know how to speak with opsi with rpc protocol.. What is the url i must use to get information with rpc.

This URL : https://myopsiserver/rpc?{%20"method":% ... :%20"setup"} don't work :? why ? :)

Re: Retrieval computer Information via RPC API

Verfasst: 24 Okt 2014, 09:56
von dkoch
I would recommend you to use a json-rpc library
wiki/userspace:webservice_corner:ruby-rpc

Re: Retrieval computer Information via RPC API

Verfasst: 24 Okt 2014, 12:18
von n.wenselowski
Hello jroch,

the URL is https://opsi.server.foo:4447/rpc.
You then do not call the URL with parameters, you post JSON to them.
Hopefully the following curl command helps you understand:

Code: Alles auswählen

curl --insecure -X POST --user opsiuser:opsipassword -H "Content-Type: application/json" -d '{"params": [], "id": 1, "method": "backend_info"}' https://opsi.server.local:4447/rpc
You can roll your own code to do JSON RPC but as dkoch suggested I'd use a library for that. There are some for Python, just check pypi or search around the web.


With kind regards

Niko

Re: Retrieval computer Information via RPC API

Verfasst: 29 Okt 2014, 16:41
von jroch
Hi, i test it and when i run this command i have this : {"result": null, "id": null, "error": {"message": "Opsi bad rpc error: Failed to
decode rpc: No JSON object could be decoded", "class": "OpsiBadRpcError"}}

im lost..

Re: Retrieval computer Information via RPC API

Verfasst: 29 Okt 2014, 16:54
von n.wenselowski
Hello jroch,

from the error message I'd think that there could be an error with the data you send to the server. Do you use any non-unicode locales? Did you type the command or copy it?


With kind regards

Niko

Re: Retrieval computer Information via RPC API

Verfasst: 30 Okt 2014, 13:17
von jroch
hi i found the solution !!

If you want do something with opsi rpc in python !
In this example, i request rpc opsi server about this method : productOnClient_getHashes

Do this :

Code: Alles auswählen

         from os import *
         from sys import *

         import httplib
         import base64
         import string
         import json
         
         def main():
                 payload = {"method": "[b]productOnClient_getHashes[/b]","params": [],"id":1}  
                 auth = "Basic "+base64.encodestring('[b]login[/b]:[b]password[/b]')
                 headers={'Content-type': 'application/json','Authorization':auth}
                 conn = httplib.HTTPSConnection('[b]opsi-server.foo[/b]',4447)
                 conn.request("POST", "/rpc",json.dumps(payload),headers)
                 response = conn.getresponse()
                  ##print response.status, response.reason
                 data = json.loads(response.read())
                 for line in data['result']:
                         if line['actionRequest'] == 'setup':
                              print line
                  conn.close()    

         if __name__ == "__main__":
              main()
With this code i have this result :

Code: Alles auswählen

{u'installationStatus': u'installed', u'actionProgress': u'', u'actionResult': u'successful', u'clientId': u'computer-name.yourdomain.fr', u'modificationTime': u'2014-06-26 19:16:42', u'actionRequest': u'setup', u'targetConfiguration': u'installed', u'productVersion': u'1.7.0u21', u'productType': u'LocalbootProduct', u'lastAction': u'setup', u'packageVersion': u'3', u'actionSequence': -1, u'type': u'ProductOnClient', u'productId': u'javavm_7'}
....
....
....
Thank You Very much for your help :D :D