clean : comment unless print in discorules.
[shinken.git] / shinken / objects / command.py
blobc537883b79e7f95c21a72c0895589199ccabd67b
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
5 # Gregory Starck, g.starck@gmail.com
6 # Hartmut Goebel, h.goebel@goebel-consult.de
8 #This file is part of Shinken.
10 #Shinken is free software: you can redistribute it and/or modify
11 #it under the terms of the GNU Affero General Public License as published by
12 #the Free Software Foundation, either version 3 of the License, or
13 #(at your option) any later version.
15 #Shinken is distributed in the hope that it will be useful,
16 #but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 #GNU Affero General Public License for more details.
20 #You should have received a copy of the GNU Affero General Public License
21 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
23 from shinken.brok import Brok
24 from shinken.property import StringProp
26 class Command(object):
27 id = 0
28 my_type = "command"
30 properties = {
31 'command_name': StringProp(fill_brok=['full_status']),
32 'command_line': StringProp(fill_brok=['full_status']),
33 'poller_tag': StringProp(default='None'),
34 'reactionner_tag': StringProp(default='None'),
35 'module_type': StringProp(default=None),
38 def __init__(self, params={}):
39 self.id = self.__class__.id
40 self.__class__.id += 1
41 for key in params:
42 setattr(self, key, params[key])
43 if not hasattr(self, 'poller_tag'):
44 self.poller_tag = 'None'
45 if not hasattr(self, 'reactionner_tag'):
46 self.reactionner_tag = 'None'
47 if not hasattr(self, 'module_type'):
48 # If the command satr with a _, set the module_type
49 # as the name of the command, without the _
50 if getattr(self, 'command_line', '').startswith('_'):
51 module_type = getattr(self, 'command_line', '').split(' ')[0]
52 # and we remove the first _
53 self.module_type = module_type[1:]
54 # If no command starting with _, be fork :)
55 else:
56 self.module_type = 'fork'
59 def pythonize(self):
60 self.command_name = self.command_name.strip()
63 def clean(self):
64 pass
67 def __str__(self):
68 return str(self.__dict__)
71 #Get a brok with initial status
72 def get_initial_status_brok(self):
73 cls = self.__class__
74 my_type = cls.my_type
75 data = {'id' : self.id}
77 self.fill_data_brok_from(data, 'full_status')
78 b = Brok('initial_'+my_type+'_status', data)
79 return b
82 def fill_data_brok_from(self, data, brok_type):
83 cls = self.__class__
84 #Now config properties
85 for prop, entry in cls.properties.items():
86 #Is this property intended for brokking?
87 # if 'fill_brok' in entry[prop]:
88 if brok_type in entry.fill_brok:
89 if hasattr(self, prop):
90 data[prop] = getattr(self, prop)
91 #elif 'default' in entry[prop]:
92 # data[prop] = entry.default
96 #This class is use when a service, contact or host define
97 #a command with args.
98 class CommandCall:
99 __slots__ = ('id', 'call', 'command', 'valid', 'args')
100 id = 0
101 my_type = 'CommandCall'
102 def __init__(self, commands, call, poller_tag='None', reactionner_tag='None'):
103 self.id = self.__class__.id
104 self.__class__.id += 1
105 self.call = call
106 tab = call.split('!')
107 self.command = tab[0]
108 self.args = tab[1:]
109 self.command = commands.find_cmd_by_name(self.command.strip())
110 if self.command is not None:
111 self.valid = True
112 else:
113 self.valid = False
114 self.command = tab[0]
115 if self.valid:
116 #If the host/service do not give an override poller_tag, take
117 #the one of the command
118 self.poller_tag = poller_tag #from host/service
119 self.reactionner_tag = reactionner_tag
120 self.module_type = self.command.module_type
121 if self.valid and poller_tag is 'None':
122 self.poller_tag = self.command.poller_tag #from command if not set
123 # Same for reactionner tag
124 if self.valid and reactionner_tag is 'None':
125 self.reactionner_tag = self.command.reactionner_tag #from command if not set
129 def is_valid(self):
130 return self.valid
133 def __str__(self):
134 return str(self.__dict__)
137 def get_name(self):
138 return self.call
141 class Commands(object):
142 def __init__(self, commands):
143 self.commands = {}
144 for c in commands:
145 self.commands[c.id] = c
148 def __iter__(self):
149 return self.commands.itervalues()
152 def __str__(self):
153 s = ''
154 for c in self.commands.values():
155 s += str(c)
156 return s
159 def find_cmd_id_by_name(self, name):
160 for id in self.commands:
161 if getattr(self.commands[id], 'command_name', '') == name:
162 return id
163 return None
165 def find_cmd_by_name(self, name):
166 id = self.find_cmd_id_by_name(name)
167 if id is not None:
168 return self.commands[id]
169 else:
170 return None