structure thermal devices
[metriccd.git] / server / metriccd
blob3debd86d5eb76da9e70785098ee3f4d0e9b0c377
1 #!/usr/bin/env perl
3 use Data::Dumper;
4 use Socket;
6 $metric_basedir = "/var/log/metriccd";
8 sub kvesc
10 my $s = shift;
11 if($s =~ /[\s""]/)
13 $s =~ s/[""]/sprintf '\\x%02X', ord $&/eg;
14 $s = "\"$s\"";
16 return $s;
19 $FileHandle = undef;
20 $REMOTE_HOST = $ENV{'REMOTE_HOST'};
21 %Param = ();
23 my ($hostname, $aliases, $addrtype, $addrlength, @addrs) = gethostbyaddr(inet_aton($REMOTE_HOST), AF_INET);
24 $Param{'hostname'} = $hostname if $hostname;
26 while(<STDIN>)
28 chomp;
29 if(/^([^\t]+)\t([^\t]+)(?:\t(\d+)|)$/ or /^([^ ]+) ([^ ]+)(?: (\d+)|)$/)
31 my ($metric, $data, $timestamp) = ($1, $2, $3);
32 $timestamp = time unless $timestamp;
34 if(not $FileHandle)
36 mkdir $metric_basedir;
37 mkdir "$metric_basedir/by-host";
38 open $FileHandle, '>>', "$metric_basedir/by-host/$REMOTE_HOST";
41 my $params = join '', map {"\t$_=".kvesc($Param{$_})} keys %Param;
42 print {$FileHandle} "$metric\t$data\t$timestamp$params\n";