(html) file generator
authorChristian Weiske <cweiske@cweiske.de>
Mon, 31 Oct 2011 19:33:05 +0000 (20:33 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 31 Oct 2011 19:33:05 +0000 (20:33 +0100)
htmlreport/.gitignore [new file with mode: 0644]
htmlreport/config.php.dist [new file with mode: 0644]
htmlreport/gen-html.php [new file with mode: 0644]
htmlreport/templates/plain.txt.php [new file with mode: 0644]

diff --git a/htmlreport/.gitignore b/htmlreport/.gitignore
new file mode 100644 (file)
index 0000000..2ea1723
--- /dev/null
@@ -0,0 +1,2 @@
+config.php
+testdata
diff --git a/htmlreport/config.php.dist b/htmlreport/config.php.dist
new file mode 100644 (file)
index 0000000..0720e33
--- /dev/null
@@ -0,0 +1,22 @@
+<?php
+/**
+ * path to the .rrd files.
+ * {item} will be replaced with humidity/temperature,
+ * {id} with the sensor number
+ */
+$filetemplate = '/var/lib/munin/home.cweiske.de/haus-usb_wde1_{item}-sensor{id}-g.rrd';
+$outdir = '/tmp/';
+
+/**
+ * Array of sensor number to sensor title associations
+ */
+$names = array(
+    0 => 'Schlafzimmer',
+    1 => 'Wohnzimmer',
+    2 => 'Buero',
+    3 => 'Aussen',
+    4 => 'Bad',
+    5 => 'Kinderzimmer',
+    6 => 'Kueche',
+);
+?>
\ No newline at end of file
diff --git a/htmlreport/gen-html.php b/htmlreport/gen-html.php
new file mode 100644 (file)
index 0000000..f495622
--- /dev/null
@@ -0,0 +1,36 @@
+<?php
+$cfgfile = __DIR__ . '/config.php';
+if (!file_exists($cfgfile)) {
+    echo "Copy config.php.dist to config.php and adjust it\n";
+    exit(1);
+}
+require $cfgfile;
+
+$data = array();
+foreach ($names as $id => $name) {
+    $data[$id]['name'] = $name;
+    foreach (array('humidity', 'temperature') as $item) {
+        $lu = rrd_lastupdate(
+            str_replace(
+                array('{item}', '{id}'),
+                array($item, $id),
+                $filetemplate
+            )
+        );
+        if ($lu === false) {
+            throw new Exception(rrd_error());
+        }
+                      
+        $data[$id][$item] = reset($lu['data']);
+    }
+}
+
+foreach (glob(__DIR__ . '/templates/*.php') as $tplfile) {
+    $outfile = $outdir . basename($tplfile, '.php');
+    ob_start();
+    include $tplfile;
+    $content = ob_get_contents();
+    ob_end_clean();
+    file_put_contents($outfile, $content);
+}
+?>
\ No newline at end of file
diff --git a/htmlreport/templates/plain.txt.php b/htmlreport/templates/plain.txt.php
new file mode 100644 (file)
index 0000000..0881184
--- /dev/null
@@ -0,0 +1,21 @@
+Zimmer             Temperatur   Luftfeuchte
+-------------------------------------------
+<?php
+plain_line($data[3]);
+echo "\n";
+plain_line($data[1]);
+plain_line($data[5]);
+plain_line($data[2]);
+echo "\n";
+plain_line($data[6]);
+plain_line($data[0]);
+plain_line($data[4]);
+
+
+function plain_line($datum) {
+    echo str_pad($datum['name'], 18, ' ');
+    echo ' ' . str_pad($datum['temperature'] . '°C', 13, ' ');
+    echo ' ' . $datum['humidity'] . '%';
+    echo "\n";
+}
+?>