-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWetter.php
More file actions
112 lines (99 loc) · 2.56 KB
/
Wetter.php
File metadata and controls
112 lines (99 loc) · 2.56 KB
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
#!/usr/bin/php -q
<?php
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'MuninPlugin.php';
/**
* Weather plugin for munin
*
* - Add symlink:
* ln -s <pathtothisfile> /etc/munin/plugins/wetter
*
* - Add configuration to /etc/munin/plugin-conf.d/
* [wetter]
* env.PLACES GMXX0138,ITXX0156,USCA0987
*
* @author Fabrizio Branca
*/
class WetterMunin extends MuninPlugin {
protected $cacheTime = 0;
/**
* @var array internal cache
*/
protected $data = array();
/**
* Setup
*
* @return array
*/
protected function getSetup() {
return array(
'graph_title' => 'Wetter',
// @see http://munin-monitoring.org/wiki/graph_category
'graph_category' => 'other',
// "second" or "minute" @see http://munin-monitoring.org/wiki/graph_period
'graph_period' => 'minute',
// "yes" or "no" @see http://munin-monitoring.org/wiki/graph_scale
'graph_scale' => 'no',
'graph_vlabel' => 'C',
// @see http://munin-monitoring.org/wiki/graph_args
'graph_args' => '--base 1000 -r --lower-limit 0'
);
}
/**
* Graphs
* Reading configuration from environment variable (set in munin's plugin configuration)
*
* @return array
*/
protected function getGraphs() {
$graphs = array();
foreach (explode(',', getenv('PLACES')) as $place) {
$place = trim($place);
$data = $this->getData($place);
$graphs[$place] = array(
'label' => $data['name'],
'info' => 'Temperatur in '. $data['name'],
'type' => MuninPlugin::GRAPH_GAUGE,
'draw' => MuninPlugin::DRAW_LINE1
);
}
return $graphs;
}
/**
* Read data from website and extract values
* Caches values internally to avoid fetching website multiple times
*
* @param string $place
* @return data
*/
protected function getData($place) {
if (!isset($this->data[$place])) {
$html = file_get_contents('http://www.weather.com/weather/today/'.$place);
// fetch name
$matches = array();
preg_match('/locName: "([\w ]+)"/', $html, $matches);
$name = $matches[1];
// fetch temperature
$matches = array();
preg_match('/realTemp: "(\d+)"/', $html, $matches);
$fahrenheit = $matches[1];
$celsius = round(($fahrenheit - 32) * 5/9);
$this->data[$place] = array('name' => $name, 'temp' => $celsius);
}
return $this->data[$place];
}
/**
* Get values
*
* @return array
*/
protected function _getValues() {
$values = array();
foreach (explode(',', getenv('PLACES')) as $place) {
$data = $this->getData($place);
$values[$place] = $data['temp'];
}
return $values;
}
}
$plugin = new WetterMunin($argv);
$plugin->process($argv);