history_tick.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """Write the periodic history row from the last complete snapshot.
  4. This runs every minute from cron, independently of the heavier full collection.
  5. The SQL guard in l2_snapshot.write_history() enforces the configured interval,
  6. so it only inserts when a new interval is due. Keeping this out of the
  7. refresh_alarms.sh full-lock gate is what guarantees a five-minute history even
  8. while a full collection is running.
  9. """
  10. import json
  11. import os
  12. import sys
  13. import time
  14. HERE = os.path.dirname(os.path.abspath(__file__))
  15. sys.path.insert(0, HERE)
  16. import l2_snapshot # noqa: E402
  17. def main():
  18. try:
  19. with open(l2_snapshot.OUT, encoding='utf-8') as f:
  20. snap = json.load(f)
  21. if not isinstance(snap, dict):
  22. return
  23. generated = int(snap.get('generated') or 0)
  24. # Do not snapshot a stale file if the full collector has stopped.
  25. if generated and time.time() - generated > 3600:
  26. return
  27. l2_snapshot.write_history(snap, int(time.time()))
  28. except Exception as e:
  29. print('history tick error:', e)
  30. if __name__ == '__main__':
  31. main()