forked from skaringa/emeocv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.pl
executable file
·120 lines (108 loc) · 2.39 KB
/
graph.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/perl
#
# CGI script to create image using RRD graph
use CGI qw(:all);
use RRDs;
use POSIX qw(locale_h);
$ENV{'LANG'}='de_DE.UTF-8';
setlocale(LC_ALL, 'de_DE.UTF-8');
# path to database
$rrd='../../emeocv/emeter.rrd';
# force to always create image from rrd
#$force=1;
$query=new CGI;
#$query=new CGI("type=consumweek&size=small");
$size=$query->param('size');
$type=$query->param('type');
if ($size eq 'big') {
$width=900;
$height=700;
$size="b";
} else {
$width=500;
$height=160;
$size="s";
}
die "invalid type\n" unless $type =~ /(count|consum)(day|week|month|year)/;
$ds=$1;
$range=$2;
$filename="/tmp/em${type}_${size}.png";
# create new image if existing file is older than rrd file
my $maxdiff = 10;
$maxdiff = 60 * 60 * 12 if $type eq 'tempyear';
if ((mtime($rrd) - mtime($filename) > $maxdiff) or $force) {
$tmpfile="/tmp/emt${type}_${size}_$$.png";
# call sub to create image according to type
&$ds($range);
# check error
my $err=RRDs::error;
die "$err\n" if $err;
rename $tmpfile, $filename;
}
# feed image to stdout
open(IMG, $filename) or die "can't open $filename";
print header('image/png');
print <IMG>;
close IMG;
# end MAIN
# Return modification date of file
sub mtime {
my $file=shift;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$fsize,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($file);
return $mtime;
}
sub count {
my $range = shift;
my @opts=(
"-w", $width,
"-h", $height,
"-s", "now - 1 $range",
"-e", "now",
"-D",
"-Y",
"-X 0",
"-A");
RRDs::graph($tmpfile,
@opts,
"DEF:counter=$rrd:counter:LAST",
"LINE2:counter#000000:Zähler",
"VDEF:countlast=counter,LAST",
"GPRINT:countlast:%.1lf kWh"
);
}
sub consum {
my $range = shift;
my @opts=(
"-w", $width,
"-h", $height,
"-D",
"-Y",
"-A",
"-e", "now",
"-s"
);
if ($range eq 'month') {
push(@opts, "now - 30 days");
} else {
push(@opts, "now - 1 $range");
}
if ($range eq 'day') {
RRDs::graph($tmpfile,
@opts,
"DEF:consum=$rrd:consum:AVERAGE",
"LINE2:consum#00FF00:W",
"CDEF:conskwh=consum,24,*,1000,/",
"VDEF:conspd=conskwh,AVERAGE",
'GPRINT:conspd:%.1lf kWh am Tag'
);
} else {
RRDs::graph($tmpfile,
@opts,
"DEF:consum=$rrd:consum:AVERAGE",
"CDEF:conspd=consum,24,*,1000,/",
"LINE2:conspd#00FF00:kWh/d"
);
}
}