-
Notifications
You must be signed in to change notification settings - Fork 2
/
cronologger
executable file
·115 lines (97 loc) · 4.23 KB
/
cronologger
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/bin/bash
# if cronologger fails, pass through the command so it still runs.
trap "$*" ERR
#########################################################################
# License: Apache
# Author: Vladimir Vuksan
# Version: 0.2
# Purpose: This script will archive cron or for that matter any output
# to a CouchDB document. There are two main drivers
# - Reduce the amount of cron e-mail spam
# - Have logs of cron jobs that can be correlated to adverse events
#########################################################################
#########################################################################
# WHAT YOU NEED TO CHANGE.
# Location of Cronologit CouchDB database
# The settings can be sourced from /etc/cronologger.conf or
# you could supply them as environmental variables e.g.
# export CRONOLOGGER_SERVER=bla.domain.com
# export CRONOLOGGER_DB=cronologger
# Otherwise it defaults to http://localhost:5984/cronologger
#########################################################################
[ -e /etc/cronologger.conf ] && . /etc/cronologger.conf
CRONOLOGGER_SERVER=${CRONOLOGGER_SERVER:=localhost}
CRONOLOGGER_DB=${CRONOLOGGER_DB:=cronologger}
URL_BASE="http://${CRONOLOGGER_SERVER}:5984/${CRONOLOGGER_DB}"
###################################################
# BELOW SHOULD LIKELY NOT BE CHANGED
###################################################
set -eu
STDOUT=`mktemp`
STDERR=`mktemp`
################################################################
# Record UNIX time when job started
################################################################
UNIX_TIME_START=`date "+%s"`
TIME_START=`date "+%Y-%m-%d %T"`
TIME_START_NOSPACES=`date "+%Y%m%d%H%M%S"`
TEMP_FILE=`mktemp`
TEMP_FILE2=`mktemp`
# Get the FQDN of the host we are running on
HOST=`hostname`
###############################################################
# Calculate command sum. This should make doing searches
# much easier. I thought about using MD5 but it
# seemed too long so I am using sum. Change if you wish
# by commenting the line below and uncommenting the line
# below it
###############################################################
SUM=`echo "$@" | sum | cut -f1 -d" "`
# SUM=`echo "$@" | md5sum -t | cut -f1 -d" "`
# Document URL. Key contains time, host and command sum
URL="$URL_BASE/${TIME_START_NOSPACES}_${HOST}_${SUM}"
###############################################################
# Generate Document JSON
# Since the job is in progress we'll set job_duration to -1
# We'll update it once the job finishes
###############################################################
cat <<EOF > $TEMP_FILE
{
"hostname": "${HOST}",
"username": "${USER}",
"command_line": "$@",
"unixtime": ${UNIX_TIME_START},
"job_duration": -1,
"time": "${TIME_START}",
"command_sum": "${SUM}",
}
EOF
# Create a document
REV=`curl -s -X PUT ${URL} -d @$TEMP_FILE | sed "s/.*rev//g" | awk -F\" '{ print $3}'`
# Get the resulting JSON since we want to change the job duration once the job_duration
# is finished.
curl -s -o $TEMP_FILE2 ${URL}
################################################################
# Run the command and redirect output
################################################################
trap - ERR # if we get to here we no longer need our handler.
set +e
"$@" > $STDOUT 2> $STDERR
# Capture command return code
RETURN_CODE=$?
set -e
###############################################################
# Calculate job duration
###############################################################
UNIX_TIME_END=`date "+%s"`
JOB_DURATION=$((UNIX_TIME_END-UNIX_TIME_START))
# Replace the job_duration in the JSON with the actual job duration. Also add the re
sed -i "s/\"job_duration\":-1/\"job_duration\":$JOB_DURATION, \"return_code\": $RETURN_CODE/g" $TEMP_FILE2
# Update the document to reflect the job duration
REV=`curl -s -X PUT ${URL} -d @$TEMP_FILE2 | sed "s/.*rev//g" | awk -F\" '{ print $3}'`
# Add the standard out attachment
REV=`curl -s -X PUT -H 'Content-Type: text/plain' --data-binary @$STDOUT "$URL/stdout?rev=${REV}"| sed "s/.*rev//g" | awk -F\" '{ print $3}'`
# Add the standard error attachment
REV=`curl -s -X PUT -H 'Content-Type: text/plain' --data-binary @$STDERR "$URL/stderr?rev=${REV}"| sed "s/.*rev//g" | awk -F\" '{ print $3}'`
# Clean up
rm -f "$STDOUT" "$STDERR" "$TEMP_FILE" "$TEMP_FILE2"