Retrieval computer Information via RPC API

jroch
Beiträge: 10
Registriert: 21 Okt 2014, 18:06

Retrieval computer Information via RPC API

Beitrag 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 ;)
Benutzeravatar
n.wenselowski
Ex-uib-Team
Beiträge: 3194
Registriert: 04 Apr 2013, 12:15

Re: Retrieval computer Information via RPC API

Beitrag 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
Zuletzt geändert von n.wenselowski am 23 Okt 2014, 09:39, insgesamt 1-mal geändert.

Code: Alles auswählen

import OPSI
jroch
Beiträge: 10
Registriert: 21 Okt 2014, 18:06

Re: Retrieval computer Information via RPC API

Beitrag 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
Benutzeravatar
n.wenselowski
Ex-uib-Team
Beiträge: 3194
Registriert: 04 Apr 2013, 12:15

Re: Retrieval computer Information via RPC API

Beitrag 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

Code: Alles auswählen

import OPSI
jroch
Beiträge: 10
Registriert: 21 Okt 2014, 18:06

Re: Retrieval computer Information via RPC API

Beitrag 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 ? :)
dkoch
Beiträge: 309
Registriert: 25 Nov 2011, 14:03

Re: Retrieval computer Information via RPC API

Beitrag von dkoch »

I would recommend you to use a json-rpc library
wiki/userspace:webservice_corner:ruby-rpc
Benutzeravatar
n.wenselowski
Ex-uib-Team
Beiträge: 3194
Registriert: 04 Apr 2013, 12:15

Re: Retrieval computer Information via RPC API

Beitrag 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

Code: Alles auswählen

import OPSI
jroch
Beiträge: 10
Registriert: 21 Okt 2014, 18:06

Re: Retrieval computer Information via RPC API

Beitrag 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..
Benutzeravatar
n.wenselowski
Ex-uib-Team
Beiträge: 3194
Registriert: 04 Apr 2013, 12:15

Re: Retrieval computer Information via RPC API

Beitrag 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

Code: Alles auswählen

import OPSI
jroch
Beiträge: 10
Registriert: 21 Okt 2014, 18:06

Re: Retrieval computer Information via RPC API

Beitrag 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
Antworten