Refactoring: Changed all check parameters starting with an 'o' to the new rulespec...
[check_mk.git] / agents / mk-job
blobc53b0e04f8758d624ccd20b3a135e7bed129150d
1 #!/bin/bash
2 # mk-job: runs monitored jobs on Linux
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2014 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # tails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
27 export MK_VARDIR=/var/lib/check_mk_agent
29 help() {
30 echo "Usage: mk-job IDENT PROGRAM [ARGS...]"
31 echo ""
32 echo "Execute PROGRAM as subprocess while measuring performance information"
33 echo "about the running process and writing it to an output file. This file"
34 echo "can be monitored using Check_MK. The Check_MK Agent will forward the"
35 echo "information of all job files to the monitoring server."
36 echo ""
37 echo "This file is being distributed with the Check_MK Agent."
40 if [ $# -lt 2 ]; then
41 help >&2
42 exit 1
45 MYSELF=$(id -nu)
46 OUTPUT_PATH=$MK_VARDIR/job/$MYSELF
47 IDENT=$1
48 shift
50 if [ ! -d "$OUTPUT_PATH" ]; then
51 if [ "$MYSELF" = root ] ; then
52 mkdir -p "$OUTPUT_PATH"
53 else
54 echo "ERROR: Missing output directory $OUTPUT_PATH for non-root user '$MYSELF'." >&2
55 exit 1
59 if ! type $1 >/dev/null 2>&1; then
60 echo -e "ERROR: Cannot run $1. Command not found.\n" >&2
61 help >&2
62 exit 1
65 date +"start_time %s" > "$OUTPUT_PATH/$IDENT.running" 2>/dev/null
67 if [ ! -w "$OUTPUT_PATH/$IDENT.running" ] ; then
68 # Looks like we are lacking the permissions to create this file..
69 # In this scenario no mk-job status file is created. We simply execute the command
70 exec "$@"
74 /usr/bin/time -o "$OUTPUT_PATH/$IDENT.running" --append \
75 -f "exit_code %x\nreal_time %E\nuser_time %U\nsystem_time %S\nreads %I\nwrites %O\nmax_res_kbytes %M\navg_mem_kbytes %K\ninvol_context_switches %c\nvol_context_switches %w" "$@"
76 RC=$?
77 mv "$OUTPUT_PATH/$IDENT.running" "$OUTPUT_PATH/$IDENT"
78 exit $RC