scripts/run-cron-zabbix
author Tomas Zeman <tomas@functionals.cz>
Tue, 21 Feb 2023 13:03:45 +0100
changeset 40 8c421321123d
permissions -rwxr-xr-x
run-cron-zabbix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     1
#!/bin/sh
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     2
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     3
# Generic run-cron script with output to be piped to zabbix_sender(1)
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     4
# Invocation:
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     5
#	run-cron name zabbix-prefix prog
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     6
#
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     7
#	where name is run name (logs, status etc.)
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     8
#	zabbix-prefix is zabbix key prefix
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
     9
#	and prog is the script which will be invoked.
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    10
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    11
name=$1
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    12
zbx=$2
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    13
shift
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    14
shift
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    15
script="$@"
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    16
start=$(date +%s)
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    17
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    18
logdir=`dirname $name`
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    19
mkdir -p $logdir
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    20
[ $? -eq 0 ] || exit 1
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    21
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    22
l=$name-`date +%FT%T`
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    23
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    24
$script >$l.out 2>$l.err
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    25
status=$?
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    26
echo $status > $l.status
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    27
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    28
eta=$(expr $(date +%s) - $start)
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    29
out_sz=$(stat -f %z $l.out)
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    30
err_sz=$(stat -f %z $l.err)
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    31
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    32
[ -L $name-last.out ] && mv $name-last.out $name-prev.out
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    33
[ -L $name-last.err ] && mv $name-last.err $name-prev.err
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    34
[ -L $name-last.status ] && mv $name-last.status $name-prev.status
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    35
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    36
ln -sf $l.out $name-last.out
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    37
ln -sf $l.err $name-last.err
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    38
ln -sf $l.status $name-last.status
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    39
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    40
cat <<EOF
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    41
- $zbx.eta $eta
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    42
- $zbx.status $status
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    43
- $zbx.stdout $out_sz
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    44
- $zbx.stderr $err_sz
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    45
EOF
8c421321123d run-cron-zabbix
Tomas Zeman <tomas@functionals.cz>
parents:
diff changeset
    46