Package Products :: Package ZenModel :: Package browser :: Module DeviceStatus |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenModel.browser.DeviceStatus

      1  ###########################################################################
      2  #
      3  # This program is part of Zenoss Core, an open source monitoring platform.
      4  # Copyright (C) 2007, Zenoss Inc.
      5  #
      6  # This program is free software; you can redistribute it and/or modify it
      7  # under the terms of the GNU General Public License version 2 as published by
      8  # the Free Software Foundation.
      9  #
     10  # For complete information please visit: http://www.zenoss.com/oss/
     11  #
     12  ###########################################################################
     13
     14  from Products.Five.browser import BrowserView
     15  from Products.ZenUtils.jsonutils import json
     16
     17 -class DeviceComponentTable(BrowserView):
     18      """
     19      Populates the component table that appears on the device status page.
     20      """
     21 -    def __call__(self):
     22          return self.getDeviceComponentEventSummary(self.context)
     23
     24      @json
     25 -    def getDeviceComponentEventSummary(self, device):
     26          """
     27          Return a list of categories of components on a device along with event
     28          pills for the maximum severity event on each category in the form of a
     29          JSON object ready for inclusion in a YUI data table. If a category
     30          contains components with events, the component and its associated event
     31          pill are returned as a separate (indented) row.
     32
     33          @param device: The device for which to gather component data
     34          @type device: L{Device}
     35          @return: A dictionary representation of the columns and rows of the
     36          table
     37          @rtype: dict
     38          """
     39          zem = self.context.ZenEventManager
     40          mydict = {'columns':[], 'data':[]}
     41          mydict['columns'] = ['Component Type', 'Status']
     42          devdata = []
     43          query = { 'getParentDeviceName':device.id,}
     44          brains = device.componentSearch(query)
     45          metatypes = set([str(x.meta_type) for x in brains])
     46          resultdict = {}
     47          for mt in metatypes: resultdict[mt] = {}
     48          evpilltemplate = ('<img src="img/%s_dot.png" '
     49                            'width="15" height="15" '
     50                            'style="cursor:hand;cursor:pointer" '
     51                            'onclick="location.href'
     52                            '=\'%s/viewEvents\'"/>')
     53          linktemplate = ("<a href='%s' class='prettylink'>"
     54                          "<div class='device-icon-container'>%s "
     55                          "</div>%s</a>")
     56          colors = "green grey blue yellow orange red".split()
     57          indent = "&nbsp;"*8
     58          def getcompfrombrains(id):
     59              for comp in brains:
     60                  compid = comp.getPrimaryId.split('/')[-1]
     61                  if compid==id:
     62                      return comp.getPrimaryId, comp.meta_type, comp.getPath()
     63              return None, None, None
     64          devurl = device.getPrimaryUrlPath()
     65          ostab = devurl.rstrip('/') + '/os'
     66          for event in zem.getEventListME(device):
     67              if not len(event.component): continue
     68              id, metatype, url = getcompfrombrains(event.component)
     69              if id is None or metatype is None or url is None:
     70                  id, metatype, url = event.component, 'Other', devurl + '/os'
     71              tally = resultdict.setdefault(metatype,
     72                              {'sev':event.severity,
     73                               'components': {id: (event.severity, 1, id, ostab)}})
     74              tally.setdefault('sev', event.severity)
     75              tally.setdefault('components', {id: (event.severity, 0, id, ostab)})
     76              if tally['sev'] < event.severity: tally['sev'] = event.severity
     77              comp = tally['components'].setdefault(id, (event.severity, 0,
     78                                                         id, ostab))
     79              comptotal = tally['components'][id][1]
     80              compsev = tally['components'][id][0]
     81              newsev = compsev
     82              if event.severity > compsev: newsev = event.severity
     83              tally['components'][id] = (newsev, comptotal+1, id, url)
     84          r = resultdict
     85          categorysort = [(r[x]['sev'], len(r[x]['components']), x,
     86                           r[x]['components'].values()) for x in r if r[x]]
     87          categorysort.sort(); categorysort.reverse()
     88          categorysort.extend([(0, 0, x, []) for x in r if not r[x]])
     89          for bunch in categorysort:
     90              catsev, catnum, catname, comps = bunch
     91              catlink = ostab
     92              catcolor = colors[catsev]
     93              evpill = evpilltemplate % (catcolor, device.getPrimaryUrlPath())
     94              if catnum: evpill = ''
     95              devdata.append((linktemplate % (catlink, '', catname), evpill))
     96              comps.sort()
     97              for comp in comps:
     98                  compsev, compnum, complink, compurl = comp
     99                  compcolor = colors[compsev]
    100                  if not complink.startswith('/zport'):
    101                      compname = complink
    102                      complink = devurl.rstrip('/') + '/viewEvents'
    103                  else:
    104                      compname = complink.split('/')[-1]
    105                      complink = compurl.rstrip('/') + '/viewEvents'
    106                  if not compname: continue
    107                  compname = "<strong>%s</strong>" % compname
    108                  devdata.append(
    109                      (linktemplate % (complink, '', indent+compname),
    110                       evpilltemplate % (compcolor,
    111                                         device.getPrimaryUrlPath())
    112                      )
    113                  )
    114          mydict['data'] = [{'Component Type':x[0],
    115                             'Status':x[1]} for x in devdata]
    116          return mydict
    117