Clean : (Grégory Starck) clean of some getattr code, bis.
[shinken.git] / shinken / command.py
blob2b5f83768f2bc3549929b46d3c32fbeda5245c97
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
21 from brok import Brok
22 from shinken.property import UnusedProp, BoolProp, IntegerProp, FloatProp, CharProp, StringProp, ListProp
24 class Command(object):
25 id = 0
26 my_type = "command"
28 properties={
29 'command_name': StringProp(
30 fill_brok=['full_status']),
31 'command_line': StringProp(
32 fill_brok=['full_status']),
33 'poller_tag': StringProp(
34 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
46 def pythonize(self):
47 self.command_name = self.command_name.strip()
50 def clean(self):
51 pass
54 def __str__(self):
55 return str(self.__dict__)
58 #Get a brok with initial status
59 def get_initial_status_brok(self):
60 cls = self.__class__
61 my_type = cls.my_type
62 data = {'id' : self.id}
64 self.fill_data_brok_from(data, 'full_status')
65 b = Brok('initial_'+my_type+'_status', data)
66 return b
69 def fill_data_brok_from(self, data, brok_type):
70 cls = self.__class__
71 #Now config properties
72 for prop in cls.properties:
73 #Is this property intended for brokking?
74 # if 'fill_brok' in cls.properties[prop]:
75 if brok_type in cls.properties[prop].fill_brok:
76 if hasattr(self, prop):
77 data[prop] = getattr(self, prop)
78 elif 'default' in cls.properties[prop]:
79 data[prop] = cls.properties[prop].default
83 #This class is use when a service, contact or host define
84 #a command with args.
85 class CommandCall:
86 __slots__ = ('id', 'call', 'command', 'valid', 'args')
87 id = 0
88 my_type = 'CommandCall'
89 def __init__(self, commands, call, poller_tag=None):
90 self.id = self.__class__.id
91 self.__class__.id += 1
92 self.call = call
93 tab = call.split('!')
94 self.command = tab[0]
95 self.args = tab[1:]
96 self.command = commands.find_cmd_by_name(self.command.strip())
97 if self.command is not None:
98 self.valid = True
99 else:
100 self.valid = False
101 self.command = tab[0]
102 #If the host/service do not give an override poller_tag, take
103 #the one of the command
104 self.poller_tag = poller_tag #from host/service
105 if self.valid and poller_tag == None:
106 self.poller_tag = self.command.poller_tag #from command if not set
109 def is_valid(self):
110 return self.valid
113 def __str__(self):
114 return str(self.__dict__)
117 def get_name(self):
118 return self.call
121 class Commands(object):
122 def __init__(self, commands):
123 self.commands = {}
124 for c in commands:
125 self.commands[c.id] = c
128 def __iter__(self):
129 return self.commands.itervalues()
132 def __str__(self):
133 s = ''
134 for c in self.commands.values():
135 s += str(c)
136 return s
139 def find_cmd_id_by_name(self, name):
140 for id in self.commands:
141 if self.commands[id].command_name == name:
142 return id
143 return None
145 def find_cmd_by_name(self, name):
146 id = self.find_cmd_id_by_name(name)
147 if id is not None:
148 return self.commands[id]
149 else:
150 return None