-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathResmon.class.php
137 lines (120 loc) · 3.74 KB
/
Resmon.class.php
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
136
137
<?php
/**
* Resmon PHP base class
*
* Can be used to generate Resmon parsable outputs. These outputs can be consumed
* in Reconnoiter or other monitoring solutions.
*
*
* See the Resmon project website for further info on the format:
* https://labs.omniti.com/labs/resmon
*
* Copyright (c) 2011 Michal Taborsky
* See LICENSE file for licensing info
*
* @author Michal Taborsky <[email protected]>
*
*/
class Resmon {
protected $module = 'main';
protected $service = 'local';
protected $data;
const TYPE_STRING = 's';
const TYPE_DEFAULT = '0';
const TYPE_LONGINT = 'l';
const TYPE_INT = 'i';
const TYPE_UNSIGNED_LONGINT = 'L';
const TYPE_UNSIGNED_INT = 'I';
const TYPE_FLOAT = 'n';
const STATE_OK = 'OK';
const STATE_BAD = 'BAD';
const STATE_WARNING = 'WARNING';
/**
* @return the $module
*/
public function getModule() {
return $this->module;
}
/**
* @return the $service
*/
public function getService() {
return $this->service;
}
/**
* Set currently used module name
*
* @param string $module
*/
public function setModule($module) {
$this->module = $module;
}
/**
* Set currently used service
*
* @param string $service
*/
public function setService($service) {
$this->service = $service;
}
/**
* Add new metric
*
* @param string $name Unique name of the metric
* @param mixed $value The value of the metric
* @param string $type Metric type (best defined by the TYPE_* constants)
*/
public function addMetric($name, $value, $type = self::TYPE_DEFAULT) {
$this->data [$this->getModule ()] [$this->getService ()] ['metrics'] [] = array ('name' => $name, 'value' => $value, 'type' => $type );
}
/**
* Set the state of currently used module/service
*
* @param string $state State of the check, can be OK, BAD or WARNING
*/
public function setState($state = self::STATE_OK) {
$this->data [$this->getModule ()] [$this->getService ()] ['state'] = $state;
}
/**
* Return the generated Resmon XML
*
* @param boolean $print If set to true (default) print the XML with correct header
* @return string The generated XML
*/
public function outputAsXML($print = true) {
$output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$output .= "<?xml-stylesheet type=\"text/xsl\" href=\"resmon.xsl\"?>\n";
$output .= "<ResmonResults>\n";
foreach ( $this->data as $module => $services ) {
foreach ( $services as $service => $serviceData ) {
$output .= sprintf ( "\t<ResmonResult module=\"%s\" service=\"%s\">\n", htmlentities ( $module ), htmlentities ( $service ) );
$output .= sprintf ( "\t\t<last_update>%d</last_update>\n", time () );
if (is_array ( $serviceData ['metrics'] )) {
foreach ( $serviceData ['metrics'] as $metric ) {
switch ($metric ['type']) {
case self::TYPE_FLOAT :
$value = sprintf ( '%f', $metric ['value'] );
break;
case self::TYPE_INT :
case self::TYPE_LONGINT :
case self::TYPE_UNSIGNED_INT :
case self::TYPE_UNSIGNED_LONGINT :
$value = sprintf ( '%d', $metric ['value'] );
break;
default :
$value = sprintf ( '%s', $metric ['value'] );
}
$output .= sprintf ( "\t\t<metric name=\"%s\" type=\"%s\">%s</metric>\n", htmlentities ( $metric ['name'] ), $metric ['type'], htmlentities ( $value ) );
}
}
$output .= sprintf ( "\t\t<state>%s</state>\n\t</ResmonResult>\n", empty ( $serviceData ['state'] ) ? self::STATE_OK : $serviceData ['state'] );
}
}
$output .= "</ResmonResults>\n";
if ($print) {
header ( "Content-Type: text/xml; encoding=UTF-8" );
echo $output;
}
return $output;
}
} // Resmon