summaryrefslogtreecommitdiff
path: root/sysutils/bsdstats/files/300.statistics.in
blob: 149934f8245866c0b91640ed19e8f6fe14729cd6 (plain) (blame)
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
#!/bin/sh -
#
# $FreeBSD: /tmp/pcvs/ports/sysutils/bsdstats/files/300.statistics.in,v 1.23 2006-09-28 18:00:04 scrappy Exp $
#

# If there is a global system configuration file, suck it in.
#
if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
    periodic_conf=/etc/periodic.conf
else
    . /etc/rc.conf	# For systems without periodic.conf, use rc.conf
    if [ -r /etc/rc.conf.local ] 
    then
        . /etc/rc.conf.local
    fi
    periodic_conf=/etc/rc.conf.local
fi

oldmask=$(umask)
umask 066

checkin_server=${monthly_statistics_checkin_server:-"bsdstats.org"}
id_token_file='/var/db/bsdstats'

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
export PATH

unset HTTP_USER_AGENT

IFS="
"

random () {
  jot -r 1 0 300
}

send_devices () {
    case $(uname) in
	FreeBSD )
	    for line in `/usr/sbin/pciconf -l | /usr/bin/grep -v none`
	    do
	      DRIVER=`echo $line | awk -F\@ '{print $1}'`
	      DEV=`echo $line | awk '{print $4}' | cut -c8-15`
	      CLASS=`echo $line | awk '{print $2}' | cut -c9-14`
	      query_string=$query_string`echo \&dev[]=$DRIVER:$DEV:$CLASS`
	    done

	    do_fetch report_devices.php?token=$TOKEN\&key=$KEY$query_string
	    ;;
	* )
	    # Not supported
	    ;;
    esac
}

get_id_token () {
    if [ ! -f $id_token_file -o ! -s $id_token_file ] ;
    then
       IDTOKEN=$(uri_escape $( openssl rand -base64 16 ) )
       
       idf=$( mktemp "$id_token_file.XXXXXX" )  && \
       chown root:wheel $idf          && \
       chmod 600 $idf

       do_fetch getid.php?key=$IDTOKEN | {
          local IFS
          IFS='=
'

	   while read var val 
	     do  
	     case $var in
		 KEY)
                     echo "KEY=$val"
		 ;;
		 TOKEN)
                     echo "TOKEN=$val"
		 ;;
		 *)
                 ;;
		 esac
	   done
       } > $idf                                  && \

       mv $idf $id_token_file
    if [ ! -s $id_token_file ] ;
    then
	echo "Nothing returned from $checkin_server"
	exit 1
    fi

       echo "To protect against abuse, the initial challenge/response phase"
       echo "contains a 15 minute pause.  Please be patient while this time"
       echo "limit elapses"
       sleep 900
    else
       FILETIME=$( stat -f %m $id_token_file )
       NOW=$( date +%s )
       if [ $(( $NOW - 900 )) -le $FILETIME ]; then
          SLEEPTIME=$(( 900 - ($NOW - $FILETIME) ))
          echo "Token key is younger than 15 minutes!"
          echo "Sleeping $SLEEPTIME seconds, please wait."
          sleep $SLEEPTIME
       fi
    fi
    . $id_token_file
    KEY=$( uri_escape $KEY )
    TOKEN=$( uri_escape $TOKEN )
}

# RFC 2396
uri_escape () {
    echo ${1+$@} | sed -e '
        s/%/%25/g
        s/;/%3b/g
        s,/,%2f,g
        s/?/%3f/g
        s/:/%3a/g
        s/@/%40/g
        s/&/%26/g
        s/=/%3d/g
        s/+/%2b/g
        s/\$/%24/g
        s/,/%2c/g
        s/ /%20/g
        '
}

do_fetch () {
    url="http://$checkin_server/scripts/$1"
    sleep `random`
    case $(uname) in
	FreeBSD )	
		/usr/bin/fetch -q -o - "$url"
		;;
	* )
		/usr/bin/ftp -V -o - "$url"
		;;
    esac
}


case "$monthly_statistics_enable" in
    [Yy][Ee][Ss])
      HN=`/bin/hostname`
      REL=`/usr/bin/uname -r`
      ARCH=`/usr/bin/uname -m`
      OS=`/usr/bin/uname -s`
      get_id_token
      do_fetch report_system.php?token=$TOKEN\&key=$KEY\&rel=$REL\&arch=$ARCH\&opsys=$OS
      echo "Posting monthly OS statistics to $checkin_server"
      case "$monthly_statistics_report_devices" in
          [Yy][Ee][Ss])
              send_devices
              echo "Posting monthly device statistics to $checkin_server"
              line=$( sysctl -n hw.model )
              VEN=$( echo $line | cut -d ' ' -f 1 )
              DEV=$( uri_escape $( echo $line | cut -d ' ' -f 2- ) )
              count=$( sysctl -n hw.ncpu )
              do_fetch report_cpu.php?token=$TOKEN\&key=$KEY\&cpus=$count\&vendor=$VEN\&cpu_type=$DEV
              echo "Posting monthly CPU statistics to $checkin_server"
             ;;
          *) 
             echo "Posting monthly device/CPU statistics disabled"
             echo '    set monthly_statistics_report_devices="YES" in $periodic_conf'
             ;;
      esac
      ;;
    *) 
      echo "Posting monthly OS statistics disabled"
      echo '    set monthly_statistics_enable="YES" in $periodic_conf'
    ;;
esac

umask $oldmask
exit $rc