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 'module_type': StringProp(default
=None),
37 def __init__(self
, params
={}):
38 self
.id = self
.__class
__.id
39 self
.__class
__.id += 1
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 :)
53 self
.module_type
= 'fork'
57 self
.command_name
= self
.command_name
.strip()
65 return str(self
.__dict
__)
68 #Get a brok with initial status
69 def get_initial_status_brok(self
):
72 data
= {'id' : self
.id}
74 self
.fill_data_brok_from(data
, 'full_status')
75 b
= Brok('initial_'+my_type
+'_status', data
)
79 def fill_data_brok_from(self
, data
, brok_type
):
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
96 __slots__
= ('id', 'call', 'command', 'valid', 'args')
98 my_type
= 'CommandCall'
99 def __init__(self
, commands
, call
, poller_tag
=None):
100 self
.id = self
.__class
__.id
101 self
.__class
__.id += 1
103 tab
= call
.split('!')
104 self
.command
= tab
[0]
106 self
.command
= commands
.find_cmd_by_name(self
.command
.strip())
107 if self
.command
is not None:
111 self
.command
= tab
[0]
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
126 return str(self
.__dict
__)
133 class Commands(object):
134 def __init__(self
, commands
):
137 self
.commands
[c
.id] = c
141 return self
.commands
.itervalues()
146 for c
in self
.commands
.values():
151 def find_cmd_id_by_name(self
, name
):
152 for id in self
.commands
:
153 if getattr(self
.commands
[id], 'command_name', '') == name
:
157 def find_cmd_by_name(self
, name
):
158 id = self
.find_cmd_id_by_name(name
)
160 return self
.commands
[id]