/
command.py
168 lines (130 loc) · 5.11 KB
/
command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (C) 2009-2012:
# Gabes Jean, naparuba@gmail.com
# Gerhard Lausser, Gerhard.Lausser@consol.de
# Gregory Starck, g.starck@gmail.com
# Hartmut Goebel, h.goebel@goebel-consult.de
#
# This file is part of Shinken.
#
# Shinken is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Shinken is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Shinken. If not, see <http://www.gnu.org/licenses/>.
from item import Item, Items
from shinken.brok import Brok
from shinken.property import StringProp
from shinken.autoslots import AutoSlots
# Ok, slots are fun : you cannot set the __autoslots__
# on the same class you use, fun isn't it? So we define*
# a dummy useless class to get such :)
class DummyCommand(object):
pass
class Command(Item):
# AutoSlots create the __slots__ with properties and
# running_properties names
__metaclass__ = AutoSlots
id = 0
my_type = "command"
properties = Item.properties.copy()
properties.update({
'command_name': StringProp(fill_brok=['full_status']),
'command_line': StringProp(fill_brok=['full_status']),
'poller_tag': StringProp(default='None'),
'reactionner_tag': StringProp(default='None'),
'module_type': StringProp(default=None),
})
def __init__(self, params={}):
setattr(self, 'id', self.__class__.id)
#self.id = self.__class__.id
self.__class__.id += 1
self.init_running_properties()
self.customs = {}
for key in params:
# Manabe customs values
if key.startswith('_'):
self.customs[key.upper()] = params[key]
else:
setattr(self, key, params[key])
if not hasattr(self, 'poller_tag'):
self.poller_tag = 'None'
if not hasattr(self, 'reactionner_tag'):
self.reactionner_tag = 'None'
if not hasattr(self, 'module_type'):
# If the command start with a _, set the module_type
# as the name of the command, without the _
if getattr(self, 'command_line', '').startswith('_'):
module_type = getattr(self, 'command_line', '').split(' ')[0]
# and we remove the first _
self.module_type = module_type[1:]
# If no command starting with _, be fork :)
else:
self.module_type = 'fork'
def get_name(self):
return self.command_name
def pythonize(self):
self.command_name = self.command_name.strip()
def __str__(self):
return str(self.__dict__)
# Get a brok with initial status
def get_initial_status_brok(self):
cls = self.__class__
my_type = cls.my_type
data = {'id' : self.id}
self.fill_data_brok_from(data, 'full_status')
b = Brok('initial_'+my_type+'_status', data)
return b
def fill_data_brok_from(self, data, brok_type):
cls = self.__class__
# Now config properties
for prop, entry in cls.properties.items():
# Is this property intended for brokking?
# if 'fill_brok' in entry[prop]:
if brok_type in entry.fill_brok:
if hasattr(self, prop):
data[prop] = getattr(self, prop)
#elif 'default' in entry[prop]:
# data[prop] = entry.default
# Call by picle for dataify the coment
# because we DO NOT WANT REF in this pickleisation!
def __getstate__(self):
cls = self.__class__
# id is not in *_properties
res = {'id' : self.id}
for prop in cls.properties:
if hasattr(self, prop):
res[prop] = getattr(self, prop)
return res
# Inversed funtion of getstate
def __setstate__(self, state):
cls = self.__class__
# We move during 1.0 to a dict state
# but retention file from 0.8 was tuple
if isinstance(state, tuple):
self.__setstate_pre_1_0__(state)
return
self.id = state['id']
for prop in cls.properties:
if prop in state:
setattr(self, prop, state[prop])
# In 1.0 we move to a dict save. Before, it was
# a tuple save, like
# ({'id': 11}, {'poller_tag': 'None', 'reactionner_tag': 'None',
# 'command_line': u'/usr/local/nagios/bin/rss-multiuser',
# 'module_type': 'fork', 'command_name': u'notify-by-rss'})
def __setstate_pre_1_0__(self, state):
for d in state:
for k,v in d.items():
setattr(self, k, v)
class Commands(Items):
inner_class = Command
name_property = "command_name"