#!/usr/bin/perl -w

use Config::Simple;
use Getopt::Long;
use JSON;

my $type = 'temp';
GetOptions(
  "type:s" => \$type
);

# empty means temp
$type = ($type eq '') ? 'temp' : $type;

my $json;
@{$json->{data}} = ();

my $cfg = new Config::Simple;
$cfg->read('/etc/zabbix/sensors.ini');
$cfg->syntax('ini');
my %sensors = ();
foreach my $k (keys %{$cfg->vars}){
  $k =~ s/\..*$//;
  $sensors{$k} = 1 unless $sensors{$k};
}

foreach my $k (keys %sensors){
  my $sensor = $cfg->get_block($k);
  next if ($type ne 'all' && $type ne $sensor->{type});
  push @{$json->{data}}, {
    "{#SENSORNAME}"      => $k,
    "{#SENSORDESC}"      => $sensor->{description},
    "{#SENSORTHRESHIGH}" => $sensor->{threshold_high},
    "{#SENSORTHRESLOW}"  => $sensor->{threshold_low},
    "{#SENSORTYPE}"      => $sensor->{type},
    "{#SENSORUNIT}"      => $sensor->{unit}
  };
}

print to_json($json);
exit(0);
