-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMuninPlugin.php
More file actions
135 lines (120 loc) · 2.73 KB
/
MuninPlugin.php
File metadata and controls
135 lines (120 loc) · 2.73 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* Abstract class for munin plugins
*
* @author Fabrizio Branca
*/
abstract class MuninPlugin {
const GRAPH_GAUGE = 'GAUGE';
const DRAW_LINE1 = 'LINE1';
/**
* @var int cachetime in minutes.
*/
protected $cacheTime = 0;
/**
* Process
*
* @param array $argv
* @return void
*/
public function process($argv) {
// logging (for debugging purposes)
file_put_contents(sys_get_temp_dir() . '/' . get_class($this) . '.log', date('Y-m-d H:i:s') . ' ' . var_export($argv, 1) . "\n", FILE_APPEND);
if (isset($argv[1]) && $argv[1] == "autoconf") {
$this->autoconf();
} elseif (isset($argv[1]) && $argv[1] == "config") {
$this->config();
} else {
$this->printValues();
}
}
/**
* Autoconf
*
* @return void
*/
public function autoconf() {
echo "yes\n";
}
/**
* Config
*
* @return void
*/
public function config() {
foreach ($this->getSetup() as $key => $value) {
echo "$key $value\n";
}
foreach ($this->getGraphs() as $graph => $setup) {
foreach ($setup as $key => $value) {
echo "$graph.$key $value\n";
}
}
}
/**
* Print values
*
* @return void
*/
public function printValues() {
foreach ($this->getValues() as $graph => $value) {
echo "$graph.value $value\n";
}
}
/**
* Get values.
* Wraps _getValues() that must be implemented in your inheriting class and takes care of caching
*
* @return array
*/
protected function getValues() {
if (empty($this->cacheTime)) { // caching is disabled
return $this->_getValues();
}
$cacheFile = sys_get_temp_dir() . '/Munin_' . get_class($this) . '_' . md5(serialize($this->getSetup())) . '_' . md5(serialize($this->getGraphs())) . '.cache';
if (is_file($cacheFile)) {
if (filemtime($cacheFile) >= (time() - $this->cacheTime * 60)) {
$values = unserialize(file_get_contents($cacheFile));
if ($values !== false) {
return $values;
}
}
}
$values = $this->_getValues();
file_put_contents($cacheFile, serialize($values));
chmod($cacheFile, 0666);
return $values;
}
/**
* Normalize field name
*
* @param string $fieldname
* @return string
*/
protected function normalizeFieldName($fieldname) {
return preg_replace('/[^A-Za-z0-9]/', '_', $fieldname);
}
/**
* Retrieve actual values.
* This must be implemented in your inheriting class
*
* @return array
*/
abstract protected function _getValues();
/**
* Get graphs.
* Expects information on the graphs to be displayed.
* Format
* array(<GraphName> => array(<GraphConfiguration> => <ConfigurationValue>, ...), ... )
*
* @return array
*/
abstract protected function getGraphs();
/**
* Get setup
* Expects rrdtool configuration
*
* @return array
*/
abstract protected function getSetup();
}