#!/usr/bin/ksh
# Copyright (C) 2019 tribe29 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.

# Reason for this no-op: shellcheck disable=... before the first command disables the error for the
# entire script.
:

# Disable unused variable error (needed to keep track of version)
# shellcheck disable=SC2034
CMK_VERSION="2.1.0p23"

# Agent for Linux/UNIX for Tivoli Storage Manager (TSM)

# Configuration is needed for username and password for dsmadmc
# You need to create a configuration file /etc/check_mk/tsm.cfg
# with the following two lines:
# TSM_USER=foo
# TSM_PASSWORD=bar
# If you have more than once instance, make sure that the upper
# login works on all of them.

# shellcheck source=agents/cfg_examples/tsm.cfg
. "$MK_CONFDIR/tsm.cfg" || exit 1

if [ -z "${TSM_USER}" ] || [ -z "${TSM_PASSWORD}" ]; then
    echo "Please set TSM_USER and TSM_PASSWORD in ${MK_CONFDIR}/tsm.cfg" >&2
    exit 1
fi

do_tsm_checks() {
    INST=${DSMSERV_DIR##*/}

    # If we have no instance name, we take 'default'
    if [ -z "$INST" ]; then INST=default; fi

    dsmcmd="dsmadmc -id=$TSM_USER -pass=$TSM_PASSWORD -dataonly=yes -tab"

    # Staging Pools
    echo '<<<tsm_stagingpools:sep(9)>>>'
    $dsmcmd <<EOF | sed -n "/^FOO/s//$INST/p"
select 'FOO',stgpool_name,pct_utilized from volumes where access='READWRITE' and devclass_name<>'DISK'
EOF

    # Drive Status
    echo '<<<tsm_drives:sep(9)>>>'
    $dsmcmd <<EOF | sed -n "/^FOO/s//$INST/p"
SELECT 'FOO', library_name, drive_name, drive_state, online, drive_serial FROM drives
EOF

    # Occupancy
    echo '<<<tsm_storagepools:sep(9)>>>'
    $dsmcmd <<EOF | sed -n "/^FOO/s//$INST/p"
SELECT 'FOO' as foo,COALESCE(type,'Unknown') as type,stgpools.stgpool_name, sum(coalesce(logical_mb,0)) as SUM_LOGICAL_MB from occupancy full outer join stgpools on occupancy.stgpool_name=stgpools.stgpool_name group by stgpools.stgpool_name,type
EOF

}

# Find in the list of processes TSM daemons. Example output of 'ps xewwg'
# 8781984      - A    127:26 dsmserv _=/usr/bin/dsmserv LANG=en_US LOGIN=root PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:/usr/bin/X11:/sbin:/usr/java5/jre/bin:/usr/java5/bin LC_ALL=en_US DSMSERV_CONFIG=/foobar_17g/dsmserv.opt LC__FASTMSG=true LOGNAME=root MAIL=/var/spool/mail/root LOCPATH=/usr/lib/nls/loc DSMSERV_DIR=/foobar_17g USER=root AUTHSTATE=compat AIXTHREAD_MNRATIO=1:1 SHELL=/usr/bin/ksh ODMDIR=/etc/objrepos HOME=/ SSH_CLIENT=192.168.21.199 37725 22 SSH_CONNECTION=192.168.21.199 37725 192.168.21.214 22 PWD=/foobar_17g TZ=Europe/Bucharest AIXTHREAD_SCOPE=S DSMSERV_ACCOUNTING_DIR=/foobar_17g/acc NLSPATH=/usr/lib/nls/msg/%L/%N:/usr/lib/nls/msg/%L/%N.cat LIBPATH=/usr/local/Centera_SDK/lib/64/

# SC2162: read without -r will mangle backslashes.
# We suppress it here for compatibility (curretly backslashes e.g. before spaces are dropped).
# Since escaping of field seperators is not relevant when reading into one variable, we probably
# would have wanted "read -r".
# shellcheck disable=SC2162
ps xewwg | sed -n '/ dsmserv .* DSMSERV_CONFIG=/s/^.* dsmserv //p' |
    while read line; do
        # Take over all relevant environment into our own
        eval "$(echo "$line" | tr ' ' '\n' | sed -n '/^DSMSERV_/s/^/export /p')"
        do_tsm_checks
    done
