glpi / src / plugins / vigilo / Vigilo / VigiloHlservice.php @ e878cf6c
History | View | Annotate | Download (3.21 KB)
1 |
<?php
|
---|---|
2 |
|
3 |
class VigiloHlservice extends VigiloXml implements ArrayAccess |
4 |
{ |
5 |
protected $name; |
6 |
protected $message; |
7 |
protected $priorities; |
8 |
protected $dependencies; |
9 |
|
10 |
const OPERATOR_AND = "and"; |
11 |
const OPERATOR_OR = "or"; |
12 |
const OPERATOR_PLUS = "plus"; |
13 |
|
14 |
const STATE_WARNING = 1; |
15 |
const STATE_CRITICAL = 2; |
16 |
const STATE_UNKNOWN = 3; |
17 |
|
18 |
public function __construct($name, $operator, $message, $warnThreshold = 0, $critThreshold = 0) |
19 |
{ |
20 |
if (!in_array($operator, array(self::OPERATOR_AND, self::OPERATOR_OR, self::OPERATOR_PLUS))) { |
21 |
throw new Exception('Invalid operator'); |
22 |
} |
23 |
|
24 |
$this->name = $name; |
25 |
$this->message = $message; |
26 |
$this->operator = $operator; |
27 |
$this->warnThreshold = (int) $warnThreshold; |
28 |
$this->critThreshold = (int) $critThreshold; |
29 |
$this->priorities = array(); |
30 |
$this->dependencies = array(); |
31 |
} |
32 |
|
33 |
public function getName() |
34 |
{ |
35 |
return $this->name; |
36 |
} |
37 |
|
38 |
public function setPriority($state, $priority) |
39 |
{ |
40 |
if (!in_array($priority, array(self::STATE_WARNING, self::STATE_CRITICAL, self::STATE_UNKNOWN))) { |
41 |
throw new Exception('Invalid state'); |
42 |
} |
43 |
|
44 |
$this->priorities[$state] = (int) $priority; |
45 |
} |
46 |
|
47 |
public function setWarningThreshold($threshold) |
48 |
{ |
49 |
$this->warnThreshold = (int) $threshold; |
50 |
} |
51 |
|
52 |
public function setCriticalThreshold($threshold) |
53 |
{ |
54 |
$this->critThreshold = (int) $threshold; |
55 |
} |
56 |
|
57 |
public function offsetExists($offset) |
58 |
{ |
59 |
return isset($this->dependencies[$offset]); |
60 |
} |
61 |
|
62 |
public function offsetGet($offset) |
63 |
{ |
64 |
return isset($this->dependencies[$offset]) ? $this->dependencies[$offset] : null; |
65 |
} |
66 |
|
67 |
public function offsetSet($offset, $value) |
68 |
{ |
69 |
if (!is_object($value) || !($value instanceof VigiloDepends)) { |
70 |
throw new \RuntimeException("Invalid dependency"); |
71 |
} |
72 |
if (null === $offset) { |
73 |
$this->dependencies[] = $value; |
74 |
} else {
|
75 |
$this->dependencies[$offset] = $value; |
76 |
} |
77 |
} |
78 |
|
79 |
public function offsetUnset($offset) |
80 |
{ |
81 |
unset($this->dependencies[$offset]); |
82 |
} |
83 |
|
84 |
public function __toString() |
85 |
{ |
86 |
$priorities = array(); |
87 |
$map = array( |
88 |
"unknown" => self::STATE_UNKNOWN, |
89 |
"warning" => self::STATE_WARNING, |
90 |
"critical" => self::STATE_CRITICAL, |
91 |
); |
92 |
|
93 |
foreach ($map as $name => $id) { |
94 |
if (isset($this->priorities[$id])) { |
95 |
$value = $this->priorities[$id]; |
96 |
$priorities[] = "<${name}_priority>$value</${name}_priority>"; |
97 |
} |
98 |
} |
99 |
|
100 |
$xml = <<<HLS |
101 |
<hlservice name="%s">
|
102 |
<message>%s</message>
|
103 |
<warning_threshold>%d</warning_threshold>
|
104 |
<critical_threshold>%d</critical_threshold>
|
105 |
%s
|
106 |
<operator>%s</operator>
|
107 |
%s
|
108 |
</hlservice>
|
109 |
HLS;
|
110 |
return self::sprintf( |
111 |
$xml,
|
112 |
$this->name,
|
113 |
$this->message,
|
114 |
$this->warnThreshold,
|
115 |
$this->critThreshold,
|
116 |
$priorities,
|
117 |
$this->operator,
|
118 |
$this->dependencies
|
119 |
); |
120 |
} |
121 |
} |