#!/usr/bin/env python

import time
import subprocess

import isi.gconfig as gconfig
import isi.app.lib.cluster as cluster
import isi.celog

def main():
    tree = gconfig.Tree("remote-support")
    base = gconfig.open(tree)
    ctx = base.ctx_new()
    connectemc_cfg = ctx.read("connectemc")
    if not connectemc_cfg["enabled"]:
        # Nothing to do
        sys.exit(0)

    # Generate a wait time based on the lnn to stagger
    # the alerts.  Mod by 150 just in case someone
    # sets a silly lnn
    c = cluster.Cluster()
    wait_time = c.devid_to_lnn(c.local_devid()) % 150

    time.sleep(wait_time * 60)
    # Oddly enough, this is the cleanest interface to send an alert
    subprocess.call(["/usr/bin/isi_celog/celog_send_events.py", "-l", "info",
            "-m", "Weekly ESRS Health Alert", "400090001"])

if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        syslog.syslog(syslog.LOG_ERR,
                "Failed isi_weekly_esrs_health_alert %s" %
                (traceback.format_exc(),))
        sys.exit(1)
