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):
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
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 :)
56 self
.module_type
= 'fork'
60 self
.command_name
= self
.command_name
.strip()
68 return str(self
.__dict
__)
71 #Get a brok with initial status
72 def get_initial_status_brok(self
):
75 data
= {'id' : self
.id}
77 self
.fill_data_brok_from(data
, 'full_status')
78 b
= Brok('initial_'+my_type
+'_status', data
)
82 def fill_data_brok_from(self
, data
, brok_type
):
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
99 __slots__
= ('id', 'call', 'command', 'valid', 'args')
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
106 tab
= call
.split('!')
107 self
.command
= tab
[0]
109 self
.command
= commands
.find_cmd_by_name(self
.command
.strip())
110 if self
.command
is not None:
114 self
.command
= tab
[0]
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
134 return str(self
.__dict
__)
141 class Commands(object):
142 def __init__(self
, commands
):
145 self
.commands
[c
.id] = c
149 return self
.commands
.itervalues()
154 for c
in self
.commands
.values():
159 def find_cmd_id_by_name(self
, name
):
160 for id in self
.commands
:
161 if getattr(self
.commands
[id], 'command_name', '') == name
:
165 def find_cmd_by_name(self
, name
):
166 id = self
.find_cmd_id_by_name(name
)
168 return self
.commands
[id]