#!/bin/bash
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

MK_VARDIR=/var/lib/check_mk_agent
export MK_VARDIR
TMPDIR=${TMPDIR:-/tmp}

help() {
    echo "Usage: mk-job JOB_NAME PROGRAM [ARGS...]"
    echo ""
    echo "Execute PROGRAM as subprocess while measuring performance information"
    echo "about the running process and writing it to an output file. This file"
    echo "can be monitored using Check_MK. The Check_MK Agent will forward the"
    echo "information of all job files to the monitoring server."
    echo ""
    echo "This file is being distributed with the Check_MK Agent."
}

if [ $# -lt 2 ]; then
    help >&2
    exit 1
fi

CURRENT_USER=$(whoami)
JOB_DIR="${MK_VARDIR}/job/${CURRENT_USER}"
JOB_NAME="${1}"
TMP_FILE="${TMPDIR}/${JOB_NAME}.$$"
RUNNING_FILE="${JOB_DIR}/${JOB_NAME}.$$running"
COMPLETED_FILE="${JOB_DIR}/${JOB_NAME}"

shift

if [ ! -d "${JOB_DIR}" ]; then
    if [ "${CURRENT_USER}" = root ]; then
        mkdir -p "${JOB_DIR}"
    else
        echo "ERROR: Missing output directory ${JOB_DIR} for non-root user '${CURRENT_USER}'." >&2
        exit 1
    fi
fi

if ! type "${1}" >/dev/null 2>&1; then
    echo -e "ERROR: Cannot run ${1}. Command not found.\n" >&2
    help >&2
    exit 1
fi

cleanup() {
    # shellcheck disable=SC2317 # shellcheck doesn't understand trap
    rm "${RUNNING_FILE}" 2>/dev/null
}

echo "start_time $(perl -e 'print time')" >"${TMP_FILE}" 2>/dev/null
cp "${TMP_FILE}" "${RUNNING_FILE}" 2>/dev/null

if [ ! -w "${RUNNING_FILE}" ]; then
    # Looks like we are lacking the permissions to create this file..
    # In this scenario no mk-job status file is created. We simply execute the command
    rm "${TMP_FILE}" 2>/dev/null
    exec "$@"
    exit $?
fi

trap "cleanup" 0

# /usr/bin/time has more comprehensive metrics under Linux.
# Under AIX/Solaris, only real, sys and reads are available.
/usr/bin/time -o "${TMP_FILE}" --append \
    -f "real %E\nuser %U\nsys %S\nreads %I\nwrites %O\nmax_res_kbytes %M\navg_mem_kbytes %K\ninvol_context_switches %c\nvol_context_switches %w" "$@"
RC=$?
echo "exit_code ${RC}" >>"${TMP_FILE}"

mv "${TMP_FILE}" "${COMPLETED_FILE}"
exit $RC
