]> git.sesse.net Git - nms/blob - web/stempmap.pl
b2df70b688461ae7f841a669fdcf2793a182df35
[nms] / web / stempmap.pl
1 #!/usr/bin/perl
2 #
3 #
4
5 use CGI;
6 use GD;
7 use DBI;
8 use strict;
9 use warnings;
10
11 GD::Image->trueColor(1);
12
13 my $img = GD::Image->new('snmp-bg.png');
14 #my $img = GD::Image->new(100,100);
15 my $cgi = CGI->new;
16
17 my $dbh = DBI->connect("dbi:Pg:dbname=snmpfetch;host=violet.tg05.gathering.org", "snmpfetch", "removed")
18         or die "Couldn't connect to database";
19
20 my $sgetpoll = $dbh->prepare('select switch,(select temp from temppoll where switches.switch=temppoll.switch AND temp != 0 order by id desc limit 1) AS temp,placement from switches natural join placements');
21
22 my $black = $img->colorAllocate(0,0,0);
23 my $white = $img->colorAllocate(255,255,255);
24 my $green = $img->colorAllocate(0,255,0);
25 my $blue = $img->colorAllocate(0,0,255);
26
27 my $avrgtemp = 54.5;
28 my $mintemp = 30.0;
29 my $maxtemp = 55.0;
30 my $steps = 100;
31
32 my $color = &getcolor($avrgtemp);
33
34 open (OUT, "> /tmp/outtemps");
35 for (my $i = 0; $i < $steps; $i++) {
36         my $diff = $maxtemp - $mintemp;
37         my $temp = $mintemp + ($maxtemp - $mintemp) * ((($diff / $steps) * $i)/$diff);
38         $img->line(5, $i, 45, $i, &getcolor2($temp));
39 }
40 close(OUT);
41
42 $img->string(gdMediumBoldFont, 50, 0, "Freezing!", $black);
43 $img->string(gdMediumBoldFont, 50, $steps - 11, "Too hot!", $black);
44 $img->string(gdMediumBoldFont, 50, 12, "$mintemp C", $black);
45 $img->string(gdMediumBoldFont, 50, $steps - 22, "$maxtemp C", $black);
46
47 #$img->string(gdMediumBoldFont, 600, 200, "Freezing!$mintemp", $black);
48 #$img->string(gdMediumBoldFont, 600, 210, "$mintemp C", $black);
49 #$img->string(gdMediumBoldFont, 600, 220 + $maxtemp, "Too hot! $maxtemp", $black);
50 #$img->string(gdMediumBoldFont, 600, 210 + $maxtemp, "$maxtemp C", $black);
51
52
53 $sgetpoll->execute();
54 while (my $ref = $sgetpoll->fetchrow_hashref()) {
55         next if (!defined($ref->{'temp'}));
56
57         $ref->{'placement'} =~ /\((\d+),(\d+)\),\((\d+),(\d+)\)/;
58         $avrgtemp = $ref->{'temp'};
59         my $px = $3;
60         my $py = $4 + 16;
61         my $roundtemp = sprintf ("%.0f", $avrgtemp);
62         $color = getcolor($avrgtemp);
63         $img->filledRectangle($px - 0, $py - 17, $px + 12, $py + 5, $white);
64         $img->stringUp(gdGiantFont,$px - 1,$py - 1,"$roundtemp",$blue);
65         $img->rectangle($px, $py, $px + 10,$py + 10,$black);
66         $img->filledRectangle($px + 1, $py + 1, $px + 9, $py + 9,$color);
67 }
68
69 print $cgi->header(-type=>'image/png');
70 print $img->png;
71
72 sub getcolor {
73         my ($temp) = @_;
74
75         my $t = ($temp - $mintemp) / ($maxtemp - $mintemp);
76         $t = 0 if ($t < 0);
77         $t = 1 if ($t > 1);
78
79         my $colorred = 255 * $t;
80         my $colorblue = 255 - $colorred;
81
82         return $img->colorResolve($colorred, 0, $colorblue);
83 }
84
85 sub getcolor2 {
86         my ($temp) = @_;
87
88         my $t = ($temp - $mintemp) / ($maxtemp - $mintemp);
89         $t = 0 if ($t < 0);
90         $t = 1 if ($t > 1);
91
92         my $colorred = 255 * $t;
93         my $colorblue = 255 - $colorred;
94
95         return $img->colorResolve($colorred, 0, $colorblue);
96 }