def _init_capabilities(self): self.__cap_list = [] if global_config["BACKUP_DATABASE"]: self.log("doing database backup, ignoring capabilities", logging_tools.LOG_LEVEL_WARN) else: # read caps _dir = os.path.dirname(__file__) self.log("init server capabilities from directory {}".format(_dir)) SRV_CAPS = [] for entry in os.listdir(_dir): if entry.endswith(".py") and entry not in ["__init__.py"]: _imp_name = "initat.cluster_server.capabilities.{}".format( entry.split(".")[0]) _mod = importlib.import_module(_imp_name) for _key in dir(_mod): _value = getattr(_mod, _key) if inspect.isclass(_value) and issubclass( _value, base.BackgroundBase ) and _value != base.BackgroundBase: SRV_CAPS.append(_value) self.log("checking {}".format( logging_tools.get_plural("capability", len(SRV_CAPS)))) self.__server_cap_dict = {} self.__cap_list = [] try: sys_cc = config_catalog.objects.get(Q(system_catalog=True)) except config_catalog.DoesNotExist: sys_cc = factories.ConfigCatalog(name="local", system_catalog=True) for _srv_cap in SRV_CAPS: cap_name = _srv_cap.Meta.name try: cap_descr = _srv_cap.Meta.description except: self.log( "capability {} has no description set, ignoring...". format(cap_name), logging_tools.LOG_LEVEL_ERROR) else: _new_c = factories.Config( name=cap_name, description=cap_descr, config_catalog=sys_cc, server_config=True, # system_config=True, ) _sql_info = config_tools.server_check(server_type=cap_name) if _sql_info.effective_device: self.__cap_list.append(cap_name) self.__server_cap_dict[cap_name] = _srv_cap( self, _sql_info) self.log("capability {} is enabled on {}".format( cap_name, unicode(_sql_info.effective_device), )) else: self.log("capability {} is disabled".format(cap_name))
def create_noctua_fixtures(): print("Creating Noctua fixtures...") # first config catalog first_cc = config_catalog.objects.all()[0] # category tree ct = category_tree() cat_serv = ct.add_category("/mon/services") cat_web = ct.add_category("/mon/services/web") cat_mail = ct.add_category("/mon/services/mail") # config print("Creeating configurations.") ping_config = factories.Config( name="check_ping", config_catalog=first_cc, ) snmp_config = factories.Config( name="check_snmp_info", config_catalog=first_cc, ) ssh_config = factories.Config( name="check_ssh", config_catalog=first_cc, ) http_config = factories.Config( name="check_http", config_catalog=first_cc, ) https_config = factories.Config( name="check_https", config_catalog=first_cc, ) ldap_config = factories.Config( name="check_ldap", config_catalog=first_cc, ) imap_config = factories.Config( name="check_imap", config_catalog=first_cc, ) imaps_config = factories.Config( name="check_imaps", config_catalog=first_cc, ) pop3s_config = factories.Config( name="check_pop3s", config_catalog=first_cc, ) smtps_config = factories.Config( name="check_smtps", config_catalog=first_cc, ) print("Creating monitoring checks.") factories.MonCheckCommand( name="snmp_info", command_line="$USER3$ -m $HOSTADDRESS$ -C $ARG1$ -V $ARG2$ snmp_info", config=snmp_config).categories.add(cat_serv) factories.MonCheckCommand( name="check_ping", command_line="$USER2$ -m localhost ping $HOSTADDRESS$ 5 5.0", config=ping_config).categories.add(cat_serv) factories.MonCheckCommand(name="check_ssh", command_line="$USER1$/check_ssh $HOSTADDRESS$", config=ssh_config).categories.add(cat_serv) factories.MonCheckCommand( name="check_http", command_line="$USER1$/check_http -H $HOSTADDRESS$", config=http_config).categories.add(cat_web) factories.MonCheckCommand( name="check_imaps", command_line="$USER1$/check_imap -H $HOSTADDRESS$ -p 993 -S", config=imaps_config).categories.add(cat_mail) factories.MonCheckCommand( name="check_ldap", command_line="$USER1$/check_ldap -H $HOSTADDRESS$ -b dc=init,dc=at -3", config=ldap_config).categories.add(cat_serv) factories.MonCheckCommand( name="check_https", command_line="$USER1$/check_http -S -H $HOSTADDRESS$ -C 30", config=https_config).categories.add(cat_web) factories.MonCheckCommand( name="check_imap", command_line="$USER1$/check_imap -H $HOSTADDRESS$ -p 143", config=imap_config).categories.add(cat_mail) factories.MonCheckCommand( name="check_pop3s", command_line="$USER1$/check_pop3 -H $HOSTADDRESS$ -p 995 -S", config=pop3s_config).categories.add(cat_mail) factories.MonCheckCommand( name="check_smtps", command_line="$USER1$/check_smtps -H $HOSTADDRESS$ -p 465 -S", config=smtps_config).categories.add(cat_mail) # domain name tree dnt = domain_name_tree() _top_level_dtn = dnt.get_domain_tree_node("") # device_group print("Creating device and device group.") first_devg = factories.DeviceGroup(name="server_group") first_dev = factories.Device( name=process_tools.get_machine_name(), device_group=first_devg, domain_tree_node=_top_level_dtn, ) print("Creating device configurations.") factories.DeviceConfig( device=first_dev, config=factories.Config(name="monitor_server", config_catalog=first_cc), ) factories.DeviceConfig( device=first_dev, config=factories.Config(name="rrd_server", config_catalog=first_cc), ) factories.DeviceConfig( device=first_dev, config=factories.Config(name="server", config_catalog=first_cc), ) factories.DeviceConfig( device=first_dev, config=factories.Config(name="rrd_collector", config_catalog=first_cc), ) factories.DeviceConfig( device=first_dev, config=factories.Config(name="discovery_server", config_catalog=first_cc), ) factories.DeviceConfig( device=first_dev, config=ssh_config, ) print("Creating monitoring periods.") initial_mon_period = factories.MonPeriod(name="always", sun_range="00:00-24:00", mon_range="00:00-24:00", tue_range="00:00-24:00", wed_range="00:00-24:00", thu_range="00:00-24:00", fri_range="00:00-24:00", sat_range="00:00-24:00") first_st = factories.MonServiceTempl( name="dummy_service_template", nsc_period=initial_mon_period, nsn_period=initial_mon_period, ) _first_dt = factories.MonDeviceTempl( name="dummy_device_template", mon_service_templ=first_st, mon_period=initial_mon_period, not_period=initial_mon_period, host_check_command=host_check_command.objects.get( Q(name="check-host-alive")), ) is_ucs = os.path.isfile("/usr/sbin/ucr") # the create_cluster script adds an admin user # if there are no users, or in case of an ucs system, if only this one new admin exists, # then we want an admin and a user user users = user.objects.all() empty_install = users.count() == 0 new_install = (users.count() == 1 and users[0].login == 'admin' and users[0].login_count == 0) if empty_install or (is_ucs and new_install): print('Creating user and groups.') user.objects.all().delete() group.objects.all().delete() # group / users _group = factories.Group( groupname="group", homestart="/", gid=100, ) _group.allowed_device_groups.add(first_devg) _user = factories.User( login="******", uid=400, group=_group, password="******", ) _user.allowed_device_groups.add(first_devg) _first_mc = factories.MonContact( user=_user, snperiod=initial_mon_period, hnperiod=initial_mon_period, ) _admin = user.objects.create_superuser( "admin", "*****@*****.**", "admin", ) # we need contacts for all initial users so that they can access icinga factories.MonContact( user=_admin, snperiod=initial_mon_period, hnperiod=initial_mon_period, ) _admin.allowed_device_groups.add(first_devg) # network if is_ucs: if_address = get_local_ip_address("62.99.204.238") # print if_address if_name = get_interface_by_ip(if_address) # print if_name p = subprocess.Popen( ['ucr', 'get', 'interfaces/%s/address' % (if_name)], stdout=subprocess.PIPE) if_address = p.stdout.read().strip().split("\n")[0] p = subprocess.Popen( ['ucr', 'get', 'interfaces/%s/network' % (if_name)], stdout=subprocess.PIPE) if_network = p.stdout.read().strip().split("\n")[0] p = subprocess.Popen( ['ucr', 'get', 'interfaces/%s/broadcast' % (if_name)], stdout=subprocess.PIPE) if_broadcast = p.stdout.read().strip().split("\n")[0] p = subprocess.Popen( ['ucr', 'get', 'interfaces/%s/netmask' % (if_name)], stdout=subprocess.PIPE) if_netmask = p.stdout.read().strip().split("\n")[0] p = subprocess.Popen(['ucr', 'get', 'gateway'], stdout=subprocess.PIPE) out = p.stdout.read().strip().split("\n")[0] if_gateway = out else: print( "Not installed on UCS, /usr/sbin/ucr not found. Using python-netifaces." ) if_address = get_local_ip_address("62.99.204.238") if_name = get_interface_by_ip(if_address) if_netmask = get_netmask_by_interface(if_name) if_broadcast = get_broadcast_by_interface(if_name) if_network = str( ipvx_tools.ipv4(if_netmask) & ipvx_tools.ipv4(if_broadcast)) if_gateway = get_default_gateway_linux() print('Creating network objects.') _network = factories.Network( identifier="lan", network_type=network_type.objects.get(Q(identifier="o")), name="lan", network=if_network, broadcast=if_broadcast, netmask=if_netmask, gateway=if_gateway, ) _netdevice = factories.NetDevice( device=first_dev, devname=if_name, routing=True, netdevice_speed=netdevice_speed.objects.get( Q(speed_bps=1000000000) & Q(full_duplex=True) & Q(check_via_ethtool=True)), network_device_type=network_device_type.objects.get( Q(identifier="eth")), ) _net_ip = factories.NetIp( ip=if_address, network=_network, netdevice=_netdevice, domain_tree_node=_top_level_dtn, )
def _service_enum_show_command(options): from initat.cluster.backbone.server_enums import icswServiceEnum from initat.cluster.backbone.models import ConfigServiceEnum, config from initat.cluster.backbone import factories from django.core.exceptions import ValidationError _c_dict = { entry.enum_name: entry for entry in ConfigServiceEnum.objects.all() } print("") print("ServiceEnums defined: {:d}".format(len(icswServiceEnum))) _list = logging_tools.NewFormList() for entry in icswServiceEnum: if entry.name not in _c_dict: if options.sync and (entry.value.server_service or entry.value.relayer_service ) and entry.value.sync_config: new_entry = ConfigServiceEnum.create_db_entry(entry) _c_dict[new_entry.enum_name] = new_entry else: _db_str = "no" if entry.name in _c_dict: if options.sync: _c_dict[entry.name].update_values(entry) _db_str = "yes ({:d})".format(_c_dict[entry.name].pk) if entry.value.server_service: _egg_action = ", ".join( [str(_action) for _action in entry.value.egg_actions]) or "none" else: _egg_action = "---" _list.append([ logging_tools.form_entry(entry.name, header="EnumName"), logging_tools.form_entry(entry.value.name, header="Name"), logging_tools.form_entry_center( "yes" if entry.value.root_service else "no", header="Root Service"), logging_tools.form_entry_center( "yes" if entry.value.server_service else "no", header="Server"), logging_tools.form_entry_center( "yes" if entry.value.relayer_service else "no", header="Relayer"), logging_tools.form_entry(entry.value.info, header="Info"), logging_tools.form_entry_center(_db_str, header="DB info"), logging_tools.form_entry(_egg_action, header="Egg actions"), ]) print(str(_list)) if options.sync: _change_list = [] # compat dict comp_dict = { "rrd_grapher": icswServiceEnum.grapher_server.name, "rrd_server": icswServiceEnum.collectd_server.name, "rrd_collector": icswServiceEnum.collectd_server.name, "server": icswServiceEnum.cluster_server.name, "ldap_server": icswServiceEnum.ldap_server.name, } for c_con in config.objects.all(): if not c_con.config_service_enum_id: _check_names = [c_con.name] if c_con.name in comp_dict: _check_names.append(comp_dict[c_con.name]) for _check_name in _check_names: if _check_name in _c_dict: c_con.config_service_enum = _c_dict[_check_name] try: c_con.save(update_fields=["config_service_enum"]) except ValidationError: print("cannot save {}: {}".format( str(c_con), process_tools.get_except_info())) else: _change_list.append(c_con) break _create_list = [] for db_enum in _c_dict.values(): if not db_enum.config_set.all().count(): _create_list.append( factories.Config( name=db_enum.name, description=db_enum.info, config_service_enum=db_enum, server_config=True, )) if len(_change_list): print("") print("{} moved to ConfigServiceEnum:".format( logging_tools.get_plural("Config", len(_change_list)))) for entry in _change_list: print(" {} ({})".format(entry.name, str(entry.config_service_enum))) if len(_create_list): print("") print("{} created:".format( logging_tools.get_plural("Config", len(_create_list)))) for entry in _create_list: print(" {} ({})".format(entry.name, str(entry.config_service_enum)))