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

# Source Code for Module Products.ZenModel.WinService

      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 Globals import InitializeClass
     15  from AccessControl import ClassSecurityInfo, Permissions
     16  from Products.ZenModel.ZenossSecurity import *
     17
     18  from Products.ZenRelations.RelSchema import *
     19  from Products.ZenUtils.Utils import prepId
     20  from Products.ZenWidgets import messaging
     21
     22  from Service import Service
     23
     24 -def manage_addWinService(context, id, description, userCreated=None,
     25                           REQUEST=None):
     26      """
     27      Create a WinService and add it to context. context should be a
     28      device.os.winservices relationship.
     29      """
     30      s = WinService(id)
     31      # Indexing is subscribed to ObjectAddedEvent, which fires
     32      # on _setObject, so we want to set service class first.
     33      args = {'name':id, 'description':description}
     34      s.__of__(context).setServiceClass(args)
     35      context._setObject(id, s)
     36      s = context._getOb(id)
     37      s.serviceName = id
     38      s.caption = description
     39      if userCreated: s.setUserCreateFlag()
     40      if REQUEST is not None:
     41          REQUEST['RESPONSE'].redirect(context.absolute_url()
     42                                    +'/manage_main')
     43      return s
     44
     45
     46 -class WinService(Service):
     47      """Windows Service Class
     48      """
     49      portal_type = meta_type = 'WinService'
     50
     51      serviceName = ""
     52      caption = ""
     53      pathName = ""
     54      serviceType = ""
     55      startMode = ""
     56      startName = ""
     57      monitoredStartModes = []
     58      collectors = ('zenwin',)
     59
     60      _properties = Service._properties + (
     61          {'id': 'serviceName', 'type':'string', 'mode':'w'},
     62          {'id': 'caption', 'type':'string', 'mode':'w'},
     63          {'id': 'pathName', 'type':'string', 'mode':'w'},
     64          {'id': 'serviceType', 'type':'string', 'mode':'w'},
     65          {'id': 'startMode', 'type':'string', 'mode':'w'},
     66          {'id': 'startName', 'type':'string', 'mode':'w'},
     67          {'id': 'monitoredStartModes', 'type':'lines', 'mode':'w'},
     68      )
     69
     70      _relations = Service._relations + (
     71          ("os", ToOne(ToManyCont, "Products.ZenModel.OperatingSystem", "winservices")),
     72      )
     73
     74      factory_type_information = (
     75          {
     76              'immediate_view' : 'winServiceDetail',
     77              'actions'        :
     78              (
     79                  { 'id'            : 'status'
     80                  , 'name'          : 'Status'
     81                  , 'action'        : 'winServiceDetail'
     82                  , 'permissions'   : (
     83                    Permissions.view, )
     84                  },
     85                  { 'id'            : 'events'
     86                  , 'name'          : 'Events'
     87                  , 'action'        : 'viewEvents'
     88                  , 'permissions'   : (ZEN_VIEW, )
     89                  },
     90                  { 'id'            : 'manage'
     91                  , 'name'          : 'Administration'
     92                  , 'action'        : 'winServiceManage'
     93                  , 'permissions'   : ("Manage DMD",)
     94                  },
     95                  { 'id'            : 'viewHistory'
     96                  , 'name'          : 'Modifications'
     97                  , 'action'        : 'viewHistory'
     98                  , 'permissions'   : (ZEN_VIEW_MODIFICATIONS,)
     99                  },
    100              )
    101           },
    102          )
    103
    104      security = ClassSecurityInfo()
    105
    106
    107 -    def getInstDescription(self):
    108          """Return some text that describes this component.  Default is name.
    109          """
    110          return "'%s' StartMode:%s StartName:%s" % (self.caption,
    111                          self.startMode, self.startName)
    112
    113
    114 -    def getMonitoredStartModes(self):
    115          if self.monitoredStartModes:
    116              return self.monitoredStartModes
    117          return self.serviceclass().monitoredStartModes
    118
    119
    120 -    def monitored(self):
    121          """Should this Windows Service be monitored
    122          """
    123          startMode = getattr(self, "startMode", None)
    124          #don't monitor Disabled services
    125          if startMode and startMode == "Disabled": return False
    126          return Service.monitored(self)
    127
    128 -    def getServiceClass(self):
    129          """Return a dict like one set by zenwinmodeler for services.
    130          """
    131          desc = self.description
    132          if not desc:
    133              svccl = self.serviceclass()
    134              if svccl: desc = svccl.description
    135          return {'name': self.serviceName, 'description': self.caption }
    136
    137
    138 -    def setServiceClass(self, kwargs):
    139          """Set the service class where name=ServiceName and description=Caption.
    140          """
    141          self.serviceName = kwargs['name']
    142          self.caption = kwargs['description']
    143          path = "/WinService/"
    144          srvs = self.dmd.getDmdRoot("Services")
    145          srvclass = srvs.createServiceClass(
    146              name=self.serviceName, description=self.caption, path=path)
    147          self.serviceclass.addRelation(srvclass)
    148
    149
    150 -    def name(self):
    151          """Return the name of this service.
    152          """
    153          return self.serviceName
    154
    155 -    def getCaption(self):
    156          return self.caption
    157      primarySortKey = getCaption
    158
    159      security.declareProtected('Manage DMD', 'manage_editService')
    160 -    def manage_editService(self, id=None, description=None,
    161                              pathName=None, serviceType=None,
    162                              startMode=None, startName=None,
    163                              monitoredStartModes=[],
    164                              monitor=False, severity=5,
    165                              REQUEST=None):
    166          """Edit a Service from a web page.
    167          """
    168          msg = []
    169          renamed = False
    170          if id is not None:
    171              self.serviceName = id
    172              self.description = description
    173              self.caption = description
    174              self.pathName = pathName
    175              self.serviceType = serviceType
    176              self.startMode = startMode
    177              self.startName = startName
    178
    179              if self.id != id:
    180                  id = prepId(id)
    181                  self.setServiceClass(dict(name=id, description=description))
    182                  renamed = self.rename(id)
    183
    184          if set(monitoredStartModes) != set(self.getMonitoredStartModes()):
    185              self.monitoredStartModes = monitoredStartModes
    186              msg.append("Updated monitored start modes")
    187
    188          tmpl = super(WinService, self).manage_editService(
    189              monitor, severity, msg=msg, REQUEST=REQUEST)
    190          if REQUEST and renamed:
    191              messaging.IMessageSender(self).sendToBrowser(
    192                  'Service Renamed',
    193                  "Object renamed to: %s" % self.id
    194              )
    195              return self.callZenScreen(REQUEST, renamed)
    196          return tmpl
    197
    198  InitializeClass(WinService)
    199