Guest User

add_new_objects.py

a guest
Jun 29th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.27 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##----------------------------------------------------------------------
  3. ## Add new objects to NOC managed object.
  4. ## add_new_objects.py - добавляет объект если его нет с файла /tmp/list.txt (формата: Имя^tIP^tОписание\n)
  5. ##----------------------------------------------------------------------
  6. ## Copyright (C) 2007-2012 The NOC Project
  7. ## See LICENSE for details
  8. ##----------------------------------------------------------------------
  9.  
  10. # Python modules
  11. import re
  12. # NOC modules
  13. import set_staticvlan_env
  14. import set_env
  15. set_env.setup(use_django=True)
  16. from noc.lib.app.site import site
  17. site.autodiscover()
  18. from noc.sa.models import ManagedObject, ManagedObjectSelector
  19.  
  20. from noc.staticvlan.models import connect
  21. c = connect()
  22. conn = c[0]
  23. cur = c[1]
  24.  
  25. # Network config:
  26. execfile('/opt/noc/etc/noc-staticvlan.conf')
  27.  
  28. rx_line = re.compile(
  29.     r"^(?P<name>\S+)\t(?P<IP>\S+)\t(?P<location>.+)$", re.MULTILINE)
  30.  
  31. list_f = open('/tmp/list.txt', 'r')
  32. file_list = list_f.read()
  33. list_f.close()
  34.  
  35. for match in rx_line.finditer(file_list):
  36.     name = match.group("name")
  37.     cur.execute("SELECT id FROM sa_managedobject WHERE name = '" + name + "'")
  38.     ID = cur.fetchall()
  39.     if not ID:
  40.         IP = match.group("IP")
  41.         descr = match.group("location")
  42.         profile = 'DLink.DxS'
  43.         user = 'tech'
  44.         password = '!TechService911'
  45.         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 + "', '' )" )
  46.  
  47.         cur.execute("SELECT id FROM sa_managedobject WHERE name = '" + name + "'")
  48.         ID = cur.fetchall()
  49.         ID = str(ID[0][0])
  50.         cur.execute("INSERT INTO cm_config ( repo_path, push_every, pull_every, managed_object_id ) VALUES ( '" + IP + "', 86400, 86400, " + ID + " )")
  51.         print (name, IP)
  52.  
  53. conn.commit()
  54. cur.close()
  55. conn.close()
  56.  
  57. quit()
Advertisement
Add Comment
Please, Sign In to add comment