#!/usr/bin/perl
#
# $Id: i2c-stat.pl,v 0.10 2006/08/10 07:03:14 root Exp root $
#
# Prints some PC health statistics for Winbond w83781d chip
# * Needs Kernel >= 2.6 with i2c-support
# * Voltage multipliers are from gkrellm-2.2.4.tar.bz2 (src/sysdeps/linux.c)
#
# Author: Martin Bock <martin (at) martin-bock (dot) de>
# Date:   01-May-2005

# modules
#
use File::Basename;

# variables
#
$app_name = basename($0);
$data_dir = "/sys/devices/platform/i2c-9191/9191-0290";
$cpu0_temp_file = "${data_dir}/temp1_input";
$cpu1_temp_file = "${data_dir}/temp2_input";
$cpu0_fan_file = "${data_dir}/fan1_input";
$cpu1_fan_file = "${data_dir}/fan2_input";
$vcor1_file ="${data_dir}/in0_input";
$vcor2_file ="${data_dir}/in1_input";
$v3_file = "${data_dir}/in2_input";
$v5_file = "${data_dir}/in3_input";
$v12_file = "${data_dir}/in4_input";
$v5sb_file ="${data_dir}/in7_input";
$vbat_file ="${data_dir}/in8_input";

# rock'n'roll
#
if (@ARGV == 0) {
  $count = 1;
} elsif ((@ARGV == 1) && (($ARGV[0] eq "-h") || ($ARGV[0] eq "--help"))) {
  hilfe();
  exit(0);
} elsif ((@ARGV == 1 ) && ($ARGV[0] =~ /[0-9]+/)) {
  $count = $ARGV[0];
  $delay = 10;
} elsif ((@ARGV == 2) && (($ARGV[0] =~ /[0-9]+/) && ($ARGV[1] =~ /[0-9]+/))) {
  $count = $ARGV[0];
  $delay = $ARGV[1];
} else {
  hilfe();
  exit(1);
}

if (-d $data_dir) {
  print "\t\tTemperature\tFan Speed\tVoltage\n";
  print "\t\tCPU0\tCPU1\tFan1\tFan2\tVcor1\tVcor2\t3,3V\t5V\t12V\tV5SB\tVBat\n";
  if ($count > 1) {
    for ($i = 1; $i < $count; $i++) {
      get_data();
      sleep $delay;
    }
  }
  get_data();
  exit(0);
} else {
  print "$data_dir not found, aborting ...\n";
  exit(1);
}

# subs
#
sub get_data() {
  ($sec,$min,$hour,$mday,$mon,$year,$temp) = localtime(time);
  $stamp = sprintf("%02d:%02d:%02d", $hour,$min,$sec);
  print "$stamp\t";
  foreach $i ($cpu0_temp_file, $cpu1_temp_file) {
    open(SENSOR, "<$i") or die "Can't open $i: $!";
    $val = <SENSOR>;
    chomp($val);
    if (length($val) == 5) {
      $val = $val / 1000;
    }
    $val = sprintf("%.1f", $val);
    print "$val\t";
    close(SENSOR) or die "Can't close $i: $!";
  }

  foreach $i ($cpu0_fan_file, $cpu1_fan_file) {
    open(SENSOR, "<$i") or die "Can't open $i: $!";
    $val = <SENSOR>;
    chomp($val);
    $val = sprintf("%4d", $val);
    print "$val\t";
    close(SENSOR) or die "Can't close $i: $!";
  }

  foreach $i ($vcor1_file, $vcor2_file, $v3_file, $v5_file, $v12_file, $v5sb_file, $vbat_file) {
    open(SENSOR, "<$i") or die "Can't open $i: $!";
    $val = <SENSOR>;
    chomp($val);
    if ($i eq "$v5_file") {
      $val = int($val * 1.68);
    } elsif ($i eq "$v12_file") {
      $val = $val * 4.0;
    } elsif ($i eq "$v5sb_file") {
      $val = int($val * 1.68);
    }
    if (length($val) == 4) {
      $val = $val / 1000;
    }
    $val = sprintf("%.3f", $val);
    print "$val\t";
    close(SENSOR) or die "Can't close $i: $!";
  }

  print "\n";
}

sub hilfe() {
  print "$app_name -- prints machine's health status\n";
  print "Usage:\n";
  print "$app_name\t[c] [d]\t\t->\tprints status c times with d seconds delay\n";
  print "\t\t[-h | --help]\t->\tprints this braindead message\n";
}

