Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- ##----------------------------------------------------------------------
- ## Add new objects to NOC managed object.
- ## add_new_objects.py - добавляет объект если его нет с файла /tmp/list.txt (формата: Имя^tIP^tОписание\n)
- ##----------------------------------------------------------------------
- ## Copyright (C) 2007-2012 The NOC Project
- ## See LICENSE for details
- ##----------------------------------------------------------------------
- # Python modules
- import re
- # NOC modules
- import set_staticvlan_env
- import set_env
- set_env.setup(use_django=True)
- from noc.lib.app.site import site
- site.autodiscover()
- from noc.sa.models import ManagedObject, ManagedObjectSelector
- from noc.staticvlan.models import connect
- c = connect()
- conn = c[0]
- cur = c[1]
- # Network config:
- execfile('/opt/noc/etc/noc-staticvlan.conf')
- rx_line = re.compile(
- r"^(?P<name>\S+)\t(?P<IP>\S+)\t(?P<location>.+)$", re.MULTILINE)
- list_f = open('/tmp/list.txt', 'r')
- file_list = list_f.read()
- list_f.close()
- for match in rx_line.finditer(file_list):
- name = match.group("name")
- cur.execute("SELECT id FROM sa_managedobject WHERE name = '" + name + "'")
- ID = cur.fetchall()
- if not ID:
- IP = match.group("IP")
- descr = match.group("location")
- profile = 'DLink.DxS'
- user = 'tech'
- password = '!TechService911'
- cur.execute('INSERT INTO sa_managedobject ( name, is_managed, administrative_domain_id, activator_id, profile_name, scheme, address, "user", password, super_password, remote_path, trap_source_ip, trap_community, is_configuration_managed, repo_path, snmp_ro, snmp_rw, description, tags ) VALUES ( ' + "'" + name + "', false, 1, 1, '" + profile + "', 0, '" + IP + "', '" + user + "', '" + password + "', '', '', '" + IP + "', 'public', false, '" + IP + "', 'public', '', '" + descr + "', '' )" )
- cur.execute("SELECT id FROM sa_managedobject WHERE name = '" + name + "'")
- ID = cur.fetchall()
- ID = str(ID[0][0])
- cur.execute("INSERT INTO cm_config ( repo_path, push_every, pull_every, managed_object_id ) VALUES ( '" + IP + "', 86400, 86400, " + ID + " )")
- print (name, IP)
- conn.commit()
- cur.close()
- conn.close()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment