+sub fetch_tcp_multi(@) {
+ my $address = shift;
+ my $port = shift;
+ my $delimiter = shift;
+ my %values;
+
+ my $sock = IO::Socket::INET->new(PeerAddr => $address,
+ PeerPort => $port,
+ Proto => 'tcp',
+ Timeout => 1);
+
+ return undef if (!$sock);
+
+ my $select = IO::Select->new($sock);
+
+ while($select->can_read(1)) {
+ if (sysread($sock, my $buf, 16384) > 0) {
+ $buf=~s/\r//g;
+ foreach my $line (split(/\n/, $buf)) {
+ (my $key, my $value) = split(/${delimiter}/, $line);
+ $value=~ s/\s//g;
+ $values{$key} = $value;
+ }
+ } else {
+ last;
+ }
+ }
+
+ close($sock);
+
+ %values;
+}
+
+sub dayGraphFunc {
+ my $dataSrc = shift;
+ my $mode = shift;
+ my $color = shift;
+ my $label = shift;
+ my $dataPoints = shift;
+ my @args = ();
+ push @args, "CDEF:prev${mode}1=PREV(${dataSrc})";
+ for (my $i = 1; $i < $dataPoints - 1; ++$i) {
+ my $prev = $i;
+ my $next = $i+1;
+ push @args, "CDEF:prev${mode}${next}=PREV(prev${mode}${prev})";
+ }
+ my $dayCons = '';
+ my $consFunc = '';
+ if ($mode ne 'AVG') {
+ for (my $i = 1; $i < $dataPoints; ++$i) {
+ $dayCons .= "prev${mode}${i},";
+ $consFunc .= ",${mode}";
+ }
+ } else {
+ for (my $i = 1; $i < $dataPoints; ++$i) {
+ $dayCons .= "prev${mode}${i},";
+ }
+ $consFunc = ",${dataPoints},${mode}";
+ }
+ push @args, "CDEF:day${mode}=${dayCons}${dataSrc}${consFunc}";
+ push @args, "CDEF:fillCalDay${mode}0=COUNT,${dataPoints},%,0,EQ,day${mode},UNKN,IF";
+ for (my $i = 1; $i < $dataPoints; ++$i) {
+ my $prev = $i-1;
+ my $next = $i;
+ push @args, "CDEF:fillCalDay${mode}${next}=PREV(fillCalDay${mode}${prev})";
+ }
+ my $fillPoint = '';
+ my $if = '';
+ for (my $i = 0; $i < $dataPoints; ++$i) {
+ $fillPoint .= "COUNT,${dataPoints},%,${i},EQ,fillCalDay${mode}${i},";
+ $if .= ",IF";
+ }
+ push @args, "CDEF:${mode}Curve=${fillPoint}UNKN${if}";
+ my $forwardShift = (24*60*60) * ($dataPoints - 1) / $dataPoints;
+ push @args, "SHIFT:${mode}Curve:-${forwardShift}";
+ push @args, "LINE1:${mode}Curve#${color}:${label}";
+ return \@args;
+}
+
+sub cfgToGraphDef {
+ my $cfg = shift;
+ my $varname = shift;
+ my $dpPerDay= shift;
+ my @graphDef = ();
+ foreach my $subGraph (@$cfg) {
+ if ($subGraph eq 'avg') {
+ push @graphDef, "LINE1:${varname}-avg#FF0000";
+ } elsif ($subGraph eq 'day-min') {
+ push @graphDef, @{dayGraphFunc("${varname}-min", 'MIN', '0000ff', 'Day Minimum Temperature', $dpPerDay)};
+ } elsif ($subGraph eq 'day-max') {
+ push @graphDef, @{dayGraphFunc("${varname}-max", 'MAX', '00ff00', 'Day Maximum Temperature', $dpPerDay)};
+ } elsif ($subGraph eq 'day-avg') {
+ push @graphDef, @{dayGraphFunc("${varname}-houravg", 'AVG', 'ff0000', 'Day Average Temperature', $dpPerDay)};
+ }
+ }
+ return \@graphDef;
+}
+