def _find_service(service_id): # Consider using cypher... service = None try: service = nc.get_unique_node_by_name(nc.graphdb.manager, service_id, 'Service') except Exception: pass return service
def set_netapp_storage_usage(storage_collection): """ :param storage_collection: list :return: None """ for service in storage_collection: service_node = nc.get_unique_node_by_name(nc.graphdb.manager, service['service_id'], 'Service') property_dict = {'netapp_storage_sum': service['total_storage']} helpers.dict_update_node(utils.get_user(), service_node.handle_id, property_dict, property_dict.keys()) service['total_storage'] = 0.0
def get_port(self, bundle, device_name, device_type, port_name): node_type = helpers.slug_to_node_type(slugify(device_type), create=True) parent_node = nc.get_unique_node_by_name(nc.graphdb.manager, device_name, node_type.get_label()) if not parent_node: raise_not_acceptable_error("End point {0} {1} not found.".format( device_type, device_name)) result = parent_node.get_port(port_name).get('Has', []) if len(result) > 1: raise_not_acceptable_error( 'Multiple port objects returned for a unique port name.') if result: port_node = result[0]['node'] else: port_node = helpers.create_port(parent_node, port_name, bundle.request.user) return port_node
def handle(self, *args, **options): user = User.objects.get(username='******') utcnow = datetime.utcnow() last_month = utcnow - relativedelta(months=1) services = getattr(django_settings, 'NETAPP_REPORT_SETTINGS', []) for service in services: service_node = nc.get_unique_node_by_name(nc.graphdb.manager, service['service_id'], 'Service') monthly_dict = json.loads( service_node.data.get('netapp_storage_monthly', '{}')) monthly_dict.setdefault(str(last_month.year), {})[str(last_month.month)] = \ service_node.data.get('netapp_storage_sum', 0.0) property_dict = { 'netapp_storage_monthly': json.dumps(monthly_dict) } helpers.dict_update_node(user, service_node.handle_id, property_dict, property_dict.keys()) self.stdout.write( 'Monthly netapp usage stored for service {}.'.format( service['service_id']))
def insert(json_list): """ Inserts the data loaded from the json files created by the nerds producer cfengine_reports. "cfengine_report": [ { "last_verified_(gmt_+00:00)": "06-10-2013 16:55", "promisehandle": "system_administration_methods_syslog_conf", "promisestatus": "kept" }, { "last_verified_(gmt_+00:00)": "06-10-2013 16:55", "promisehandle": "system_administration_methods_scsi_timeout", "promisestatus": "notkept" }, ] """ user = utils.get_user() for i in json_list: node = None name = i['host']['name'].lower() i['host']['name'] = name promises = i['host']['cfengine_report'] try: node = nc.get_unique_node_by_name(nc.graphdb.manager, name, 'Host') except MultipleNodesReturned as e: logger.error(e) if node: host_properties = {} for promise in promises: if promise['promisehandle'] in CFENGINE_MAP.keys(): promise_name = promise['promisehandle'] promise_status = promise['promisestatus'] host_properties.update( CFENGINE_MAP[promise_name][promise_status]) helpers.dict_update_node(user, node.handle_id, host_properties, host_properties.keys()) logger.info('{name} done.'.format(name=name))
def insert(json_list): # Setup persistent storage for collections done over multiple hosts netapp_collection = getattr(django_settings, 'NETAPP_REPORT_SETTINGS', []) # Parse collected Nagios data for item in json_list: base = item['host'].get('checkmk_livestatus') if not base: base = item['host']['nagiosxi_api'] base['host_name'] = base['host_name'].lower() host = nc.get_unique_node_by_name(nc.graphdb.manager, base['host_name'], 'Host') if not host: host = get_host(base['host_address']) if host: check_descriptions = [] for check in base['checks']: check_descriptions.append( check.get('description', 'Missing description')) if check[ 'check_command'] == 'CHECK_NRPE!check_uptime': # Host uptime set_uptime(host, check) if check[ 'check_command'] == 'CHECK_NRPE!check_backup': # TSM backup process set_backup(host, check) if check['check_command'].startswith( 'check_netapp_vol'): # NetApp storage usage netapp_collection = collect_netapp_storage_usage( host, check, netapp_collection) if check[ 'check_command'] == 'CHECK_NRPE!check_openmanage': # Dell OpenManage info set_dell_service_tag(host, check) if check['check_command'] == 'check_esxi': # Dell esxi HW info set_dell_service_tag(host, check) set_nagios_checks(host, check_descriptions) helpers.update_noclook_auto_manage(host) logger.info('{name} done.'.format(name=host.data['name'])) # Set data collected from multiple hosts set_netapp_storage_usage(netapp_collection)
def handle(self, *args, **options): to = getattr(django_settings, 'REPORTS_TO', []) cc = getattr(django_settings, 'REPORTS_CC', None) bcc = getattr(django_settings, 'REPORTS_BCC', None) extra_report = getattr(django_settings, 'EXTRA_REPORT_TO', {}) free_gb = Decimal('1000.00') price_per_gb = Decimal('0.40') quarter_month_map = { 1: ['01', '02', '03'], 2: ['04', '05', '06'], 3: ['07', '08', '09'], 4: ['10', '11', '12'] } utcnow = datetime.utcnow() last_month = utcnow - relativedelta(months=1) year = str(last_month.year) last_quarter = (last_month.month - 1) // 3 + 1 services = getattr(django_settings, 'NETAPP_REPORT_SETTINGS', []) for service in services: report_data = [] service_node = nc.get_unique_node_by_name(nc.graphdb.manager, service['service_id'], 'Service') customers = [item['node'].data.get('name', '') for item in service_node.get_customers().get('customers', [])] customers = ', '.join(customers) data_dict = json.loads(service_node.data.get('netapp_storage_monthly', '{}')).get(year, None) if data_dict: for month in quarter_month_map[last_quarter]: key = '%s-%s' % (year, month) report_data.append({key: data_dict.get(month.lstrip('0'), 0.0)}) # Create and send the mail subject = 'Storage cost for %s Q%d %s' % (service['contract_reference'], last_quarter, year) heading = 'Adobe connect storage volume billing Q%d, %s, for %s.' % (last_quarter, year, customers) body = ''' This is an auto generated report from NOCLook for service ID %s. %s This report was generated on %s UTC. ''' % (service['service_id'], heading, utcnow.strftime('%Y-%m-%d %H:%M')) extended_to = to + extra_report.get(service['service_id'], []) # Avoid changing REPORTS_TO :) filename = 'Storage cost for %s Q%d %s.xls' % (service['contract_reference'], last_quarter, year) wb = helpers.dicts_to_xls({}, [], '%s Q%d %s' % (service['service_id'], last_quarter, year)) # Calculate and write pricing info ws = wb.get_sheet(0) ws.write(1, 1, heading) ws.write(2, 1, 'Reference: %s' % service['contract_reference']) ws.write(4, 1, 'Month') ws.write(4, 2, 'Storage (GB)') ws.write(4, 3, 'Service inc.') ws.write(4, 4, 'Billable storage') ws.write(4, 5, u'Price per GB (€)') ws.write(4, 6, u'Monthly cost (€)') total_cost = Decimal(0) for i in range(0, len(report_data)): row = 5 + i storage_month = Decimal(report_data[i].values()[0]) billable = Decimal(max(storage_month - free_gb, 0)) cost_month = billable * price_per_gb total_cost += cost_month ws.write(row, 1, list(report_data[i].keys())[0]) ws.write(row, 2, storage_month.quantize(Decimal('.01'), rounding=ROUND_DOWN)) ws.write(row, 3, -free_gb.quantize(Decimal('.01'), rounding=ROUND_DOWN)) ws.write(row, 4, billable.quantize(Decimal('.01'), rounding=ROUND_DOWN)) ws.write(row, 5, price_per_gb.quantize(Decimal('.01'), rounding=ROUND_DOWN)) ws.write(row, 6, cost_month.quantize(Decimal('.01'), rounding=ROUND_DOWN)) ws.write(9, 1, 'Quarterly storage cost') ws.write(9, 6, total_cost.quantize(Decimal('.01'), rounding=ROUND_DOWN)) with tempfile.TemporaryFile() as temp: wb.save(temp) temp.seek(0) msg = helpers.create_email(subject, body, extended_to, cc, bcc, temp.read(), filename, 'application/excel') msg.send() self.stdout.write('Sent netapp usage report for service {}.'.format(service['service_id']))