| [2373] | 1 | #!/usr/bin/perl -wT |
|---|
| 2 | |
|---|
| 3 | # sensors-detect-stat.pl |
|---|
| 4 | # Statistical analysis of sensors-detect i2c addresses scanner |
|---|
| 5 | # Part of the lm_sensors project |
|---|
| [5508] | 6 | # Copyright (C) 2003-2008 Jean Delvare <khali@linux-fr.org> |
|---|
| [2373] | 7 | # |
|---|
| 8 | # This program is free software; you can redistribute it and/or modify |
|---|
| 9 | # it under the terms of the GNU General Public License as published by |
|---|
| 10 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 11 | # (at your option) any later version. |
|---|
| 12 | # |
|---|
| 13 | # This program is distributed in the hope that it will be useful, |
|---|
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | # GNU General Public License for more details. |
|---|
| 17 | # |
|---|
| 18 | # You should have received a copy of the GNU General Public License |
|---|
| 19 | # along with this program; if not, write to the Free Software |
|---|
| 20 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 21 | |
|---|
| 22 | use strict; |
|---|
| [4970] | 23 | use vars qw(%histo $chips $file $skip); |
|---|
| [2373] | 24 | |
|---|
| 25 | # Where is sensors-detect? |
|---|
| 26 | # Use first argument, else try default locations. |
|---|
| 27 | if (defined($ARGV[0])) |
|---|
| 28 | { |
|---|
| 29 | $file = $ARGV[0]; |
|---|
| 30 | } |
|---|
| 31 | elsif (-r '/usr/local/sbin/sensors-detect') |
|---|
| 32 | { |
|---|
| 33 | $file = '/usr/local/sbin/sensors-detect'; |
|---|
| 34 | } |
|---|
| 35 | elsif (-r '/usr/sbin/sensors-detect') |
|---|
| 36 | { |
|---|
| 37 | $file = '/usr/sbin/sensors-detect'; |
|---|
| 38 | } |
|---|
| 39 | else |
|---|
| 40 | { |
|---|
| 41 | print "Usage: $0 /path/to/sensors-detect\n"; |
|---|
| 42 | exit 1; |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | # Can we read that file? |
|---|
| 46 | if (! -r $file) |
|---|
| 47 | { |
|---|
| 48 | print "Couldn't open $file for reading.\n"; |
|---|
| 49 | exit 2; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | # Get the data. |
|---|
| 53 | open (SD, $file) || die; |
|---|
| [4970] | 54 | $skip = 0; |
|---|
| [2373] | 55 | while (<SD>) |
|---|
| 56 | { |
|---|
| [4970] | 57 | # Some chips are handled differently depending on the kernel |
|---|
| 58 | # version, avoid counting them twice. |
|---|
| 59 | if (m/^\@chip_kern24_ids\s*=/ || m/^\@chip_oldfsc_ids\s*=/) { |
|---|
| 60 | $skip = 1; |
|---|
| 61 | next; |
|---|
| 62 | } |
|---|
| 63 | if ($skip && m/^\);$/) { |
|---|
| 64 | $skip = 0; |
|---|
| 65 | next; |
|---|
| 66 | } |
|---|
| 67 | next if $skip; |
|---|
| 68 | |
|---|
| [2373] | 69 | # The regular expression may seem a little bit complex, but we wouldn't |
|---|
| 70 | # want to exec malicious code. |
|---|
| 71 | next unless m/^\s*i2c_addrs\s*=>\s*(\[( *0x[\dA-Fa-f]{2}( *\.\. *0x[\dA-Fa-f]{2})? *,?)+\])\s*,/; |
|---|
| 72 | my $addresses = eval $1 || die "Failed to eval \"$1\""; |
|---|
| 73 | $chips++; |
|---|
| 74 | foreach my $a (@{$addresses}) |
|---|
| 75 | { |
|---|
| 76 | $histo{$a}++; |
|---|
| 77 | } |
|---|
| 78 | } |
|---|
| 79 | close SD; |
|---|
| 80 | |
|---|
| 81 | # Print the data. |
|---|
| [5508] | 82 | printf "\%s knows \%d chips and scans \%d addresses.\n\n", |
|---|
| 83 | $file, $chips, scalar keys %histo; |
|---|
| [2373] | 84 | print " 0 1 2 3 4 5 6 7 8 9 a b c d e f\n"; |
|---|
| 85 | for (my $i=0; $i<128; $i+=16) |
|---|
| 86 | { |
|---|
| 87 | printf '%02x:', $i; |
|---|
| 88 | for (my $j=0; $j<16; $j++) |
|---|
| 89 | { |
|---|
| 90 | my $valid = ($i+$j >= 0x04 && $i+$j <= 0x77); |
|---|
| 91 | if (defined $histo{$i+$j}) |
|---|
| 92 | { |
|---|
| 93 | printf '%s%02d', ($valid?' ':'!'), $histo{$i+$j}; |
|---|
| 94 | } |
|---|
| 95 | else |
|---|
| 96 | { |
|---|
| 97 | printf ' %s', ($valid?'--':' '); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | print "\n"; |
|---|
| 101 | } |
|---|