def __init__(self): try: logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filename=cluster_config.PATH_TO_LOG_FILE) self.db = Db_Connector() except: e = sys.exc_info()[1] print 'NagiosPassiveServer error :', e
def __init__(self): try: logging.basicConfig(level=logging.DEBUG, format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filename=cluster_config.PATH_TO_LOG_FILE) self.db = Db_Connector() except: e = sys.exc_info()[1] print 'NagiosPassiveServer error :', e
class NagiosPassiveServer: def __init__(self): try: logging.basicConfig(level=logging.DEBUG, format = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filename=cluster_config.PATH_TO_LOG_FILE) self.db = Db_Connector() except: e = sys.exc_info()[1] print 'NagiosPassiveServer error :', e def register_passive_check(self, hostname, service, status, number): print './passive_check.sh %s %s %s %s'%(hostname,service,status,number) status, output = commands.getstatusoutput('./send_passive_check_nagios.sh %s %s %s %s'%(hostname,service,status,number) ) print 'status: ',status print 'output: ',output if status == 0: print 'ok' else: print'error' def process_notification(self,data, ipAddress): print 'data : ' , data instance_id = self.db.get_id_by_ip(ipAddress) vm = self.db.get_vm_info(instance_id['instance_id']) hostname = "%s_%s_%s"%(vm['user'],vm['instance_id'],vm['node_hostname']) memPercentage = int( 100*(data['memory']['used'])/float(data['memory']['total'])) self.register_passive_check(hostname, 'RAM', 0, '"'+str(memPercentage) + ';'+str((data['memory']['used']/1024))+'/'+str((data['memory']['total']/1024))+'"') #CPU if data['loadavg']['1'] >= 3.0: output = "CRITICAL - load average: %.2f, %.2f, %.2f" % (data['loadavg']['1'],data['loadavg']['5'],data['loadavg']['10']) elif data['loadavg']['1'] >= 1.5: output = "WARNING - load average: %.2f, %.2f, %.2f" % (data['loadavg']['1'],data['loadavg']['5'],data['loadavg']['10']) else: print 'data: ', data['loadavg']['1'] output = "OK - load average: %.2f, %.2f, %.2f" % (data['loadavg']['1'],data['loadavg']['5'],data['loadavg']['10']) self.register_passive_check(hostname, 'Cpu_Load', 0, '"'+output+'"') #HTTP Connections self.register_passive_check(hostname, 'HTTP_Connections', 0, '"'+data['http']['nconexoes']+'"') return True
def __init__(self): logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filename=cluster_config.PATH_TO_LOG_FILE) self.db = Db_Connector() server = SimpleXMLRPCServer( (cluster_config.LISTEN_IP, cluster_config.LISTEN_PORT), logRequests=True, requestHandler=RequestHandler) server.register_function(self.get_vm_notification) try: server.serve_forever() except: e = sys.exc_info()[1] logging.error( "server execution error in server_passive_checks: %s" % e)
class Generate_VMs_Nagios_Conf: hostnames = [] nodes_group = [] def __init__(self): self.db = Db_Connector() self.create_nagios_conf_running_vms(); self.reload_nagios_if_necessary() def reload_nagios_if_necessary(self): # getting date for logging purposes now = datetime.datetime.now() print now.strftime("%d-%m-%Y %H:%M:%S") if os.path.isfile(running_vms.cluster.cluster_config.VMS_NAGIOS_FILE): md5a = commands.getoutput("md5sum "+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF+" | awk '{ print $1 }'") md5b = commands.getoutput("md5sum %s | awk ' { print $1 }'"%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) if md5a != md5b: print 'Moving File' os.system('mv '+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s'%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) self.reload_nagios() else: print 'no changes' else: os.system('mv '+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s'%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) self.reload_nagios() def reload_nagios(self): os.system(running_vms.cluster.cluster_config.COMMAND_TO_RELOAD_NAGIOS) def create_nagios_conf_running_vms(self): vms = self.db.get_info_for_monitoring_running_Vms() if len(vms) > 0: try: fh = open(running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF,'w') try: tree = ET.parse('basic_monitoring.xml') except TypeError, e: print 'error :',e for vm in vms: #Check if the machine has a valide ip address, if not , it's not possible to monitorate if vm['public_dns_name'] != '0.0.0.0': hostname = "%s_%s_%s"%(vm['user'],vm['instance_id'],vm['node_hostname']) self.hostnames.append(hostname) alias = "%s/%s(%s-%s)"%(vm['user'],vm['instance_id'],vm['node_hostname'],vm['node_ip']) if vm['node_ip'] != '': vm_host = [vm['node_ip'],hostname] self.nodes_group.append(vm_host) host_def = self.define_host(hostname, alias, vm['public_dns_name']) fh.write(host_def) service = tree.findall('services/service') for s in service: child = s.getchildren() services = self.define_passive_service(hostname, child) fh.write(services) hostgroup = self.define_hostgroup() fh.write(hostgroup) fh.close() except IOError, (errno, strerror): print "I/O error ({0}): {1}".format(errno,strerror)
def __init__(self): self.db = Db_Connector() self.create_nagios_conf_running_vms(); self.reload_nagios_if_necessary()
class NagiosPassiveServer: def __init__(self): try: logging.basicConfig( level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filename=cluster_config.PATH_TO_LOG_FILE) self.db = Db_Connector() except: e = sys.exc_info()[1] print 'NagiosPassiveServer error :', e def register_passive_check(self, hostname, service, status, number): print './passive_check.sh %s %s %s %s' % (hostname, service, status, number) status, output = commands.getstatusoutput( './send_passive_check_nagios.sh %s %s %s %s' % (hostname, service, status, number)) print 'status: ', status print 'output: ', output if status == 0: print 'ok' else: print 'error' def process_notification(self, data, ipAddress): print 'data : ', data instance_id = self.db.get_id_by_ip(ipAddress) vm = self.db.get_vm_info(instance_id['instance_id']) hostname = "%s_%s_%s" % (vm['user'], vm['instance_id'], vm['node_hostname']) memPercentage = int(100 * (data['memory']['used']) / float(data['memory']['total'])) self.register_passive_check( hostname, 'RAM', 0, '"' + str(memPercentage) + ';' + str( (data['memory']['used'] / 1024)) + '/' + str( (data['memory']['total'] / 1024)) + '"') #CPU if data['loadavg']['1'] >= 3.0: output = "CRITICAL - load average: %.2f, %.2f, %.2f" % ( data['loadavg']['1'], data['loadavg']['5'], data['loadavg']['10']) elif data['loadavg']['1'] >= 1.5: output = "WARNING - load average: %.2f, %.2f, %.2f" % ( data['loadavg']['1'], data['loadavg']['5'], data['loadavg']['10']) else: print 'data: ', data['loadavg']['1'] output = "OK - load average: %.2f, %.2f, %.2f" % ( data['loadavg']['1'], data['loadavg']['5'], data['loadavg']['10']) self.register_passive_check(hostname, 'Cpu_Load', 0, '"' + output + '"') #HTTP Connections self.register_passive_check(hostname, 'HTTP_Connections', 0, '"' + data['http']['nconexoes'] + '"') return True
def __init__(self): self.db = Db_Connector() now = datetime.datetime.now() self.create_nagios_conf_running_vms() self.reload_nagios_if_necessary()
class Generate_VMs_Nagios_Conf: def __init__(self): self.db = Db_Connector() now = datetime.datetime.now() self.create_nagios_conf_running_vms() self.reload_nagios_if_necessary() def reload_nagios_if_necessary(self): # getting date for logging purposes now = datetime.datetime.now() print now.strftime("%d-%m-%Y %H:%M:%S") if os.path.isfile(running_vms.cluster.cluster_config.VMS_NAGIOS_FILE): md5a = commands.getoutput( "md5sum " + running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + " | awk '{ print $1 }'") md5b = commands.getoutput( "md5sum %s | awk ' { print $1 }'" % running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) if md5a != md5b: print 'Moving File' os.system('mv ' + running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s' % running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) self.reload_nagios() else: print 'no changes' else: os.system('mv ' + running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s' % running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) self.reload_nagios() def reload_nagios(self): os.system(running_vms.cluster.cluster_config.COMMAND_TO_RELOAD_NAGIOS) def create_nagios_conf_running_vms(self): vms = self.db.get_info_for_monitoring_running_Vms() vms_str = '' try: fh = open(running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF, 'w') nodes_groups = [] hostnames = [] for vm in vms: #Check if the machine has a valide ip address, if not , it's not possible to monitorate if vm['public_dns_name'] != '0.0.0.0': hostname = "%s_%s_%s" % (vm['user'], vm['instance_id'], vm['node_hostname']) hostnames.append(hostname) alias = "%s/%s(%s-%s)" % (vm['user'], vm['instance_id'], vm['node_hostname'], vm['node_ip']) #Verify if there's a node where the vm runs associate, if yes add dependence if vm['node_ip'] != '': conf = "define host {\n \ use linux-server \n \ address %s\n \ host_name %s\n \ alias %s\n \ }\n" % (vm['public_dns_name'], hostname, alias) else: conf = "define host {\n \ use linux-server \n \ address %s\n \ host_name %s\n \ alias %s\n \ }\n" % (vm['public_dns_name'], hostname, alias) service_conf = self.define_service( hostname, 'PING', 'check_ping!100.0,20%!500.0,60%') service_conf_passive = self.define_service_passive( hostname) fh.write(conf) fh.write(service_conf) fh.write(service_conf_passive) vms_str = vms_str + "%s, " % hostname vm_group = [hostname, vm['node_ip']] nodes_groups.append(vm_group) service_group = self.define_service_group(vms_str) hostgroup = self.define_hostgroup(vms_str, nodes_groups) self.remove_old_conf() # fh.write(service_group) fh.write(hostgroup) fh.close() except IOError as (errno, strerror): print "I/O error ({0}): {1}".format(errno, strerror)
class Generate_VMs_Nagios_Conf: hostnames = [] nodes_group = [] def __init__(self): self.db = Db_Connector() self.create_nagios_conf_running_vms() self.reload_nagios_if_necessary() def reload_nagios_if_necessary(self): # getting date for logging purposes now = datetime.datetime.now() print now.strftime("%d-%m-%Y %H:%M:%S") if os.path.isfile(running_vms.cluster.cluster_config.VMS_NAGIOS_FILE): md5a = commands.getoutput( "md5sum " + running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + " | awk '{ print $1 }'") md5b = commands.getoutput( "md5sum %s | awk ' { print $1 }'" % running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) if md5a != md5b: print 'Moving File' os.system('mv ' + running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s' % running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) self.reload_nagios() else: print 'no changes' else: os.system('mv ' + running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s' % running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) self.reload_nagios() def reload_nagios(self): os.system(running_vms.cluster.cluster_config.COMMAND_TO_RELOAD_NAGIOS) def create_nagios_conf_running_vms(self): vms = self.db.get_info_for_monitoring_running_Vms() if len(vms) > 0: try: fh = open(running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF, 'w') try: tree = ET.parse('basic_monitoring.xml') except TypeError, e: print 'error :', e for vm in vms: #Check if the machine has a valide ip address, if not , it's not possible to monitorate if vm['public_dns_name'] != '0.0.0.0': hostname = "%s_%s_%s" % (vm['user'], vm['instance_id'], vm['node_hostname']) self.hostnames.append(hostname) alias = "%s/%s(%s-%s)" % ( vm['user'], vm['instance_id'], vm['node_hostname'], vm['node_ip']) if vm['node_ip'] != '': vm_host = [vm['node_ip'], hostname] self.nodes_group.append(vm_host) host_def = self.define_host(hostname, alias, vm['public_dns_name']) fh.write(host_def) service = tree.findall('services/service') for s in service: child = s.getchildren() services = self.define_passive_service( hostname, child) fh.write(services) hostgroup = self.define_hostgroup() fh.write(hostgroup) fh.close() except IOError, (errno, strerror): print "I/O error ({0}): {1}".format(errno, strerror)
class Generate_VMs_Nagios_Conf: def __init__(self): self.db = Db_Connector() now = datetime.datetime.now() self.create_nagios_conf_running_vms(); self.reload_nagios_if_necessary() def reload_nagios_if_necessary(self): # getting date for logging purposes now = datetime.datetime.now() print now.strftime("%d-%m-%Y %H:%M:%S") if os.path.isfile(running_vms.cluster.cluster_config.VMS_NAGIOS_FILE): md5a = commands.getoutput("md5sum "+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF+" | awk '{ print $1 }'") md5b = commands.getoutput("md5sum %s | awk ' { print $1 }'"%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) if md5a != md5b: print 'Moving File' os.system('mv '+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s'%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) self.reload_nagios() else: print 'no changes' else: os.system('mv '+running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF + ' %s'%running_vms.cluster.cluster_config.VMS_NAGIOS_FILE) self.reload_nagios() def reload_nagios(self): os.system(running_vms.cluster.cluster_config.COMMAND_TO_RELOAD_NAGIOS) def create_nagios_conf_running_vms(self): vms = self.db.get_info_for_monitoring_running_Vms() vms_str = '' try: fh = open(running_vms.cluster.cluster_config.NAGIOS_TEMP_CONF,'w') nodes_groups = [] hostnames = [] for vm in vms: #Check if the machine has a valide ip address, if not , it's not possible to monitorate if vm['public_dns_name'] != '0.0.0.0': hostname = "%s_%s_%s"%(vm['user'],vm['instance_id'],vm['node_hostname']) hostnames.append(hostname) alias = "%s/%s(%s-%s)"%(vm['user'],vm['instance_id'],vm['node_hostname'],vm['node_ip']) #Verify if there's a node where the vm runs associate, if yes add dependence if vm['node_ip'] != '': conf = "define host {\n \ use linux-server \n \ address %s\n \ host_name %s\n \ alias %s\n \ }\n" % (vm['public_dns_name'],hostname,alias) else: conf = "define host {\n \ use linux-server \n \ address %s\n \ host_name %s\n \ alias %s\n \ }\n" % (vm['public_dns_name'],hostname,alias) service_conf = self.define_service(hostname,'PING','check_ping!100.0,20%!500.0,60%') service_conf_passive = self.define_service_passive(hostname) fh.write(conf) fh.write(service_conf) fh.write(service_conf_passive) vms_str = vms_str + "%s, "%hostname vm_group = [ hostname,vm['node_ip'] ] nodes_groups.append(vm_group) service_group = self.define_service_group(vms_str) hostgroup = self.define_hostgroup(vms_str,nodes_groups) self.remove_old_conf() # fh.write(service_group) fh.write(hostgroup) fh.close() except IOError as (errno, strerror): print "I/O error ({0}): {1}".format(errno,strerror)