*TAG : 0.5 release
[shinken.git] / shinken / comment.py
blob38005d3bcfa95c23c1f529b72bfabf8528650559
1 #!/usr/bin/env python
2 #Copyright (C) 2010 Gabes Jean, naparuba@gmail.com
4 #This file is part of Shinken.
6 #Shinken is free software: you can redistribute it and/or modify
7 #it under the terms of the GNU Affero General Public License as published by
8 #the Free Software Foundation, either version 3 of the License, or
9 #(at your option) any later version.
11 #Shinken is distributed in the hope that it will be useful,
12 #but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 #GNU Affero General Public License for more details.
16 #You should have received a copy of the GNU Affero General Public License
17 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
20 import time
22 class Comment:
23 id = 1
25 properties = {
26 'entry_time' : None,
27 'persistent' : None,
28 'author' : None,
29 'comment' : None,
30 'comment_type' : None,
31 'entry_type' : None,
32 'source' : None,
33 'expires' : None,
34 'expire_time' : None,
35 'can_be_deleted' : None,
41 #Adds a comment to a particular service. If the "persistent" field
42 #is set to zero (0), the comment will be deleted the next time
43 #Nagios is restarted. Otherwise, the comment will persist
44 #across program restarts until it is deleted manually.
45 def __init__(self, ref, persistent, author, comment, comment_type, entry_type, source, expires, expire_time):
46 self.id = self.__class__.id
47 self.__class__.id += 1
48 self.ref = ref #pointer to srv or host we are apply
49 self.entry_time = int(time.time())
50 self.persistent = persistent
51 self.author = author
52 self.comment = comment
53 #Now the hidden attributes
54 #HOST_COMMENT=1,SERVICE_COMMENT=2
55 self.comment_type = comment_type
56 #USER_COMMENT=1,DOWNTIME_COMMENT=2,FLAPPING_COMMENT=3,ACKNOWLEDGEMENT_COMMENT=4
57 self.entry_type = entry_type
58 #COMMENTSOURCE_INTERNAL=0,COMMENTSOURCE_EXTERNAL=1
59 self.source = source
60 self.expires = expires
61 self.expire_time = expire_time
62 self.can_be_deleted = False
65 def __str__(self):
66 return "Comment id=%d %s" % (self.id, self.comment)
69 #Call by picle for dataify the ackn
70 #because we DO NOT WANT REF in this pickleisation!
71 def __getstate__(self):
72 cls = self.__class__
73 # id is not in *_properties
74 res = {'id' : self.id}
75 for prop in cls.properties:
76 if hasattr(self, prop):
77 res[prop] = getattr(self, prop)
78 return res
81 #Inversed funtion of getstate
82 def __setstate__(self, state):
83 cls = self.__class__
85 # Maybe it's not a dict but a list like in the old 0.4 format
86 # so we should call the 0.4 function for it
87 if isinstance(state, list):
88 self.__setstate_deprecated__(state)
89 return
91 self.id = state['id']
92 for prop in cls.properties:
93 if prop in state:
94 setattr(self, prop, state[prop])
97 # Theses 2 functions are DEPRECATED and will be removed in a future version of
98 # Shinken. They should not be useful any more after a first load/save pass.
100 #Inversed funtion of getstate
101 def __setstate_deprecated__(self, state):
102 cls = self.__class__
103 #Check if the len of this state is like the previous,
104 # if not, we will do errors!
105 # -1 because of the 'id' prop
106 if len(cls.properties) != (len(state) - 1):
107 print "Passing comment"
108 return
110 self.id = state.pop()
111 for prop in cls.properties:
112 val = state.pop()
113 setattr(self, prop, val)