#!/usr/bin/env python # -*- coding: utf-8 -*- __author__ = "Alejandro Sánchez Carrion" __copyright__ = "Copyright 2021, PandoraFMS" __maintainer__ = "Projects department" __status__ = "Production" __version__ = "200721" import json, requests, argparse, sys from datetime import datetime ##Help info = f""" Pandora FMS Openstack monitoring plugin Version = {__version__} Description = Connects with digital ocean cloud service an retreave agent data for the instances running. Execution: { str(sys.argv[0]) } -u -t [ -g ] [ --data_dir ] """ ### Variables and arg parser ### parser = argparse.ArgumentParser(description= info, formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('-u', '--url', help='openstack api keystone url', required=True) parser.add_argument('-t', '--token', help='openstack api token', required=True) parser.add_argument('-g', '--group', help='PandoraFMS destination group', default='openstack') parser.add_argument('--data_dir', help='PandoraFMS data dir (default: /var/spool/pandora/data_in/)', default='/var/spool/pandora/data_in/') # parser.add_argument('--prefix', help='Agent prefix, by default openstack_', default='openstack_') args = parser.parse_args() ### Pandora Tools ### modules = [] config = { "data_in": args.data_dir, "group" : args.group } def print_agent(agent, modules, prt=1): """Prints agent XML. Requires Agent object as argument.""" header = "\n" header += "\n" "\t\n" "\t" + str(data["type"]) + "\n" ) if type(data["type"]) is not str and "string" not in data["type"]: #### Limpia espacios si el módulo no es tipo string data["value"] = data["value"].strip() if isinstance(data["value"], list): # Checks if value is a list module_xml += "\t\n" for value in data["value"]: if type(value) is dict and "value" in value: module_xml += "\t\n" module_xml += "\t\t\n" if "timestamp" in value: module_xml += "\t\t\n" module_xml += "\t\n" else: module_xml += "\t\n" if "desc" in data: module_xml += "\t\n" if "unit" in data: module_xml += "\t\n" if "interval" in data: module_xml += "\t\n" if "tags" in data: module_xml += "\t" + str(data["tags"]) + "\n" if "module_group" in data: module_xml += "\t" + str(data["module_group"]) + "\n" if "module_parent" in data: module_xml += "\t" + str(data["module_parent"]) + "\n" if "min_warning" in data: module_xml += "\t\n" if "max_warning" in data: module_xml += "\t\n" if "min_critical" in data: module_xml += "\t\n" if "max_critical" in data: module_xml += "\t\n" if "str_warning" in data: module_xml += "\t\n" if "str_critical" in data: module_xml += "\t\n" if "critical_inverse" in data: module_xml += "\t\n" if "warning_inverse" in data: module_xml += "\t\n" if "max" in data: module_xml += "\t\n" if "min" in data: module_xml += "\t\n" if "post_process" in data: module_xml += "\t\n" if "disabled" in data: module_xml += "\t\n" if "min_ff_event" in data: module_xml += "\t\n" if "status" in data: module_xml += "\t\n" if "timestamp" in data: module_xml += "\t\n" if "custom_id" in data: module_xml += "\t\n" if "critical_instructions" in data: module_xml += "\t\n" if "warning_instructions" in data: module_xml += "\t\n" if "unknown_instructions" in data: module_xml += "\t\n" if "quiet" in data: module_xml += "\t\n" if "module_ff_interval" in data: module_xml += "\t\n" if "crontab" in data: module_xml += "\t\n" if "min_ff_event_normal" in data: module_xml += "\t\n" if "min_ff_event_warning" in data: module_xml += "\t\n" if "min_ff_event_critical" in data: module_xml += "\t\n" if "ff_type" in data: module_xml += "\t\n" if "ff_timeout" in data: module_xml += "\t\n" if "each_ff" in data: module_xml += "\t\n" if "module_parent_unlink" in data: module_xml += "\t\n" if "global_alerts" in data: for alert in data["alert"]: module_xml += "\t\n" module_xml += "\n" if not not_print_flag: print (module_xml) return (module_xml) def write_xml(xml, agent_name): Utime = datetime.now().strftime('%s') data_file = "%s/%s.%s.data" %(str(config["data_in"]),agent_name,str(Utime)) #print (data_file) try : with open(data_file, 'x') as data: data.write(xml) data.close() except OSError as e : pass return 0 # # default agent def clean_agent() : global agent agent = { "agent_name" : "", "agent_alias" : "", "parent_agent_name" : "", "description" : "", "version" : "", "os_name" : "", "os_version" : "", "timestamp" : datetime.today().strftime('%Y/%m/%d %H:%M:%S'), #"utimestamp" : int(datetime.timestamp(datetime.today())), "address" : "127.0.0.1", "group" : config["group"], "interval" : "", } return agent # default module def clean_module() : global modulo modulo = { "name" : "", "type" : "generic_data_string", "desc" : "", "value" : "", } return modulo ### MAIN #url and token user_url = args.url user_token = args.token headers = { 'Content-Type': 'application/json', 'X-Auth-Token': user_token } # request url req = { "server": user_url + "/servers", "hypervisors": user_url + "/os-hypervisors/detail" } try: result = requests.get(req["hypervisors"], headers=headers) result_data = json.loads(result.content) except Exception as e : print('0') sys.exit("\nError requesting %s, please check conectivity" %(req["hypervisors"],)) for dato in result_data["hypervisors"] : clean_agent() agent.update( agent_name = "Openstack Hypervisor_%s" %(dato['id']), agent_alias = "Openstack Hypervisor_%s" %(dato['id']), description = "Openstack Hypervisor_detailed stats" ) clean_module() modulo.update( name = "free_disk_gb", desc = "avalaible disk space", value = str(dato['free_disk_gb']) ) modules.append(modulo) clean_module() modulo.update( name = "free_ram_mb", desc = "available ram ", value = str(dato['free_ram_mb']) ) modules.append(modulo) clean_module() modulo.update( name = "disk_available_least", desc = "this value is dependent on over committed value of disk, disk_available_least = disk_free_gb - disk_over_committed" , value = str(dato['disk_available_least']) ) modules.append(modulo) clean_module() modulo.update( name = "local_gb", desc = "the total available disk for the node's virtual machine, local_gb = local_gb_used + free_disk_gb " , value = str(dato['local_gb']) ) modules.append(modulo) clean_module() modulo.update( name = "local_gb_used", desc = "the sum of the node's virtual machine disk ", value = str(dato['local_gb_used']) ) modules.append(modulo) clean_module() modulo.update( name = "memory_mb", desc = "the total ram of the node, memory_mb_used + free_ram_mb " , value = str(dato['memory_mb']) ) modules.append(modulo) clean_module() modulo.update( name = "memory_mb_used", desc = "the sum of the rams of the node's virtual machine " , value = str(dato['memory_mb_used']) ) modules.append(modulo) clean_module() modulo.update( name = "vcpus", desc = "node ​​physical cpu total threads ", value = str(dato['vcpus']) ) modules.append(modulo) clean_module() modulo.update( name = "vcpus_used", desc = "the sum of the vcpus of the node virtual machine ", value = str(dato['vcpus_used']) ) modules.append(modulo) clean_module() modulo.update( name = "current_workload", desc = "hypervisor current workload " , value = str(dato['current_workload']) ) modules.append(modulo) clean_module() modulo.update( name = "host_ip", desc = "hypervisor host ip ", value = str(dato['host_ip']) ) modules.append(modulo) clean_module() modulo.update( name = "hypervisor_type", desc = "hypervisor type ", value = str(dato['hypervisor_type']) ) modules.append(modulo) clean_module() modulo.update( name = "hypervisor_version", desc = "hypervisor version ", value = str(dato['hypervisor_version']) ) modules.append(modulo) clean_module() modulo.update( name = "running_vms", desc = "number of virtual machines running " , value = str(dato['running_vms']) ) modules.append(modulo) print_agent(agent, modules) modules = [] try: result = requests.get(req["server"], headers=headers) result_data = json.loads(result.content) except Exception as e : print('0') sys.exit("\nError requesting %s, please check conectivity" %(req["server"],)) id_instancia=[] for dato in result_data["servers"] : id_instancia.append(dato['id']) for dato in id_instancia: req = { "servers": user_url + "/servers/" + dato + "/diagnostics", "instancia": user_url + "/servers/detail" } # get account data try: result = requests.get(req["servers"], headers=headers) result_data = json.loads(result.content) except Exception as e : print('0') sys.exit("\nError requesting %s, please check conectivity" %(req["servers"],)) if 'errors' in result_data : print('0') sys.exit(F'Error token, please check token: {result_data}') clean_agent() agent.update( agent_name = "Openstack instance id: " + dato , agent_alias ="Openstack instance id: " + dato, description ="Instance metrics" ) clean_module() modulo.update( name = "memory", type = "generic_data", desc = "memory used by the machine", value = float(result_data['memory']), unit = "" ) modules.append(modulo) clean_module() modulo.update( name = "memory-actual", type = "generic_data", desc = "memory actual used by the machine", value = float(result_data['memory-actual']), unit = "" ) modules.append(modulo) clean_module() modulo.update( name = "memory-rss", type = "generic_data", desc = "Resident Set Size and is used to show how much memory is allocated", value = float(result_data['memory-rss']), unit = "" ) modules.append(modulo) clean_module() modulo.update( name = "vda_errors", type = "generic_data", desc = "diagnostics for a libvirt based instance, vda receibed errors", value = float(result_data['vda_errors']), unit = "" ) modules.append(modulo) clean_module() modulo.update( name = "vda_read", type = "generic_data", desc = "diagnostics for a libvirt based instance, vda dates read", value = float(result_data['vda_read']), unit = "" ) modules.append(modulo) clean_module() modulo.update( name = "vda_read_req", type = "generic_data", desc = "diagnostics for a libvirt based instance, vda dates requests read", value = float(result_data['vda_read_req']), unit = "" ) modules.append(modulo) clean_module() modulo.update( name = "vda_write", type = "generic_data", desc = "diagnostics for a libvirt based instance, vda dates write", value = float(result_data['vda_write']), unit = "" ) modules.append(modulo) clean_module() modulo.update( name = "vda_write_req", type = "generic_data", desc = "diagnostics for a libvirt based instance, vda write requests dates", value = float(result_data['vda_write_req']), unit = "" ) modules.append(modulo) print_agent(agent, modules) modules = [] print('1')