glpi / src / plugins / vigilo / Vigilo / VigiloHlservice.php @ 92b19eed
History | View | Annotate | Download (3.05 KB)
1 | 92b19eed | Francois POIROTTE | <?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::OP_AND, self::OP_OR, self::OP_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 | $this->dependencies[$offset] = $value; |
||
73 | } |
||
74 | |||
75 | public function offsetUnset($offset) |
||
76 | { |
||
77 | unset($this->dependencies[$offset]); |
||
78 | } |
||
79 | |||
80 | public function __toString() |
||
81 | { |
||
82 | $priorities = array(); |
||
83 | $map = array( |
||
84 | "unknown" => self::STATE_UNKNOWN, |
||
85 | "warning" => self::STATE_WARNING, |
||
86 | "critical" => self::STATE_CRITICAL, |
||
87 | ); |
||
88 | |||
89 | foreach ($map as $name => $id) { |
||
90 | if (isset($this->priorities[$id])) { |
||
91 | $value = $this->priorities[$id]; |
||
92 | $priorities[] = "<${name}_priority>$value</${name}_priority>"; |
||
93 | } |
||
94 | } |
||
95 | |||
96 | $xml = <<<HLS |
||
97 | <hlservice>
|
||
98 | <message>%s</message>
|
||
99 | <warning_threshold>%d</warning_threshold>
|
||
100 | <critical_threshold>%d</critical_threshold>
|
||
101 | %s
|
||
102 | <operator>%s</operator>
|
||
103 | %s
|
||
104 | </hlservice>
|
||
105 | HLS;
|
||
106 | return self::sprintf( |
||
107 | $xml,
|
||
108 | $this->message,
|
||
109 | $this->warnThreshold,
|
||
110 | $this->critThreshold,
|
||
111 | $priorities,
|
||
112 | $this->operator,
|
||
113 | $this->dependencies
|
||
114 | ); |
||
115 | } |
||
116 | } |