📄 editdb.py
字号:
# -*- coding: ISO8859-1 -*-# $Id: editdb.py 3976 2007-04-24 08:39:16Z mortenv $## Copyright 2003, 2004 Norwegian University of Science and Technology## This file is part of Network Administration Visualized (NAV)## NAV is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## NAV is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with NAV; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA### Authors: Hans J鴕gen Hoel <hansjorg@orakel.ntnu.no>#################################################### Importsimport editTables,nav.Snmp,sys,re,copy,initBox,forgetSQL,nav.webfrom nav.db import manageimport nav.utilfrom mod_python import util,apachefrom editdbSQL import *from socket import gethostbyaddr,gethostbyname,gaierrorfrom nav.web.serviceHelper import getCheckers,getDescriptionfrom nav.web.selectTree import selectTree,selectTreeLayoutBoxfrom nav.web.selectTree import simpleSelect,updateSelect# Temporary fix:mod = __import__('encodings.utf_8',globals(),locals(),'*')mod = __import__('encodings.utf_16_be',globals(),locals(),'*')mod = __import__('encodings.latin_1',globals(),locals(),'*')mod = __import__('encodings.utf_16',globals(),locals(),'*')################################################### Templatesfrom nav.web.templates.editdbTemplate import editdbTemplate################################################### ConstantsBASEPATH = '/editdb/'CONFIGFILE = 'editdb.conf'EDITPATH = [('Home','/'),('Tools','/toolbox'),('Edit database',BASEPATH)]ADDNEW_ENTRY = 'addnew_entry'UPDATE_ENTRY = 'update_entry'IGNORE_BOX = 'ignore_this_box'# Bulk import imagesBULK_IMG_GREEN = '/images/lys/green.png'BULK_IMG_YELLOW = '/images/lys/yellow.png'BULK_IMG_RED = '/images/lys/red.png'# Bulk import statusBULK_STATUS_OK = 1BULK_STATUS_YELLOW_ERROR = 2BULK_STATUS_RED_ERROR = 3# Bulk fieldname for unspecified fieldsBULK_UNSPECIFIED_FIELDNAME = 'excess'# REQ_TRUE: a required field# REQ_FALSE: not required# REQ_NONEMPTY: not required, but don't insert empty field into dbREQ_TRUE = 1REQ_FALSE = 2REQ_NONEMPTY = 3# FieldtypesFIELD_STRING = 1FIELD_INTEGER = 2################################################### Functionsdef handler(req): ''' mod_python handler ''' path = req.uri match = re.search('editdb/(.+)$',path) if match: request = match.group(1) request = request.split('/') else: request = "" # Read configuration file #if not globals().has_key('CONFIG_CACHED'): readConfig() # Get form from request object keep_blank_values = True fieldStorage = util.FieldStorage(req,keep_blank_values) form = {} for field in fieldStorage.list: if form.has_key(field.name): # This input name already exists if type(form[field.name]) is list: # and it's already a list, so just append the # the new value form[field.name].append(str(field.value)) else: # it's a string, so make a list and append the # new value to it valueList = [] valueList.append(form[field.name]) valueList.append(str(field.value)) form[field.name] = valueList else: form[field.name] = str(field.value) # Check that all input is in the default encoding (utf8) unicodeError = False try: for key,value in form.items(): if type(value) is list: for field in value: unicode_str = field.decode(DEFAULT_ENCODING) else: unicode_str = value.decode(DEFAULT_ENCODING) except UnicodeError: # Some of the input values is not in the default encoding unicodeError = True # Set form in request object req.form = form output = None showHelp = False if len(request) == 2: if request[0] == 'help': showHelp = True request = [] if not len(request) > 1: output = index(req,showHelp) else: table = request[0] action = request[1] if table == 'bulk': output = bulkImport(req,action) elif pageList.has_key(table): output = editPage(req,pageList[table](),request,unicodeError) if output: req.content_type = "text/html" req.write(output) return apache.OK else: return apache.HTTP_NOT_FOUNDdef readConfig(): ''' Reads configuration from editdb.conf and sets global variables. ''' global CONFIG_CACHED,DEFAULT_ENCODING,BULK_TRY_ENCODINGS,\ CATEGORY_LIST,SPLIT_LIST,SPLIT_OPPOSITE config = nav.config.readConfig(CONFIGFILE) CONFIG_CACHED = True DEFAULT_ENCODING = config['default_encoding'] BULK_TRY_ENCODINGS = eval(config['bulk_try_encodings']) # Make list of cable categories catlist = eval(config['categories']) categories = [] for cat in catlist: categories.append(eval(config[cat])) CATEGORY_LIST = categories # Make list of splits and dict of opposite splits splitlist = eval(config['splits']) splits = [] opposite = {} for splitName in splitlist: split = eval(config[splitName]) # id=0, descr=1, opposite=2 splits.append((split[0],split[1])) if split[2]: # References another split, set id opposite[split[0]] = eval(config[split[2]])[0] else: opposite[split[0]] = None SPLIT_LIST = splits SPLIT_OPPOSITE = oppositedef index(req,showHelp=False,status=None): ''' Generates the index page (main menu) ''' # Empty body class body: ''' Empty struct for template ''' def __init__(self): pass body.status = status body.title = 'Edit Database - Modify seed information for the NAV database' body.infotext = 'Here you can add, delete or edit seed information ' +\ 'that are needed for the NAV database. Keep in mind ' +\ 'that most of the data in the NAV database are ' +\ 'collected automatically by NAV background processes.' body.showHelp = showHelp body.help = [BASEPATH + 'help/','Show help'] body.nohelp = [BASEPATH,'Hide help'] body.tables = [] headings = [] # Table for boxes and services rows = [['IP devices', 'Input seed information on the IP devices you want to ' +\ 'monitor', [BASEPATH + 'netbox/edit','Add'], [BASEPATH + 'netbox/list','Edit'], [BASEPATH + 'bulk/netbox','Bulk import']], ['Services', 'Which services on which servers do you want to monitor?', [BASEPATH + 'service/edit','Add'], [BASEPATH + 'service/list','Edit'], [BASEPATH + 'bulk/service','Bulk import']]] body.tables.append(Table('IP devices and services','',headings,rows)) # Table for rooms and locations rows = [['Room', 'Register all wiring closets and server rooms that contain ' +\ 'IP devices which NAV monitors', [BASEPATH + 'room/edit','Add'], [BASEPATH + 'room/list','Edit'], [BASEPATH + 'bulk/room','Bulk import']], ['Location', 'Rooms are organised in locations', [BASEPATH + 'location/edit','Add'], [BASEPATH + 'location/list','Edit'], [BASEPATH + 'bulk/location','Bulk import']]] body.tables.append(Table('Rooms and locations','',headings,rows)) # Table org and usage cat rows = [['Organisation', 'Register all organisational units that are relevant. I.e. ' +\ 'all units that have their own subnet/server facilities.', [BASEPATH + 'org/edit','Add'], [BASEPATH + 'org/list','Edit'], [BASEPATH + 'bulk/org','Bulk import']], ['Usage categories', 'NAV encourages a structure in the subnet structure. ' +\ 'Typically a subnet has users from an organisational ' +\ 'unit. In addition this may be subdivided into a ' +\ 'category of users, i.e. students, employees, ' +\ 'administration etc.', [BASEPATH + 'usage/edit','Add'], [BASEPATH + 'usage/list','Edit'], [BASEPATH + 'bulk/usage','Bulk import']]] body.tables.append(Table('Organisation and usage categories','', headings,rows)) # Table for types, products and vendors rows = [['Type', 'The type describes the type of network device, uniquely ' +\ 'described from the SNMP sysobjectID', [BASEPATH + 'type/edit','Add'], [BASEPATH + 'type/list','Edit'], [BASEPATH + 'bulk/type','Bulk import']], ['Product', 'Similar to type, but with focus on the product number and ' +\ 'description. A product may be a type, it may also be a ' +\ 'component (i.e. module) within an equipment type', [BASEPATH + 'product/edit','Add'], [BASEPATH + 'product/list','Edit'], [BASEPATH + 'bulk/product','Bulk import']], ['Vendor', 'Register the vendors that manufacture equipment that are ' +\ 'represented in your network.', [BASEPATH + 'vendor/edit','Add'], [BASEPATH + 'vendor/list','Edit'], [BASEPATH + 'bulk/vendor','Bulk import']], ['Snmpoid', 'Manually add snmpoids (candidates for the cricket collector)', [BASEPATH + 'snmpoid/edit','Add'], ['',''], ['','']], ['Subcategory', 'The main categories of a device are predefined by NAV (i.e. ' +\ 'GW,SW,SRV). You may however create subcategories yourself.', [BASEPATH + 'subcat/edit','Add'], [BASEPATH + 'subcat/list','Edit'], [BASEPATH + 'bulk/subcat','Bulk import']],] body.tables.append(Table('Types, products and vendors','',headings,rows)) # Table for vlans and special subnets rows = [['Vlan', 'Register the vlan number that are in use (this info may ' +\ 'also be derived automatically from the routers)', None, [BASEPATH + 'vlan/list','Edit'], None], ['Prefix', 'Register special ip prefixes. Typically reserved prefixes ' +\ 'or prefixes that are not directly connected to monitored ' +\ 'routers/firewalls fall into this category', [BASEPATH + 'prefix/edit','Add'], [BASEPATH + 'prefix/list','Edit'], [BASEPATH + 'bulk/prefix','Bulk import']]] body.tables.append(Table('Vlans and special subnets','',headings,rows)) # Table for cabling and patch rows = [['Cabling', 'Here you may document the horizontal cabling system ', [BASEPATH + 'cabling/edit','Add'], [BASEPATH + 'cabling/list','Edit'], [BASEPATH + 'bulk/cabling','Bulk import']], ['Patch', 'Register the cross connects in the wiring closets ', [BASEPATH + 'patch/edit','Add'], [BASEPATH + 'patch/list','Edit'], [BASEPATH + 'bulk/patch','Bulk import']]] body.tables.append(Table('Cabling system','',headings,rows)) # Table for containers (Network load map) rows = [['Container', 'A container is a collection of routers for the traffic map ', [BASEPATH + 'container/edit','Add'], [BASEPATH + 'container/list','Edit'], None], ['Routers In Container', 'Add or remove routers from containers ', [BASEPATH + 'ric/edit','Add'], [BASEPATH + 'ric/list','Edit'], None]] body.tables.append(Table('Traffic Map','',headings,rows))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -