74407f9d6483bc1856ca315d8ae60287b9eacd08
[monitor.git] / monitor / database / zabbixapi / model.py
1 import pkg_resources
2 pkg_resources.require("SQLAlchemy>=0.3.10")
3 pkg_resources.require("Elixir>=0.4.0")
4 # import the basic Elixir classes and functions for declaring the data model
5 # (see http://elixir.ematia.de/trac/wiki/TutorialDivingIn)
6 from elixir import EntityMeta, Entity, Field, OneToMany, ManyToOne, ManyToMany
7 from elixir import options_defaults, using_options, setup_all, entities
8 # import some datatypes for table columns from Elixir
9 # (see http://www.sqlalchemy.org/docs/04/types.html for more)
10 from elixir import String, Unicode, Integer, DateTime
11 from sqlalchemy import ColumnDefault
12 from sqlalchemy import Table
13 from sqlalchemy.orm import ColumnProperty, object_session
14
15 from xml.marshal.generic import Marshaller
16 from xml.dom.ext import PrettyPrint
17 from xml.dom.ext.reader.Sax import FromXml
18 from elementtree import ElementTree
19
20 options_defaults['autosetup'] = False
21
22 from elixir.statements import Statement
23 from sqlalchemy import Sequence
24
25 import defines
26
27 from monitor.database.dborm import zab_metadata, zab_session
28
29 __metadata__ = zab_metadata
30 __session__  = zab_session
31
32 # TODO:
33 #   - declare association between Media and MediaType so that look ups can
34 #       occur on 'description'
35
36 class ZabbixSerialize(object):
37
38         @classmethod
39         def xmlDeserialize(cls, xml):
40                 d = cls.xml2dict(xml)
41                 return cls.dict2object(d)
42
43         def xmlSerialize(self, elem=None):
44                 dict = self.convert_dict(self.to_dict())
45
46                 if hasattr(self, 'deepcopy'):
47                         for val in self.deepcopy:
48                                 dict[val] = getattr(self, val)
49
50                 skip_keys = [self._descriptor.auto_primarykey]
51                 if hasattr(self, 'skip_keys'):
52                         skip_keys += self.skip_keys
53
54                 return self.xmlMessage(dict, skip_keys, elem)
55
56         @classmethod
57         def xmlMessage(cls, dict=None, skip_keys=[], use_elem=None):
58
59                 elem = ElementTree.Element(cls.classname())
60
61                 if isinstance(dict, type({})):
62                         for key, value in dict.items():
63                                 if key in skip_keys:
64                                         continue
65
66                                 if isinstance(value, type(0)):
67                                         ElementTree.SubElement(elem, key, type="int").text = str(value)
68
69                                 elif isinstance(value, type(0L)):
70                                         ElementTree.SubElement(elem, key, type="long").text = str(value)
71
72                                 elif isinstance(value, type([])):
73                                         if len(value) > 0:
74                                                 e = ElementTree.SubElement(elem, key, type="list") 
75                                                 for obj in value:
76                                                         d = obj.convert_dict(obj.to_dict())
77                                                         obj.xmlSerialize(e) 
78                                 else:
79                                         ElementTree.SubElement(elem, key).text = value
80
81                 elif isinstance(dict, type([])):
82                         if len(dict) > 0:
83                                 o = dict[0]
84                                 key = "%s_list" % o.__class__.__name__.lower()
85                                 e = ElementTree.SubElement(elem, key, type="list") 
86                                 for obj in dict:
87                                         d = obj.convert_dict(obj.to_dict())
88                                         obj.xmlSerialize(e) 
89
90                 if use_elem is not None:
91                         use_elem.append(elem)
92                                 
93                 return ElementTree.tostring(elem)
94
95         @classmethod
96         def xml2dict(cls, message, elem=None):
97                 em = get_zabbix_entitymap()
98
99                 if message and elem is None:
100                         elem = ElementTree.XML(message)
101                 elif elem is None:
102                         raise Exception("Cannot proceed with empty xml, and no elem")
103
104                 #print "tag: %s : classname : %s" % (elem.tag, cls.classname())
105                 if cls is not ZabbixSerialize:
106                         assert elem.tag == cls.classname()
107                 dict = {}
108                 for elem in elem:
109                         if elem.get("type") == "int":
110                                 dict[elem.tag] = int(elem.text)
111                         elif elem.get("type") == "long":
112                                 dict[elem.tag] = long(elem.text)
113                         elif elem.get("type") == "list":
114                                 if cls is not ZabbixSerialize:
115                                         assert elem.tag in cls.deepcopy, "List (%s) in XML is not a recognized type for this object (%s)" % (elem.tag, cls.classname())
116                                 dict[elem.tag] = []
117                                 for e in elem:
118                                         dict[elem.tag].append( em[e.tag].xml2dict(None, e) )
119                         elif elem.text is None:
120                                 dict[elem.tag] = ""
121                         else:
122                                 dict[elem.tag] = elem.text
123                 return dict
124
125         @classmethod
126         def dict2object(cls, dict):
127                 em = get_zabbix_entitymap()
128                 if cls is ZabbixSerialize:
129                         # note: assume that there's only one type of class
130                         retdict = {}
131                         for key in dict.keys():
132                                 clsobj = get_zabbix_class_from_name(key)
133                                 retdict[key] = [ clsobj.dict2object(data) for data in dict[key] ]
134                         return retdict
135
136                 # take deepcopy values out of dict.
137                 backup = {}
138                 if hasattr(cls, 'deepcopy'):
139                         for val in cls.deepcopy:
140                                 if val in dict:
141                                         backup[val] = dict[val]
142                                         del dict[val]
143
144                 # instantiate them
145                 # for each deepcopy object, convert all values in list
146                 for k in backup.keys():
147                         clsobj = get_zabbix_class_from_name(k)
148                         l = [ clsobj.dict2object(data) for data in backup[k] ]
149                         backup[k] = l
150
151                 # find or create the primary object
152                 obj = cls.find_or_create(**dict)
153                 #if cls is DiscoveryCheck or \
154                 #       cls is ActionCondition or \
155                 #       cls is ActionOperation:
156                 #       # NOTE: Some objects should always be created. like DiscoveryCheck
157                 #       obj = None
158                 #else:
159                 #       obj = cls.get_by(**dict)
160 #
161 #               if obj is None:
162 #                       print "CREATING NEW %s" % cls.classname()
163 #                       obj = cls(**dict)
164 #               else:
165 #                       print "FOUND EXISTING OBJECT: %s"% obj
166
167                 # add deepcopy values to primary object
168                 for k in backup.keys():
169                         print type(backup[k][0])
170
171                         if isinstance(obj, User) and isinstance(backup[k][0], UsrGrp):
172                                 print "adding groups to user"
173                                 for g in backup[k]:
174                                         obj.append_group(g)
175
176                         elif isinstance(obj, User) and isinstance(backup[k][0], Media):
177                                 print "adding media to user"
178                                 for g in backup[k]:
179                                         obj.media_list.append(g)
180
181                         elif isinstance(obj, UsrGrp) and isinstance(backup[k][0], HostGroup):
182                                 print "adding hostgroup to usergroup"
183                                 print "NOT IMPLEMENTED!!!"
184                                 for g in backup[k]:
185                                         obj.append_hostgroup(g)
186                                         pass
187
188                         elif isinstance(obj, Action) and isinstance(backup[k][0], ActionCondition):
189                                 print "adding actionconditon to action"
190                                 for g in backup[k]:
191                                         obj.actioncondition_list.append(g)
192
193                         elif isinstance(obj, Action) and isinstance(backup[k][0], ActionOperation):
194                                 print "adding actionoperation to action"
195                                 for g in backup[k]:
196                                         obj.actionoperation_list.append(g)
197
198                         elif isinstance(obj, ActionOperation) and \
199                                  isinstance(backup[k][0], OperationCondition):
200                                 print "adding operationcondition to actionoperation"
201                                 for g in backup[k]:
202                                         obj.operationcondition_list.append(g)
203
204                         elif isinstance(obj, DiscoveryRule) and isinstance(backup[k][0], DiscoveryCheck):
205                                 print "adding discoverycheck to discoveryrule"
206                                 for v in backup[k]:
207                                         obj.discoverycheck_list.append(v)
208
209                 return obj
210
211         def convert_dict(self, d):
212                 rd = {}
213                 for key in d.keys():
214                         if type(d[key]) == type([]):
215                                 rd[str(key)] = [ self.convert_dict(v) for v in d[key] ]
216                         else:
217                                 rd[str(key)] = d[key]
218                 return rd
219
220         @classmethod
221         def classname(cls):
222                 return cls.__name__
223
224         def prettyserialize(self):
225                 xml = self.xmlSerialize()
226                 d = FromXml(xml)
227                 PrettyPrint(d)
228         
229 class ZabbixEntity(ZabbixSerialize):
230         __metaclass__ = EntityMeta
231
232         def __init__(self, **kwargs):
233                 print "__INIT__ %s" % self.classname()
234                 tablename = self._descriptor.tablename
235                 fieldname = self._descriptor.auto_primarykey
236                 index = IDs.get_by(table_name=tablename, field_name=fieldname)
237                 if not index:
238                         print "NEW IDs index INSIDE INIT"
239                         index = IDs(table_name=tablename, field_name=fieldname, nodeid=0, nextid=10)
240                         index.flush()
241                 index.nextid = index.nextid + 1
242                 kwargs[fieldname] = index.nextid
243                 self.set(**kwargs)
244
245         def __repr__(self):
246                 rd = {}
247                 if hasattr(self, 'deepcopy'):
248                         for k in self.deepcopy:
249                                 rd[k] = [ str(v) for v in getattr(self, k) ]
250
251                 rd.update(self.to_dict())
252                 val = ""
253                 for k in rd.keys():
254                         val += k
255                         val += "="
256                         val += str(rd[k])
257                         val += ", "
258                 return self.classname() + "(" + val + ")"
259
260         @classmethod
261         def classname(cls):
262                 return cls.__name__
263
264         def set(self, **kwargs):
265                 for key, value in kwargs.iteritems():
266                         setattr(self, key, value)
267         
268         @classmethod
269         def find_or_create(cls, exec_if_new=None, set_if_new={}, **kwargs):
270                 if cls is DiscoveryCheck or cls is ActionCondition or \
271                         cls is ActionOperation:
272                         # NOTE: Some objects should always be created. like DiscoveryCheck
273                         obj = None
274                 else:
275                         # NOTE: ignore *_list items
276                         query = {}
277                         for key in kwargs:
278                                 if "_list" not in key:
279                                         query[key] = kwargs[key]
280                         print "SEARCHING USING %s" % query
281                         obj = cls.get_by(**query)
282
283                 if obj is None:
284                         print "CREATING NEW %s" % cls.classname()
285                         print "USING %s" % kwargs
286                         obj = cls(**kwargs)
287                         obj.set(**set_if_new)
288                         if exec_if_new:
289                                 exec_if_new(obj)
290                 else:
291                         print "FOUND EXISTING OBJECT: %s"% obj
292
293                 return obj
294
295         def update_or_create(cls, data, surrogate=True):
296                 pk_props = cls._descriptor.primary_key_properties
297
298                 # if all pk are present and not None
299                 if not [1 for p in pk_props if data.get(p.key) is None]:
300                         pk_tuple = tuple([data[prop.key] for prop in pk_props])
301                         record = cls.query.get(pk_tuple)
302                         if record is None:
303                                 if surrogate:
304                                         raise Exception("cannot create surrogate with pk")
305                                 else:
306                                         record = cls()
307                 else:
308                         if surrogate:
309                                 record = cls()
310                         else:
311                                 raise Exception("cannot create non surrogate without pk")
312                 record.from_dict(data)
313                 return record
314         update_or_create = classmethod(update_or_create)
315
316         def from_dict(self, data):
317                 """
318                 Update a mapped class with data from a JSON-style nested dict/list
319                 structure.
320                 """
321                 # surrogate can be guessed from autoincrement/sequence but I guess
322                 # that's not 100% reliable, so we'll need an override
323
324                 mapper = sqlalchemy.orm.object_mapper(self)
325
326                 for key, value in data.iteritems():
327                         if isinstance(value, dict):
328                                 dbvalue = getattr(self, key)
329                                 rel_class = mapper.get_property(key).mapper.class_
330                                 pk_props = rel_class._descriptor.primary_key_properties
331
332                                 # If the data doesn't contain any pk, and the relationship
333                                 # already has a value, update that record.
334                                 if not [1 for p in pk_props if p.key in data] and \
335                                    dbvalue is not None:
336                                         dbvalue.from_dict(value)
337                                 else:
338                                         record = rel_class.update_or_create(value)
339                                         setattr(self, key, record)
340                         elif isinstance(value, list) and \
341                                  value and isinstance(value[0], dict):
342
343                                 rel_class = mapper.get_property(key).mapper.class_
344                                 new_attr_value = []
345                                 for row in value:
346                                         if not isinstance(row, dict):
347                                                 raise Exception(
348                                                                 'Cannot send mixed (dict/non dict) data '
349                                                                 'to list relationships in from_dict data.')
350                                         record = rel_class.update_or_create(row)
351                                         new_attr_value.append(record)
352                                 setattr(self, key, new_attr_value)
353                         else:
354                                 setattr(self, key, value)
355
356         def to_dict(self, deep={}, exclude=[]):
357                 """Generate a JSON-style nested dict/list structure from an object."""
358                 col_prop_names = [p.key for p in self.mapper.iterate_properties \
359                                                                           if isinstance(p, ColumnProperty)]
360                 data = dict([(name, getattr(self, name))
361                                          for name in col_prop_names if name not in exclude])
362                 for rname, rdeep in deep.iteritems():
363                         dbdata = getattr(self, rname)
364                         #FIXME: use attribute names (ie coltoprop) instead of column names
365                         fks = self.mapper.get_property(rname).remote_side
366                         exclude = [c.name for c in fks]
367                         if isinstance(dbdata, list):
368                                 data[rname] = [o.to_dict(rdeep, exclude) for o in dbdata]
369                         else:
370                                 data[rname] = dbdata.to_dict(rdeep, exclude)
371                 return data
372
373         # session methods
374         def flush(self, *args, **kwargs):
375                 return object_session(self).flush([self], *args, **kwargs)
376
377         def delete(self, *args, **kwargs):
378                 return object_session(self).delete(self, *args, **kwargs)
379
380         def expire(self, *args, **kwargs):
381                 return object_session(self).expire(self, *args, **kwargs)
382
383         def refresh(self, *args, **kwargs):
384                 return object_session(self).refresh(self, *args, **kwargs)
385
386         def expunge(self, *args, **kwargs):
387                 return object_session(self).expunge(self, *args, **kwargs)
388
389         # This bunch of session methods, along with all the query methods below
390         # only make sense when using a global/scoped/contextual session.
391         def _global_session(self):
392                 return self._descriptor.session.registry()
393         _global_session = property(_global_session)
394
395         def merge(self, *args, **kwargs):
396                 return self._global_session.merge(self, *args, **kwargs)
397
398         def save(self, *args, **kwargs):
399                 return self._global_session.save(self, *args, **kwargs)
400
401         def update(self, *args, **kwargs):
402                 return self._global_session.update(self, *args, **kwargs)
403
404         # only exist in SA < 0.5
405         # IMO, the replacement (session.add) doesn't sound good enough to be added
406         # here. For example: "o = Order(); o.add()" is not very telling. It's
407         # better to leave it as "session.add(o)"
408         def save_or_update(self, *args, **kwargs):
409                 return self._global_session.save_or_update(self, *args, **kwargs)
410
411         # query methods
412         def get_by(cls, *args, **kwargs):
413                 return cls.query.filter_by(*args, **kwargs).first()
414         get_by = classmethod(get_by)
415
416         def get(cls, *args, **kwargs):
417                 return cls.query.get(*args, **kwargs)
418         get = classmethod(get)
419
420 class IDs(Entity):
421         using_options(
422                 tablename='ids',
423                 autoload=True,
424         )
425
426 class Escalation(ZabbixEntity):
427         using_options(
428                 tablename='escalations',
429                 autoload=True,
430                 auto_primarykey='escalationid'
431         )
432
433 class Event(ZabbixEntity):
434         using_options(
435                 tablename='events',
436                 autoload=True,
437                 auto_primarykey='eventid'
438         )
439
440 class Item(ZabbixEntity):
441         using_options(
442                 tablename='items',
443                 autoload=True,
444                 auto_primarykey='itemid'
445         )
446
447 class Acknowledge(ZabbixEntity):
448         using_options(
449                 tablename='acknowledges',
450                 autoload=True,
451                 auto_primarykey='acknowledgeid'
452         )
453
454 class Trigger(ZabbixEntity):
455         using_options(
456                 tablename='triggers',
457                 autoload=True,
458                 auto_primarykey='triggerid'
459         )
460         
461
462 class Right(ZabbixEntity):
463         # rights of a usergroup to interact with hosts of a hostgroup
464         using_options(
465                 tablename='rights',
466                 autoload=True,
467                 auto_primarykey='rightid',
468         )
469         # column groupid is an index to usrgrp.usrgrpid
470         # column id is an index into the host-groups.groupid
471         # permission is 3=rw, 2=ro, 1=r_list, 0=deny
472
473         # TODO: NOTE: When serialization occurs, the 'permissions' field is lost,
474         # currently since the rights table is merely treated as an intermediate
475         # table for the m2m between usrgrp and groups.
476
477 rights = Table('rights', __metadata__, autoload=True)
478 hostsgroups = Table('hosts_groups', __metadata__, autoload=True)
479 hoststemplates = Table('hosts_templates', __metadata__, autoload=True)
480
481         
482 # m2m table between hosts and groups below
483 class HostsGroups(ZabbixEntity):
484         using_options(
485                 tablename='hosts_groups',
486                 autoload=True,
487                 auto_primarykey='hostgroupid',
488         )
489
490 class HostsTemplates(ZabbixEntity):
491         using_options(
492                 tablename='hosts_templates',
493                 autoload=True,
494                 auto_primarykey='hosttemplateid',
495         )
496
497 class Host(ZabbixEntity):
498         using_options(
499                 tablename='hosts',
500                 autoload=True,
501                 auto_primarykey='hostid',
502         )
503         hostgroup_list = ManyToMany(
504                 'HostGroup',
505                 table=hostsgroups,
506                 foreign_keys=lambda: [hostsgroups.c.groupid, hostsgroups.c.hostid],
507                 primaryjoin=lambda: Host.hostid==hostsgroups.c.hostid,
508                 secondaryjoin=lambda: HostGroup.groupid==hostsgroups.c.groupid,
509         )
510         template_list = ManyToMany(
511                 'Host',
512                 table=hoststemplates,
513                 foreign_keys=lambda: [hoststemplates.c.hostid, hoststemplates.c.templateid],
514                 primaryjoin=lambda: Host.hostid==hoststemplates.c.hostid,
515                 secondaryjoin=lambda: Host.hostid==hoststemplates.c.templateid,
516         )
517
518         def append_template(self, template):
519                 row = HostsTemplates(hostid=self.hostid, templateid=template.hostid)
520                 return template
521
522         def remove_template(self, template):
523                 row = HostsTemplates.get_by(hostid=self.hostid, templateid=template.hostid)
524                 if row is not None:
525                         row.delete()
526
527         def delete(self):
528                 # NOTE: media objects are automatically handled.
529                 hosts_templates_match = HostsTemplates.query.filter_by(hostid=self.hostid).all()
530                 for row in hosts_templates_match:
531                         row.delete()
532
533                 hosts_groups_match = HostsGroups.query.filter_by(hostid=self.hostid).all()
534                 for row in hosts_groups_match:
535                         row.delete()
536                 super(Host, self).delete()
537
538 class HostGroup(ZabbixEntity):
539         using_options(
540                 tablename='groups',
541                 autoload=True,
542                 auto_primarykey='groupid',
543         )
544         usrgrp_list = ManyToMany(
545                 'UsrGrp',
546                 table=rights,
547                 foreign_keys=lambda: [rights.c.groupid, rights.c.id],
548                 primaryjoin=lambda: HostGroup.groupid==rights.c.id,
549                 secondaryjoin=lambda: UsrGrp.usrgrpid==rights.c.groupid,
550         )
551         host_list = ManyToMany(
552                 'Host',
553                 table=hostsgroups,
554                 foreign_keys=lambda: [hostsgroups.c.groupid, hostsgroups.c.hostid],
555                 primaryjoin=lambda: HostGroup.groupid==hostsgroups.c.groupid,
556                 secondaryjoin=lambda: Host.hostid==hostsgroups.c.hostid,
557         )
558         def delete(self):
559                 # NOTE: media objects are automatically handled.
560                 hosts_groups_match = HostsGroups.query.filter_by(groupid=self.groupid).all()
561                 for row in hosts_groups_match:
562                         row.delete()
563                 super(HostGroup, self).delete()
564
565 class UsersGroups(ZabbixEntity):
566         using_options(
567                 tablename='users_groups',
568                 autoload=True,
569                 auto_primarykey='id',
570         )
571
572 class MediaType(ZabbixEntity):
573         using_options(
574                 tablename='media_type',
575                 autoload=True,
576                 auto_primarykey='mediatypeid',
577         )
578
579 class Script(ZabbixEntity):
580         using_options(
581                 tablename='scripts',
582                 autoload=True,
583                 auto_primarykey='scriptid',
584         )
585
586
587 # DISCOVERY ################################################3
588
589 class DiscoveryCheck(ZabbixEntity):
590         using_options(
591                 tablename='dchecks',
592                 autoload=True,
593                 auto_primarykey='dcheckid',
594         )
595         skip_keys = ['druleid']
596         discoveryrule = ManyToOne('DiscoveryRule', 
597                                         primaryjoin=lambda: DiscoveryCheck.druleid == DiscoveryRule.druleid,
598                                         foreign_keys=lambda: [DiscoveryCheck.druleid],
599                                         ondelete='cascade') 
600
601 class DiscoveryRule(ZabbixEntity):  # parent of dchecks
602         using_options(
603                 tablename='drules',
604                 autoload=True,
605                 auto_primarykey='druleid',
606         )
607         deepcopy = ['discoverycheck_list']
608         discoverycheck_list = OneToMany('DiscoveryCheck', cascade='all, delete-orphan',
609                                         primaryjoin=lambda: DiscoveryCheck.druleid == DiscoveryRule.druleid,
610                                         foreign_keys=lambda: [DiscoveryCheck.druleid])
611
612         discoveredhost_list = OneToMany('DiscoveredHost', cascade='all, delete-orphan',
613                                         primaryjoin=lambda: DiscoveredHost.druleid == DiscoveryRule.druleid,
614                                         foreign_keys=lambda: [DiscoveredHost.druleid])
615
616 class DiscoveredHost(ZabbixEntity):
617         using_options(
618                 tablename='dhosts',
619                 autoload=True,
620                 auto_primarykey='dhostid',
621         )
622         discoveryrule = ManyToOne('DiscoveryRule',
623                                         primaryjoin=lambda: DiscoveredHost.druleid == DiscoveryRule.druleid,
624                                         foreign_keys=lambda: [DiscoveredHost.druleid],
625                                         ondelete='cascade') 
626
627         discoveryservice_list = OneToMany('DiscoveryService', cascade='all, delete-orphan',
628                                         primaryjoin=lambda: DiscoveryService.dhostid== DiscoveredHost.dhostid,
629                                         foreign_keys=lambda: [DiscoveryService.dhostid],) 
630
631 class DiscoveryService(ZabbixEntity):
632         using_options(
633                 tablename='dservices',
634                 autoload=True,
635                 auto_primarykey='dserviceid',
636         )
637         discoveryrule = ManyToOne('DiscoveredHost',
638                                         primaryjoin=lambda: DiscoveryService.dhostid== DiscoveredHost.dhostid,
639                                         foreign_keys=lambda: [DiscoveryService.dhostid],
640                                         ondelete='cascade') 
641                                                 
642
643 # ACTIONS ################################################3
644
645 class ActionOperation(ZabbixEntity):
646         using_options(
647                 tablename='operations', autoload=True, auto_primarykey='operationid',
648         )
649         deepcopy = ['operationcondition_list']
650         skip_keys = ['actionid']
651         action = ManyToOne('Action', ondelete='cascade',
652                                         primaryjoin=lambda: ActionOperation.actionid == Action.actionid,
653                                         foreign_keys=lambda: [ActionOperation.actionid])
654                                         
655         operationcondition_list = OneToMany('OperationCondition', cascade='all, delete-orphan',
656                                         primaryjoin=lambda: OperationCondition.operationid == ActionOperation.operationid,
657                                         foreign_keys=lambda: [OperationCondition.operationid])
658
659 class OperationCondition(ZabbixEntity):
660         using_options(
661                 tablename='opconditions', autoload=True, auto_primarykey='opconditionid',
662         )
663         skip_keys = ['operationid']
664         actionoperation = ManyToOne('ActionOperation', ondelete='cascade',
665                                         primaryjoin=lambda: OperationCondition.operationid == ActionOperation.operationid,
666                                         foreign_keys=lambda: [OperationCondition.operationid])
667
668 class ActionCondition(ZabbixEntity):
669         using_options(
670                 tablename='conditions', autoload=True, auto_primarykey='conditionid',
671         )
672         skip_keys = ['actionid']
673         action = ManyToOne('Action', ondelete='cascade',
674                                         primaryjoin=lambda: ActionCondition.actionid == Action.actionid,
675                                         foreign_keys=lambda: [ActionCondition.actionid])
676
677 class Action(ZabbixEntity):
678         using_options(
679                 tablename='actions', autoload=True, auto_primarykey='actionid',
680         )
681         deepcopy = ['actionoperation_list', 'actioncondition_list']
682         actionoperation_list = OneToMany('ActionOperation', cascade='all, delete-orphan',
683                                         primaryjoin=lambda: ActionOperation.actionid == Action.actionid,
684                                         foreign_keys=lambda: [ActionOperation.actionid])
685                                         
686         actioncondition_list = OneToMany('ActionCondition', cascade='all, delete-orphan',
687                                         primaryjoin=lambda: ActionCondition.actionid == Action.actionid,
688                                         foreign_keys=lambda: [ActionCondition.actionid])
689
690 # USERS & EMAIL MEDIA ################################################3
691
692 class Media(ZabbixEntity):
693         using_options(
694                 tablename='media',
695                 autoload=True,
696                 auto_primarykey='mediaid',
697         )
698         skip_keys = ['userid']
699         user = ManyToOne('User', 
700                                         primaryjoin=lambda: Media.userid == User.userid,
701                                         foreign_keys=lambda: [Media.userid],
702                                         ondelete='cascade') 
703
704 users_groups = Table('users_groups', __metadata__, autoload=True)
705
706 class User(ZabbixEntity): # parent of media
707         using_options(
708                 tablename='users',
709                 autoload=True,
710                 auto_primarykey='userid',
711         )
712         deepcopy = ['media_list', 'usrgrp_list']
713         media_list = OneToMany('Media', 
714                                           primaryjoin=lambda: Media.userid == User.userid,
715                                           foreign_keys=lambda: [Media.userid],
716                                           cascade='all, delete-orphan')
717
718         # READ-ONLY: do not append or remove groups here.
719         usrgrp_list = ManyToMany('UsrGrp',
720                                 table=users_groups,
721                                 foreign_keys=lambda: [users_groups.c.userid, users_groups.c.usrgrpid],
722                                 primaryjoin=lambda: User.userid==users_groups.c.userid,
723                                 secondaryjoin=lambda: UsrGrp.usrgrpid==users_groups.c.usrgrpid)
724
725         def delete(self):
726                 # NOTE: media objects are automatically handled.
727                 users_groups_match = UsersGroups.query.filter_by(userid=self.userid).all()
728                 for row in users_groups_match:
729                         row.delete()
730                 super(User, self).delete()
731                 
732         def append_group(self, group):
733                 ug_row = UsersGroups(usrgrpid=group.usrgrpid, userid=self.userid)
734                 return group
735
736         def remove_group(self, group):
737                 ug_row = UsersGroups.get_by(usrgrpid=group.usrgrpid, userid=self.userid)
738                 if ug_row is not None:
739                         ug_row.delete()
740                 return
741                 
742 class UsrGrp(ZabbixEntity):
743         using_options(
744                 tablename='usrgrp',
745                 autoload=True,
746                 auto_primarykey='usrgrpid',
747         )
748         deepcopy= ['hostgroup_list']
749
750         user_list = ManyToMany(
751                 'User',
752                 table=users_groups,
753                 foreign_keys=lambda: [users_groups.c.userid, users_groups.c.usrgrpid],
754                 secondaryjoin=lambda: User.userid==users_groups.c.userid,
755                 primaryjoin=lambda: UsrGrp.usrgrpid==users_groups.c.usrgrpid,
756         )
757
758         hostgroup_list = ManyToMany(
759                 'HostGroup',
760                 table=rights,
761                 foreign_keys=lambda: [rights.c.groupid, rights.c.id],
762                 primaryjoin=lambda: UsrGrp.usrgrpid==rights.c.groupid,
763                 secondaryjoin=lambda: HostGroup.groupid==rights.c.id,
764         )
765
766         def delete(self):
767                 rights_match = Right.query.filter_by(groupid=self.usrgrpid).all()
768                 for row in rights_match:
769                         row.delete()
770
771                 users_groups_match = UsersGroups.query.filter_by(usrgrpid=self.usrgrpid).all()
772                 for row in users_groups_match:
773                         row.delete()
774
775                 super(UsrGrp, self).delete()
776
777         def append_hostgroup(self, hg):
778                 # NOTE: I know it looks wrong, but this is how the keys are mapped.
779                 print "APPENDING HOSTGROUP %s!!!!!!!!!!" % hg.name
780                 ug_row = Right(groupid=self.usrgrpid, id=hg.groupid, permission=3)
781                 ug_row.save()
782                 return
783
784         def append_user(self, user):
785                 ug_row = UsersGroups(userid=user.userid, usrgrpid=self.usrgrpid)
786                 ug_row.save()
787                 return
788
789         def remove_user(self, user):
790                 ug_row = UsersGroups.get_by(userid=user.userid, usrgrpid=self.usrgrpid)
791                 if ug_row is not None:
792                         ug_row.delete()
793                 return
794
795 def confirm_ids():
796         fields = {
797                 'scripts' : 'scriptid',
798                 'usrgrp' : 'usrgrpid',
799                 'users' : 'userid',
800                 'media' : 'mediaid',
801                 'users_groups' : 'id',
802                 'groups' : 'groupid',
803                 'rights' : 'rightid',
804                 'drules' : 'druleid',
805                 'dchecks' : 'dcheckid',
806                 'actions' : 'actionid',
807                 'conditions' : 'conditionid',
808                 'operations' : 'operationid',
809                 'opconditions' : 'opconditionid',
810         }
811         need_to_flush = False
812
813         for tablename in fields.keys():
814                 fieldname = fields[tablename]
815         
816                 index = IDs.get_by(table_name=tablename, field_name=fieldname)
817                 if not index:
818                         print "NEW IDs index INSIDE confirm_ids"
819                         index = IDs(table_name=tablename, field_name=fieldname, nodeid=0, nextid=10)
820                         index.flush()
821                         need_to_flush=True
822
823         if need_to_flush:
824                 zab_session.flush()
825         
826
827 setup_all()
828 confirm_ids()
829
830 def get_zabbix_class_from_name(name):
831         em = get_zabbix_entitymap()
832         cls = None
833         if "_list" in name:
834                 name=name[:-5]  # strip off the _list part.
835
836         for k in em.keys():
837                 if name == k.lower():
838                         cls = em[k]
839         return cls
840         
841 def get_zabbix_entitymap():
842         entity_map = {}
843         for n,c in zip([ u.__name__ for u in entities], entities): 
844                 entity_map[n] = c
845         return entity_map
846
847 # COMMON OBJECT TYPES
848 class OperationConditionNotAck(object):
849         def __new__(cls):
850                 o = OperationCondition(
851                                 conditiontype=defines.CONDITION_TYPE_EVENT_ACKNOWLEDGED, 
852                                 operator=defines.CONDITION_OPERATOR_EQUAL, 
853                                 value=0 ) # NOT_ACK
854                 return  o