Ejemplo n.º 1
0
import openhab

from java.lang.management import ManagementFactory
from com.sun.management import HotSpotDiagnosticMXBean
hotSpotDiagnosticMXBean = ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean)

@openhab.rule
class HeapDumpRule(object):
  """Dump the Java heap when thread count exceeds a specified threshold"""
  def __init__(self, maxThreads, outpath, notify=False):
    self._maxThreads = maxThreads
    self._outpath = outpath
    self._notify = notify
    self._dumped = False

  def getEventTrigger(self):
    return [ ChangedEventTrigger("JmxThreadCount") ]

  def execute(self, event):
    if not self._dumped and event.item.state.intValue() > self._maxThreads:
      self._dumped = True
      message = "EXCESSIVE THREADS: count={}, dumping heap".format(event.item.state)
      self.log.error(message)
      if self._notify:
        self.sendNotification(message)
      hotSpotDiagnosticMXBean.dumpHeap(self._outpath, True)


def getRules():
  return RuleSet([ HeapDumpRule(400, "/opt/openhab-1.7.1/memory_dump.hprof", True) ])