MRTG Script to Graph the Current Outdoor Temperature

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 thoughts on “MRTG Script to Graph the Current Outdoor Temperature”

  1. 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. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *