Ad Widget

Collapse

Python get() API giving "internal server error" for 10000+ items

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theKashyap
    Junior Member
    • Sep 2013
    • 5

    #1

    Python get() API giving "internal server error" for 10000+ items

    Hey Guys,

    I've written the following piece of python code to fetch the list of items for a given host. It works for hostAAA and hostBBB, which have ~80 and ~4000 items respectively. But it consistently fails for hostCCC which has about 10000 items. Error is always the same: "urllib2.HTTPError: HTTP Error 500: Internal Server Error"

    Any ideas? Workarounds are welcome too as long as I can get the list in python..

    I believe I'm using this python port of the API.

    Code:
    import sys
    import zabbix_api
    import logging
    import csv
    
    if __name__ != '__main__':
        print 'Run from command line. Do not import.'
        sys.exit(1)
    
    # Connect...
    ZABBIX_USERNAME, ZABBIX_PASSWORD, zabbixServer = 'admin', 'password', 'zabbix.dev.com'
    zapi = zabbix_api.ZabbixAPI(server="http://%s/zabbix/" % zabbixServer)
    zapi.login(ZABBIX_USERNAME, ZABBIX_PASSWORD)
    
    def getItems(host):
        # get the hostId from hostName
        hostId = zapi.host.get({"filter": {"host": host}})[0]["hostid"]
        return zapi.item.getObjects({'hostid': hostId})
    
    def getItemsForHost(host):
        items = getItems(host)
        with open(host + '_items.csv', 'wb') as csvFile:
            # we're interested only in these fields for our analysis.
            fieldNames = ['hostid', 'itemid', 'name', 'type', 'key_', 'templateid', 'status', 'description']
            out = csv.DictWriter(csvFile, fieldNames, extrasaction='ignore')
            out.writeheader()
            for item in items:
                out.writerow(item)
    
    getItemsForHost('hostAAA') # around 78 items
    getItemsForHost('hostBBB') # around 3800 items
    getItemsForHost('hostCCC') # 10000+ items
    Code:
    Traceback (most recent call last):
      File "C:\Workspaces\eclipse-kepler\ZabbixConfig\zabbixClient.py", line 64, in <module>
        getItemsForHost('hostCCC')
      File "C:\Workspaces\eclipse-kepler\ZabbixConfig\zabbixClient.py", line 52, in getItemsForHost
        items = getItems(host)
      File "C:\Workspaces\eclipse-kepler\ZabbixConfig\zabbixClient.py", line 34, in getItems
        items = zapi.item.getObjects({'hostid': hostId})
      File "C:\Workspaces\eclipse-kepler\zabbix_api.py", line 347, in method
        return self.universal("%s.%s" % (self.data["prefix"], name), opts[0])
      File "C:\Workspaces\eclipse-kepler\zabbix_api.py", line 85, in wrapper
        return self.do_request(self.json_obj(method, opts))['result']
      File "C:\Workspaces\eclipse-kepler\zabbix_api.py", line 354, in do_request
        return self.parent.do_request(req)
      File "C:\Workspaces\eclipse-kepler\zabbix_api.py", line 283, in do_request
        response = opener.open(request, timeout=self.timeout)
      File "C:\ProgFiles\Python275\lib\urllib2.py", line 410, in open
        response = meth(req, response)
      File "C:\ProgFiles\Python275\lib\urllib2.py", line 523, in http_response
        'http', request, response, code, msg, hdrs)
      File "C:\ProgFiles\Python275\lib\urllib2.py", line 448, in error
        return self._call_chain(*args)
      File "C:\ProgFiles\Python275\lib\urllib2.py", line 382, in _call_chain
        result = func(*args)
      File "C:\ProgFiles\Python275\lib\urllib2.py", line 531, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 500: Internal Server Error
  • Pada
    Senior Member
    • Apr 2012
    • 236

    #2
    Hi,

    This issue is most likely a server side issue, and not an issue with your client side Python code.

    Have a look at your Zabbix server's Apache error logs.

    I'm guessing that your Zabbix api_jsonrpc.php script exceeded its memory limit, which is typically only set to 128MB in a Zabbix frontend environment.

    Comment

    • theKashyap
      Junior Member
      • Sep 2013
      • 5

      #3
      Thanks..

      Being new to 'webservers/zabbix' I was looking at logs in /var/www/... instead of /var/log/httpd..

      "PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 11064910 bytes) in /usr/share/zabbix/include/classes/class.cjson.php on line 144"

      Comment

      • Pada
        Senior Member
        • Apr 2012
        • 236

        #4
        ... just as I said in my original post: php script ran out of memory

        Currently your php.ini file contains the following: "memory_limit = 128M"
        If you change it to say 256M, then it may work, but I'd reckon with 10000+ items you'll need to assign way more than that! Take note that your server on which the Apache webserver is running will need lots of memory if you're going to allow PHP to consume (peak/max) that much memory.

        After you've made the change, simply reload apache's configuration.
        in Fedora Core/RHEL/CentOS, run: service httpd reload
        in Ubuntu, run: service apache2 reload

        I'm not sure if other people has had this issue before, because if they have, then it may be worth while to ask for a feature/patch so that the Zabbix PHP script does not consume that much memory.

        Good luck with this issue of yours!

        Comment

        Working...