File: functions

package info (click to toggle)
rrdtool 1.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,772 kB
  • sloc: ansic: 39,371; sh: 1,810; perl: 1,268; cs: 652; makefile: 573; python: 169; ruby: 61; awk: 30
file content (281 lines) | stat: -rw-r--r-- 7,868 bytes parent folder | download
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
set -x
TZ=Europe/Zurich
export TZ

# Use dot as decimal separator, which is required for some tests to pass
LC_NUMERIC=C
export LC_NUMERIC

BASEDIR="${BASEDIR:-$(dirname -- $0)}"
BASEDIR="$(readlink -f -- $BASEDIR)"

BUILDDIR="${BUILDDIR:-${BASEDIR}}"
TOP_BUILDDIR="${TOP_BUILDDIR:-${BASEDIR}/..}"

DIFF="diff -u"
DIFF9=seddif

# On Windows, names of times zones are different. Substitute 'W. Europe Standard Time' with 'CET'
function seddif {
   perl -p -e 's/([-+]?\d\.\d+e[-+]\d+)/sprintf("%0.7e",$1)/ge; s/W. Europe Standard Time/CET/g' | $DIFF $@
}

BLANK=blank

function blank {
   perl -p -e 'm/(left|top|right|bottom|width|height|coords)/ && s/\d+/\#/g' $@
}

#
# because valgrind has trouble with glib memory handling
# (see e.g. https://developer.gnome.org/glib/2.30/glib-running.html or 
# https://stackoverflow.com/questions/4254610/valgrind-reports-memory-possibly-lost-when-using-glib-data-types)
# we set some env. vars to work around them:

VALGRIND_ERR_FILE="${BASEDIR}/$(basename $0)-valgrind-err.tmp"
if [ -e "$VALGRIND_ERR_FILE" ]; then
  rm "$VALGRIND_ERR_FILE"
fi

function valgrind {
  # use a sub shell, so setting environment variables only affects the current
  # rrdtool run and not subsequent ones
  local CMD="$1"
  shift
  (
    G_DEBUG=gc-friendly
    G_SLICE=all

    export G_DEBUG 
    export G_SLICE

    VALGRIND_OPTS=
    if [ -n "$VALGRIND_LOGFILE" ] ; then
        TMP_LOG="${VALGRIND_LOGFILE}.$$.tmp"
        VALGRIND_OPTS=--log-file="${TMP_LOG}"
    fi

    libtool --mode=execute valgrind --tool=memcheck \
			--suppressions="$BASEDIR/valgrind-supressions" \
			--leak-check=full --show-reachable=yes \
			--leak-resolution=high --num-callers=20 \
			--error-exitcode=111 \
			--track-fds=yes \
                        --track-origins=yes \
			--free-fill=ad \
                        $VALGRIND_OPTS \
			"$CMD" "$@"
    RC=$?

    if [ -n "$VALGRIND_LOGFILE" ] ; then
        cat -- "${TMP_LOG}" >> "$VALGRIND_LOGFILE"
        if [ $RC == 111 ] ; then
            sed -n '/LEAK SUMMARY:/,$p' -- "${TMP_LOG}" >&2
        fi
        rm -f -- "${TMP_LOG}"
    fi

    # make sure to properly report the rrdtool exit code to the caller by:
    #  - first: exiting with the original exit code from the subshell and
    exit $RC
  )
  #    - second: by returning the subshell exit code to the function caller
  #   Also create a file, in case the caller has difficulty testing the rc.
  local RC=$?
  if [ "$RC" == 111 ]; then
    touch "$VALGRIND_ERR_FILE"
  fi
  return $RC
}

function verbose_rrdtool {
	echo "$RRDTOOL_V" "$@" >&2
	"$RRDTOOL_V" "$@"
}

function fail {
	RC="$1"
	shift

	echo >&2 "FAILED: (rc=$RC)" "$@"
	if [ -n "$CONTINUE" ] ; then
		return
	fi
	if [ -n "$INTERACTIVE" ] ; then
		read -p "Continue? (y/n)" YN
		if [ "$YN" != 'y' ] ; then 
			exit 1
		fi
	else
		exit 1
	fi
}

function ok {
	echo >&2 "OK:" "$@"
}

function report {
	RC=$?
	if [ -e "$VALGRIND_ERR_FILE" ]; then
		RC="valgrind error"
		rm "$VALGRIND_ERR_FILE"
	fi

	if [ "$RC" = 0 ] ; then
		ok "$@"
	else
		fail "$RC" "$@"
	fi


}

