def handle(self, *args, **options): one_day = timedelta(hours=24) today_midnight = datetime.now().replace( hour=0, minute=0, second=0, microsecond=0) + one_day tomorrow_midnight = today_midnight + one_day exp_services = common_models.Service.objects.filter( end_time__gte=today_midnight, end_time__lt=tomorrow_midnight, status='?').order_by('client__manager') if not exp_services: return None for s in exp_services: subject = 'Тест %s для %s (%s) заканчивается %s' % ( s.name, s.client.clientname, s.client.city.name, s.end_time.strftime('%d.%m в %H:%M')) text = 'Тест услуги %s %s [%s] ' % (s.name.upper(), s.servicetype.upper(), s) text += 'завершится %s\n' % s.end_time.strftime('%d.%m.%Y в %H:%M') emails = s.client.manager.profile.email + ', %s' % ADMIN_EMAIL try: email_admin(emails, subject, text) except MailError: pass
def handle(self, *args, **options): print('Bind BundleVlans to services') changes = [] services = Service.objects.filter(name__in=self.service_names, status__in=['on', 'test']) for service in services: process_service(service=service, changes=changes) for subservice in service.subservices.all(): process_service(subservice=subservice, changes=changes) if not changes: print('No changes') return report_text = '' for change in changes: row = '%s %s %s %s %s\n' % (change.sign, change.changed_entity, change.client, change.bundle_vlan, 'added' if change.sign == '+' else 'removed') report_text += row print('Sending an email to admins') email_admin(ADMIN_EMAIL, 'Port bindings', report_text) print(report_text)
def handle(self, *args, **options): try: collector = Collector() if options['netname'] == 'all': collector.import_bundles_for_devices() collector.import_vlans_for_devices() else: try: device = Device.objects.get(netname=options['netname'], is_managed=True) except Device.DoesNotExist: message = '%s not found in Hostel or "is_managed" isn\'t checked' % options[ 'netname'] self.stdout.write(self.style.ERROR(message)) return else: collector.import_bundles_for_device(device) collector.import_vlans_for_device(device) for error in collector.errors: self.stdout.write(self.style.ERROR(error)) for action in collector.actions: self.stdout.write(action) message = '' if collector.errors: message += 'Errors:\r\n' for error in collector.errors: message += error message += '\r\n' if collector.actions: message += '\r\nUpdates:\r\n' for action in collector.actions: message += action message += '\r\n' if message: email_admin(ADMIN_EMAIL, 'Collector', message) except Exception as e: email_admin(ADMIN_EMAIL, 'Collector Exception', str(e))
def handle(self, *args, **options): try: from hostel.settings import ZABBIX_DATA except ImportError: print('Cannot import ZABBIX_DATA from settings file') quit(1) z = ZabbixAPI(ZABBIX_DATA['url'], user=ZABBIX_DATA['login'], password=ZABBIX_DATA['password']) devices = Device.objects.filter(is_managed=True, type__in=['router', 'switch'], status='+').order_by('netname') report_text = '' for device in devices: data = z.item.get(host=device.netname, search={'name': 'Firmware'}, output=['lastvalue']) if not data: continue firmware = data[0].get('lastvalue') if not firmware: continue if device.version == firmware: continue message_string = '%s: Firmware changed from "%s" to "%s"' % ( device.netname, device.version, firmware) device.version = firmware device.save() report_text += message_string + '\n' print(message_string) if report_text: try: email_admin(ADMIN_EMAIL, 'Firmware upgrades', report_text) except MailError: pass
def handle(self, *args, **options): print("Started") self.unbind_old_remotes() self.fund_new_remotes() message = '' if self.errors: message += 'Errors\r\n' + '\r\n'.join(self.errors) if self.actions: message += 'Updates\r\n' + '\r\n'.join(self.actions) if message: print(message) try: email_admin(ADMIN_EMAIL, 'Remote devices', message) except MailError as e: print('Unable to send email:') print(e)