#! /usr/bin/env python

# Copyright (C) 2016 Christopher M. Biwer
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

import argparse
import json

# command line usage
parser = argparse.ArgumentParser(usage="pycbc_nagios_daily_monitor [--options]",
                  description="Updates daily CBC offline search nagios JSON file.")

# command line options
parser.add_argument("--gps-time", type=int,
                  help="GPS time to time stamp the output JSON file.")
parser.add_argument("--output-file", type=str,
                  help="Path to output JSON file.")
parser.add_argument("--update-workflow-generator", action="store_true",
                  help="Updates workflow generator monitor.")
parser.add_argument("--update-transfer-files", action="store_true",
                  help="Updates file transfer monitor.")

# parse command line
opts = parser.parse_args()

# create a dict that will be fed to the json module for parsing
json_dict = {}

# add current time
json_dict["created_gps"] = opts.gps_time

# add a list that will define the status of the monitor
json_dict["status_intervals"] = []

# add information about the author
json_dict["author"] = {
    "name" : "Ligo Virgo Collaboration - PyCBC team",
    "email" : "cmbiwer@gmail.com",
}

# add the statuses for the workflow generator monitor
if opts.update_workflow_generator:

    # add a status
    status_dict = {
        "start_sec" : 0,
        "end_sec" : 90000,
        "txt_status" : "The daily CBC workflow generator has run within the past 25 hours.",
        "num_status" : 0,
    }
    json_dict["status_intervals"].append(status_dict)

    # add a status
    status_dict = {
        "start_sec" : 90000,
        "txt_status" : "The daily CBC workflow generator has not run within the past 25 hours.",
        "num_status" : 2,
    }
    json_dict["status_intervals"].append(status_dict)

    # define text for each state
    json_dict["ok_txt"] = ""
    json_dict["warning_txt"] = ""
    json_dict["unknown_txt"] = ""
    json_dict["critical_txt"] = ""

# add the statuses for transferring files to the sites montior
elif opts.update_transfer_files:

    # add a status
    status_dict = {
        "start_sec" : 0,
        "end_sec" : 86400,
        "txt_status" : "The daily CBC workflow generator has transferred files to the sites within the past 24 hours.",
        "num_status" : 0,
    }
    json_dict["status_intervals"].append(status_dict)

    # add a status
    status_dict = {
        "start_sec" : 86400,
        "end_sec" : 172800,
        "txt_status" : "The daily CBC workflow generator has transferred files to the sites within the past 48 hours.",
        "num_status" : 3,
    }
    json_dict["status_intervals"].append(status_dict)

    # add a status
    status_dict = {
        "start_sec" : 172800,
        "txt_status" : "The daily CBC workflow generator has not transferred files to the sites within the past 48 hours.",
        "num_status" : 2,
    }
    json_dict["status_intervals"].append(status_dict)

    # define text for each state
    json_dict["ok_txt"] = ""
    json_dict["warning_txt"] = ""
    json_dict["unknown_txt"] = ""
    json_dict["critical_txt"] = ""

# write output JSON file
with open(opts.output_file, "w") as fp:
    fp.write(json.dumps(json_dict))