function run_cached {
        local D="$1"
        local CACHED="${D:-$RRDCACHED}"

        stop_cached

        local ADDR
        [ -z "$RRDCACHED_SOCK" ] && RRDCACHED_SOCK="unix"

        if [ $RRDCACHED_SOCK == "unix" ]; then
            export BASEDIR="${BUILDDIR}/cached"
            export BUILDDIR="${BASEDIR}"
            ADDR="unix:$BASEDIR/$(basename $0)-rrdcached.sock"
        else
            export BASEDIR="${BUILDDIR}/cached"
            export RRDCACHED_STRIPPATH="${BUILDDIR}"

            function RRDTOOLCOMPAT() {
                RRDCACHED_STRIPPATH=${BUILDDIR}
                RRDTOOL_ORIG=$TOP_BUILDDIR/src/rrdtool
                ARGS=("$@")

                # rrdcached does not support update with remote template
                if [ $1 == "update" ] && [ $3 == '--template' ]; then
                    RRDCACHED_STRIPPATH=${BASEDIR}
                    ARGS=( $(sed "s#${BUILDDIR}#${BASEDIR}#" <<< "${ARGS[@]}") )
                fi

                # rrdcached does not support remote restore
                if [ $1 == "restore" ]; then
                    ARGS=( "restore" "$2" $(sed "s#${BUILDDIR}#${BASEDIR}#" <<< $3) )
                    RRDCACHED_STRIPPATH=${BASEDIR}
                fi

                # rrdcached does not support tune
                # if [ $1 == "tune" ]; then
                #     ARGS=( $(sed "s#${BUILDDIR}#${BASEDIR}#" <<< "${ARGS[@]}") )
                # fi

                # rrdcached does not support create with remote source or template
                if [ $1 == "create" ]; then
                    NEWARGS=()
                    IS_NEXT_SOURCE_TEMPLATE=0
                    for arg in $(echo "${ARGS[@]}"); do
                        if [ $IS_NEXT_SOURCE_TEMPLATE == "1" ]; then
                            arg=$(sed "s#${BUILDDIR}#${BASEDIR}#" <<< $arg)
                            IS_NEXT_SOURCE_TEMPLATE=0
                        fi
                        ([ $arg == "--source" ] || [ $arg == "--template" ]) && IS_NEXT_SOURCE_TEMPLATE=1
                        NEWARGS+="$arg "
                    done
                    ARGS=( ${NEWARGS[@]} )
                fi

                $RRDTOOL_ORIG "${ARGS[@]}"
                ret=$?

                if [ $1 == "update" ]; then
                    RRDCACHED_STRIPPATH=${BUILDDIR} $RRDTOOL_ORIG flushcached $2 || fail flushcached
                fi

                return $ret
            }
            export RRDTOOL=RRDTOOLCOMPAT

            port=$(python2 -S << HERE
import SocketServer
s = SocketServer.TCPServer(("localhost", 0), None)
print(s.server_address[1])
s.server_close()
HERE
)
            ADDR="localhost:$port"
        fi

        CACHED_PID_FILE="$BASEDIR/$(basename $0)-rrdcached.pid"

        $CACHED -p "$CACHED_PID_FILE" -l "$ADDR" -b "$(readlink -f -- $BASEDIR)" -F -B

        RRDCACHED_ADDRESS=$ADDR
        export RRDCACHED_ADDRESS

        sleep 1
        trap 'stop_cached' EXIT
}

function stop_cached {
        if [ -n "$CACHED_PID_FILE" ] ; then
                kill $(cat "$CACHED_PID_FILE")
                rm -f -- "$CACHED_PID_FILE"
                CACHED_PID_FILE=""
                RRDCACHED_ADDRESS=""
        fi
}

function is_cached {
        [ -n "$RRDCACHED_ADDRESS" ]
}

function exit_if_cached_running {
        local E="$1"
        local MSG="$2"
        if is_cached ; then
                echo >&2 "$MSG"
                exit ${E:-1}
        fi
}

if [ -z "$RRDTOOL" ] ; then 
        RRDTOOL=$TOP_BUILDDIR/src/rrdtool
        RRDCACHED=$TOP_BUILDDIR/src/rrdcached

        NEED_CACHED=
        for ST in $TESTS_STYLE ; do
                case "$ST" in
                rrdcached-unix)
                        NEED_CACHED=1
                        STANDARD_RRDCACHED="$RRDCACHED"
                        RRDCACHED_SOCK=unix
                        ;;
                rrdcached-tcp)
                        NEED_CACHED=1
                        STANDARD_RRDCACHED="$RRDCACHED"
                        RRDCACHED_SOCK=tcp
                        ;;
                valgrind)
                        RRDTOOL="valgrind $TOP_BUILDDIR/src/rrdtool"
                        RRDCACHED="valgrind $TOP_BUILDDIR/src/rrdcached"
                        ;;
                valgrind-logfile)
                        RRDTOOL="valgrind $TOP_BUILDDIR/src/rrdtool"
                        RRDCACHED="valgrind $TOP_BUILDDIR/src/rrdcached"
                        VALGRIND_LOGFILE="${BASEDIR}/$(basename $0)"-valgrind.log
                        rm -f "$VALGRIND_LOGFILE"
                        ;;
                *)
                        ;;
                esac
        done
        if [ -n "$NEED_CACHED" ] ; then
                run_cached "$STANDARD_RRDCACHED"
        fi
fi

if [ -n "$VERBOSE" ] ; then
	RRDTOOL_V="$RRDTOOL"
	RRDTOOL=verbose_rrdtool
fi