#!/usr/bin/env python3
#
#   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 2 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 yaml
import json
import os


possible_state_files = [
    "/opt/puppetlabs/puppet/cache/state/last_run_summary.yaml",
    "/var/lib/puppet/state/last_run_summary.yaml"
]

possible_disable_check_files = [
    "/opt/puppetlabs/puppet/cache/state/agent_disabled.lock",
    "/var/lib/puppet/state/agent_disabled.lock"
]

dict_state = {}
for state_file in possible_state_files:
    if os.path.exists(state_file):
        try:
            with open(state_file, 'r') as stream:
                dict_state = yaml.load(stream)
        except Exception as e:
            dict_state["state_file"] = "ERROR: unable to load : %s / %s " % (str(e), state_file)
        break


dict_state["disabled"] = "NO"

for disable_file in possible_disable_check_files:
    if os.path.exists(disable_file):
        try:
            with open(disable_file) as json_data:
                dict_state["disabled"] = "YES: " + json.load(json_data)["disabled_message"]
        except Exception as e:
            dict_state["disabled"] = "ERROR: unable to load %s / %s " % (str(e), disable_file)
            pass
        break

print(json.dumps(dict_state, ensure_ascii=False, indent=4))
