Add : the full demoCA certification autority so people can sign their own keys.
[shinken.git] / shinken / check.py
blob6dd991ebcdeabad907e72e7cd0e89bb013a24d94
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/>.
22 from shinken.action import Action
25 class Check(Action):
26 __slots__ = ('id', 'is_a', 'type', '_in_timeout', 'status', 'exit_status',\
27 '_command', 'output', 'long_output', 'ref', 'ref_type', \
28 't_to_go', 'depend_on', 'depend_on_me', 'check_time', \
29 'execution_time', 'env')
31 properties={'is_a' : {'required': False, 'default':'check'},
32 'type' : {'required': False, 'default': ''},
33 '_in_timeout' : {'required': False, 'default': False},
34 'status' : {'required': False, 'default':''},
35 'exit_status' : {'required': False, 'default':3},
36 'state' : {'required': False, 'default':0},
37 'output' : {'required': False, 'default':''},
38 'long_output' : {'required': False, 'default':''},
39 'ref' : {'required': False, 'default': -1},
40 #'ref_type' : {'required': False, 'default':''},
41 't_to_go' : {'required': False, 'default': 0},
42 'depend_on' : {'required': False, 'default': []},
43 'dep_check' : {'required': False, 'default': []},
44 'check_time' : {'required': False, 'default': 0},
45 'execution_time' : {'required': False, 'default': 0},
46 'perf_data' : {'required': False, 'default':''},
47 'poller_tag' : {'required': False, 'default': None},
48 'env' : {'required' : False, 'default' : {}},
49 'internal' : {'required': False, 'default':False},
52 #id = 0 #Is common to Actions
53 def __init__(self, status, command, ref, t_to_go, dep_check=None, id=None, timeout=10, poller_tag=None, env={}):
54 self.is_a = 'check'
55 self.type = ''
56 if id == None: #id != None is for copy call only
57 self.id = Action.id
58 Action.id += 1
59 self._in_timeout = False
60 self.timeout = timeout
61 self.status = status
62 self.exit_status = 3
63 self.command = command
64 self.output = ''
65 self.long_output = ''
66 self.ref = ref
67 #self.ref_type = ref_type
68 self.t_to_go = t_to_go
69 self.depend_on = []
70 if dep_check is None:
71 self.depend_on_me = []
72 else:
73 self.depend_on_me = [dep_check]
74 self.check_time = 0
75 self.execution_time = 0
76 self.perf_data = ''
77 self.poller_tag = poller_tag
78 self.env = env
79 # If it's a business rule, manage it as a special check
80 if ref and ref.got_business_rule or command.startswith('_internal'):
81 self.internal = True
82 else:
83 self.internal = False
86 #return a copy of the check but just what is important for execution
87 #So we remove the ref and all
88 def copy_shell(self):
89 #We create a dummy check with nothing in it, jsut defaults values
90 new_c = Check('', '', '', '', '', id=self.id)
91 only_copy_prop = ['id', 'status', 'command', 't_to_go', 'timeout', 'env']
92 for prop in only_copy_prop:
93 val = getattr(self, prop)
94 setattr(new_c, prop, val)
95 return new_c
98 def get_return_from(self, c):
99 self.exit_status = c.exit_status
100 self.output = c.output
101 self.long_output = c.long_output
102 self.check_time = c.check_time
103 self.execution_time = c.execution_time
104 self.perf_data = c.perf_data
107 # def get_outputs(self, out):
108 # elts = out.split('\n')
109 # #For perf data
110 # elts_line1 = elts[0].split('|')
111 # #First line before | is output
112 # self.output = elts_line1[0]
113 # #After | is perfdata
114 # if len(elts_line1) > 1:
115 # self.perf_data = elts_line1[1]
116 # #The others lines are long_output
117 # if len(elts) > 1:
118 # self.long_output = '\n'.join(elts[1:])
121 # def execute(self):
122 # if os.name == 'nt':
123 # self.execute_windows()
124 # else:
125 # self.execute_unix()
128 # def execute_windows(self):
129 # """call shell-command and either return its output or kill it
130 # if it doesn't normally exit within timeout seconds and return None"""
131 # timeout = 5
132 # self.status = 'launched'
133 # self.check_time = time.time()
134 # start = datetime.datetime.now()
135 # try:
136 # process = subprocess.Popen(self.command.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
137 # except WindowsError:
138 # print "On le kill"
139 # self.status = 'timeout'
140 # self.execution_time = time.time() - self.check_time
141 # return
142 # while process.poll() is None:
143 # time.sleep(0.01)
144 # now = datetime.datetime.now()
145 # if (now - start).seconds> timeout:
146 # TerminateProcess(int(process._handle), -1)
147 # print "On le kill"
148 # self.status = 'timeout'
149 # self.execution_time = time.time() - self.check_time
150 # return
151 # self.get_outputs(process.stdout.read())
152 # self.exit_status = process.returncode
153 # print "Output:", self.output, self.long_output, "exit status", self.exit_status
154 # self.status = 'done'
155 # self.execution_time = time.time() - self.check_time
158 # def execute_unix(self):
159 # #print "Launching command", self.command
160 # child = spawn ('/bin/sh -c "%s"' % self.command)
161 # self.status = 'launched'
162 # self.check_time = time.time()
164 # try:
165 # child.expect_exact(EOF, timeout=5)
166 # self.get_outputs(child.before)
167 # child.terminate(force=True)
168 # self.exit_status = child.exitstatus
169 # self.status = 'done'
170 # except TIMEOUT:
171 # print "On le kill"
172 # self.status = 'timeout'
173 # child.terminate(force=True)
174 # self.execution_time = time.time() - self.check_time
177 def is_launchable(self, t):
178 return t > self.t_to_go
181 def set_status(self, status):
182 self.status = status
185 def get_status(self):
186 return self.status
189 def get_output(self):
190 return self.output
193 def __str__(self):
194 return "Check %d status:%s command:%s ref:%s" % (self.id, self.status, self.command, self.ref)
197 def get_id(self):
198 return self.id