improvements.
[monitor.git] / monitor / database / zabbixapi / model.py
index 45fbd75..74407f9 100644 (file)
@@ -4,7 +4,7 @@ pkg_resources.require("Elixir>=0.4.0")
 # import the basic Elixir classes and functions for declaring the data model
 # (see http://elixir.ematia.de/trac/wiki/TutorialDivingIn)
 from elixir import EntityMeta, Entity, Field, OneToMany, ManyToOne, ManyToMany
-from elixir import options_defaults, using_options, setup_all, metadata, entities
+from elixir import options_defaults, using_options, setup_all, entities
 # import some datatypes for table columns from Elixir
 # (see http://www.sqlalchemy.org/docs/04/types.html for more)
 from elixir import String, Unicode, Integer, DateTime
@@ -24,11 +24,10 @@ from sqlalchemy import Sequence
 
 import defines
 
+from monitor.database.dborm import zab_metadata, zab_session
 
-#from elixir import metadata
-#from monitor.database.dborm import zabbix_db, zabbix_session
-#__metadata__ = zabbix_db
-#__session__  = zabbix_session
+__metadata__ = zab_metadata
+__session__  = zab_session
 
 # TODO:
 #   - declare association between Media and MediaType so that look ups can
@@ -236,6 +235,7 @@ class ZabbixEntity(ZabbixSerialize):
                fieldname = self._descriptor.auto_primarykey
                index = IDs.get_by(table_name=tablename, field_name=fieldname)
                if not index:
+                       print "NEW IDs index INSIDE INIT"
                        index = IDs(table_name=tablename, field_name=fieldname, nodeid=0, nextid=10)
                        index.flush()
                index.nextid = index.nextid + 1
@@ -423,6 +423,42 @@ class IDs(Entity):
                autoload=True,
        )
 
+class Escalation(ZabbixEntity):
+       using_options(
+               tablename='escalations',
+               autoload=True,
+               auto_primarykey='escalationid'
+       )
+
+class Event(ZabbixEntity):
+       using_options(
+               tablename='events',
+               autoload=True,
+               auto_primarykey='eventid'
+       )
+
+class Item(ZabbixEntity):
+       using_options(
+               tablename='items',
+               autoload=True,
+               auto_primarykey='itemid'
+       )
+
+class Acknowledge(ZabbixEntity):
+       using_options(
+               tablename='acknowledges',
+               autoload=True,
+               auto_primarykey='acknowledgeid'
+       )
+
+class Trigger(ZabbixEntity):
+       using_options(
+               tablename='triggers',
+               autoload=True,
+               auto_primarykey='triggerid'
+       )
+       
+
 class Right(ZabbixEntity):
        # rights of a usergroup to interact with hosts of a hostgroup
        using_options(
@@ -438,8 +474,9 @@ class Right(ZabbixEntity):
        # currently since the rights table is merely treated as an intermediate
        # table for the m2m between usrgrp and groups.
 
-rights = Table('rights', metadata, autoload=True)
-hostsgroups = Table('hosts_groups', metadata, autoload=True)
+rights = Table('rights', __metadata__, autoload=True)
+hostsgroups = Table('hosts_groups', __metadata__, autoload=True)
+hoststemplates = Table('hosts_templates', __metadata__, autoload=True)
 
        
 # m2m table between hosts and groups below
@@ -450,6 +487,13 @@ class HostsGroups(ZabbixEntity):
                auto_primarykey='hostgroupid',
        )
 
+class HostsTemplates(ZabbixEntity):
+       using_options(
+               tablename='hosts_templates',
+               autoload=True,
+               auto_primarykey='hosttemplateid',
+       )
+
 class Host(ZabbixEntity):
        using_options(
                tablename='hosts',
@@ -463,8 +507,29 @@ class Host(ZabbixEntity):
                primaryjoin=lambda: Host.hostid==hostsgroups.c.hostid,
                secondaryjoin=lambda: HostGroup.groupid==hostsgroups.c.groupid,
        )
+       template_list = ManyToMany(
+               'Host',
+               table=hoststemplates,
+               foreign_keys=lambda: [hoststemplates.c.hostid, hoststemplates.c.templateid],
+               primaryjoin=lambda: Host.hostid==hoststemplates.c.hostid,
+               secondaryjoin=lambda: Host.hostid==hoststemplates.c.templateid,
+       )
+
+       def append_template(self, template):
+               row = HostsTemplates(hostid=self.hostid, templateid=template.hostid)
+               return template
+
+       def remove_template(self, template):
+               row = HostsTemplates.get_by(hostid=self.hostid, templateid=template.hostid)
+               if row is not None:
+                       row.delete()
+
        def delete(self):
                # NOTE: media objects are automatically handled.
+               hosts_templates_match = HostsTemplates.query.filter_by(hostid=self.hostid).all()
+               for row in hosts_templates_match:
+                       row.delete()
+
                hosts_groups_match = HostsGroups.query.filter_by(hostid=self.hostid).all()
                for row in hosts_groups_match:
                        row.delete()
@@ -636,7 +701,7 @@ class Media(ZabbixEntity):
                                        foreign_keys=lambda: [Media.userid],
                                        ondelete='cascade') 
 
-users_groups = Table('users_groups', metadata, autoload=True)
+users_groups = Table('users_groups', __metadata__, autoload=True)
 
 class User(ZabbixEntity): # parent of media
        using_options(
@@ -672,7 +737,6 @@ class User(ZabbixEntity): # parent of media
                ug_row = UsersGroups.get_by(usrgrpid=group.usrgrpid, userid=self.userid)
                if ug_row is not None:
                        ug_row.delete()
-                       #ug_row.flush()
                return
                
 class UsrGrp(ZabbixEntity):
@@ -726,10 +790,42 @@ class UsrGrp(ZabbixEntity):
                ug_row = UsersGroups.get_by(userid=user.userid, usrgrpid=self.usrgrpid)
                if ug_row is not None:
                        ug_row.delete()
-                       #ug_row.flush()
                return
 
+def confirm_ids():
+       fields = {
+               'scripts' : 'scriptid',
+               'usrgrp' : 'usrgrpid',
+               'users' : 'userid',
+               'media' : 'mediaid',
+               'users_groups' : 'id',
+               'groups' : 'groupid',
+               'rights' : 'rightid',
+               'drules' : 'druleid',
+               'dchecks' : 'dcheckid',
+               'actions' : 'actionid',
+               'conditions' : 'conditionid',
+               'operations' : 'operationid',
+               'opconditions' : 'opconditionid',
+       }
+       need_to_flush = False
+
+       for tablename in fields.keys():
+               fieldname = fields[tablename]
+       
+               index = IDs.get_by(table_name=tablename, field_name=fieldname)
+               if not index:
+                       print "NEW IDs index INSIDE confirm_ids"
+                       index = IDs(table_name=tablename, field_name=fieldname, nodeid=0, nextid=10)
+                       index.flush()
+                       need_to_flush=True
+
+       if need_to_flush:
+               zab_session.flush()
+       
+
 setup_all()
+confirm_ids()
 
 def get_zabbix_class_from_name(name):
        em = get_zabbix_entitymap()
@@ -756,7 +852,3 @@ class OperationConditionNotAck(object):
                                operator=defines.CONDITION_OPERATOR_EQUAL, 
                                value=0 ) # NOT_ACK
                return  o
-
-#import md5
-#u = User(alias="stephen.soltesz@gmail.com", name="stephen.soltesz@gmail.com", surname="", passwd=md5.md5("test").hexdigest(), url="", autologin=0, autologout=900, lang="en_gb", refresh=30, type=1, theme="default.css")
-#u.flush()