MRTG Script to Graph the Current Outdoor Temperature

Posted on June 9th, 2009 in General by Brandon

I’m graphing a few things around my home and wanted to graph the current outdoor temperature. I wrote this quick script to grab the current temperature or my zip code from weatherunderground.com and have it graphed with MRTG:

#!/usr/bin/php
<?php

$zipcode = $argv[1];
$page = file_get_contents("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=$zipcode&wuSelect=WEATHER");

$current_temp = 0;
if (preg_match('#pwsvariable="tempf" english="&deg;F" metric="&deg;C" value="([0-9\.]+)">#', $page, $matches)) {
    $current_temp = $matches[1];
}

if (preg_match('#Heat Index:</td>.*?<span class="nobr"><span class="b">([0-9\.]+)</span>#s', $page, $matches)) {
    $heat_index = $matches[1];
}

echo "$current_temp\n$heat_index\non\non\n";

?>

And add it to your MRTG config with something like this (Note that you need to specify your zip code in the ‘Target’ line):

Target[temp]: `/etc/mrtg/temperature.php 30605`
Options[temp]: nopercent,growright,nobanner,nolegend,noinfo,integer,gauge
Title[temp]: Outdoor Temperature
PageTop[temp]: <h3>Outdoor Temperature</h3>
YLegend[temp]: Degrees Farenheight
ShortLegend[temp]: &nbsp;&deg;F
LegendI[temp]: Temperature &deg;F&nbsp;
LegendO[temp]: Heat Index &deg;F&nbsp;

outdoor-temperatur

3 Responses to 'MRTG Script to Graph the Current Outdoor Temperature'

Subscribe to comments with RSS or TrackBack to 'MRTG Script to Graph the Current Outdoor Temperature'.

  1. calivw78 said,

    on December 3rd, 2009 at 5:01 am

    Exactly what I needed, thanks. I’m not the best at scripting though, so when I adjusted to make a second graph for gathering pressure and humidity, I failed brilliantly :) Below is what I was attempting to do, and I get output. The problem is that the output is “1″ off, meaning that instead of grabbing the humidity, it grabs the value listed below it on the web page, like visibility. Any tips? Thanks again.


    #!/usr/bin/php
    <?php

    $zipcode = $argv[1];
    $page = file_get_contents("http://www.wunderground.com/cgi-bin/findweather/getForecast?query=$zipcode&wuSelect=WEATHER");

    if (preg_match('#Humidity:.*?([0-9\.]+)#s', $page, $matches)) {
    $humidity = $matches[1];
    }

    if (preg_match('#Pressure:.*?([0-9\.]+)#s', $page, $matches)) {
    $pressure = $matches[1];
    }

    echo "$humidity\n$pressure\n\n";

    ?>

  2. calivw78 said,

    on December 3rd, 2009 at 9:15 am

    Also, I would like it to display negative values if they are present. I tried stripping out some of the code here and there without luck.

  3. az said,

    on January 4th, 2010 at 3:11 am

    hi.good day.i am developing a system that has to do with mrtg. the system has log in page and user may enter and see their mrtg graph.
    But i am not sure how to create the graph.
    Could u help me to sort out this thing.
    thank you.

Post a comment

Please copy the string ogyNTT to the field below: