/shinken/objects/resultmodulation.py

https://github.com/sduchesneau/shinken · Python · 101 lines · 41 code · 24 blank · 36 comment · 10 complexity · beda3447aa26b40014c8ccca0cc5cfbe MD5 · raw file

  1. #!/usr/bin/env python
  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
  7. #
  8. #This file is part of Shinken.
  9. #
  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.
  14. #
  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.
  19. #
  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/>.
  22. #The resultmodulation class is used for in scheduler modulation of resulsts
  23. #like the return code or the output.
  24. import time
  25. from item import Item, Items
  26. from shinken.property import StringProp, ListProp
  27. class Resultmodulation(Item):
  28. id = 1#0 is always special in database, so we do not take risk here
  29. my_type = 'resultmodulation'
  30. properties = Item.properties.copy()
  31. properties.update({
  32. 'resultmodulation_name': StringProp(),
  33. 'exit_codes_match': ListProp (default=''),
  34. 'exit_code_modulation': StringProp(default=None),
  35. 'modulation_period': StringProp(default=None),
  36. })
  37. # For debugging purpose only (nice name)
  38. def get_name(self):
  39. return self.resultmodulation_name
  40. # Make the return code modulation if need
  41. def module_return(self, return_code):
  42. #Only if in modulation_period of modulation_period == None
  43. if self.modulation_period is None or self.modulation_period.is_time_valid(time.time()):
  44. #Try to change the exit code only if a new one is defined
  45. if self.exit_code_modulation is not None:
  46. #First with the exit_code_match
  47. if return_code in self.exit_codes_match:
  48. return_code = self.exit_code_modulation
  49. return return_code
  50. # We override the pythonize because we have special cases that we do not want
  51. # to be do at running
  52. def pythonize(self):
  53. # First apply Item pythonize
  54. super(self.__class__, self).pythonize()
  55. # Then very special cases
  56. # Intify the exit_codes_match, and make list
  57. self.exit_codes_match = [ int(ec) for ec in getattr(self, 'exit_codes_match', []) ]
  58. if hasattr(self, 'exit_code_modulation'):
  59. self.exit_code_modulation = int(self.exit_code_modulation)
  60. else:
  61. self.exit_code_modulation = None
  62. class Resultmodulations(Items):
  63. name_property = "resultmodulation_name"
  64. inner_class = Resultmodulation
  65. def linkify(self, timeperiods):
  66. self.linkify_rm_by_tp(timeperiods)
  67. # We just search for each timeperiod the tp
  68. # and replace the name by the tp
  69. def linkify_rm_by_tp(self, timeperiods):
  70. for rm in self:
  71. mtp_name = rm.modulation_period.strip()
  72. # The new member list, in id
  73. mtp = timeperiods.find_by_name(mtp_name)
  74. if mtp_name != '' and mtp is None:
  75. err = "Error : the result modulation '%s' got an unknown modulation_period '%s'" % (rm.get_name(), mtp_name)
  76. rm.configuration_errors.append(err)
  77. rm.modulation_period = mtp