Fix : get back LiveStatus as default.
[shinken.git] / shinken / objects / command.py
blob4df61ed428cbd27673422284fc73ce6bae552c1e
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 'module_type': StringProp(default=None),
37 def __init__(self, params={}):
38 self.id = self.__class__.id
39 self.__class__.id += 1
40 for key in params:
41 setattr(self, key, params[key])
42 if not hasattr(self, 'poller_tag'):
43 self.poller_tag = None
44 if not hasattr(self, 'module_type'):
45 # If the command satr with a _, set the module_type
46 # as the name of the command, without the _
47 if getattr(self, 'command_line', '').startswith('_'):
48 module_type = getattr(self, 'command_line', '').split(' ')[0]
49 # and we remove the first _
50 self.module_type = module_type[1:]
51 # If no command starting with _, be fork :)
52 else:
53 self.module_type = 'fork'
56 def pythonize(self):
57 self.command_name = self.command_name.strip()
60 def clean(self):
61 pass
64 def __str__(self):
65 return str(self.__dict__)
68 #Get a brok with initial status
69 def get_initial_status_brok(self):
70 cls = self.__class__
71 my_type = cls.my_type
72 data = {'id' : self.id}
74 self.fill_data_brok_from(data, 'full_status')
75 b = Brok('initial_'+my_type+'_status', data)
76 return b
79 def fill_data_brok_from(self, data, brok_type):
80 cls = self.__class__
81 #Now config properties
82 for prop, entry in cls.properties.items():
83 #Is this property intended for brokking?
84 # if 'fill_brok' in entry[prop]:
85 if brok_type in entry.fill_brok:
86 if hasattr(self, prop):
87 data[prop] = getattr(self, prop)
88 #elif 'default' in entry[prop]:
89 # data[prop] = entry.default
93 #This class is use when a service, contact or host define
94 #a command with args.
95 class CommandCall:
96 __slots__ = ('id', 'call', 'command', 'valid', 'args')
97 id = 0
98 my_type = 'CommandCall'
99 def __init__(self, commands, call, poller_tag=None):
100 self.id = self.__class__.id
101 self.__class__.id += 1
102 self.call = call
103 tab = call.split('!')
104 self.command = tab[0]
105 self.args = tab[1:]
106 self.command = commands.find_cmd_by_name(self.command.strip())
107 if self.command is not None:
108 self.valid = True
109 else:
110 self.valid = False
111 self.command = tab[0]
112 if self.valid:
113 #If the host/service do not give an override poller_tag, take
114 #the one of the command
115 self.poller_tag = poller_tag #from host/service
116 self.module_type = self.command.module_type
117 if self.valid and poller_tag is None:
118 self.poller_tag = self.command.poller_tag #from command if not set
121 def is_valid(self):
122 return self.valid
125 def __str__(self):
126 return str(self.__dict__)
129 def get_name(self):
130 return self.call
133 class Commands(object):
134 def __init__(self, commands):
135 self.commands = {}
136 for c in commands:
137 self.commands[c.id] = c
140 def __iter__(self):
141 return self.commands.itervalues()
144 def __str__(self):
145 s = ''
146 for c in self.commands.values():
147 s += str(c)
148 return s
151 def find_cmd_id_by_name(self, name):
152 for id in self.commands:
153 if getattr(self.commands[id], 'command_name', '') == name:
154 return id
155 return None
157 def find_cmd_by_name(self, name):
158 id = self.find_cmd_id_by_name(name)
159 if id is not None:
160 return self.commands[id]
161 else:
162 return None