Skip to content

Instantly share code, notes, and snippets.

@jstanley23
Created September 25, 2014 15:02
Show Gist options
  • Save jstanley23/aae412d511366a2b2d70 to your computer and use it in GitHub Desktop.
Save jstanley23/aae412d511366a2b2d70 to your computer and use it in GitHub Desktop.
__doc__='''Secure24HTTPMonitorDataSource.py
Defines datasource for Secure24HTTPMonitor. A slightly modified version of the
default HttpMonitor DataSource
'''
import os
import sys
import time
import subprocess
from twisted.internet import error, protocol, reactor
from twisted.protocols.basic import LineReceiver
import logging
from AccessControl import ClassSecurityInfo, Permissions
from zope.component import adapts
from zope.interface import implements
from twisted.internet import defer
from Products.Zuul.infos.template import RRDDataSourceInfo
from Products.Zuul.interfaces import IRRDDataSourceInfo
from Products.Zuul.form import schema
from Products.Zuul.infos import ProxyProperty
from Products.Zuul.utils import ZuulMessageFactory as _t
from Products.ZenUtils.Utils import binPath
from Products.ZenEvents import ZenEventClasses
from ZenPacks.zenoss.PythonCollector.datasources.PythonDataSource \
import PythonDataSource, PythonDataSourcePlugin
SingleLineText = schema.TextLine
MultiLineText = schema.Text
ZENPACKID = 'ZenPacks.jls.test'
log = logging.getLogger("zen.Secure24HTTP")
class TestDataSource(PythonDataSource):
"""
Subclass PythonDataSource to put a new datasources into Zenoss
"""
HTTP_MONITOR = 'Test HTTP Monitor'
sourcetypes = (HTTP_MONITOR,)
sourcetype = HTTP_MONITOR
timeout = '${here/timeout}'
eventClass = '/Status/Web'
hostname = '${dev/id}'
domain = '${here/domain}'
ipAddress = '${dev/manageIp}'
port = '${here/port}'
useSsl= '${here/ssl}'
url = '${here/url}'
regex = '${here/regex}'
caseSensitive = '${here/case}'
invert = '${here/invert}'
basicAuthUser = '${here/httpuser}'
basicAuthPass = '${here/httppass}'
onRedirect = '${here/redirect}'
cycle = '${here/cycle}'
cycletime = 400
plugin_classname = ZENPACKID + \
'.datasources.TestDataSource.TestDataSourcePlugin'
_properties = PythonDataSource._properties + (
{'id':'hostname', 'type':'string', 'mode':'w'},
{'id':'domain', 'type':'string', 'mode':'w'},
{'id':'ipAddress', 'type':'string', 'mode':'w'},
{'id':'port', 'type':'string', 'mode':'w'},
{'id':'useSsl', 'type':'string', 'mode':'w'},
{'id':'url', 'type':'string', 'mode':'w'},
{'id':'regex', 'type':'string', 'mode':'w'},
{'id':'caseSensitive', 'type':'string', 'mode':'w'},
{'id':'invert', 'type':'string', 'mode':'w'},
{'id':'basicAuthUser', 'type':'string', 'mode':'w'},
{'id':'basicAuthPass', 'type':'string', 'mode':'w'},
{'id':'onRedirect', 'type':'string', 'mode':'w'},
{'id':'timeout', 'type':'string', 'mode':'w'},
{'id':'cycle', 'type':'string', 'mode':'w'},
)
security = ClassSecurityInfo()
class ITestDataSourceInfo(IRRDDataSourceInfo):
timeout = SingleLineText(title=_t(u'Timeout (seconds)'))
cycle = SingleLineText(title=_t(u'Cycle Time (seconds)'))
hostname = SingleLineText(title=_t(u'Host Name'), group=_t('HTTP Monitor'))
domain = SingleLineText(title=_t(u'Domain Name'), group=_t('HTTP Monitor'))
port = SingleLineText(title=_t(u'Port'), group=_t('HTTP Monitor'))
ipAddress = SingleLineText(title=_t(u'IP Address or Proxy Address'), group=_t('HTTP Monitor'))
url = SingleLineText(title=_t(u'URL'), group=_t('HTTP Monitor'))
useSsl = SingleLineText(title=_t(u'Use SSL?'), group=_t('HTTP Monitor'))
regex = SingleLineText(title=_t(u'Regular Expression'), group=_t('HTTP Monitor'))
caseSensitive = SingleLineText(title=_t(u'Case Sensitive'), group=_t('HTTP Monitor'))
basicAuthUser = schema.TextLine(title=_t(u'Basic Auth User'), group=_t('HTTP Monitor'))
invert = SingleLineText(title=_t(u'Invert Expression'), group=_t('HTTP Monitor'))
basicAuthPass = schema.Password(title=_t(u'Basic Auth Password'), group=_t('HTTP Monitor'))
onRedirect = SingleLineText(title=_t(u'Redirect Behavior'), group=_t('HTTP Monitor'))
class TestDataSourceInfo(RRDDataSourceInfo):
implements(ITestDataSourceInfo)
adapts(TestDataSource)
timeout = ProxyProperty('timeout')
cycle = ProxyProperty('cycle')
hostname = ProxyProperty('hostname')
domain = ProxyProperty('domain')
ipAddress = ProxyProperty('ipAddress')
port = ProxyProperty('port')
useSsl = ProxyProperty('useSsl')
url = ProxyProperty('url')
regex = ProxyProperty('regex')
caseSensitive = ProxyProperty('caseSensitive')
invert = ProxyProperty('invert')
basicAuthUser = ProxyProperty('basicAuthUser')
basicAuthPass = ProxyProperty('basicAuthPass')
onRedirect = ProxyProperty('onRedirect')
class TestDataSourcePlugin(PythonDataSourcePlugin):
@classmethod
def config_key(cls, datasource, context):
params = cls.params(datasource, context)
return(
context.device().id,
datasource.getCycleTime(context),
datasource.id,
datasource.plugin_classname,
params.get('cmd'),
)
@classmethod
def params(cls, datasource, context):
params = {}
parts = [binPath('check_http')]
if context.ip:
parts.append('-H %s' % context.ip)
if context.domain:
parts.append('-I %s' % context.domain)
if context.port:
parts.append('-p %s' % context.port)
if context.timeout:
parts.append('-t %s' % context.timeout)
if context.ssl:
parts.append('-S')
if context.url:
parts.append("-u '%s'" % context.url)
if context.regex:
if context.case:
parts.append("-r '%s'" % context.regex)
else:
parts.append("-R '%s'" % context.regex)
if context.invert:
parts.append('--invert-regex')
if context.httpuser or context.httppass:
parts.append("-a '%s':'%s'" % (context.httpuser, context.httppass))
if context.redirect:
parts.append('-f %s' % context.redirect)
cmd = ' '.join(parts)
params['cmd'] = cmd
return params
@defer.inlineCallbacks
def collect(self, config):
cmd = config.datasources[0].params['cmd']
cmd_process = subprocess.Popen(cmd.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
cmd_out = cmd_process.communicate()
d = defer.Deferred()
d.callback(cmd_out)
return d
# import pdb; pdb.set_trace()
def onSuccess(self, results, config):
data = self.new_data()
ds0 = config.datasources[0]
sitestatusinfo = results[results.keys()[0]]
import pdb; pdb.set_trace()
sitestatus = {'true': 'Running', 'false': 'Stopped'}.get(
sitestatusinfo[0].ServerAutoStart, 'Unknown')
evtmessage = 'IIS Service {0} is in {1} state'.format(
ds0.config_key[4],
sitestatus
)
data['events'].append({
'eventClassKey': 'IISSiteStatus',
'eventKey': 'IISSite',
'severity': ZenEventClasses.Info,
'summary': evtmessage,
'component': ds0.component,
'device': config.id,
})
return data
def onError(self, result, config):
msg = result
log.error(msg)
data = self.new_data()
import pdb; pdb.set_trace()
data['events'].append({
'severity': ZenEventClasses.Warning,
'eventClassKey': 'IISSiteStatusError',
'eventKey': 'IISSite',
'summary': msg,
'device': config.id})
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment