66
66
import re
67
67
import random
68
68
import time
69
- import datetime
69
+ from datetime import datetime
70
70
import traceback
71
71
import logging
72
72
@@ -1167,7 +1167,7 @@ def schedule(self, hosts, services, timeperiods, macromodulations, checkmodulati
1167
1167
:type force_time: None | int
1168
1168
:return: None
1169
1169
"""
1170
- # next_chk il already set, do not change
1170
+ # next_chk is already set, do not change
1171
1171
# unless we force the check or the time
1172
1172
if self .in_checking and not (force or force_time ):
1173
1173
return None
@@ -1185,8 +1185,8 @@ def schedule(self, hosts, services, timeperiods, macromodulations, checkmodulati
1185
1185
if self .check_interval == 0 and not force :
1186
1186
if cls .my_type == 'service' :
1187
1187
return None
1188
- else : # host
1189
- self .check_interval = 300 / cls .interval_length
1188
+
1189
+ self .check_interval = 300 / cls .interval_length
1190
1190
1191
1191
# Interval change is in a HARD state or not
1192
1192
# If the retry is 0, take the normal value
@@ -1252,8 +1252,13 @@ def schedule(self, hosts, services, timeperiods, macromodulations, checkmodulati
1252
1252
1253
1253
logger .debug ("-> schedule: %s / %s (interval: %d, added: %d)" ,
1254
1254
self .get_full_name (),
1255
- datetime .datetime . fromtimestamp (self .next_chk ).strftime ('%Y-%m-%d %H:%M:%S' ),
1255
+ datetime .utcfromtimestamp (self .next_chk ).strftime ('%Y-%m-%d %H:%M:%S' ),
1256
1256
interval , time_add )
1257
+ if os .getenv ('ALIGNAK_LOG_CHECKS' , None ):
1258
+ logger .info ("--ALC-- -> next check for %s at %s (interval: %d, added: %d)" ,
1259
+ self .get_full_name (),
1260
+ datetime .utcfromtimestamp (self .next_chk ).strftime ('%Y-%m-%d %H:%M:%S' ),
1261
+ interval , time_add )
1257
1262
# Get the command to launch, and put it in queue
1258
1263
return self .launch_check (self .next_chk , hosts , services , timeperiods , macromodulations ,
1259
1264
checkmodulations , checks , force = force )
@@ -1586,27 +1591,10 @@ def consume_result(self, chk, notification_period, hosts,
1586
1591
logger .info ("Got check result: %d for %s" ,
1587
1592
chk .exit_status , self .get_full_name ())
1588
1593
1589
- # # Protect against bad type output
1590
- # try:
1591
- # # if str, convert to unicode
1592
- # if isinstance(chk.output, str):
1593
- # chk.output = chk.output.decode('utf8', 'ignore')
1594
- # chk.long_output = chk.long_output.decode('utf8', 'ignore')
1595
- # except AttributeError:
1596
- # # Python 3 raises an exception!
1597
- # pass
1598
- #
1599
- # try:
1600
- # if isinstance(chk.perf_data, str):
1601
- # chk.perf_data = chk.perf_data.decode('utf8', 'ignore')
1602
- # except AttributeError:
1603
- # # Python 3 raises an exception!
1604
- # pass
1605
-
1606
1594
if os .getenv ('ALIGNAK_LOG_CHECKS' , None ):
1607
1595
level = ['info' , 'warning' , 'error' , 'critical' ][min (chk .exit_status , 3 )]
1608
1596
func = getattr (logger , level )
1609
- func ("Check result for '%s' , exit: %d, output: %s" ,
1597
+ func ("--ALC-- check result for %s , exit: %d, output: %s" ,
1610
1598
self .get_full_name (), chk .exit_status , chk .output )
1611
1599
1612
1600
# ============ MANAGE THE CHECK ============ #
@@ -1928,7 +1916,7 @@ def update_event_and_problem_id(self):
1928
1916
SchedulingItem .current_problem_id += 1
1929
1917
self .last_problem_id = self .current_problem_id
1930
1918
self .current_problem_id = SchedulingItem .current_problem_id
1931
- elif self .state != ok_up and self .last_state != ok_up :
1919
+ elif ok_up not in ( self .state , self .last_state ) :
1932
1920
# State transitions between non-OK states
1933
1921
# (e.g. WARNING to CRITICAL) do not cause
1934
1922
# this problem id to increase.
@@ -2718,6 +2706,9 @@ def manage_internal_check(self, hosts, services, check, hostgroups, servicegroup
2718
2706
:return: None
2719
2707
"""
2720
2708
logger .debug ("Internal check: %s - %s" , self .get_full_name (), check .command )
2709
+ if os .getenv ('ALIGNAK_LOG_CHECKS' , None ):
2710
+ logger .info ("--ALC-- Internal check: %s - %s" , self .get_full_name (), check .command )
2711
+ # Business rule
2721
2712
if check .command .startswith ('bp_' ):
2722
2713
try :
2723
2714
# Re evaluate the business rule to take into account macro
@@ -2744,6 +2735,7 @@ def manage_internal_check(self, hosts, services, check, hostgroups, servicegroup
2744
2735
logger .debug ("[%s] Error while re-evaluating business rule:\n %s" ,
2745
2736
self .get_name (), traceback .format_exc ())
2746
2737
state = 3
2738
+
2747
2739
# _internal_host_up is for putting host as UP
2748
2740
elif check .command == '_internal_host_up' :
2749
2741
state = 0
@@ -2777,14 +2769,21 @@ def manage_internal_check(self, hosts, services, check, hostgroups, servicegroup
2777
2769
state = 3
2778
2770
check .output = u'Malformed host internal check'
2779
2771
else :
2780
- state = 3
2772
+ state = check_result [1 ].strip ()
2773
+ # If multiple possible states - choose a random one
2774
+ if ',' in check_result [1 ]:
2775
+ states = check_result [1 ].split (',' )
2776
+ state = states [random .randint (0 , len (states ) - 1 )]
2777
+
2781
2778
try :
2782
- state = int (check_result [ 1 ] )
2779
+ state = int (state )
2783
2780
except ValueError :
2784
2781
pass
2785
2782
check .output = u'Host internal check result: %d' % state
2786
- if len (check_result ) > 2 :
2783
+ if len (check_result ) > 2 and check_result [ 2 ] :
2787
2784
check .output = check_result [2 ]
2785
+ if '%d' in check .output :
2786
+ check .output = check .output % state
2788
2787
2789
2788
check .execution_time = 0
2790
2789
if 'ALIGNAK_LOG_ACTIONS' in os .environ :
@@ -2804,14 +2803,25 @@ def manage_internal_check(self, hosts, services, check, hostgroups, servicegroup
2804
2803
state = 3
2805
2804
check .output = u'Malformed service internal check'
2806
2805
else :
2807
- state = 3
2806
+ state = check_result [1 ].strip ()
2807
+ # If multiple possible states - choose a random one
2808
+ if ',' in check_result [1 ]:
2809
+ states = check_result [1 ].split (',' )
2810
+ state = states [random .randint (0 , len (states ) - 1 )]
2811
+
2812
+ # In SOFT state type, do not change current state - let the new state go to HARD
2813
+ if self .state_type == 'SOFT' :
2814
+ state = self .state_id
2815
+
2808
2816
try :
2809
- state = int (check_result [ 1 ] )
2817
+ state = int (state )
2810
2818
except ValueError :
2811
2819
pass
2812
2820
check .output = u'Service internal check result: %d' % state
2813
- if len (check_result ) > 1 :
2814
- check .output = check_result [1 ]
2821
+ if len (check_result ) > 2 and check_result [2 ]:
2822
+ check .output = check_result [2 ]
2823
+ if '%d' in check .output :
2824
+ check .output = check .output % state
2815
2825
2816
2826
check .execution_time = 0
2817
2827
if 'ALIGNAK_LOG_ACTIONS' in os .environ :
@@ -3105,14 +3115,14 @@ def unset_impact_state(self):
3105
3115
self .state = self .state_before_impact
3106
3116
self .state_id = self .state_id_before_impact
3107
3117
3108
- def last_time_non_ok_or_up (self ): # pragma: no cover, base function
3109
- """Get the last time the item was in a non-OK state
3110
-
3111
- :return: return 0
3112
- :rtype: int
3113
- """
3114
- pass
3115
-
3118
+ # def last_time_non_ok_or_up(self): # pragma: no cover, base function
3119
+ # """Get the last time the item was in a non-OK state
3120
+ #
3121
+ # :return: return 0
3122
+ # :rtype: int
3123
+ # """
3124
+ # pass
3125
+ #
3116
3126
def set_unreachable (self ):
3117
3127
"""Set unreachable: all our parents (dependencies) are not ok
3118
3128
Unreachable is different from down/critical
0 commit comments