Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / etc / periodic / monthly / 300.statistics
1 #!/bin/sh -
2 #
3 # $FreeBSD: ports/sysutils/bsdstats/files/300.statistics,v 1.31 2006/10/03 01:33:11 scrappy Exp $
4 # $DragonFly: src/etc/periodic/monthly/300.statistics,v 1.1 2006/10/04 21:39:23 dillon Exp $
5
6 # If there is a global system configuration file, suck it in.
7 #
8 if [ -r /etc/defaults/periodic.conf ]
9 then
10     . /etc/defaults/periodic.conf
11     source_periodic_confs
12     periodic_conf=/etc/periodic.conf
13 else
14     . /etc/rc.conf      # For systems without periodic.conf, use rc.conf
15     if [ -r /etc/rc.conf.local ] 
16     then
17         . /etc/rc.conf.local
18     fi
19     periodic_conf=/etc/rc.conf.local
20 fi
21
22 oldmask=$(umask)
23 umask 066
24
25 version="4.7"
26 checkin_server=${monthly_statistics_checkin_server:-"bsdstats.org"}
27 bsdstats_log=${monthly_statistics_logfile:-"/var/log/bsdstats"}
28 id_token_file='/var/db/bsdstats'
29
30 PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
31 export PATH
32
33 unset HTTP_USER_AGENT
34
35 IFS="
36 "
37
38 random () {
39   jot -r 1 0 900
40 }
41
42 # RFC 2396
43 uri_escape () {
44     echo ${1+$@} | sed -e '
45         s/%/%25/g
46         s/;/%3b/g
47         s,/,%2f,g
48         s/?/%3f/g
49         s/:/%3a/g
50         s/@/%40/g
51         s/&/%26/g
52         s/=/%3d/g
53         s/+/%2b/g
54         s/\$/%24/g
55         s/,/%2c/g
56         s/ /%20/g
57         '
58 }
59
60 do_fetch () {
61     url="http://$checkin_server/scripts/$1"
62     case $(uname) in
63         FreeBSD )       
64                 /usr/bin/fetch -q -o - "$url"
65                 ;;
66         * )
67                 /usr/bin/ftp -V -o - "$url"
68                 ;;
69     esac
70 }
71
72 check_dns () {
73     if [ `drill bsdstats.org txt | grep TXT | grep UP | wc -l` = 0 ]
74     then
75       echo "DNS not reachable, Network Down?"
76       exit
77     fi
78 }
79 send_devices () {
80     case $(uname) in
81         FreeBSD )
82             for line in `/usr/sbin/pciconf -l | /usr/bin/grep -v none`
83             do
84               DRIVER=`echo $line | awk -F\@ '{print $1}'`
85               DEV=`echo $line | awk '{print $4}' | cut -c8-15`
86               CLASS=`echo $line | awk '{print $2}' | cut -c9-14`
87               query_string=$query_string`echo \&dev[]=$DRIVER:$DEV:$CLASS`
88             done
89
90             report_devices
91             ;;
92         * )
93             # Not supported
94             ;;
95     esac
96 }
97
98 report_devices () {
99       do_fetch report_devices.php?token=$TOKEN\&key=$KEY$query_string | { 
100           local IFS
101           IFS='=
102 '
103
104            while read var val
105              do
106              case $var in
107                  STATUS)
108                      if [ $val = "OK" ]
109                      then
110                        echo "[`date`] System Devices reported"
111                      else
112                        echo "[`date`] System Devices not reported, exiting"
113                        exit
114                      fi
115                  ;;
116                  *)
117                      echo "[`date`] Error with fetch to server"
118                      exit
119                  ;;
120                  esac
121            done  
122        } >> $bsdstats_log
123 }
124
125 get_id_token () {
126     if [ -f $id_token_file ]
127     then
128       if [ `cat /var/db/bsdstats | wc -l` -lt 3 ] 
129       then
130         rm $id_token_file
131       fi
132     fi
133
134     if [ ! -f $id_token_file -o ! -s $id_token_file ] ;
135     then
136        IDTOKEN=$(uri_escape $( openssl rand -base64 32 ) )
137        
138        idf=$( mktemp "$id_token_file.XXXXXX" )  && \
139        chown root:wheel $idf          && \
140        chmod 600 $idf
141
142        do_fetch getid.php?key=$IDTOKEN | {
143           local IFS
144           IFS='=
145 '
146
147            while read var val 
148              do  
149              case $var in
150                  KEY)
151                      echo "KEY=$val"
152                  ;;
153                  TOKEN)
154                      echo "TOKEN=$val"
155                  ;;
156                  *)
157                  ;;
158                  esac
159            done
160            echo "VERSION=$version"
161        } > $idf                                  && \
162
163        mv $idf $id_token_file
164        if [ ! -s $id_token_file ] ;
165        then
166           echo "Nothing returned from $checkin_server"
167           exit 1
168        fi
169     fi
170     . $id_token_file
171     KEY=$( uri_escape $KEY )
172     TOKEN=$( uri_escape $TOKEN )
173 }
174
175
176 enable_token () {
177       do_fetch enable_token.php?key=$TOKEN\&token=$KEY | {
178           local IFS
179           IFS='=
180 '
181
182            while read var val
183              do
184              case $var in
185                  STATUS)
186                      if [ $val = "OK" ]
187                      then
188                        echo "[`date`] System enabled"
189                      else
190                        echo "[`date`] System not enabled, exiting"
191                        exit
192                      fi
193                  ;;
194                  *)
195                      echo "[`date`] Error with fetch to server"
196                      exit
197                  ;;
198                  esac
199            done  
200        } >> $bsdstats_log
201 }
202
203 disable_token () {
204       do_fetch disable_token.php?key=$TOKEN\&token=$KEY | {
205           local IFS
206           IFS='=
207 '
208
209            while read var val
210              do
211              case $var in
212                  STATUS)
213                      if [ $val = "OK" ]
214                      then
215                        echo "[`date`] System disabled"
216                      else
217                        echo "[`date`] System not disabled, exiting"
218                        exit
219                      fi
220                  ;;
221                  *)
222                      echo "[`date`] Error with fetch to server"
223                      exit
224                  ;;
225                  esac
226            done  
227        } >> $bsdstats_log
228 }
229
230 report_system () {
231       do_fetch report_system.php?token=$TOKEN\&key=$KEY\&rel=$REL\&arch=$ARCH\&opsys=$OS | {
232           local IFS
233           IFS='=
234 '
235
236            while read var val
237              do
238              case $var in
239                  STATUS)
240                      if [ $val = "OK" ]
241                      then
242                        echo "[`date`] System reported"
243                      else
244                        echo "[`date`] System report failed, exiting"
245                        exit
246                      fi
247                  ;;
248                  *)
249                      echo "[`date`] Error with fetch to server"
250                      exit
251                  ;;
252                  esac
253            done  
254        } >> $bsdstats_log
255 }
256
257 report_cpu () {
258     do_fetch report_cpu.php?token=$TOKEN\&key=$KEY\&cpus=$count\&vendor=$VEN\&cpu_type=$DEV | {
259           local IFS
260           IFS='=
261 '
262
263            while read var val
264              do
265              case $var in
266                  STATUS)
267                      if [ $val = "OK" ]
268                      then
269                        echo "[`date`] System CPU reported"
270                      else
271                        echo "[`date`] System CPU report failed, exiting"
272                        exit
273                      fi
274                  ;;
275                  *)
276                      echo "[`date`] Error with fetch to server"
277                      exit
278                  ;;
279                  esac
280            done  
281        } >> $bsdstats_log
282 }
283 case "$monthly_statistics_enable" in
284     [Yy][Ee][Ss])
285       check_dns
286       HN=`/bin/hostname`
287       REL=`/usr/bin/uname -r`
288       ARCH=`/usr/bin/uname -m`
289       OS=`/usr/bin/uname -s`
290       get_id_token
291       sleep `random`
292       enable_token
293       report_system
294       echo "Posting monthly OS statistics to $checkin_server"
295       case "$monthly_statistics_report_devices" in
296           [Yy][Ee][Ss])
297               send_devices
298               echo "Posting monthly device statistics to $checkin_server"
299               line=$( sysctl -n hw.model )
300               VEN=$( echo $line | cut -d ' ' -f 1 )
301               DEV=$( uri_escape $( echo $line | cut -d ' ' -f 2- ) )
302               count=$( sysctl -n hw.ncpu )
303               report_cpu
304               echo "Posting monthly CPU statistics to $checkin_server"
305              ;;
306           *) 
307              echo "Posting monthly device/CPU statistics disabled"
308              echo "    set monthly_statistics_report_devices=\"YES\" in $periodic_conf"
309              ;;
310       esac
311       disable_token
312       ;;
313     *) 
314       echo "Posting monthly OS statistics disabled"
315       echo "    set monthly_statistics_enable=\"YES\" in $periodic_conf"
316     ;;
317 esac
318
319 umask $oldmask
320 exit $rc