2020-01-14 21:59:46 +01:00
|
|
|
import os
|
|
|
|
import time
|
2017-10-05 19:34:33 +02:00
|
|
|
from datetime import timedelta
|
2020-01-14 21:59:46 +01:00
|
|
|
from typing import Any, Dict
|
2017-10-05 19:34:33 +02:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
|
|
|
from analytics.lib.counts import COUNT_STATS, CountStat
|
2020-01-14 21:59:46 +01:00
|
|
|
from analytics.models import installation_epoch, last_successful_fill
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.timestamp import TimezoneNotUTCException, floor_to_day, floor_to_hour, verify_UTC
|
2017-10-05 19:34:33 +02:00
|
|
|
from zerver.models import Realm
|
|
|
|
|
|
|
|
states = {
|
|
|
|
0: "OK",
|
|
|
|
1: "WARNING",
|
|
|
|
2: "CRITICAL",
|
python: Use trailing commas consistently.
Automatically generated by the following script, based on the output
of lint with flake8-comma:
import re
import sys
last_filename = None
last_row = None
lines = []
for msg in sys.stdin:
m = re.match(
r"\x1b\[35mflake8 \|\x1b\[0m \x1b\[1;31m(.+):(\d+):(\d+): (\w+)", msg
)
if m:
filename, row_str, col_str, err = m.groups()
row, col = int(row_str), int(col_str)
if filename == last_filename:
assert last_row != row
else:
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
with open(filename) as f:
lines = f.readlines()
last_filename = filename
last_row = row
line = lines[row - 1]
if err in ["C812", "C815"]:
lines[row - 1] = line[: col - 1] + "," + line[col - 1 :]
elif err in ["C819"]:
assert line[col - 2] == ","
lines[row - 1] = line[: col - 2] + line[col - 1 :].lstrip(" ")
if last_filename is not None:
with open(last_filename, "w") as f:
f.writelines(lines)
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-10 05:23:40 +02:00
|
|
|
3: "UNKNOWN",
|
2017-10-05 19:34:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Checks FillState table.
|
|
|
|
|
|
|
|
Run as a cron job that runs every hour."""
|
|
|
|
|
2018-03-12 02:47:49 +01:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-10-05 19:34:33 +02:00
|
|
|
fill_state = self.get_fill_state()
|
|
|
|
status = fill_state['status']
|
|
|
|
message = fill_state['message']
|
|
|
|
|
|
|
|
state_file_path = "/var/lib/nagios_state/check-analytics-state"
|
|
|
|
state_file_tmp = state_file_path + "-tmp"
|
|
|
|
|
|
|
|
with open(state_file_tmp, "w") as f:
|
2020-06-10 06:41:04 +02:00
|
|
|
f.write(f"{int(time.time())}|{status}|{states[status]}|{message}\n")
|
2018-07-18 23:50:15 +02:00
|
|
|
os.rename(state_file_tmp, state_file_path)
|
2017-10-05 19:34:33 +02:00
|
|
|
|
2018-03-12 02:47:49 +01:00
|
|
|
def get_fill_state(self) -> Dict[str, Any]:
|
2017-10-05 19:34:33 +02:00
|
|
|
if not Realm.objects.exists():
|
|
|
|
return {'status': 0, 'message': 'No realms exist, so not checking FillState.'}
|
|
|
|
|
|
|
|
warning_unfilled_properties = []
|
|
|
|
critical_unfilled_properties = []
|
|
|
|
for property, stat in COUNT_STATS.items():
|
|
|
|
last_fill = last_successful_fill(property)
|
|
|
|
if last_fill is None:
|
|
|
|
last_fill = installation_epoch()
|
|
|
|
try:
|
|
|
|
verify_UTC(last_fill)
|
|
|
|
except TimezoneNotUTCException:
|
2020-06-10 06:41:04 +02:00
|
|
|
return {'status': 2, 'message': f'FillState not in UTC for {property}'}
|
2017-10-05 19:34:33 +02:00
|
|
|
|
|
|
|
if stat.frequency == CountStat.DAY:
|
|
|
|
floor_function = floor_to_day
|
|
|
|
warning_threshold = timedelta(hours=26)
|
|
|
|
critical_threshold = timedelta(hours=50)
|
|
|
|
else: # CountStat.HOUR
|
|
|
|
floor_function = floor_to_hour
|
|
|
|
warning_threshold = timedelta(minutes=90)
|
|
|
|
critical_threshold = timedelta(minutes=150)
|
|
|
|
|
|
|
|
if floor_function(last_fill) != last_fill:
|
2020-06-14 02:57:50 +02:00
|
|
|
return {'status': 2, 'message': f'FillState not on {stat.frequency} boundary for {property}'}
|
2017-10-05 19:34:33 +02:00
|
|
|
|
|
|
|
time_to_last_fill = timezone_now() - last_fill
|
|
|
|
if time_to_last_fill > critical_threshold:
|
|
|
|
critical_unfilled_properties.append(property)
|
|
|
|
elif time_to_last_fill > warning_threshold:
|
|
|
|
warning_unfilled_properties.append(property)
|
|
|
|
|
|
|
|
if len(critical_unfilled_properties) == 0 and len(warning_unfilled_properties) == 0:
|
|
|
|
return {'status': 0, 'message': 'FillState looks fine.'}
|
|
|
|
if len(critical_unfilled_properties) == 0:
|
2020-06-14 02:57:50 +02:00
|
|
|
return {
|
|
|
|
'status': 1,
|
|
|
|
'message': 'Missed filling {} once.'.format(
|
|
|
|
', '.join(warning_unfilled_properties),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
'status': 2,
|
|
|
|
'message': 'Missed filling {} once. Missed filling {} at least twice.'.format(
|
|
|
|
', '.join(warning_unfilled_properties),
|
|
|
|
', '.join(critical_unfilled_properties),
|
|
|
|
),
|
|
|
|
}
|