comparison misc/dcc-stats-collect.in @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c7f6b056b673
1 #! /bin/sh
2
3 # collect spam statistics in .rrd files
4 # [-x] debugging
5 # [-q] quiet
6 # [-S] read `cdcc stats` from stdin
7 # [-h dcc_homedir]
8 # [-D data-dir] where to put the graphs and rrdtool files
9 # [-s stats-file] save raw `cdcc stats` output in stats-file
10 # [-t time] seconds since the Epoch
11 # [-T @RRDTOOL@]
12 # see http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/
13 # or the FreeBSD package.
14 # [-O rrdopts] "--heartbeat X" or "--step Y"
15 # [-i client-ID] that DCC servers will accept
16 # [-p password] that DCC servers will accept
17 # host1, host2, ... servers to ask for data
18
19
20 # Copyright (c) 2008 by Rhyolite Software, LLC
21 #
22 # This agreement is not applicable to any entity which sells anti-spam
23 # solutions to others or provides an anti-spam solution as part of a
24 # security solution sold to other entities, or to a private network
25 # which employs the DCC or uses data provided by operation of the DCC
26 # but does not provide corresponding data to other users.
27 #
28 # Permission to use, copy, modify, and distribute this software without
29 # changes for any purpose with or without fee is hereby granted, provided
30 # that the above copyright notice and this permission notice appear in all
31 # copies and any distributed versions or copies are either unchanged
32 # or not called anything similar to "DCC" or "Distributed Checksum
33 # Clearinghouse".
34 #
35 # Parties not eligible to receive a license under this agreement can
36 # obtain a commercial license to use DCC by contacting Rhyolite Software
37 # at sales@rhyolite.com.
38 #
39 # A commercial license would be for Distributed Checksum and Reputation
40 # Clearinghouse software. That software includes additional features. This
41 # free license for Distributed ChecksumClearinghouse Software does not in any
42 # way grant permision to use Distributed Checksum and Reputation Clearinghouse
43 # software
44 #
45 # THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL
46 # WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
47 # OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC
48 # BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
49 # OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
50 # WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
51 # ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
52 # SOFTWARE.
53 # Rhyolite Software DCC 1.3.103-1.27 $Revision$
54 # @configure_input@
55
56 DCC_HOMEDIR=@prefix@
57 DEBUG=
58 # check the args once to get the home directory
59 while getopts "xqUSh:D:s:t:T:O:i:p:" c; do
60 case $c in
61 x) set -x; DEBUG=-x;;
62 h) DCC_HOMEDIR="$OPTARG";;
63 *) ;;
64 esac
65 done
66 . $DCC_HOMEDIR/dcc_conf
67
68 QUIET=
69 UPDATERRD=
70 GET_ARGS=
71 DATADIR=$DCC_HOMEDIR/stats
72 STATSFILE=/dev/null
73 TS=N
74 RRDTOOL=@RRDTOOL@
75 RRDOPTS=
76 CLNT_ID="1"
77 PASSWD=""
78 USAGE="`basename $0`: [-xqUS] [-h homedir] [-D data-dir] [-s stats-file] [-t time]
79 [-T rrdtool] [-O rrdopts] [-i client-ID] [-p password] host1 host2 ..."
80 OPTIND=1
81 while getopts "xqUSh:D:s:t:T:O:i:p:" c; do
82 case $c in
83 x) ;;
84 q) QUIET="-q";;
85 U) UPDATERRD=yes;;
86 S) GET_ARGS="$GET_ARGS -S";;
87 h) ;;
88 D) DATADIR="$OPTARG";;
89 s) STATSFILE="$OPTARG";;
90 t) TS="$OPTARG";;
91 T) RRDTOOL="$OPTARG";;
92 O) RRDOPTS="$RRDOPTS $OPTARG";;
93 i) CLNT_ID="$OPTARG";;
94 p) PASSWD="$OPTARG";;
95 *) echo "$USAGE" 1>&2; exit 1;;
96 esac
97 done
98 shift `expr $OPTIND - 1 || true`
99 if test "$#" -eq 0; then
100 echo "$USAGE" 1>&2
101 exit 1
102 fi
103
104 cd $DATADIR
105
106 # generate a timestamp that can be used with new and old `touch` commands
107 # to give the .rrd and status files the right mtime
108 TTS=
109 if test -n "$TS" -a "$TS" != N; then
110 if TTS=`date -r $TS '+%m%d%H%S' 2>/dev/null`; then : ;
111 else
112 # deal with systems that do not have `date -r`
113 TTS=`@PERL@ -e "use POSIX qw(strftime); \
114 print strftime '%m%d%H%S', localtime($TS);"`
115 fi
116 fi
117
118 for HOST in $*; do
119 HOST="`basename $HOST .rrd`"
120
121 eval XSTATSFILE="$STATSFILE"
122 if test "$PASSWD" != ""; then
123 LINE=`$DCC_LIBEXEC/stats-get $GET_ARGS $DEBUG $QUIET \
124 -s$XSTATSFILE -i$CLNT_ID -p "$PASSWD" $HOST`
125 else
126 LINE=`$DCC_LIBEXEC/stats-get $GET_ARGS $DEBUG $QUIET \
127 -s$XSTATSFILE -i$CLNT_ID $HOST`
128 fi
129 if test -n "$TTS"; then
130 touch $TTS XSTATSFILE
131 fi
132
133 FILE="$HOST.rrd"
134 # update RRD file to include maximums
135 if test -s "$FILE" -a -n "$UPDATERRD" \
136 && test -z "`$RRDTOOL info $FILE 2>/dev/null \
137 | grep '^rra.*cf = .MAX.'`"; then
138 mv "$FILE" "$FILE.old"
139 # get all but the final "</rrd>" line of the xml
140 $RRDTOOL dump "$FILE.old" | sed -e '$d' >"$FILE.xml"
141 # add a "MAX" database from a copy of the last "MIN" database
142 $RRDTOOL dump "$FILE.old" \
143 | sed -e 's@<cf> MIN </cf>@<rra><cf> MAX </cf>@p' \
144 -e '1,/<cf> MAX <.cf>/d' \
145 -e 's@<v> [0-9.+e]* </v>@<v> NaN </v>@g' >>"$FILE.xml"
146 $RRDTOOL restore "$FILE.xml" "$FILE"
147 rm "$FILE.xml"
148 fi
149
150 if test -n "$LINE"; then
151 # create the RRD file if it does not exist
152 if test ! -s "$FILE"; then
153 $DCC_LIBEXEC/dcc-stats-init $QUIET $DEBUG -h$DCC_HOMEDIR \
154 -D$DATADIR -T "$RRDTOOL" -O "$RRDOPTS" "$FILE"
155 fi
156 # do not add flood checksum counts to new database
157 if test -z "`$RRDTOOL info $FILE | grep '^ds.flooded.*DERIVE'`"; then
158 LINE=`expr "$LINE" : '\(.*\):[0-9]*$'`
159 fi
160 $RRDTOOL update "$FILE" "$TS:$LINE"
161 # ensure that the .rrd file has the right mtime
162 if test -n "$TTS"; then
163 touch $TTS $FILE
164 fi
165 fi
166 done