def _add_service_monitoring(self, drone, monitoredservice, moninfo): ''' We start the monitoring of 'monitoredservice' using the information in 'moninfo' - which came from MonitoringRule.constructaction() ''' monitorclass = moninfo['monitorclass'] monitortype = moninfo['monitortype'] if 'provider' in moninfo: monitorprovider = moninfo['provider'] classtype = "%s::%s:%s" % (monitorclass, monitorprovider, monitortype) else: monitorprovider = None classtype = "%s::%s" % (monitorclass, monitortype) # Compute interval and timeout - based on global 'config' params = ConfigFile.agent_params(self.config, 'monitoring', classtype, drone.designation) monitorinterval = params['parameters']['repeat'] monitortimeout = params['parameters']['timeout'] monitorarglist = moninfo.get('arglist', {}) monargv = moninfo.get('argv', None) # Make up a monitor name that should be unique to us -- but reproducible # We create the monitor name from the host name, the monitor class, # monitoring type and a hash of the arguments to the monitoring agent # Note that this is different from the hashed service/entity name, since we could # have multiple ways of monitoring the same entity d = hashlib.md5() # pylint mistakenly thinks md5 objects don't have update member # pylint: disable=E1101 d.update( '%s:%s:%s:%s' % (drone.designation, monitorclass, monitortype, monitorprovider)) if monitorarglist is not None: names = monitorarglist.keys() names.sort() for name in names: # pylint thinks md5 objects don't have update member # pylint: disable=E1101 d.update('"%s": "%s"' % (name, monitorarglist[name])) monitorname = ( '%s:%s:%s::%s' % (drone.designation, monitorclass, monitortype, d.hexdigest())) monnode = self.store.load_or_create(MonitorAction, domain=drone.domain, monitorname=monitorname, monitorclass=monitorclass, monitortype=monitortype, interval=monitorinterval, timeout=monitortimeout, provider=monitorprovider, arglist=monitorarglist, argv=monargv) if monitorclass == 'nagios': monnode.nagiospath = self.config['monitoring']['nagiospath'] if not Store.is_abstract(monnode): print >> sys.stderr, ('Previously monitored %s on %s' % (monitortype, drone.designation)) monnode.activate(monitoredservice, drone)
def processpkt_netconfig(self, drone, _unused_srcaddr, jsonobj): '''We want to trigger Switch discovery when we hear a 'netconfig' packet Build up the parameters for the discovery action, then send it to drone.request_discovery(...) To build up the parameters, you use ConfigFile.agent_params() which will pull values from the system configuration. ''' init_params = ConfigFile.agent_params(self.config, 'discovery', '#SWITCH', drone.designation) data = jsonobj['data'] # the data portion of the JSON message discovery_args = [] for devname in data.keys(): devinfo = data[devname] if (str(devinfo['operstate']) == 'up' and str(devinfo['carrier']) == 'True' and str(devinfo['address']) != '00-00-00-00-00-00' and str(devinfo['address']) != ''): params = pyConfigContext(init_params) params[CONFIGNAME_INSTANCE] = '#SWITCH_' + devname params[CONFIGNAME_DEVNAME] = devname params[CONFIGNAME_SWPROTOS] = ["lldp", "cdp"] #print >> sys.stderr, '***#SWITCH parameters:', params discovery_args.append(params) if discovery_args: drone.request_discovery(discovery_args)
def send_hbmsg(self, dest, fstype, addrlist): '''Send a message with an attached pyNetAddr list - each including port numbers' This is intended primarily for start or stop heartbeating messages.''' # Now we create a collection of frames that looks like this: # # One FrameTypes.RSCJSON frame containing JSON Heartbeat parameters # one frame per dest, type FrameTypes.IPPORT # params = ConfigFile.agent_params(CMAdb.io.config, 'heartbeats', None, self.designation) framelist = [ { 'frametype': FrameTypes.RSCJSON, 'framevalue': str(params) }, ] for addr in addrlist: if addr is None: continue framelist.append({ 'frametype': FrameTypes.IPPORT, 'framevalue': addr }) CMAdb.transaction.add_packet(dest, fstype, framelist)
def processpkt_netconfig(self, drone, unused_srcaddr, jsonobj): '''We want to trigger Switch discovery when we hear a 'netconfig' packet Build up the parameters for the discovery action, then send it to drone.request_discovery(...) To build up the parameters, you use ConfigFile.agent_params() which will pull values from the system configuration. ''' unused_srcaddr = unused_srcaddr # make pylint happy init_params = ConfigFile.agent_params(self.config, 'discovery', '#SWITCH' , drone.designation) data = jsonobj['data'] # the data portion of the JSON message discovery_args = [] for devname in data.keys(): devinfo = data[devname] if (str(devinfo['operstate']) == 'up' and str(devinfo['carrier']) == 'True' and str(devinfo['address']) != '00-00-00-00-00-00' and str(devinfo['address']) != ''): params = pyConfigContext(init_params) params[CONFIGNAME_INSTANCE] = '#SWITCH_' + devname params[CONFIGNAME_DEVNAME] = devname params[CONFIGNAME_SWPROTOS] = ["lldp", "cdp"] #print >> sys.stderr, '***#SWITCH parameters:', params discovery_args.append(params) if discovery_args: drone.request_discovery(discovery_args)
def request_discovery(self, args): ##< A vector of arguments containing '''Send our System a request to perform discovery We send a DISCNAME frame with the instance name then an optional DISCINTERVAL frame with the repeat interval then a DISCJSON frame with the JSON data for the discovery operation. Our argument is a vector of pyConfigContext objects with values for 'instance' Name of this discovery instance 'interval' How often to repeat this discovery action 'timeout' How long to wait before considering this discovery failed... ''' #fs = pyFrameSet(FrameSetTypes.DODISCOVER) frames = [] for arg in args: agent_params = ConfigFile.agent_params(CMAdb.io.config, 'discovery', arg[CONFIGNAME_TYPE], self.designation) for key in ('repeat', 'warn' 'timeout', 'nice'): if key in agent_params and key not in arg: arg[key] = agent_params[arg] instance = arg['instance'] frames.append({'frametype': FrameTypes.DISCNAME, 'framevalue': instance}) if 'repeat' in arg: interval = int(arg['repeat']) frames.append({'frametype': FrameTypes.DISCINTERVAL, 'framevalue': int(interval)}) else: interval = 0 frames.append({'frametype': FrameTypes.DISCJSON, 'framevalue': str(arg)}) self.send_frames(FrameSetTypes.DODISCOVER, frames)
def request_discovery(self, args): ##< A vector of arguments containing '''Send our drone a request to perform discovery We send a DISCNAME frame with the instance name then an optional DISCINTERVAL frame with the repeat interval then a DISCJSON frame with the JSON data for the discovery operation. Our argument is a vector of pyConfigContext objects with values for 'instance' Name of this discovery instance 'interval' How often to repeat this discovery action 'timeout' How long to wait before considering this discovery failed... ''' #fs = pyFrameSet(FrameSetTypes.DODISCOVER) frames = [] for arg in args: agent_params = ConfigFile.agent_params(CMAdb.io.config, 'discovery', arg[CONFIGNAME_TYPE], self.designation) for key in ('repeat', 'warn' 'timeout', 'nice'): if key in agent_params and key not in arg: arg[key] = agent_params[arg] instance = arg['instance'] frames.append({'frametype': FrameTypes.DISCNAME, 'framevalue': instance}) if 'repeat' in arg: interval = int(arg['repeat']) frames.append({'frametype': FrameTypes.DISCINTERVAL, 'framevalue': int(interval)}) frames.append({'frametype': FrameTypes.DISCJSON, 'framevalue': str(arg)}) # This doesn't work if the client has bound to a VIP ourip = self.select_ip() # meaning select our primary IP ourip = pyNetAddr(ourip) if ourip.port() == 0: ourip.setport(self.port) #print >> sys.stderr, ('ADDING PACKET TO TRANSACTION: %s', str(frames)) if CMAdb.debug: CMAdb.log.debug('Sending Discovery request(%s, %s) to %s Frames: %s' % (instance, str(interval), str(ourip), str(frames))) CMAdb.transaction.add_packet(ourip, FrameSetTypes.DODISCOVER, frames)
def processpkt_netconfig(self, drone, _unused_srcaddr, jsonobj): '''We want to trigger ARP discovery when we hear a 'netconfig' packet Build up the parameters for the discovery action, then send it to drone.request_discovery(...) To build up the parameters, you use ConfigFile.agent_params() which will pull values from the system configuration. ''' init_params = ConfigFile.agent_params(self.config, 'discovery', '#ARP', drone.designation) data = jsonobj['data'] # the data portion of the JSON message discovery_args = [] for devname in data.keys(): #print >> sys.stderr, "*** devname:", devname devinfo = data[devname] if discovery_indicates_link_is_up(devinfo): params = pyConfigContext(init_params) params[CONFIGNAME_INSTANCE] = '_ARP_' + devname params[CONFIGNAME_DEVNAME] = devname #print >> sys.stderr, '#ARP parameters:', params discovery_args.append(params) if discovery_args: drone.request_discovery(discovery_args)
def processpkt(self, drone, unused_srcaddr, jsonobj): "Send commands to gather discovery data from /proc/sys" unused_srcaddr = unused_srcaddr data = jsonobj['data'] # The data portion of the JSON message osfield = 'operating-system' if osfield not in data: self.log.warning('OS name not found in %s' % str(data)) return osname = data[osfield] if osname.find('Linux') == -1 and osname.find('linux') == -1: self.log.info('ProcSysDiscovery: OS name is not Linux: %s' % str(osname)) return params = ConfigFile.agent_params(self.config, 'discovery', 'proc_sys', drone.designation) params['parameters'] = pyConfigContext( {'ASSIM_discoverdir': '/proc/sys'}) params[CONFIGNAME_TYPE] = 'proc_sys' params[CONFIGNAME_INSTANCE] = '_auto_proc_sys' # Request discovery of checksums of all the binaries talking (tcp) over the network if self.debug: self.log.debug('REQUESTING /proc/sys DISCOVERY') drone.request_discovery((params, ))
def _add_service_monitoring(self, drone, monitoredservice, moninfo): ''' We start the monitoring of 'monitoredservice' using the information in 'moninfo' - which came from MonitoringRule.constructaction() ''' monitorclass = moninfo['monitorclass'] monitortype = moninfo['monitortype'] if 'provider' in moninfo: monitorprovider = moninfo['provider'] classtype = "%s::%s:%s" % (monitorclass, monitorprovider, monitortype) else: monitorprovider = None classtype = "%s::%s" % (monitorclass, monitortype) # Compute interval and timeout - based on global 'config' params = ConfigFile.agent_params(self.config, 'monitoring', classtype, drone.designation) monitorinterval = params['parameters']['repeat'] monitortimeout = params['parameters']['timeout'] monitorarglist = moninfo.get('arglist', {}) monargv = moninfo.get('argv', None) # Make up a monitor name that should be unique to us -- but reproducible # We create the monitor name from the host name, the monitor class, # monitoring type and a hash of the arguments to the monitoring agent # Note that this is different from the hashed service/entity name, since we could # have multiple ways of monitoring the same entity d = hashlib.md5() # pylint mistakenly thinks md5 objects don't have update member # pylint: disable=E1101 d.update('%s:%s:%s:%s' % (drone.designation, monitorclass, monitortype, monitorprovider)) if monitorarglist is not None: names = monitorarglist.keys() names.sort() for name in names: # pylint thinks md5 objects don't have update member # pylint: disable=E1101 d.update('"%s": "%s"' % (name, monitorarglist[name])) monitorname = ('%s:%s:%s::%s' % (drone.designation, monitorclass, monitortype, d.hexdigest())) monnode = self.store.load_or_create(MonitorAction, domain=drone.domain , monitorname=monitorname, monitorclass=monitorclass , monitortype=monitortype, interval=monitorinterval, timeout=monitortimeout , provider=monitorprovider, arglist=monitorarglist, argv=monargv) if monitorclass == 'nagios': monnode.nagiospath = self.config['monitoring']['nagiospath'] if not Store.is_abstract(monnode): print >> sys.stderr, ('Previously monitored %s on %s' % (monitortype, drone.designation)) monnode.activate(monitoredservice, drone)
def processtcpdiscoverypkt(self, drone, unused_srcaddr, jsonobj): "Send commands to generate checksums for this Drone's net-facing things" unused_srcaddr = unused_srcaddr params = ConfigFile.agent_params(self.config, 'discovery', 'checksums', drone.designation) params['parameters'] = pyConfigContext({ 'ASSIM_sumcmds': [ '/usr/bin/sha256sum', '/usr/bin/sha224sum', '/usr/bin/sha384sum', '/usr/bin/sha512sum', '/usr/bin/sha1sum', '/usr/bin/md5sum', '/usr/bin/cksum', '/usr/bin/crc32' ], 'ASSIM_filelist': [ '/bin/sh', '/bin/bash', '/bin/login', '/usr/bin/passwd', '/tmp/foobar' ] }) params[CONFIGNAME_TYPE] = 'checksums' params[CONFIGNAME_INSTANCE] = '_auto_checksumdiscovery' data = jsonobj['data'] # The data portion of the JSON message for procname in data.keys( ): # List of nanoprobe-assigned names of processes... procinfo = data[procname] if 'exe' not in procinfo: continue exename = procinfo.get('exe') # dups (if any) are removed by the agent params['parameters']['ASSIM_filelist'].append(exename) if exename.endswith('/java'): # Special case for some/many JAVA programs - find the jars... if 'cmdline' not in procinfo: continue cmdline = procinfo.get('cmdline') for j in range(0, len(cmdline)): # The argument following -cp is the ':'-separated CLASSPATH if cmdline[j] == '-cp' and j < len(cmdline) - 1: jars = cmdline[j + 1].split(':') for jar in jars: params['parameters']['ASSIM_filelist'].append(jar) break # Request discovery of checksums of all the binaries talking (tcp) over the network print >> sys.stderr, ('REQUESTING CHECKSUM MONITORING OF %d files' % (len(params['parameters']['ASSIM_filelist']))) drone.request_discovery((params, ))
def send_hbmsg(self, dest, fstype, addrlist): '''Send a message with an attached pyNetAddr list - each including port numbers' This is intended primarily for start or stop heartbeating messages.''' # Now we create a collection of frames that looks like this: # # One FrameTypes.RSCJSON frame containing JSON Heartbeat parameters # one frame per dest, type FrameTypes.IPPORT # params = ConfigFile.agent_params(CMAdb.io.config , 'heartbeats', None, self.designation) framelist = [{'frametype': FrameTypes.RSCJSON, 'framevalue': str(params)},] for addr in addrlist: if addr is None: continue framelist.append({'frametype': FrameTypes.IPPORT, 'framevalue': addr}) CMAdb.transaction.add_packet(dest, fstype, framelist)
def processpkt(self, drone, unused_srcaddr, jsonobj): "Send commands to gather discovery data from /proc/sys" unused_srcaddr = unused_srcaddr data = jsonobj["data"] # The data portion of the JSON message osfield = "operating-system" if osfield not in data: self.log.warning("OS name not found in %s" % str(data)) return osname = data[osfield] if osname.find("Linux") == -1 and osname.find("linux") == -1: self.log.info("ProcSysDiscovery: OS name is not Linux: %s" % str(osname)) return params = ConfigFile.agent_params(self.config, "discovery", "proc_sys", drone.designation) params["parameters"] = pyConfigContext({"ASSIM_discoverdir": "/proc/sys"}) params[CONFIGNAME_TYPE] = "proc_sys" params[CONFIGNAME_INSTANCE] = "_auto_proc_sys" # Request discovery of checksums of all the binaries talking (tcp) over the network if self.debug: self.log.debug("REQUESTING /proc/sys DISCOVERY") drone.request_discovery((params,))
def request_discovery(self, args): ##< A vector of arguments containing '''Send our System a request to perform discovery We send a DISCNAME frame with the instance name then an optional DISCINTERVAL frame with the repeat interval then a DISCJSON frame with the JSON data for the discovery operation. Our argument is a vector of pyConfigContext objects with values for 'instance' Name of this discovery instance 'interval' How often to repeat this discovery action 'timeout' How long to wait before considering this discovery failed... ''' #fs = pyFrameSet(FrameSetTypes.DODISCOVER) frames = [] for arg in args: agent_params = ConfigFile.agent_params(CMAdb.io.config, 'discovery', arg[CONFIGNAME_TYPE], self.designation) for key in ('repeat', 'warn' 'timeout', 'nice'): if key in agent_params and key not in arg: arg[key] = agent_params[arg] instance = arg['instance'] frames.append({ 'frametype': FrameTypes.DISCNAME, 'framevalue': instance }) if 'repeat' in arg: interval = int(arg['repeat']) frames.append({ 'frametype': FrameTypes.DISCINTERVAL, 'framevalue': int(interval) }) else: interval = 0 frames.append({ 'frametype': FrameTypes.DISCJSON, 'framevalue': str(arg) }) self.send_frames(FrameSetTypes.DODISCOVER, frames)
def processpkt(self, drone, unused_srcaddr, jsonobj): "Send commands to gather discovery data from /proc/sys" unused_srcaddr = unused_srcaddr data = jsonobj['data'] # The data portion of the JSON message osfield='operating-system' if osfield not in data: self.log.warning('OS name not found in %s' % str(data)) return osname = data[osfield] if osname.find('Linux') == -1 and osname.find('linux') == -1: self.log.info('ProcSysDiscovery: OS name is not Linux: %s' % str(osname)) return params = ConfigFile.agent_params(self.config, 'discovery', 'proc_sys', drone.designation) params['parameters'] = pyConfigContext({'ASSIM_discoverdir': '/proc/sys' }) params[CONFIGNAME_TYPE] = 'proc_sys' params[CONFIGNAME_INSTANCE] = '_auto_proc_sys' # Request discovery of checksums of all the binaries talking (tcp) over the network if self.debug: self.log.debug('REQUESTING /proc/sys DISCOVERY') drone.request_discovery((params,))
def processtcpdiscoverypkt(self, drone, unused_srcaddr, jsonobj): "Send commands to generate checksums for this Drone's net-facing things" unused_srcaddr = unused_srcaddr params = ConfigFile.agent_params(self.config, 'discovery', 'checksums', drone.designation) sumcmds = self.config['checksum_cmds'] filelist = self.config['checksum_files'] filelist.extend(sumcmds) params['parameters'] = pyConfigContext() params[CONFIGNAME_TYPE] = 'checksums' params[CONFIGNAME_INSTANCE] = '_auto_checksumdiscovery' data = jsonobj['data'] # The data portion of the JSON message for procname in data.keys(): # List of of process names... procinfo = data[procname] # (names assigned by the nanoprobe) if 'exe' not in procinfo: continue exename = procinfo.get('exe') # dups (if any) are removed by the agent filelist.append(exename) if exename.endswith('/java'): # Special case for some/many JAVA programs - find the jars... if 'cmdline' not in procinfo: continue cmdline = procinfo.get('cmdline') for j in range(0, len(cmdline)): # The argument following -cp is the ':'-separated CLASSPATH if cmdline[j] == '-cp' and j < len(cmdline)-1: jars = cmdline[j+1].split(':') for jar in jars: filelist.append(jar) break params['parameters']['ASSIM_sumcmds'] = sumcmds params['parameters']['ASSIM_filelist'] = filelist # Request checksums of all the binaries talking (tcp) over the network print >> sys.stderr, ('REQUESTING CHECKSUM MONITORING OF %d files' % (len(params['parameters']['ASSIM_filelist']))) drone.request_discovery((params,))
def processtcpdiscoverypkt(self, drone, _unused_srcaddr, jsonobj): "Send commands generating checksums for the Systems's net-facing things" params = ConfigFile.agent_params(self.config, 'discovery', 'checksums', drone.designation) sumcmds = self.config['checksum_cmds'] filelist = self.config['checksum_files'] filelist.extend(sumcmds) params['parameters'] = pyConfigContext() params[CONFIGNAME_TYPE] = 'checksums' params[CONFIGNAME_INSTANCE] = '_auto_checksumdiscovery' data = jsonobj['data'] # The data portion of the JSON message for procname in data.keys(): # List of of process names... procinfo = data[procname] # (names assigned by the nanoprobe) if 'exe' not in procinfo: continue exename = procinfo.get('exe') # dups (if any) are removed by the agent filelist.append(exename) if exename.endswith('/java'): # Special case for some/many JAVA programs - find the jars... if 'cmdline' not in procinfo: continue cmdline = procinfo.get('cmdline') for j in range(0, len(cmdline)): # The argument following -cp is the ':'-separated CLASSPATH if cmdline[j] == '-cp' and j < len(cmdline) - 1: jars = cmdline[j + 1].split(':') for jar in jars: filelist.append(jar) break params['parameters']['ASSIM_sumcmds'] = sumcmds params['parameters']['ASSIM_filelist'] = filelist # Request checksums of all the binaries talking (tcp) over the network print >> sys.stderr, ('REQUESTING CHECKSUM MONITORING OF %d files' % (len(params['parameters']['ASSIM_filelist']))) drone.request_discovery((params, ))
def processtcpdiscoverypkt(self, drone, _unused_srcaddr, jsonobj): "Send commands generating checksums for the Systems's net-facing things" params = ConfigFile.agent_params(self.config, "discovery", "checksums", drone.designation) sumcmds = self.config["checksum_cmds"] filelist = self.config["checksum_files"] filelist.extend(sumcmds) params["parameters"] = pyConfigContext() params[CONFIGNAME_TYPE] = "checksums" params[CONFIGNAME_INSTANCE] = "_auto_checksumdiscovery" data = jsonobj["data"] # The data portion of the JSON message for procname in data.keys(): # List of of process names... procinfo = data[procname] # (names assigned by the nanoprobe) if "exe" not in procinfo: continue exename = procinfo.get("exe") # dups (if any) are removed by the agent filelist.append(exename) if exename.endswith("/java"): # Special case for some/many JAVA programs - find the jars... if "cmdline" not in procinfo: continue cmdline = procinfo.get("cmdline") for j in range(0, len(cmdline)): # The argument following -cp is the ':'-separated CLASSPATH if cmdline[j] == "-cp" and j < len(cmdline) - 1: jars = cmdline[j + 1].split(":") for jar in jars: filelist.append(jar) break params["parameters"]["ASSIM_sumcmds"] = sumcmds params["parameters"]["ASSIM_filelist"] = filelist # Request checksums of all the binaries talking (tcp) over the network print >>sys.stderr, ( "REQUESTING CHECKSUM MONITORING OF %d files" % (len(params["parameters"]["ASSIM_filelist"])) ) drone.request_discovery((params,))
def _add_service_monitoring(self, drone, monitoredservice, moninfo): ''' We start the monitoring of 'monitoredservice' using the information in 'moninfo' - which came from MonitoringRule.constructaction() and is based on discovery and general rules for that monitoring action Moninfo includes the following kinds of metadata: - identification parameters - class, provider, type - environment variables - command line arguments ''' monitorclass = moninfo['monitorclass'] monitortype = moninfo['monitortype'] monitorprovider = moninfo.get('provider', None) if monitorprovider is not None: classtype = "%s::%s:%s" % (monitorclass, moninfo['provider'], monitortype) else: classtype = "%s::%s" % (monitorclass, monitortype) # Compute interval and timeout - based on global 'config' agent_params = ConfigFile.agent_params(self.config, 'monitoring', classtype, drone.designation) # This produces the following metadata: # - class-independent parameters: repeat, timeout, etc # - environment variables # - command line arguments # These are merged together with the moninfo parameters in the following way # - Class-independent variables are taken from 'params' if conflicting # - Environment variables are taken from 'params' if there's a conflict # - command line arguments from params come first, followed by the # any that come from 'moninfo' parameters = agent_params['parameters'] paraminterval = parameters['repeat'] paramtimeout = parameters['timeout'] paramwarntime = parameters['warn'] paramargv = parameters.get('argv', []) paramenv = parameters.get('env', {}) monargv = moninfo.get('argv', []) monenv = moninfo.get('arglist', {}) argv = [] # pyConfigContext doesn't handle arrays well... if paramargv is not None: argv.extend(paramargv) if monargv is not None: argv.extend(monargv) environ = {} for envset in (monenv, paramenv): if envset is not None: for env in envset: environ[env] = envset[env] # Make up a monitor name that should be unique to us -- but reproducible # We create the monitor name from the host name, the monitor class, # monitoring type and a hash of the arguments to the monitoring agent # Note that this is different from the hashed service/entity name, since we could # have multiple ways of monitoring the same entity d = hashlib.md5() # pylint mistakenly thinks md5 objects don't have update member # pylint: disable=E1101 d.update( '%s:%s:%s:%s' % (drone.designation, monitorclass, monitortype, monitorprovider)) if environ is not None: names = environ.keys() names.sort() for name in names: # pylint thinks md5 objects don't have update member # pylint: disable=E1101 d.update('"%s": "%s"' % (name, environ[name])) for arg in argv: d.update('"%s"' % str(arg)) monitorname = ( '%s:%s:%s::%s' % (drone.designation, monitorclass, monitortype, d.hexdigest())) monnode = self.store.load_or_create( MonitorAction, domain=drone.domain, monitorname=monitorname, monitorclass=monitorclass, monitortype=monitortype, interval=paraminterval, timeout=paramtimeout, warntime=paramwarntime, provider=monitorprovider, arglist=environ if environ else None, argv=argv if argv else None) # Neo4j restriction... if monitorclass == 'nagios': monnode.nagiospath = self.config['monitoring']['nagiospath'] if not Store.is_abstract(monnode): print >> sys.stderr, ('Previously monitored %s on %s' % (monitortype, drone.designation)) monnode.activate(monitoredservice, drone)
def _add_service_monitoring(self, drone, monitoredservice, moninfo): ''' We start the monitoring of 'monitoredservice' using the information in 'moninfo' - which came from MonitoringRule.constructaction() and is based on discovery and general rules for that monitoring action Moninfo includes the following kinds of metadata: - identification parameters - class, provider, type - environment variables - command line arguments ''' monitorclass = moninfo['monitorclass'] monitortype = moninfo['monitortype'] monitorprovider = moninfo.get('provider', None) if monitorprovider is not None: classtype = "%s::%s:%s" % (monitorclass, moninfo['provider'], monitortype) else: classtype = "%s::%s" % (monitorclass, monitortype) # Compute interval and timeout - based on global 'config' agent_params = ConfigFile.agent_params(self.config, 'monitoring', classtype, drone.designation) # This produces the following metadata: # - class-independent parameters: repeat, timeout, etc # - environment variables # - command line arguments # These are merged together with the moninfo parameters in the following way # - Class-independent variables are taken from 'params' if conflicting # - Environment variables are taken from 'params' if there's a conflict # - command line arguments from params come first, followed by the # any that come from 'moninfo' parameters = agent_params['parameters'] paraminterval = parameters['repeat'] paramtimeout = parameters['timeout'] paramwarntime = parameters['warn'] paramargv = parameters.get('argv', []) paramenv = parameters.get('env', {}) monargv = moninfo.get('argv', []) monenv = moninfo.get('arglist', {}) argv = [] # pyConfigContext doesn't handle arrays well... if paramargv is not None: argv.extend(paramargv) if monargv is not None: argv.extend(monargv) environ = {} for envset in (monenv, paramenv): if envset is not None: for env in envset: environ[env] = envset[env] # Make up a monitor name that should be unique to us -- but reproducible # We create the monitor name from the host name, the monitor class, # monitoring type and a hash of the arguments to the monitoring agent # Note that this is different from the hashed service/entity name, since we could # have multiple ways of monitoring the same entity d = hashlib.md5() # pylint mistakenly thinks md5 objects don't have update member # pylint: disable=E1101 d.update('%s:%s:%s:%s' % (drone.designation, monitorclass, monitortype, monitorprovider)) if environ is not None: names = environ.keys() names.sort() for name in names: # pylint thinks md5 objects don't have update member # pylint: disable=E1101 d.update('"%s": "%s"' % (name, environ[name])) for arg in argv: d.update('"%s"' % str(arg)) monitorname = ('%s:%s:%s::%s' % (drone.designation, monitorclass, monitortype, d.hexdigest())) monnode = self.store.load_or_create(MonitorAction, domain=drone.domain , monitorname=monitorname, monitorclass=monitorclass , monitortype=monitortype, interval=paraminterval, timeout=paramtimeout , warntime=paramwarntime , provider=monitorprovider , arglist = environ if environ else None , argv = argv if argv else None) # Neo4j restriction... if monitorclass == 'nagios': monnode.nagiospath = self.config['monitoring']['nagiospath'] if not Store.is_abstract(monnode): print >> sys.stderr, ('Previously monitored %s on %s' % (monitortype, drone.designation)) monnode.activate(monitoredservice, drone)
def dispatch(self, origaddr, frameset): json = None addrstr = repr(origaddr) fstype = frameset.get_framesettype() localtime = None listenaddr = None keyid = None pubkey = None keysize = None #print >> sys.stderr, ("DispatchSTARTUP: received [%s] FrameSet from [%s]" #% (FrameSetTypes.get(fstype)[0], addrstr)) if CMAdb.debug: CMAdb.log.debug( "DispatchSTARTUP: received [%s] FrameSet from [%s]" % (FrameSetTypes.get(fstype)[0], addrstr)) if not self.io.connactive(origaddr): self.io.closeconn(DEFAULT_FSP_QID, origaddr) CMAdb.transaction.post_transaction_packets.append( FrameSetTypes.ACKSTARTUP) for frame in frameset.iter(): frametype = frame.frametype() if frametype == FrameTypes.WALLCLOCK: localtime = str(frame.getint()) elif frametype == FrameTypes.IPPORT: listenaddr = frame.getnetaddr() elif frametype == FrameTypes.HOSTNAME: sysname = frame.getstr() if sysname == CMAdb.nodename: if origaddr.islocal(): CMAdb.log.info( "Received STARTUP from local system (%s)" % addrstr) else: addresses = ['127.0.0.1', '::ffff:127.0.0.1', '::1'] for address in addresses: localhost = pyNetAddr(address) self.io.addalias(localhost, origaddr) CMAdb.log.info("Aliasing %s to %s" % (localhost, origaddr)) elif frametype == FrameTypes.JSDISCOVER: json = frame.getstr() #print >> sys.stderr, 'GOT JSDISCOVER JSON: [%s] (strlen:%s,framelen:%s)' \ #% (json, len(json), frame.framelen()) elif frametype == FrameTypes.KEYID: keyid = frame.getstr() elif frametype == FrameTypes.PUBKEYCURVE25519: pubkey = frame.framevalue() keysize = frame.framelen() joininfo = pyConfigContext(init=json) origaddr, isNAT = self.validate_source_ip(sysname, origaddr, joininfo, listenaddr) CMAdb.log.info( 'Drone %s registered from address %s (%s) port %s, key_id %s' % (sysname, origaddr, addrstr, origaddr.port(), keyid)) drone = self.droneinfo.add(sysname, 'STARTUP packet', port=origaddr.port(), primary_ip_addr=str(origaddr)) drone.listenaddr = str(listenaddr) # Seems good to hang onto this... drone.isNAT = isNAT # ditto... if CMAdb.debug: CMAdb.log.debug('DRONE select_ip() result: %s' % (drone.select_ip())) CMAdb.log.debug('DRONE listenaddr: %s' % (drone.listenaddr)) CMAdb.log.debug('DRONE port: %s (%s)' % (drone.port, type(drone.port))) # Did they give us the crypto info we need? if keyid is None or pubkey is None: if CMAdb.debug: CMAdb.log.debug( 'Drone %s registered with keyid %s and pubkey provided: %s' % (self, keyid, pubkey is not None)) else: if drone.key_id == '': if not keyid.startswith(sysname + "@@"): CMAdb.log.warning( "Drone %s wants to register with key_id %s -- permitted.", sysname, keyid) if not cryptcurve25519_save_public_key(keyid, pubkey, keysize): raise ValueError( "Drone %s public key (key_id %s, %d bytes) is invalid." % (sysname, keyid, keysize)) elif drone.key_id != keyid: raise ValueError( "Drone %s tried to register with key_id %s instead of %s." % (sysname, keyid, drone.key_id)) drone.set_crypto_identity(keyid=keyid) pyCryptFrame.dest_set_key_id(origaddr, keyid) # # THIS IS HERE BECAUSE OF A PROTOCOL BUG... # @FIXME Protocol bug when starting up a connection if our first (this) packet gets lost, # then the protocol doesn't retransmit it. # More specifically, it seems to clear it out of the queue. # This might be CMA bug or a protocol bug. It's not clear... # The packet goes into the queue, but if that packet is lost in transmission, then when # we come back around here, it's not in the queue any more, even though it # definitely wasn't ACKed. # Once this is fixed, this "add_packet" call needs to go *after* the 'if' statement below. # CMAdb.transaction.add_packet(origaddr, FrameSetTypes.SETCONFIG, (str(self.config), ), FrameTypes.CONFIGJSON) if (localtime is not None): if (drone.lastjoin == localtime): CMAdb.log.warning('Drone %s [%s] sent duplicate STARTUP' % (sysname, origaddr)) if CMAdb.debug: self.io.log_conn(origaddr) return drone.lastjoin = localtime #print >> sys.stderr, 'DRONE from find: ', drone, type(drone), drone.port drone.startaddr = str(origaddr) if json is not None: drone.logjson(origaddr, json) if CMAdb.debug: CMAdb.log.debug('Joining TheOneRing: %s / %s / %s' % (drone, type(drone), drone.port)) CMAdb.cdb.TheOneRing.join(drone) if CMAdb.debug: CMAdb.log.debug('Requesting Discovery from %s' % str(drone)) discovery_params = [] for agent in self.config['initial_discovery']: params = ConfigFile.agent_params(self.config, 'discovery', agent, sysname) params['agent'] = agent params['instance'] = '_init_%s' % agent discovery_params.append(params) # Discover the permissions of all the lists of files we're configured to ask about # Note that there are several lists to keep the amount of data in any one list # down to a somewhat more reasonable level. 'fileattrs' output is really verbose for pathlist_name in self.config['perm_discovery_lists']: paths = self.config[pathlist_name] params = ConfigFile.agent_params(self.config, 'discovery', 'fileattrs', sysname) params['agent'] = 'fileattrs' params['instance'] = pathlist_name params['parameters'] = {'ASSIM_filelist': paths} discovery_params.append(params) if CMAdb.debug: CMAdb.log.debug('Discovery details: %s' % str(discovery_params)) for item in discovery_params: CMAdb.log.debug('Discovery item details: %s' % str(item)) drone.request_discovery(discovery_params) AssimEvent(drone, AssimEvent.OBJUP)
def dispatch(self, origaddr, frameset): json = None addrstr = repr(origaddr) fstype = frameset.get_framesettype() localtime = None listenaddr = None keyid = None pubkey = None keysize = None #print >> sys.stderr, ("DispatchSTARTUP: received [%s] FrameSet from [%s]" #% (FrameSetTypes.get(fstype)[0], addrstr)) if CMAdb.debug: CMAdb.log.debug("DispatchSTARTUP: received [%s] FrameSet from [%s]" % (FrameSetTypes.get(fstype)[0], addrstr)) if not self.io.connactive(origaddr): self.io.closeconn(DEFAULT_FSP_QID, origaddr) CMAdb.transaction.post_transaction_packets.append(FrameSetTypes.ACKSTARTUP) for frame in frameset.iter(): frametype = frame.frametype() if frametype == FrameTypes.WALLCLOCK: localtime = str(frame.getint()) elif frametype == FrameTypes.IPPORT: listenaddr = frame.getnetaddr() elif frametype == FrameTypes.HOSTNAME: sysname = frame.getstr() if sysname == CMAdb.nodename: if origaddr.islocal(): CMAdb.log.info("Received STARTUP from local system (%s)" % addrstr) else: addresses = ['127.0.0.1', '::ffff:127.0.0.1', '::1' ] for address in addresses: localhost = pyNetAddr(address) self.io.addalias(localhost, origaddr) CMAdb.log.info("Aliasing %s to %s" % (localhost, origaddr)) elif frametype == FrameTypes.JSDISCOVER: json = frame.getstr() #print >> sys.stderr, 'GOT JSDISCOVER JSON: [%s] (strlen:%s,framelen:%s)' \ #% (json, len(json), frame.framelen()) elif frametype == FrameTypes.KEYID: keyid = frame.getstr() elif frametype == FrameTypes.PUBKEYCURVE25519: pubkey = frame.framevalue() keysize = frame.framelen() joininfo = pyConfigContext(init=json) origaddr, isNAT = self.validate_source_ip(sysname, origaddr, joininfo, listenaddr) CMAdb.log.info('Drone %s registered from address %s (%s) port %s, key_id %s' % (sysname, origaddr, addrstr, origaddr.port(), keyid)) drone = self.droneinfo.add(sysname, 'STARTUP packet', port=origaddr.port() , primary_ip_addr=str(origaddr)) drone.listenaddr = str(listenaddr) # Seems good to hang onto this... drone.isNAT = isNAT # ditto... # Did they give us the crypto info we need? if keyid is None or pubkey is None: if CMAdb.debug: CMAdb.log.debug('Drone %s registered with keyid %s and pubkey provided: %s' % (self, keyid, pubkey is not None)) else: if drone.key_id == '': if not keyid.startswith(sysname + "@@"): CMAdb.log.warning("Drone %s wants to register with key_id %s -- permitted." , sysname, keyid) if not cryptcurve25519_save_public_key(keyid, pubkey, keysize): raise ValueError("Drone %s public key (key_id %s, %d bytes) is invalid." % (sysname, keyid, keysize)) elif drone.key_id != keyid: raise ValueError("Drone %s tried to register with key_id %s instead of %s." % (sysname, keyid, drone.key_id)) drone.set_crypto_identity(keyid=keyid) pyCryptFrame.dest_set_key_id(origaddr, keyid) # # THIS IS HERE BECAUSE OF A PROTOCOL BUG... # @FIXME Protocol bug when starting up a connection if our first (this) packet gets lost, # then the protocol doesn't retransmit it. # More specifically, it seems to clear it out of the queue. # This might be CMA bug or a protocol bug. It's not clear... # The packet goes into the queue, but if that packet is lost in transmission, then when # we come back around here, it's not in the queue any more, even though it # definitely wasn't ACKed. # Once this is fixed, this "add_packet" call needs to go *after* the 'if' statement below. # CMAdb.transaction.add_packet(origaddr, FrameSetTypes.SETCONFIG, (str(self.config), ) , FrameTypes.CONFIGJSON) if (localtime is not None): if (drone.lastjoin == localtime): CMAdb.log.warning('Drone %s [%s] sent duplicate STARTUP' % (sysname, origaddr)) if CMAdb.debug: self.io.log_conn(origaddr) return drone.lastjoin = localtime #print >> sys.stderr, 'DRONE from find: ', drone, type(drone), drone.port drone.startaddr = str(origaddr) if json is not None: drone.logjson(origaddr, json) if CMAdb.debug: CMAdb.log.debug('Joining TheOneRing: %s / %s / %s' % (drone, type(drone), drone.port)) CMAdb.cdb.TheOneRing.join(drone) if CMAdb.debug: CMAdb.log.debug('Requesting Discovery from %s' % str(drone)) discovery_params = [] for agent in self.config['initial_discovery']: params = ConfigFile.agent_params(self.config, 'discovery', agent, sysname) params['agent'] = agent params['instance'] = '_init_%s' % agent discovery_params.append(params) # Discover the permissions of all the lists of files we're configured to ask about # Note that there are several lists to keep the amount of data in any one list # down to a somewhat more reasonable level. 'fileattrs' output is really verbose for pathlist_name in self.config['perm_discovery_lists']: paths = self.config[pathlist_name] params = ConfigFile.agent_params(self.config, 'discovery', 'fileattrs', sysname) params['agent'] = 'fileattrs' params['instance'] = pathlist_name params['parameters'] = {'ASSIM_filelist': paths} discovery_params.append(params) if CMAdb.debug: CMAdb.log.debug('Discovery details: %s' % str(discovery_params)) drone.request_discovery(discovery_params) AssimEvent(drone, AssimEvent.OBJUP)